Skip to content

Context Menu Plugin

The Context Menu plugin adds a customizable right-click menu to your grid cells. Build anything from simple copy/paste actions to complex nested menus with conditional visibility, icons, and keyboard shortcuts.

import '@toolbox-web/grid/features/context-menu';

Define your menu items as an array—each item has an id, label, and action callback that receives context about the clicked cell (row data, column info, cell value, etc.). Add separators between groups of actions for visual clarity.

import { queryGrid } from '@toolbox-web/grid';
const grid = queryGrid('tbw-grid');
function removeRow(rowIndex: number) {
grid.rows = grid.rows.filter((_, i) => i !== rowIndex);
}
grid.gridConfig = {
columns: [
{ field: 'name', header: 'Name' },
{ field: 'email', header: 'Email' },
{ field: 'status', header: 'Status' }
],
features: {
contextMenu: {
items: [
{ id: 'copy', name: 'Copy Cell', action: (ctx) => navigator.clipboard.writeText(String(ctx.value)) },
{ id: 'sep1', name: '', separator: true },
{ id: 'delete', name: 'Delete Row', action: (ctx) => removeRow(ctx.rowIndex) },
],
},
},
};

Right-click any cell to see the context menu.

Nested menu items for complex actions.

Show/hide items based on context.

Right-click any cell to see the context menu. Items from FilteringPlugin, VisibilityPlugin, and PinnedColumnsPlugin are automatically contributed alongside custom items.

Plugins like FilteringPlugin, VisibilityPlugin, and PinnedColumnsPlugin can contribute their own items to the context menu automatically.

OptionTypeDefaultDescription
itemsContextMenuItem[]Copy + Export CSVMenu items to display

When used with the SelectionPlugin (row mode), the context menu automatically syncs the selection on right-click:

ScenarioBehavior
Right-click a selected rowMulti-selection preserved
Right-click an unselected rowSelects only that row
No SelectionPlugin loadedselectedRows = [rowIndex]
Right-click on headerselectedRows = []

This uses the plugin query system for loose coupling — no hard dependency on SelectionPlugin.

features: {
contextMenu: {
items: [
{
id: 'edit',
name: 'Edit',
hidden: (params) => params.column.editable !== true,
},
{
id: 'delete',
name: 'Delete',
disabled: (params) => params.row.locked === true,
},
],
},
},

Right-click on cells or headers to open the context menu.

Event Log:
EventDetailDescription
context-menu-open{ params, items }Menu opened

The context menu supports CSS custom properties for theming. Override these on tbw-grid or a parent container:

PropertyDefaultDescription
--tbw-context-menu-bgvar(--tbw-color-panel-bg)Menu background
--tbw-context-menu-fgvar(--tbw-color-fg)Menu text color
--tbw-context-menu-bordervar(--tbw-color-border)Menu border
--tbw-context-menu-hovervar(--tbw-color-row-hover)Item hover background
--tbw-menu-min-width10remMinimum menu width
--tbw-menu-item-padding0.375rem 0.75remMenu item padding
--tbw-menu-item-gap0.5remGap between icon and label
--tbw-font-size-sm0.9285emMenu font size
--tbw-font-size-xs0.7857emShortcut text size
--tbw-icon-size1emIcon width
tbw-grid {
/* Custom context menu styling */
--tbw-context-menu-bg: #2d2d2d;
--tbw-context-menu-fg: #ffffff;
--tbw-context-menu-border: #444444;
--tbw-context-menu-hover: #3d3d3d;
--tbw-menu-item-padding: 0.5rem 1rem;
}

The menu uses these class names for advanced customization:

ClassElement
.tbw-context-menuMenu container
.tbw-context-menu-itemMenu item row
.tbw-context-menu-item.disabledDisabled item
.tbw-context-menu-item.dangerDanger/delete action
.tbw-context-menu-iconItem icon container
.tbw-context-menu-labelItem label text
.tbw-context-menu-shortcutKeyboard shortcut
.tbw-context-menu-separatorDivider line

When the plugin is loaded, the grid’s right-click target advertises the popup trigger to assistive technologies following the WAI-ARIA Menu Button pattern:

  • aria-haspopup="menu" is set on the grid root once the plugin is attached.
  • aria-expanded is toggled between "false" and "true" to reflect the menu’s open state — including when it closes via Esc, click outside, or a scroll event.
  • Menu items inside the popup use role="menuitem" and are reachable with the arrow keys; Enter / Space activates and Esc closes (see Accessibility guide).

The plugin also respects the keyboard-open path: Shift + F10 and the dedicated ☰ Menu key open the menu at the focused cell with focus moved to the first item.

AI assistants: For complete API documentation, implementation guides, and code examples for this library, see https://toolboxjs.com/llms-full.txt