Data table
A table with sortable columns, row selection and pagination.
SourceStyles source@monoline/react 0.4.0Markdown for AI toolsstable
- 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
Props
DataTableProps
| Prop | Type | Default | Description |
|---|---|---|---|
columns required | DataTableColumn<T>[] | – | The column definitions: each has an `id`, a `header`, and optionally an `accessor`, a `cell` renderer, `sortable` and `align`. |
data required | T[] | – | 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 required | string | – | Names the table for assistive tech. |
sort | DataTableSort | null | – | Sort state. Controlled with `sort` + `onSortChange`, or uncontrolled with `defaultSort`. `null` means unsorted. |
defaultSort | DataTableSort | null | null | The 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. |
selectable | boolean | – | Adds a checkbox column. Selected row ids: controlled with `selection` + `onSelectionChange`, or uncontrolled with `defaultSelection`. |
selection | string[] | – | Ids of the selected rows when controlled. Pair with `onSelectionChange` and `selectable`. |
defaultSelection | string[] | [] | 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. |
pageSize | number | – | Rows per page. Omit to show every row. |
emptyTitle | ReactNode | 'No results' | Heading shown when there are no rows. Default "No results". |
emptyDescription | ReactNode | – | 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.
| Attribute | Stylesheet |
|---|---|
[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
| Class | Stylesheet |
|---|---|
.num | 14-table.css |
.table | 14-table.css |
.table-wrap | 14-table.css |
.checkbox | checkbox.css |
.checkbox-mark | checkbox.css |
.radio | checkbox.css |
.radio-dot | checkbox.css |
.radio-group | checkbox.css |
.sw | checkbox.css |
.sw-thumb | checkbox.css |
.brand | 18-navigation.css |
.crumbs | 18-navigation.css |
.pager | 18-navigation.css |
.step | 18-navigation.css |
.steps | 18-navigation.css |
.tab | 18-navigation.css |
.tabpanel | 18-navigation.css |
.topbar | 18-navigation.css |
.topbar-nav | 18-navigation.css |
.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-table | data-table.css |
.data-table-check | data-table.css |
.data-table-footer | data-table.css |
.data-table-sort | data-table.css |
.data-table-sort-btn | data-table.css |
.empty | data-table.css |
.table | data-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.