Install cytoscape-cxtmenu
masterYou can install the extension using npm or bower. It requires cytoscape.js ^3.2.0 as a dependency.
npm:
npm install cytoscape-cxtmenubower:
bower install cytoscape-cxtmenurepository·master·Indexed 41 days ago
https://github.com/cytoscape/cytoscape.js-cxtmenuA circular, swipeable context menu extension for Cytoscape.js version 3.5.0. It provides a radial UI for performing actions on nodes, edges, or the graph background, supporting customizable commands, dynamic menu generation, and configurable appearance options such as menuRadius and activeFillColor.
You can install the extension using npm or bower. It requires cytoscape.js ^3.2.0 as a dependency.
npm:
npm install cytoscape-cxtmenubower:
bower install cytoscape-cxtmenuDepending on your module system, use the following patterns to register the extension with Cytoscape.js.
ES Import:
import cytoscape from 'cytoscape';
import cxtmenu from 'cytoscape-cxtmenu';
cytoscape.use( cxtmenu );CommonJS (require):
let cytoscape = require('cytoscape');
let cxtmenu = require('cytoscape-cxtmenu');
cytoscape.use( cxtmenu );AMD:
require(['cytoscape', 'cytoscape-cxtmenu'], function( cytoscape, cxtmenu ){
cxtmenu( cytoscape );
});Plain HTML/JS: If you are using plain HTML/JS without a module loader, the extension is registered automatically.
To use the context menu extension, you must register it with your Cytoscape.js instance. The extension provides a register function that attaches the cxtmenu capability to the Cytoscape core.
const cytoscape = require('cytoscape');
const registerCxtmenu = require('cytoscape-cxtmenu');
// Register the extension
registerCxtmenu(cytoscape);
// Now you can initialize cytoscape with cxtmenu options
const cy = cytoscape({
container: document.getElementById('cy'),
elements: [ /* ... */ ],
cxtmenu: { /* configuration options */ }
});const cytoscape = require('cytoscape');
const registerCxtmenu = require('cytoscape-cxtmenu');
registerCxtmenu(cytoscape);Initialize the plugin by calling .cxtmenu(options) on your Cytoscape instance. The menu is initialized on the same HTML DOM element container used for Cytoscape.js.
| Option | Type | Default | Description |
|---|---|---|---|
menuRadius | number or function(ele) | function(ele){ return 100; } | The outer radius (node center to the end of the menu) in pixels. Added to the rendered size of the node. |
selector | string | 'node' | Cytoscape.js selector for elements that trigger the menu. |
commands | Array<Object> or function(ele) | [] | An array of command objects or a function returning them (or a promise of them). |
fillColor | string | 'rgba(0, 0, 0, 0.75)' | Background color of the menu. |
activeFillColor | string | 'rgba(1, 105, 217, 0.75)' | Color used to indicate the selected command. |
activePadding | number | 20 | Additional size in pixels for the active command. |
indicatorSize | number | 24 | Size of the pointer to the active command. |
separatorWidth | number | 3 | Empty spacing in pixels between successive commands. |
spotlightPadding | number | 4 | Extra spacing between the element and the spotlight. |
adaptativeNodeSpotlightRadius | boolean | false | Whether the spotlight radius should adapt to the node size. |
minSpotlightRadius | number | 24 | Minimum radius of the spotlight (used for edge/background if adaptative is enabled). |
maxSpotlightRadius | number | 38 | Maximum radius of the spotlight (used for edge/background if adaptative is enabled). |
openMenuEvents | string | 'cxttapstart taphold' | Space-separated Cytoscape events to open the menu. Only cxttapstart and taphold work. |
itemColor | string | 'white' | Color of text in the command's content. |
itemTextShadowColor | string | 'transparent' | Text shadow color of the command's content. |
zIndex | number | 9999 | Z-index of the UI div. |
atMouse | boolean | false | If true, draws menu at mouse position. |
outsideMenuCancel | number or boolean | false | If a number, cancels the command if the pointer is released outside the spotlight + this padding. |
Each object in the commands array can contain:
content: (string/HTML) Text/content displayed in the menu.fillColor: (string) Custom background color for the item.contentStyle: (Object) CSS key:value pairs for the command's CSS.select: (function(ele)) Executed when the command is selected. ele is the active element.hover: (function(ele)) Executed when the command is hovered. ele is the active element.enabled: (boolean) Whether the command is selectable.let cy = cytoscape({
container: document.getElementById('cy'),
/* ... */
});
let menu = cy.cxtmenu({
selector: 'node',
commands: [
{
content: 'Delete Node',
select: function(ele){
ele.remove();
}
}
]
});The .cxtmenu() method returns a menu instance. You can use this instance to clean up or remove the menu from the Cytoscape instance by calling .destroy().
let menu = cy.cxtmenu( someOptions );
// Later, to clean up:
menu.destroy();When initializing the cytoscape-cxtmenu extension, you can provide a configuration object to customize the menu's appearance, behavior, and commands.
fillColor: The background color of the menu (e.g., 'rgba(0, 0, 0, 0.75)').activeFillColor: The color used to indicate the currently selected command.activePadding: Additional size in pixels for the active command.itemColor: The color of the text in the command's content.itemTextShadowColor: The text shadow color of the command's content.zIndex: The z-index of the UI div.menuRadius: A function function(ele){ ... } that returns the radius of the circular menu in pixels.separatorWidth: The empty spacing in pixels between successive commands.indicatorSize: The size in pixels of the pointer to the active command. Defaults to the node size if the node size is smaller than this value.spotlightPadding: Extra spacing in pixels between the element and the spotlight.adaptativeNodeSpotlightRadius: Boolean. If true, the spotlight radius adapts to the node size.minSpotlightRadius: The minimum radius in pixels of the spotlight (used for edges/background or when adaptativeNodeSpotlightRadius is enabled).maxSpotlightRadius: The maximum radius in pixels of the spotlight (used for edges/background or when adaptativeNodeSpotlightRadius is enabled).atMouse: Boolean. If true, the menu is drawn at the mouse position.outsideMenuCancel: If set to a number, the command is cancelled if the pointer is released outside of the spotlight, padded by this number.selector: A Cytoscape.js selector string. Elements matching this selector will trigger the context menu (default: 'node').openMenuEvents: A space-separated string of Cytoscape.js events that will open the menu. Only cxttapstart and/or taphold are supported.commands: An array of command objects or a function function(ele){ ... } that returns an array of command objects.The cxtmenu configuration object allows you to customize the appearance, behavior, and commands of the radial context menu. You can provide a static array of commands or a function that dynamically generates commands based on the target element.
Each command in the commands array can contain:
content: HTML string or HTMLElement to display in the menu.fillColor: (Optional) Custom background color for the specific menu item.contentStyle: (Optional) CSS key-value pairs to apply to the command's content.hover: A function called when the command is hovered. Receives (ele, gestureStartEvent).select: A function called when the command is selected. Receives (ele, gestureStartEvent).enabled: Boolean indicating if the command is selectable (defaults to true).| Option | Default | Description |
|---|---|---|
selector | 'node' | Elements matching this selector will trigger the menu. |
commands | [] | Array of command objects or a function (ele) => commands[] |
menuRadius | (ele) => 100 | Function or number defining the menu radius in pixels. |
fillColor | 'rgba(0, 0, 0, 0.75)' | Background color of the menu. |
activeFillColor | 'rgba(1, 105, 217, 0.75)' | Color used to indicate the currently hovered/active command. |
activePadding | 20 | Additional size in pixels for the active command. |
indicatorSize | 24 | Size of the pointer to the active command. |
separatorWidth | 3 | Spacing in pixels between successive commands. |
spotlightPadding | 4 | Extra spacing between the element and the spotlight. |
adaptativeNodeSpotlightRadius | false | If true, spotlight radius adapts to node size. |
minSpotlightRadius | 24 | Minimum radius for the spotlight. |
maxSpotlightRadius | 38 | Maximum radius for the spotlight. |
openMenuEvents | 'cxttapstart taphold' | Cytoscape events that trigger the menu. |
itemColor | 'white' | Color of the text in the command's content. |
itemTextShadowColor | 'transparent' | Text shadow color for command content. |
zIndex | 9999 | Z-index of the menu UI container. |
atMouse | false | If true, draws menu at mouse position instead of element position. |
outsideMenuCancel | false | If a number, cancels command if pointer is released outside spotlight + this value. |
const cy = cytoscape({
container: document.getElementById('cy'),
elements: [ /* ... */ ],
cxtmenu: {
selector: 'node',
commands: (ele) => [
{
content: 'Delete',
select: (e) => e.remove(),
fillColor: 'rgba(255, 0, 0, 0.75)'
},
{
content: '<b>Bold</b>',
contentStyle: { fontWeight: 'bold' }
}
]
}
});You can use standard CSS to style specific parts of the context menu:
.cxtmenu-content: Use this class to style the font of the command text..cxtmenu-disabled: Use this class to style disabled menu entries.If you need to manually clean up the context menu (e.g., when removing the Cytoscape instance or switching views), the extension returns a destroy method. This method removes all event listeners and the menu's DOM elements from the container.
// Note: The destroy method is part of the object returned by the cxtmenu implementation
// when called internally. In standard usage, you manage the lifecycle via the cy instance.
// If you have a reference to the instance:
instance.destroy();The commands option defines the items available in the context menu. You can provide a static array of command objects or a function that receives the active element (ele) and returns an array of commands, allowing for dynamic menus based on the selected element.
Each command object can contain:
content: The HTML or text content to be displayed in the menu.fillColor: (Optional) Custom background color for the specific menu item.contentStyle: (Optional) CSS key:value pairs to set the command's CSS via JavaScript.hover: A function function(ele){ ... } executed when the command is hovered. ele is the reference to the active element.select: A function function(ele){ ... } executed when the command is selected. ele is the reference to the active element.enabled: Boolean. Whether the command is selectable (default: true).commands: [
{
content: 'a command name',
fillColor: 'rgba(200, 200, 200, 0.75)',
contentStyle: { fontWeight: 'bold' },
hover: function(ele) {
console.log('Hovering:', ele.id());
},
select: function(ele) {
console.log('Selected:', ele.id());
},
enabled: true
}
]