LyteNyte Grid logo for light mode. Links back to the documentation home page.
Components

Context Menu

LyteNyte Grid's context menu appears on right-click and provides auxiliary actions for the clicked element.

Note

This guide assumes familiarity with the Menu component. For more details, review the Menu Button guide.

Grid Context Menu

Use the grid’s events property to handle contextmenu on the grid region that should open the context menu. In the demo below, right-click any cell to trigger the context menu and perform an action.

Context Menu

Fork code on stack blitzFork code on code sandbox

The event handler code is shown below. The demo registers contextmenu handlers on both header and row cells. Since the browser natively opens its own menu on right-click, call preventDefault to suppress it.

The handlers use LyteNyte Grid’s virtualFromXY utility to create a virtual element and assign it to the anchor state. The menu uses the virtual element as its positioning anchor, eliminating the need for a menu trigger.

<Grid
rowHeight={50}
columns={columns}
onColumnsChange={setColumns}
ref={setApi}
rowSource={ds}
events={useMemo<Grid.Events<GridSpec>>(() => {
return {
headerCell: {
contextMenu: ({ event, column }) => {
event.preventDefault();
event.stopPropagation();
setAnchor(virtualFromXY(event.clientX, event.clientY));
setMenu({ column, row: null });
},
},
cell: {
contextMenu: ({ event, row, column }) => {
event.preventDefault();
event.stopPropagation();
setAnchor(virtualFromXY(event.clientX, event.clientY));
setMenu({ row, column });
},
},
};
}, [])}
/>

Next Steps

  • Popover: Display anchored, on-demand content in an overlay.
  • Dialog: Present critical information or request decisions in a focused modal.
  • Menu Button: Display a list of actions or options in an accessible dropdown menu.