<!-- index: /slotted/llms.txt -->
# data-grid

`data-ui="data-grid"` · Table that sorts, filters and selects records.

## Import

```tsx
import { DataGrid, DataGridDefaultsProvider, type DataGridColumn } from '@slotted/react';
import type { GridColumn, GridQuery, GridColumnState } from '@slotted/core/grid';
import { applyQuery, emptyQuery, toGridParams, fromGridParams } from '@slotted/core/grid';
```

## Minimal example

Example

```tsx
import type { GridColumn } from '@slotted/core/grid';
import { Badge } from '@slotted/react/badge';
import { DataGrid } from '@slotted/react/data-grid';

interface Order {
  id: string;
  customer: string;
  warehouse: string;
  status: string;
  placed: string;
  total: number;
}

const CUSTOMERS = [
  'Alfreds Futterkiste',
  "Bon app'",
  'Ernst Handel',
  'Folk och fä HB',
  'Hanari Carnes',
  'Island Trading',
  'QUICK-Stop',
  'Wartian Herkku',
];
const WAREHOUSES = ['Reno', 'Columbus', 'Savannah'];
const STATUSES = ['Processing', 'Shipped', 'On hold', 'Delivered'];

const ORDERS: Order[] = Array.from({ length: 24 }, (_, index) => ({
  id: `SO-${10421 + index}`,
  customer: CUSTOMERS[(index * 5) % CUSTOMERS.length]!,
  warehouse: WAREHOUSES[index % WAREHOUSES.length]!,
  status: STATUSES[(index * 3) % STATUSES.length]!,
  placed: `2026-09-${String(1 + (index % 28)).padStart(2, '0')}`,
  total: 140 + ((index * 733) % 4100),
}));

const USD = new Intl.NumberFormat('en-US', { currency: 'USD', style: 'currency' });

const TONE: Record<string, 'accent' | 'secondary' | 'success' | 'warning'> = {
  Delivered: 'success',
  'On hold': 'warning',
  Processing: 'accent',
  Shipped: 'secondary',
};

const columns: GridColumn<Order>[] = [
  { id: 'id', label: 'Order', accessor: 'id', rowHeader: true, width: 120, sortable: true },
  {
    id: 'customer',
    label: 'Customer',
    accessor: 'customer',
    flex: 1,
    minWidth: 180,
    sortable: true,
  },
  { id: 'warehouse', label: 'Warehouse', accessor: 'warehouse', width: 130, sortable: true },
  { id: 'status', label: 'Status', accessor: 'status', width: 130, sortable: true },
  { id: 'placed', label: 'Placed', accessor: 'placed', kind: 'date', width: 130, sortable: true },
  {
    id: 'total',
    label: 'Total',
    accessor: 'total',
    numeric: true,
    width: 130,
    sortable: true,
    format: (value) => USD.format(value as number),
  },
];

export default function DataGridBasic() {
  return (
    <DataGrid
      aria-label="Orders"
      cellRenderers={{
        status: ({ value }) => (
          <Badge tone={TONE[String(value)]} variant="soft">
            {String(value)}
          </Badge>
        ),
      }}
      columns={columns}
      height="24rem"
      rowKey="id"
      rows={ORDERS}
      selectionMode="multiple"
    />
  );
}
```

## Anatomy

| Part | Element | Required |
| --- | --- | --- |
| `root` | div[role="grid"] | yes |
| `scroll` | div | yes |
| `head` | div[role="rowgroup"] | yes |
| `header-row` | div[role="row"] | yes |
| `column-group` | div[role="columnheader"] | no |
| `column-header` | div[role="columnheader"] | yes |
| `sort` | span | no |
| `column-description` | span | no |
| `column-actions` | span | no |
| `column-help` | button | no |
| `column-menu` | button | no |
| `column-menu-description` | div[aria-hidden] | no |
| `resizer` | div[aria-hidden] | no |
| `resize-guide` | div[aria-hidden] | no |
| `drop-indicator` | div | no |
| `filter-row` | div[role="row"] | no |
| `filter-cell` | div[role="gridcell"] | no |
| `filter` | button | no |
| `filter-panel` | div | no |
| `filter-condition` | div | no |
| `filter-operator` | div | no |
| `filter-operand` | div | no |
| `filter-join` | div | no |
| `filter-actions` | div | no |
| `body` | div[role="rowgroup"] | yes |
| `pinned-rows` | div[role="rowgroup"] | no |
| `row` | div[role="row"] | yes |
| `row-header` | div[role="rowheader"] | no |
| `cell` | div[role="gridcell"] | yes |
| `editor` | div | no |
| `selection` | div[role="gridcell"] | no |
| `expander` | button | no |
| `detail` | div[role="row"] | no |
| `popin-header` | div[role="columnheader"] | no |
| `popin` | div[role="gridcell"] | no |
| `popin-field` | div | no |
| `popin-label` | span | no |
| `popin-value` | div | no |
| `group-row` | div[role="row"] | no |
| `group-toggle` | button | no |
| `tree-toggle` | button | no |
| `row-handle` | button | no |
| `summary` | div[role="rowgroup"] | no |
| `summary-cell` | div[role="gridcell"] | no |
| `empty` | div | no |
| `loader` | div[role="rowgroup"] | no |
| `footer` | div | no |
| `actions` | div | no |

