Repository navigation
Context Menu
📝 Generated from
docs/features/context-menu.md. Edit it there; changes made in the wiki are overwritten.
contextMenu (v3.11) shows a menu of cell actions on right-click, or on Shift+F10 / the Menu key over a focused body cell. It is off by default: until you enable it, the browser's own menu shows as before. Demo: Context Menu (/context-menu).
import { DataGrid, exportToCsv, type GridContextMenuEntry, type GridContextMenuParams } from '@opencorestack/opengridx';
<DataGrid
rows={rows}
columns={columns}
cellSelection // optional: Copy then copies the selected range
undoRedo // optional: adds Undo and Redo to the menu
contextMenu
getContextMenuItems={(params: GridContextMenuParams<Employee>, defaultItems): GridContextMenuEntry[] => [
...defaultItems,
'divider',
{ id: 'export', label: 'Export CSV', onClick: () => exportToCsv(params.api.getAllFilteredRows(), columns) },
{ id: 'delete', label: 'Delete row', danger: true, onClick: () => removeRow(params.id) },
]}
/>Item (id) |
Shown | What it does |
|---|---|---|
Copy ('copy') |
always | Copies the range, or the clicked cell (see below) |
Copy with headers ('copyWithHeaders') |
always | The same, with a header line of headerName ?? field
|
Undo ('undo') |
with undoRedo
|
apiRef.undo(); disabled when there is nothing to undo |
Redo ('redo') |
with undoRedo
|
apiRef.redo(); disabled when there is nothing to redo |
There is no divider between the copy items and Undo / Redo.
Called each time the menu opens, with the built-in items, and returns the entries to show: items (GridContextMenuItem) and 'divider' lines.
-
params(GridContextMenuParams<R>):id,field,row,colDef,value(read throughvalueGetter),rowMeta,cellRange(the current range,nullwithoutcellSelectionor with no range) andapi(the grid API). -
Item (
GridContextMenuItem):id(stable key),label,onClick,disabled,shortcut(display only, e.g.'Ctrl+C'),iconanddanger(styling only). - Leading, trailing and consecutive dividers are dropped. A list with no items (empty, or only dividers) shows no menu, and the browser's own menu is then not suppressed either. Use it to switch the menu off for some cells (a synthetic row, a column).
-
A throwing callback (or a throwing
onClick) is contained: the grid falls back to the built-in items (or does nothing for the click) and warns once in development. - Setting
getContextMenuItemswithoutcontextMenudoes nothing; in development it warns once.
| Where the right-click lands | Focus and range | Copy writes |
|---|---|---|
| Inside the current cell range | Both stay as they are | The range: the same text as Ctrl/Cmd+C. Synthetic rows (group, subtotal, auto-parent) are skipped |
| Elsewhere (or no range) | Focus moves to the cell and the range collapses | The clicked cell |
"Copy with headers" adds a first line of column headers (headerName ?? field). A range right-clicked with cellSelection keeps its outline, so the menu acts on what the user sees selected.
Opens on:
- right-click on a body cell;
- Shift+F10 or the Menu key on a focused body cell (the menu appears at the cell);
- Ctrl+click on Apple platforms (macOS, iPadOS).
Keeps the browser menu: column headers, the header filter row, the footer, detail panels, open cell editors, the checkbox, expand and reorder cells, and nested grids.
Closes on: choosing an item, Escape or Tab (focus returns to the cell), a press outside, grid scroll, window resize, and the row leaving the rendered rows (a filter, a sort, new rows). Arrow keys, Home, End and Enter move through and activate the items.
The menu opens towards the inline end of the pointer and flips to stay inside the window; in a dir="rtl" grid it opens towards the inline start.
apiRef.current.openContextMenu(id, field); // does nothing without `contextMenu`; v3.11
apiRef.current.closeContextMenu();openContextMenu scrolls the cell into view and opens the menu on a later animation frame. If the cell is not rendered within a few frames, or its row moved in the meantime (a sort, a filter, new rows), no menu opens. Tests should wait for role="menu" rather than expect it at once.
| Key | Default |
|---|---|
contextMenuCopy |
'Copy' |
contextMenuCopyWithHeaders |
'Copy with headers' |
contextMenuUndo |
'Undo' |
contextMenuRedo |
'Redo' |
contextMenuLabel |
'Cell actions' (the menu's aria-label) |
Labels you add in getContextMenuItems are yours to translate.
Public classes: ogx-context-menu, ogx-context-menu__item, ogx-context-menu__item--danger, ogx-context-menu__item--disabled and ogx-context-menu__shortcut. The menu also carries the column menu's ogx-column-menu / ogx-menu-item classes and shares its theme tokens (overlays.* theme keys), so a theme or CSS aimed at .ogx-column-menu styles both.
- On Windows the ContextMenu (Menu) key may also fire the browser's
contextmenuevent on key-up; the grid's menu may then re-open at the same cell. - Calling
closeContextMenu()before a pendingopenContextMenuhas opened does not cancel it.
- The column header menu (the column menu already has its own button).
- Long-press on touch devices (iOS Safari fires no
contextmenuevent). CallapiRef.openContextMenufrom your own gesture if you need it. - Paste and export items. Add them through
getContextMenuItems, as the demo does for CSV export.
See also Cell Range Selection, Clipboard and Undo & Redo.
OpenGridX 3.11.1 · MIT · This wiki is generated from docs/ on every push to main. To fix a page, open a PR against the source file.
Start here
Components
- DataGrid
- Header
- Row
- Cell
- Toolbar
- Pagination
- Filter Panel
- Tooltip
- Column Visibility
- Column Grouping
- Column Resizing
- Empty State
- Error Overlay
- Aggregation Footer
Features
- Virtualization
- Filtering & Search
- Header Filters
- Sorting & Pagination
- Custom Pagination
- Editing & Reordering
- Row Selection
- Clipboard
- Cell Range Selection
- Undo & Redo
- Fill Handle
- Context Menu
- Pinning
- State Persistence
- Aggregation & Pivot
- Tree Data & Grouping
- Cell Spanning
- Master-Detail
- Keyboard & Accessibility
- Right-to-Left (RTL)
- List View
- Infinite Scroll
- Data Source
- Loading States
- Toolbar Customization
- Export (CSV, Excel, JSON, Print)
- PDF Export
- AI Toolkit (Schema & Validator)
Customization
Upgrading
Contributing
- Contributing
- Testing
- React Compiler
- Roadmap
- DataGrid orchestration
- GridRowMeta
- useGridControlledState
- useGridRowPipeline
- useGridColumns
- useGridVirtualization
- useGridVisibleRows
- useGridScrollSync
- useGridStateSnapshot