Playground

Data grid

A virtualised, keyboard-navigable grid for very large data sets, with sortable columns and row selection.

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

Row selection

Space or a click toggles the focused row.

Empty

Props

DataGridProps

PropTypeDefaultDescription
aria-labelstring–
aria-labelledbystring–
columns requiredDataGridColumn<T>[]–The column definitions: id, header, how to read each cell, width and sortability.
rows requiredT[]–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`.
rowHeightnumber36Row height in px. Every row has the same height; that is what makes virtualising cheap. Default 36.
heightstring | number400Height of the scrolling grid, header included. Default 400.
overscannumber5Extra 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`.
selectedIdsstring[]–Controlled selected row ids. Pair with `onSelectedIdsChange`.
defaultSelectedIdsstring[]–Initially selected row ids when uncontrolled.
onSelectedIdsChange((ids: string[]) => void)–Called with the new list of selected row ids.
sortDataGridSort | null–Controlled sort; `null` is unsorted.
defaultSortDataGridSort | nullnullInitial sort when uncontrolled; `null` is unsorted.
onSortChange((sort: DataGridSort | null) => void)–Called with the new sort, or `null` when sorting is cleared.
manualSortboolean–Do not sort in the grid: `rows` arrive already ordered (server-side sorting).
emptyStateReactNode–Shown when `rows` is empty.

Data Attributes

State is exposed through attributes. Style it with these selectors, not with class toggles.

AttributeStylesheet
[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

ClassStylesheet
.emptyempty-state.css
.empty-actionsempty-state.css
.empty-descriptionempty-state.css
.empty-iconempty-state.css
.empty-titleempty-state.css
.data-griddata-grid.css
.data-grid-bodydata-grid.css
.data-grid-body-rowdata-grid.css
.data-grid-celldata-grid.css
.data-grid-emptydata-grid.css
.data-grid-empty-rowdata-grid.css
.data-grid-headdata-grid.css
.data-grid-rowdata-grid.css
.data-grid-sortdata-grid.css
.data-grid-textdata-grid.css
.data-grid-thdata-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

KeysAction
Arrow keysMove between cells
Home / EndGo to the row ends
Ctrl+Home / Ctrl+EndTo the first and last cell
PageUp / PageDownMove a screenful
Enter / SpaceOn a focused header sorts