## Props

See [data-grid.props.md](./data-grid.props.md).

## Events

| Name | Payload | When |
| --- | --- | --- |
| `onQueryChange` | `(query: GridQuery, request: DataGridQueryRequest)` | Before the query moves: a sort press, a filter applied, a search, a page, a group. request.change says which; the reason is pointer, keyboard, clear or programmatic; cancel() refuses it. |
| `onColumnStateChange` | `(state: GridColumnState, request: DataGridColumnStateRequest)` | Before a column is resized, moved, hidden or pinned. request.change says which; the reason is pointer, keyboard or programmatic. A hide is announced; the columns in value.hidden and not in previous.hidden are what an undo shows again. |
| `onOutOfSpaceChange` | `(outOfSpace: GridOutOfSpace)` | After the frame is measured, whenever the columns in the pop-in or the columns hidden for room change. |
| `onValueChange` | `(value: string \| string[] \| null, request)` | Before the row selection moves: a checkbox press, Shift+Space, Ctrl+A, the header checkbox (reason select), or clearSelection (programmatic). |
| `onRangeChange` | `(range: GridRange \| null, request)` | Before the cell rectangle moves, or is cleared with Escape (reason clear). |
| `onCurrentChange` | `(cell: GridCell \| null, request)` | Before the cursor moves: an arrow key, a press, or the grid settling the cursor after its row left (programmatic). |
| `onExpandedChange` | `(keys: string[], request)` | Before a detail opens or closes. |
| `onExpandedGroupsChange` | `(keys: string[] \| 'all', request)` | Before a group opens or closes. |
| `onEditStart / onEditCommit / onEditCancel` | `(cell) / (commit: DataGridEditCommit) / (cell)` | When an editor opens; when a draft is committed by Enter, Tab, F2, a press elsewhere or blur (the commit may return a promise); when Escape cancels it. |
| `onActivate` | `(event: DataGridActivateEvent)` | A double press or Enter on a text cell, or one touch or pen press, while activatable is true. |
| `onLoadMore / onRetry / onManageColumns` | `()` | The window nears the end with hasMore; the error state’s Try again; the column menu’s Manage columns. |

## Axes and defaults

| Axis | Values | Default |
| --- | --- | --- |
| `variant` | `plain`, `ruled`, `framed` | `ruled` |
| `striped` | `true`, `false` | `false` |
| `stickyHeader` | `true`, `false` | `true` |
| `stickyTo` | `scroll`, `page` | `scroll` |
| `selectionMode` | `none`, `single`, `multiple`, `range` | `none` |
| `editMode` | `cell`, `row` | `cell` |
| `mode` | `client`, `server` | — |
| `filterRow` | `true`, `false` | `false` |
| `virtualize` | `auto`, `true`, `false` | `auto` |
| `columnOverflow` | `auto`, `popin`, `scroll` | `auto` |
| `activatable` | `true`, `false` | `false` |
| `align` | `start`, `center`, `end` | `start` |
| `numeric` | `true`, `false` | `false` |
| `text` | `wrap`, `truncate`, `nowrap` | `truncate` |
| `density` | `comfortable`, `compact`, `dense` | — |

## Rules

- `columns`, `disabledKeys` and a function `rowKey` are compared by identity: a new array or function on a render of the parent re-renders every mounted row, so declare them at module scope or memoize them. The renderers — `cellRenderers`, `editorRenderers`, `renderCell`, `renderEditor`, `renderHeader`, `renderGroup`, `renderDetail` — `messages` and `tree` are read when a row renders and may be written inline, and so may `isRowSelectable` and `isRowMuted`: a row renders again only when its own answer changes. A row renders again for its own row object, selection, cursor, edit or expansion, and for a change of `columns` or the language; a renderer whose output reads state outside its row is refreshed by memoizing `columns` on that state.
- The root is role="grid": a composite widget with one tab stop. The current cell — or the single simple widget inside it, a link, a button or a checkbox — is the only element with tabindex="0"; Tab enters there and leaves the grid to the next control of the page, which is what the APG requires of a grid and what a table cannot give.
- Every row carries aria-rowindex against aria-rowcount on the root, and every cell aria-colindex against aria-colcount, always: the grid windows its rows and columns, so the DOM is never the whole set, and the counts are what let a screen reader say “row 4,812 of 9,000”.
- aria-sort is written on the first sort key’s header only — the practice says more than one is ambiguous — and the whole order is announced through the live announcer: “Sorted by Carrier ascending, Weight descending”. A resize announces the column and its width; a filter announces its sentence through describeFilter; a copy announces the count.
- A cell that does not edit while others do carries aria-readonly="true"; a draft that fails carries aria-invalid and its message in the title. A selected row carries aria-selected; a cell in a range carries it too, and the root aria-multiselectable in multiple and range.
- A column with a description is described by it: the header carries aria-describedby to a visually hidden span, and the column menu points at the same span while showing the text aria-hidden, so the words are heard once. The column help is a button with a name — “About ARR” — reached by F2 on the header; Tab cycles the header’s widgets and Escape returns to the cell. Hiding a column is announced as “Carrier hidden”.
- The header checkbox names what it covers — “Select all 3,412 rows”, or the page in server mode — because a selection over a whole result set is a decision only the query can make. Every control the grid draws has a name: the column menu, the filter trigger, the expander, the group toggle, the operator symbol.

