Skip to content
Cynosurev0.5.0
SearchStorybookGitHubnpmColour mode: System. Switch to Light

Components / More

DataGrid

A read-only grid for large results: only the rows and columns in view are rendered, so a hundred thousand rows by fifty columns scroll and move focus as quickly as twenty.

Ahundred thousand rows by fifty columns

When to useLink to "When to use"

  • Query results and table data: many rows, many columns, which people scan, sort, resize, select and copy.

Use something else when:

  • the data is a few dozen rows people read rather than work through: Table, a semantic table with row checkboxes
  • rows have no columns: VirtualList
  • cells are edited: not yet (inline editing, column reorder and a filter row come later)

UsageLink to "Usage"

tsx
Copy
import { DataGrid, type DataGridColumn } from "@arshad-shah/cynosure";

const columns: DataGridColumn<Row>[] = fields.map((field) => ({
  key: field.name,
  header: field.name,
  sortable: true,
  resizable: true,
  minWidth: 80,
  align: field.numeric ? "end" : "start",
  cell: (row) => format(row[field.name]),
  cellKind: (row) => (row[field.name] === null ? "null" : field.numeric ? "number" : undefined),
}));

<DataGrid label="Results" columns={columns} rows={rows} getRowId={(row) => row.id} size="sm" />

Columns are width CSS pixels wide (default 160). cell formats a value for display (default: the value as text, objects as JSON); value is the raw value that sorting compares and copying writes (default row[key]). Set the grid's height with style={{ blockSize: … }} or the height token.

SortingLink to "Sorting"

A sortable column's header is a button (a SortButton filling the header cell): ascending, descending, then unsorted. The sort is a TableSort ({ key, direction }), the same as Table's. Numbers compare as numbers, text in the locale's order, and empty values go last either way. Control it with sort and onSortChange; with manualSort you sort the rows (on a server) and the grid only shows and reports the sort.

Sorting cycles and compares

Selection and copyingLink to "Selection and copying"

