Skip to content

Context menu

Configure the built-in right-click menu, build your own menu, and draw temporary highlights.
On this page

GridConfig.contextMenu turns the built-in right-click menu on or off, or replaces its rows with a static list or a context-aware factory.

Install a context-aware menu
declare const config: GridConfig;
// Right-click a cell, then choose Inspect cell to log its stored value.
A
1Ada
Copy valueCtrl+C

Inspect cell
Download CSV
Result: right-clicking Ada opens these three menu rows, with a separator after Copy value. The cell value is unchanged. Static preview; the copy shortcut is shown for Windows/Linux.
Configuration example (requires an initialized grid)

This configuration excerpt needs an initialized grid. For the result above, use a sheet with "Ada" in A1, then pass config to createGrid or call grid.setConfig(config).

Context-aware bundled menu
const config = {
{ id: "copy", action: "copy", label: "Copy value", shortcut: "copy" },
{ action: "separator" },
{
id: "inspect",
label: "Inspect cell",
visible: context.cell !== null,
disabled: ({ cell }) => cell === null,
if (cell !== null) console.log(grid.store.getCell(cell));
},
},
{ action: "exportCsv", label: "Download CSV" },
],
} satisfies GridConfig;

A right-click on a cell shows three rows: Copy value, Inspect cell and Download CSV. When the pointer resolves no cell, Inspect cell is hidden.

How the bundled menu works

On a right-click, Sheetwrite prevents the browser menu, hit-tests the pointer, and selects the resolved cell. Then it evaluates each item's visible and disabled policy.

ContextMenuItems is either a readonly ContextMenuItem[] or a factory evaluated for each ContextMenuContext.

contextMenu valueResult
true (default)The default menu.
falseNo bundled menu.
readonly ContextMenuItem[]Static readonly rows.
(context) => ContextMenuItem[]Rows built for each right-click context.
Item fieldMeaning
idStable row id.
actionA built-in action name (below).
labelVisible text.
shortcutShortcut hint.
visible / disabledA boolean, or a predicate that receives the lean ContextMenuContext.
onClick(grid, cell)Host callback. It overrides action.

The lean ContextMenuContext carries only cell, clientX, and clientY.

The built-in action names are:

Clipboard/contentRowsColumnsView/export
cut, copy, paste, clearContentsinsertRowAbove, insertRowBelow, deleteRow, hideRow, showAllRows, autoFitRowinsertColumnLeft, insertColumnRight, deleteColumn, hideColumn, showAllColumns, autoFitColumnclearFilter, merge, unmerge, exportCsv, exportXlsx

The default menu contains copy, cut, paste, clear contents, row insert/delete/hide/show/auto-fit, column insert/delete/hide/show/auto-fit, clear column filter, merge, and unmerge actions, with separators between groups. exportCsv and exportXlsx are not in the default menu, but you can use them in a custom list. XLSX still requires @sheetwrite/xlsx/register.

Host-owned menu

For fully host-owned UI, set contextMenu: false and bind the DOM contextmenu event on the host. grid.getCellAtPoint(event.clientX, event.clientY) resolves the pointer without exposing renderer internals. The host then owns preventDefault, selection, positioning, dismissal, and calls to grid.actions. Sheetwrite does not prescribe the host's event lifecycle:

Host-owned context menu
host.addEventListener("contextmenu", (event) => {
event.preventDefault();
const cell = grid.getCellAtPoint(event.clientX, event.clientY);
if (cell !== null) grid.setSelection({ kind: "cell", addr: cell });
host.dispatchEvent(
new CustomEvent("sheetwrite:context-menu", {
detail: { cell, x: event.clientX, y: event.clientY, actions: grid.actions },
}),
);
});

Temporary highlights

grid.highlightCells(ranges, color?) draws an overlay, not a document edit. Pass null to clear all temporary highlights. color overrides the theme highlight color; a range can also carry its own color.

Highlight, then clear
[{ sheet, start: { row: 0, col: 0 }, end: { row: 2, col: 1 } }],
"#ffe08a",
);
// later
  • Undo does not clear a highlight.
  • Choose a distinct overlay color, so users can tell it from selection and cell formatting.
  • Use grid.styleRange when a fill must be saved and undoable.

The Vanilla workbench offers Clear highlight in its context menu. Right-click the highlighted cell to toggle it off, or right-click another cell and choose Clear highlight. Both actions write to the host event log.

Edge cases and limits

  • Hidden separators are normalized.
  • Disabled rows cannot invoke actions.
  • Built-in export actions show an alert and emit one export-error event on failure.

See also

Documentation