Playground

Data table

A table with sortable columns, row selection and pagination.

  • Built on native elements
  • Native table; sortable headers hold a real button and the th carries aria-sort (ascending, descending, none).
  • Each row and the select-all box are checkboxes with names; select-all shows the indeterminate state for a partial selection.
  • The count of selected rows is announced through a status region.
  • Styles in one file: 14-table.css

Import

import '@monoline/css';
import {DataTable} from '@monoline/react';

Not installed yet? Pick a route.

Copies the source and its styles into your project.

npx monoline add data-table

Usage

import {DataTable, type DataTableColumn} from '@monoline/react';

interface Person {id: string; name: string; role: string; projects: number}

const people: Person[] = [
  ['Ada Lovelace', 'Engineer', 4], ['Grace Hopper', 'Admiral', 9], ['Alan Turing', 'Researcher', 6], ['Katherine Johnson', 'Mathematician', 7],
  ['Linus Torvalds', 'Maintainer', 12], ['Margaret Hamilton', 'Director', 5], ['Dennis Ritchie', 'Engineer', 3], ['Barbara Liskov', 'Professor', 8],
].map(([name, role, projects], i) => ({id: String(i), name: name as string, role: role as string, projects: projects as number}));

const columns: DataTableColumn<Person>[] = [
  {id: 'name', header: 'Name', sortable: true},
  {id: 'role', header: 'Role', sortable: true},
  {id: 'projects', header: 'Projects', sortable: true, align: 'end'},
];

<DataTable
  label="Team"
  columns={columns}
  data={people}
  defaultSort={{id: 'name', direction: 'asc'}}
/>

Examples

Sortable

Selectable with pages

Empty

Sort by date

accessor returns the Date to sort by; cell formats it for display.

Props

DataTableProps

PropTypeDefaultDescription
columns requiredDataTableColumn<T>[]–The column definitions: each has an `id`, a `header`, and optionally an `accessor`, a `cell` renderer, `sortable` and `align`.
data requiredT[]–The rows to show; each row's `id` field (or `getRowId`) must be unique.
getRowId((row: T, index: number) => string)–Stable id for a row. Defaults to `row.id`, then the index.
label requiredstring–Names the table for assistive tech.
sortDataTableSort | null–Sort state. Controlled with `sort` + `onSortChange`, or uncontrolled with `defaultSort`. `null` means unsorted.
defaultSortDataTableSort | nullnullThe initial sort when uncontrolled, as `{id, direction}`. Default `null` (unsorted).
onSortChange((sort: DataTableSort | null) => void)–Called with the new sort (`null` when sorting is cleared) when the user clicks a sortable header.
selectableboolean–Adds a checkbox column. Selected row ids: controlled with `selection` + `onSelectionChange`, or uncontrolled with `defaultSelection`.
selectionstring[]–Ids of the selected rows when controlled. Pair with `onSelectionChange` and `selectable`.
defaultSelectionstring[][]Ids of the rows selected at first when uncontrolled. Default none.
onSelectionChange((ids: string[]) => void)–Called with the full list of selected row ids when the user checks or unchecks a row.
pageSizenumber–Rows per page. Omit to show every row.
emptyTitleReactNode'No results'Heading shown when there are no rows. Default "No results".
emptyDescriptionReactNode–Optional sentence under the empty heading, for example how to add the first row.

Data Attributes

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

AttributeStylesheet
[aria-invalid="true"]checkbox.css
[data-orientation="horizontal"]checkbox.css
[data-state="checked"]checkbox.css
[data-state="indeterminate"]checkbox.css
[aria-current="page"]18-navigation.css
[aria-current="step"]18-navigation.css
[aria-selected="true"]18-navigation.css
[data-state="done"]18-navigation.css
[data-direction]data-table.css
[data-direction="desc"]data-table.css
[data-state="selected"]data-table.css

CSS classes

ClassStylesheet
.num14-table.css
.table14-table.css
.table-wrap14-table.css
.checkboxcheckbox.css
.checkbox-markcheckbox.css
.radiocheckbox.css
.radio-dotcheckbox.css
.radio-groupcheckbox.css
.swcheckbox.css
.sw-thumbcheckbox.css
.brand18-navigation.css
.crumbs18-navigation.css
.pager18-navigation.css
.step18-navigation.css
.steps18-navigation.css
.tab18-navigation.css
.tabpanel18-navigation.css
.topbar18-navigation.css
.topbar-nav18-navigation.css
.emptyempty-state.css
.empty-actionsempty-state.css
.empty-descriptionempty-state.css
.empty-iconempty-state.css
.empty-titleempty-state.css
.data-tabledata-table.css
.data-table-checkdata-table.css
.data-table-footerdata-table.css
.data-table-sortdata-table.css
.data-table-sort-btndata-table.css
.emptydata-table.css
.tabledata-table.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
.num {
  border-color: var(--accent);
}
.num[aria-invalid="true"] {
  background: var(--wash);
}

Accessibility

  • Native table; sortable headers hold a real button and the th carries aria-sort (ascending, descending, none).
  • Each row and the select-all box are checkboxes with names; select-all shows the indeterminate state for a partial selection.
  • The count of selected rows is announced through a status region.
  • An empty result shows an empty state inside the table; pagination is a nav landmark of buttons.