Context menu options
Look up the props of DataGridContextMenu and DataGridHeaderDropdown, the item ids, and the context each menu resolver receives.
DataGridContextMenu wraps the grid and opens a menu on right-click and on long-press. DataGridHeaderDropdown is the column menu behind the header chevron. A resolver prop returns the complete item list of a menu. The install steps and the behavior are on Context menu.
| Symbol | Purpose | Guide |
|---|---|---|
DataGridContextMenu | Opens the cell and header menus | Context menu |
DataGridHeaderDropdown | The column menu behind the header chevron | Context menu |
| Resolver context | The context that each resolver receives | Context menu |
useDataGridRowOps | Tells whether the grid can create and duplicate rows | Row operations |
| Menu item | The fields of one item | Context menu |
| Built-in item ids | The ids that a resolver keeps or drops | Context menu |
DataGridContextMenu
The wrapper leaves a right-click in a text field to the browser, for example in the Find field, a filter popover, or a cell editor. It also leaves a right-click anywhere in a popup outside the grid, such as a button of the Find panel, to the browser.
A resolver runs while the menu renders. Do not call hooks in it. Read grid state and run grid actions through ctx, and close over other hook values from the component that renders the menu.
DataGridHeaderDropdown
Prop
Type
Pass the render context of renderHeaderMenu with a spread. Inside a DataGridContextMenu, the dropdown uses the getHeaderMenuItems of the wrapper. Its own getHeaderMenuItems replaces that resolver for this dropdown.
Resolver context
Each resolver receives a context and the built-in items as defaults.
CellMenuItemsCtx
getCellMenuItems receives this context:
HeaderMenuItemsCtx
getHeaderMenuItems receives this context:
useDataGridRowOps
useDataGridRowOps() returns { canCreateRow, canDuplicateRow }. canCreateRow is true when the grid has a createRow prop, and canDuplicateRow is true when it has a duplicateRow prop. The built-in menu reads the same flags. Call it below DataGridProvider, for example to disable a custom duplicate button:
import { useDataGridActions } from "@/components/data-grid/data-grid";
import {
useDataGridRowOps,
} from "@/components/data-grid-context-menu/data-grid-context-menu";
function DuplicateButton({ rowId }: { rowId: string }) {
const actions = useDataGridActions();
const { canDuplicateRow } = useDataGridRowOps();
return (
<button
type="button"
disabled={!canDuplicateRow}
onClick={() => actions.duplicateRows([rowId])}
>
Duplicate
</button>
);
}Menu item
A menu item is an item or a separator. A separator is { id, separator: true }. An item has these fields:
| Field | Type | Default |
|---|---|---|
id | BuiltInMenuItemId | (string & {}) | required |
label | string | required |
icon | ReactNode | none |
shortcut | string | none |
disabled | boolean | false |
destructive | boolean | false |
tooltip | string | none |
onClick | () => void | none |
shortcut is a keymap binding. The menu shows it as keycaps. tooltip shows on a disabled item.
Built-in item ids
A resolver keeps or drops a built-in item by its id. The BuiltInMenuItemId type lists these ids. Separator ids start with sep-.
| Menu | Ids, in order |
|---|---|
| Cell | cut, copy, paste, sep-clipboard, clear-contents, fill-down, fill-right, sep-rows, insert-row-above, insert-row-below, duplicate-rows, delete-rows |
| Group row | copy, sep-group, toggle-group, expand-all, collapse-all |
| Header | sort-asc, sort-desc, clear-sort, sep-sort, group-by, ungroup, sep-group, pin-start, pin-end, unpin, hide-column, sep-move, move-toward-start, move-toward-end, sep-autosize, widen-column, narrow-column, autosize, autosize-all |
These ids appear only under a condition:
| Ids | Present |
|---|---|
fill-down, fill-right | On a touch selection, with the fill add-on |
insert-row-above, insert-row-below | With the createRow prop |
duplicate-rows | With the duplicateRow prop |
sort-asc, sort-desc, sep-sort | On a sortable column |
clear-sort | On a sorted column |
group-by, ungroup, sep-group | On a grid that groups, on a column that can group or that is a group column |
expand-all | When the client builds the groups |
unpin | On a pinned column |
hide-column | On a column that can hide |
sep-move, move-toward-start, move-toward-end | On a column that can reorder, with column reorder on |
sep-autosize, widen-column, narrow-column, autosize, autosize-all | When the grid and the column allow resizing |
A column that cannot pin gets disabled pin-start, pin-end, and unpin items.
Example
This sample keeps Copy and Delete in the cell menu and one sort item in both header menus:
import {
DataGridBody,
DataGridHeader,
DataGridProvider,
DataGridRoot,
defineColumns,
} from "@/components/data-grid/data-grid";
import {
DataGridContextMenu,
DataGridHeaderDropdown,
} from "@/components/data-grid-context-menu/data-grid-context-menu";
type Row = { id: string; name: string };
const rows: Row[] = [{ id: "1", name: "Ada" }];
const columns = defineColumns<Row>()([
{ id: "name", header: "Name", accessorKey: "name" },
]);
const KEEP_CELL = ["copy", "delete-rows"];
export function TrimmedMenus() {
return (
<DataGridProvider defaultData={rows} columns={columns}>
<DataGridContextMenu
getCellMenuItems={(_ctx, defaults) =>
defaults.filter((item) => KEEP_CELL.includes(item.id))
}
getHeaderMenuItems={(_ctx, defaults) =>
defaults.filter((item) => item.id === "sort-asc")
}
>
<DataGridRoot
className="h-80"
renderHeaderMenu={(ctx) => <DataGridHeaderDropdown {...ctx} />}
>
<DataGridHeader />
<DataGridBody />
</DataGridRoot>
</DataGridContextMenu>
</DataGridProvider>
);
}The header dropdown inherits getHeaderMenuItems from the wrapper. The ids copy and delete-rows always exist. Outside DataGridProvider, the menu throws.
Related
- Context menu: the menus, the surfaces, and the touch gesture.
- Row operations: the operations behind the row items.
- Labels: the text of every item.
Client pagination options
Look up the options and result of useDataGridPagination, the props of DataGridPaginationBar, and the parts you compose into a custom footer.
Pinned rows options
Look up the options of useDataGridPinnedRows, aggregateRow, serverAggregateRow, and useDataGridAggregate, plus the reducers and the reporter props.