selectionMode is "none" (default), "row", "cell" or "range". Rows are selected (ids, as Table's); cells are selectedRange, two corners ({ anchor, focus }, each { rowId, key }), so a selection survives sorting and loading more rows. Ctrl/Cmd+C copies the selection as tab-separated text: rows on lines, raw values, and a value holding a tab, a line break or a quote is quoted.

Text inside cells stays selectable: dragging selects text, and a copy with text selected copies that text, the browser's way. Cells are selected by clicking, Shift+click and the keyboard instead, so both work without a mode switch.

Select arange and copy

Full values, nulls and numbersLink to "Full values, nulls and numbers"

tooltip returns the full value of a cell (shown in a Tooltip on hover and keyboard focus only while the cell's text is cut short with an ellipsis, and not read to screen readers, since the cell's text already holds it), or nothing. cellKind says what a cell holds: "null" is muted and italic, "number" takes the number colour token. cellTone colours a cell with a tone (accent, neutral, danger, success, warning, info), over the kind's colour. size="sm" is dense: 28px rows under a 32px header.

Load more, or pagesLink to "Load more, or pages"

onEndReached is called once the last rows come into view, once per row count: append the next rows. For pages, put a Pagination under the grid and give the grid one page of rows; paging is not built in.

tsx
Copy
const [page, setPage] = useState(1);
<DataGrid label="Results" columns={columns} rows={rows.slice((page - 1) * 50, page * 50)} getRowId={(r) => r.id} />
<Pagination count={pageCount(rows.length, 50)} page={page} onChange={setPage} />
Paged with pagination

PropsLink to "Props"

Every prop of DataGrid
PropWhat it doesTypeDefault
labelRequired
Accessible name of the grid.string
columnsRequired
The columns, in display order.DataGridColumn<Row>[]
rowsRequired
The data, one entry per row.readonly Row[]
getRowIdRequired
A stable, unique id per row (selection and React keys).(row: Row) => string
sort
The current sort (controlled); null is unsorted.TableSort | null
defaultSort
Starting sort when uncontrolled; unsorted when left out.TableSort | null
onSortChange
Called when a sortable header is pressed, with the new sort, or null when it goes back to unsorted.((sort: TableSort | null) => void)
manualSort
You sort the rows (e.g. on a server); the grid only shows and reports the sort.boolean
onColumnResize
Called when a column's width has been changed by dragging its edge or by Alt+Left/Right, with the new width.((key: string, width: number) => void)
selectionMode
What clicking, Space and the arrow keys select: "none" (default), "row" (rows: Ctrl/Cmd+click toggles, Shift+click and Shift+arrows extend), "cell" (one cell, following focus) or "range" (a rectangle of cells: Shift+click and Shift+arrows extend it). Ctrl/Cmd+C copies the selection as tab-separated text."none" | "row" | "cell" | "range"
selected
Ids of the selected rows, with selectionMode="row" (controlled), as Table's selected.string[]
defaultSelected
Ids of the rows selected at the start when uncontrolled.string[]
onSelectionChange
Called with the selected row ids when they change.((ids: string[]) => void)
selectedRange
The selected cells, with selectionMode "cell" or "range" (controlled); null is none.DataGridRange | null
defaultSelectedRange
The cells selected at the start when uncontrolled.DataGridRange | null
onSelectedRangeChange
Called with the selected cells when they change.((range: DataGridRange | null) => void)
onRowClick
A row's cell was clicked or tapped.((row: Row, index: number) => void)
onAction
Enter on a row's cell, or a double-click on it (open the row), as VirtualList's and Tree's onAction.((row: Row, index: number) => void)
onEndReached
Called once the last rows come into view, once per row count: load the next rows and append them ("load more"). For pages, put a Pagination under the grid and pass it one page of rows.(() => void)
size
Row height and text size: "sm" is dense. Default "md"."sm" | "md""md"
labels
Replace the built-in English text: the message when there are no rows.Partial<DataGridLabels>
className
Added to the root element, for layout.string
style
Layout only (e.g. blockSize). Restyle through --cy-data-grid-* tokens.CSSProperties

Generated from the type definitions in @arshad-shah/cynosure 0.5.0.

Behaviour and accessibilityLink to "Behaviour and accessibility"

An APG grid (role="grid" with rows, column headers and cells) with one tab stop. aria-rowcount, aria-colcount and each row's and cell's index count every row and column, rendered or not. The focused cell is scrolled into view below the sticky header, and stays rendered while it is scrolled away.

Behaviour and accessibility, table 1
KeyDoes
ArrowsMove between cells; Left and Right follow the reading direction; Up from the first row reaches the header
Home / EndFirst or last cell of the row
Ctrl+Home / Ctrl+EndFirst or last cell of the grid
Page Up / Page DownA view of rows up or down (Page Up from the first row reaches the header)
EnterOn a cell: onAction (so does a double-click). On a sortable header: sorts
Alt+Left / Alt+RightOn a resizable header: narrow or widen the column (Shift for a bigger step); announced through aria-keyshortcuts
SpaceSelects the focused row or cell; Ctrl/Cmd+Space toggles a row; Shift+Space extends
Shift+arrowsExtend the selection (rows, or a range of cells)
Ctrl/Cmd+ASelect every row, or every cell with range
Ctrl/Cmd+CCopy the selection as tab-separated text
TabLeaves the grid

Clicking a cell selects it (or its row); Ctrl/Cmd+click toggles a row; Shift+click extends. onRowClick reports every click or tap on a row. A resizable column's edge is dragged with a mouse, pen or finger; Escape cancels the drag. In right-to-left text the first column is on the right and the edge on a column's left.

TouchLink to "Touch"

A finger scrolls the grid in both directions and a tap acts as a click. Rows and the header are at least 44px tall on a coarse pointer, and a column's edge has a 44px hit area.

ThemingLink to "Theming"

Theming, table 2
TokenDefault
--cy-data-grid-height12 control heights
--cy-data-grid-row-height--cy-control-height-md; with sm, --cy-control-height-sm plus --cy-space-1
--cy-data-grid-header-height--cy-control-height-lg; with sm, --cy-control-height-md
--cy-data-grid-header-background--cy-color-soft
--cy-data-grid-selected-background--cy-color-accent-soft (as Table's selected rows)
--cy-data-grid-cell-padding--cy-space-5; with sm, --cy-space-4
--cy-data-grid-null-color--cy-color-muted
--cy-data-grid-number-color--cy-color-ink

These examples run Cynosure 0.5.0 (commit 50ce30b), the version on npm.

Getting started
Theming
Icons
Accessibility
Code editors and highlighters
Using Cynosure without React
Build-tool adapters
Components
Accordion
AppShell
AspectRatio
Breadcrumbs
Calendar
Card
Container
Disclosure
Divider
DocumentViewport
Grid
Inline
PageHeader
PanZoomCanvas
ScrollArea
SideNav
SortableList
SplitPane
Stack
StatusBar
Tabs
TitleBar
Toolbar
Tree
VirtualList
ZoomControls
Announcer
Avatar
Badge
CodeBlock
DescriptionList
EmptyState
ErrorBoundary
Field
Heading
IconTile
Image
Kbd
List
LoadingDots
Notice
ProgressBar
Prose
Skeleton
Stat
Steps
Table
Text
Button
Checkbox
Checklist
ColorField
ColorSwatches
Combobox
CompareSlider
CopyButton
DateField
FileDrop
IconButton
KeyValueEditor
Link
NumberField
Pagination
PathField
RadioGroup
SearchField
SegmentedControl
Select
SelectionFrame
SignaturePad
Slider
Switch
TagInput
TextArea
TextField
ThemeToggle
TimeField
ButtonBase
ChoiceBase
DialogBase
FileInputBase
HiddenValue
Icon
InputBase
LinkBase
LiveRegion
TextAreaBase
ChatMessage
Choice
DataGrid
DeviceFrame
HighlightedText
HotkeyField
ListRow
OptionList
OverlayFrame
QRCode
RowActions
Section
SiteFooter
SiteHeader
SkipLink
SortButton
VisuallyHidden
CommandPalette
ContextMenu
Dialog
FloatingToolbar
HoverCard
Menu
Menubar
Popover
ProgressOverlay
Sheet
Toast
Tooltip
Changelog