Skip to content

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.

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

Revenue by month
MonthRevenue
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 th and td elements; 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 Card with overflow-hidden.

Props

Table

Other table attributes are passed through.

Table props
PropTypeDefaultDescription
captionstringrequiredDescribes the table to screen readers. It is not shown.
compactbooleanfalseTight rows, no side padding and no hover colour.
classNamestring—Added to the table element.
childrenReactNode—The thead and tbody.

SortHeader

A th with a button inside. Other cell attributes are passed through.

SortHeader props
PropTypeDefaultDescription
onSort() => voidrequiredCalled when the heading is pressed.
direction'ascending' | 'descending'—Set only on the sorted column.
childrenReactNode—The heading text.

Tokens

Tokens
TokenDefaultSets
--table-font-size0.875remText size
--table-padding-x1remCell side padding (not compact)
--table-padding-y0.75remCell top and bottom padding (not compact)
--table-row-hovera faint accentRow colour on hover