Data grid
A virtualised, keyboard-navigable grid for very large data sets, with sortable columns and row selection.
SourceStyles source@monoline/react 0.4.0Markdown for AI toolsbeta
- Built on native elements
- role="grid" with aria-rowcount, aria-colcount, aria-rowindex and aria-colindex so assistive technology knows the full size although only visible rows are in the DOM.
- Roving tabindex: one Tab stop; arrow keys move between cells, Home and End go to the row ends, Ctrl+Home and Ctrl+End to the first and last cell, PageUp and PageDown move a screenful.
- Sortable headers carry aria-sort (ascending, descending, none); Enter or Space on a focused header sorts.
- Styles in one file:
empty-state.css
Import
import '@monoline/css';
import {DataGrid} from '@monoline/react';
Not installed yet? Pick a route.
Copies the source and its styles into your project.
npx monoline add data-grid
Usage
import {DataGrid, type DataGridColumn} from '@monoline/react';
interface Order { id: string; customer: string; city: string; items: number; total: number }
const cities = ['Pune', 'Berlin', 'Lagos', 'Lima', 'Oslo', 'Hanoi', 'Cairo', 'Quito'];
const make = (count: number): Order[] => Array.from({length: count}, (_, i) => ({
id: `ORD-${i + 1}`, customer: `Customer ${(i * 7919) % 10007}`, city: cities[i % cities.length]!, items: (i * 13) % 9 + 1, total: ((i * 2654435761) % 100000) / 100,
}));
const columns: DataGridColumn<Order>[] = [
{id: 'id', header: 'Order', field: 'id', width: 120, sortable: true},
{id: 'customer', header: 'Customer', field: 'customer', sortable: true},
{id: 'city', header: 'City', field: 'city', sortable: true},
{id: 'items', header: 'Items', field: 'items', width: 90, sortable: true, align: 'end'},
{id: 'total', header: 'Total', cell: (r) => r.total.toFixed(2), sortValue: (r) => r.total, width: 110, sortable: true, align: 'end'},
];
const big = make(100000);
<DataGrid
aria-label="Orders, 100,000 rows"
columns={columns}
rows={big}
height={320}
getRowId={(r) => r.id}
/>
Examples
100,000 rows
Only the visible rows are in the DOM. Click a header to sort, use the arrow keys, PageUp, PageDown, Ctrl+Home and Ctrl+End to move.
Empty
Props
DataGridProps
| Prop | Type | Default | Description |
|---|---|---|---|
aria-label | string | – | |
aria-labelledby | string | – | |
columns required | DataGridColumn<T>[] | – | The column definitions: id, header, how to read each cell, width and sortability. |
rows required | T[] | – | All the rows. Only the visible ones are rendered, so tens of thousands are fine. |
getRowId | ((row: T, index: number) => string) | – | Stable id per row, used for selection. Default: the row's position in `rows`. |
rowHeight | number | 36 | Row height in px. Every row has the same height; that is what makes virtualising cheap. Default 36. |
height | string | number | 400 | Height of the scrolling grid, header included. Default 400. |
overscan | number | 5 | Extra rows rendered above and below the visible ones. Default 5. |
selectionMode | "none" | "multiple" | "single" | 'none' | Whether rows can be selected: `none` (default), `single` or `multiple`. |
selectedIds | string[] | – | Controlled selected row ids. Pair with `onSelectedIdsChange`. |
defaultSelectedIds | string[] | – | Initially selected row ids when uncontrolled. |
onSelectedIdsChange | ((ids: string[]) => void) | – | Called with the new list of selected row ids. |
sort | DataGridSort | null | – | Controlled sort; `null` is unsorted. |
defaultSort | DataGridSort | null | null | Initial sort when uncontrolled; `null` is unsorted. |
onSortChange | ((sort: DataGridSort | null) => void) | – | Called with the new sort, or `null` when sorting is cleared. |
manualSort | boolean | – | Do not sort in the grid: `rows` arrive already ordered (server-side sorting). |
emptyState | ReactNode | – | Shown when `rows` is empty. |
Data Attributes
State is exposed through attributes. Style it with these selectors, not with class toggles.
| Attribute | Stylesheet |
|---|---|
[aria-sort="ascending"] | data-grid.css |
[aria-sort="descending"] | data-grid.css |
[data-align="end"] | data-grid.css |
[data-direction] | data-grid.css |
[data-direction="desc"] | data-grid.css |
[data-sortable] | data-grid.css |
[data-state="selected"] | data-grid.css |
CSS classes
| Class | Stylesheet |
|---|---|
.empty | empty-state.css |
.empty-actions | empty-state.css |
.empty-description | empty-state.css |
.empty-icon | empty-state.css |
.empty-title | empty-state.css |
.data-grid | data-grid.css |
.data-grid-body | data-grid.css |
.data-grid-body-row | data-grid.css |
.data-grid-cell | data-grid.css |
.data-grid-empty | data-grid.css |
.data-grid-empty-row | data-grid.css |
.data-grid-head | data-grid.css |
.data-grid-row | data-grid.css |
.data-grid-sort | data-grid.css |
.data-grid-text | data-grid.css |
.data-grid-th | data-grid.css |
Customization
Style it with the kit's tokens in your own CSS, loaded after @monoline/css. Prefer the attributes above over extra classes. See Theming for every token.
app.css
.empty {
border-color: var(--accent);
}
.empty[aria-sort="ascending"] {
background: var(--wash);
}
Accessibility
- role="grid" with aria-rowcount, aria-colcount, aria-rowindex and aria-colindex so assistive technology knows the full size although only visible rows are in the DOM.
- Roving tabindex: one Tab stop; arrow keys move between cells, Home and End go to the row ends, Ctrl+Home and Ctrl+End to the first and last cell, PageUp and PageDown move a screenful.
- Sortable headers carry aria-sort (ascending, descending, none); Enter or Space on a focused header sorts.
- With selectionMode, rows carry aria-selected and Space or a click toggles the focused row; the grid is aria-multiselectable in multiple mode.
- The header row stays visible while scrolling; an empty grid shows an empty state.
Keyboard
| Keys | Action |
|---|---|
| Arrow keys | Move between cells |
| Home / End | Go to the row ends |
| Ctrl+Home / Ctrl+End | To the first and last cell |
| PageUp / PageDown | Move a screenful |
| Enter / Space | On a focused header sorts |