gridcn2.0.0
Add-on optionsRows, pages, and menus

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.

SymbolPurposeGuide
DataGridContextMenuOpens the cell and header menusContext menu
DataGridHeaderDropdownThe column menu behind the header chevronContext menu
Resolver contextThe context that each resolver receivesContext menu
useDataGridRowOpsTells whether the grid can create and duplicate rowsRow operations
Menu itemThe fields of one itemContext menu
Built-in item idsThe ids that a resolver keeps or dropsContext menu

DataGridContextMenu

Prop

Type

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:

Prop

Type

HeaderMenuItemsCtx

getHeaderMenuItems receives this context:

Prop

Type

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:

FieldTypeDefault
idBuiltInMenuItemId | (string & {})required
labelstringrequired
iconReactNodenone
shortcutstringnone
disabledbooleanfalse
destructivebooleanfalse
tooltipstringnone
onClick() => voidnone

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-.

MenuIds, in order
Cellcut, copy, paste, sep-clipboard, clear-contents, fill-down, fill-right, sep-rows, insert-row-above, insert-row-below, duplicate-rows, delete-rows
Group rowcopy, sep-group, toggle-group, expand-all, collapse-all
Headersort-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:

IdsPresent
fill-down, fill-rightOn a touch selection, with the fill add-on
insert-row-above, insert-row-belowWith the createRow prop
duplicate-rowsWith the duplicateRow prop
sort-asc, sort-desc, sep-sortOn a sortable column
clear-sortOn a sorted column
group-by, ungroup, sep-groupOn a grid that groups, on a column that can group or that is a group column
expand-allWhen the client builds the groups
unpinOn a pinned column
hide-columnOn a column that can hide
sep-move, move-toward-start, move-toward-endOn a column that can reorder, with column reorder on
sep-autosize, widen-column, narrow-column, autosize, autosize-allWhen 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.

On this page