Table
Rows and columns of data, with optional sorting headings.
import { Table, SortHeader } from '@/design-system';Examples
Sortable
The table does not sort anything itself. Sort your rows and tell the heading the direction.
| Company | |
|---|---|
| Drift Freight | $29 |
| Cobalt Labs | $99 |
| Birch Energy | $299 |
'use client';
import { useState } from 'react';
import { Card, SortHeader, Table } from '@/design-system';
const ROWS = [
{ company: 'Birch Energy', price: 299 },
{ company: 'Cobalt Labs', price: 99 },
{ company: 'Drift Freight', price: 29 },
];
export default function TableSortable() {
const [ascending, setAscending] = useState(true);
const rows = [...ROWS].sort(
(a, b) => (a.price - b.price) * (ascending ? 1 : -1),
);
return (
<Card flush className="overflow-hidden">
<Table caption="Customers">
<thead>
<tr>
<th scope="col">Company</th>
<SortHeader
direction={ascending ? 'ascending' : 'descending'}
onSort={() => setAscending(!ascending)}
>
Monthly
</SortHeader>
</tr>
</thead>
<tbody>
{rows.map(row => (
<tr key={row.company}>
<th scope="row">{row.company}</th>
<td className="tabular-nums">${row.price}</td>
</tr>
))}
</tbody>
</Table>
</Card>
);
}Compact
For a small table inside other content.
| Month | Revenue |
|---|---|
| October | $20,120 |
| November | $20,834 |
import { Table } from '@/design-system';
export default function TableCompact() {
return (
<Table caption="Revenue by month" compact>
<thead>
<tr>
<th scope="col">Month</th>
<th scope="col">Revenue</th>
</tr>
</thead>
<tbody>
<tr>
<td>October</td>
<td>$20,120</td>
</tr>
<tr>
<td>November</td>
<td>$20,834</td>
</tr>
</tbody>
</Table>
);
}How it works
- Write ordinary
thandtdelements; they are styled for you. - A wide table scrolls sideways inside its own space. You can also hide columns on small screens with classes such as
hidden sm:table-cell. - The direction is shown with an arrow and set as
aria-sort. - For a framed table, put it in a flush
Cardwithoverflow-hidden.
Props
Table
Other table attributes are passed through.
| Prop | Type | Default | Description |
|---|---|---|---|
caption | string | required | Describes the table to screen readers. It is not shown. |
compact | boolean | false | Tight rows, no side padding and no hover colour. |
className | string | — | Added to the table element. |
children | ReactNode | — | The thead and tbody. |
SortHeader
A th with a button inside. Other cell attributes are passed through.
| Prop | Type | Default | Description |
|---|---|---|---|
onSort | () => void | required | Called when the heading is pressed. |
direction | 'ascending' | 'descending' | — | Set only on the sorted column. |
children | ReactNode | — | The heading text. |
Tokens
| Token | Default | Sets |
|---|---|---|
--table-font-size | 0.875rem | Text size |
--table-padding-x | 1rem | Cell side padding (not compact) |
--table-padding-y | 0.75rem | Cell top and bottom padding (not compact) |
--table-row-hover | a faint accent | Row colour on hover |