## Tokens

`--slotted-border-strong`, `--slotted-border-subtle`, `--slotted-border-width-thick`, `--slotted-border-width-thin`, `--slotted-control-font-family`, `--slotted-control-font-size-md`, `--slotted-control-font-size-sm`, `--slotted-control-gap-md`, `--slotted-control-height-lg`, `--slotted-control-height-md`, `--slotted-control-height-sm`, `--slotted-control-icon-size-md`, `--slotted-control-inner-size-sm`, `--slotted-control-invalid-edge-width`, `--slotted-control-padding-inline-md`, `--slotted-control-padding-inline-sm`, `--slotted-control-transition-duration`, `--slotted-control-transition-easing`, `--slotted-data-grid-band-surface`, `--slotted-data-grid-block-size`, `--slotted-data-grid-border-color`, `--slotted-data-grid-border-color-strong`, `--slotted-data-grid-border-width`, `--slotted-data-grid-cell-padding-block`, `--slotted-data-grid-cell-padding-inline`, `--slotted-data-grid-changed-surface`, `--slotted-data-grid-color`, `--slotted-data-grid-color-muted`, `--slotted-data-grid-control-radius`, `--slotted-data-grid-cursor-color`, `--slotted-data-grid-filter-operator-size`, `--slotted-data-grid-filter-panel-size`, `--slotted-data-grid-filter-panel-size-date`, `--slotted-data-grid-filter-panel-size-datetime`, `--slotted-data-grid-font-family`, `--slotted-data-grid-font-size`, `--slotted-data-grid-gap`, `--slotted-data-grid-head-block-size`, `--slotted-data-grid-header-surface`, `--slotted-data-grid-header-surface-hover`, `--slotted-data-grid-icon-size`, `--slotted-data-grid-indent`, `--slotted-data-grid-pin-shadow-color`, `--slotted-data-grid-pin-shadow-size`, `--slotted-data-grid-radius`, `--slotted-data-grid-range-surface`, `--slotted-data-grid-resize-guide-color`, `--slotted-data-grid-resize-guide-width`, `--slotted-data-grid-row-block-size`, `--slotted-data-grid-row-border-color-selected`, `--slotted-data-grid-row-color-selected`, `--slotted-data-grid-row-max-block-size`, `--slotted-data-grid-row-surface-hover`, `--slotted-data-grid-row-surface-selected`, `--slotted-data-grid-row-surface-selected-hover`, `--slotted-data-grid-row-surface-stripe`, `--slotted-data-grid-shadow`, `--slotted-data-grid-sticky-offset`, `--slotted-data-grid-sticky-offset-end`, `--slotted-data-grid-surface`, `--slotted-disabled-foreground`, `--slotted-duration-long`, `--slotted-duration-loop`, `--slotted-duration-short`, `--slotted-easing-standard`, `--slotted-focus-ring-color`, `--slotted-focus-ring-color-invalid`, `--slotted-focus-ring-offset`, `--slotted-focus-ring-width`, `--slotted-font-size-xs`, `--slotted-font-weight-medium`, `--slotted-font-weight-regular`, `--slotted-font-weight-semibold`, `--slotted-ground`, `--slotted-line-height-md`, `--slotted-popover-gap`, `--slotted-popover-padding-block-sm`, `--slotted-popover-padding-inline-sm`, `--slotted-radius-md`, `--slotted-radius-sm`, `--slotted-scrim`, `--slotted-shadow-xs`, `--slotted-space-1`, `--slotted-space-2`, `--slotted-space-3`, `--slotted-space-4`, `--slotted-space-5`, `--slotted-space-6`, `--slotted-space-8`, `--slotted-surface`, `--slotted-surface-sunken`, `--slotted-target-comfortable`, `--slotted-target-min`, `--slotted-text`, `--slotted-text-muted`, `--slotted-tone-accent-border`, `--slotted-tone-accent-solid`, `--slotted-tone-accent-surface`, `--slotted-tone-accent-text`, `--slotted-tone-danger-border`, `--slotted-tone-secondary-subtle-hover`, `--slotted-z-index-sticky`

## Examples

See [data-grid.examples.md](./data-grid.examples.md).
