Table

A hand-drawn data table with sortable headers and row selection.

Overview

A Table presents rows of structured data with sortable columns and optional row selection. Sorting and selection are controlled — the table renders the rows you give it (in the order you give them) and emits intent via onSortChange / onSelectionChange.

Anatomy

A sketchy outline (@ghds/sketch-core) frames a semantic <table> (flex rows on React Native). Sortable headers are buttons carrying aria-sort; an optional leading checkbox column (with a select-all header) drives selection. This is the data foundation for the M12 data-state patterns.

Variants & States

State Status Notes
Sortable column Implemented Header button toggles asc/desc; aria-sort reflects it (web).
Row selection Implemented Opt-in checkbox column + select-all with an indeterminate state.
Selected row Implemented Highlighted background.

Usage

Do

  • Give each row a stable id and provide the rows already sorted (the table is controlled).
  • Provide a caption for the accessible name.
  • Right-align numeric columns with align="right".

Don't

  • Don't expect the table to sort or paginate data for you — wire onSortChange to your data.
  • Don't put huge datasets in one table without pagination or virtualization.

Accessibility

Key Action
Tab Move between sortable headers, selection checkboxes, and interactive cells.
Enter / Space Activate a header (sort) or toggle a checkbox.

Content

Concise column headers and cell values. Represent booleans/status with a Badge inside a cell.

Cells can hold rich content — a <code> key name, a colour swatch, a size preview. In React, cell values are already ReactNode, so pass the element directly. In Web Components, row values stay string | number; give the column a render(value, row) function that returns a Lit TemplateResult (import html from lit). Columns without render show the raw value. In React Native, cell values are string | number and are shown as text — rich cell content is not supported yet.

Props API

React@ghds/react

PropTypeDefaultDescription
columnsTableColumn[]—{ key, header, sortable?, align? } per column.
rowsTableRow[]—{ id, [key]: ReactNode } — rendered in the given order.
captionstring—Accessible caption.
sort / onSortChangeTableSort / (sort) => void—Controlled sort + intent callback.
selectedIds / onSelectionChangestring[] / (ids) => void—Controlled selection (shows checkboxes).

Web Components@ghds/web-components

PropTypeDefaultDescription
columns / rowsGhTableColumn[] / GhTableRow[]—Set as properties. Row values are string | number.
columns[].render(value, row) => unknown—Optional per-column cell renderer; return a Lit TemplateResult for rich content.
captionstring—Accessible caption.
sortGhTableSort—Current sort (property).
selectedIdsstring[]—Selected ids (property); enables checkboxes.
sort-change / selection-changeCustomEvent—Dispatched intents.

React Native@ghds/react-native

PropTypeDefaultDescription
columns / rowsTableColumn[] / TableRow[]—Same shape as React, but cell values are string | number (rendered as text).
captionstring—Accessible name.
sort / onSortChangeTableSort / (sort) => void—Controlled sort + intent callback.
selectedIds / onSelectionChangestring[] / (ids) => void—Controlled selection.
testIDstring—Test handle for queries.

Code examples

// React — @ghds/react
import { Table, type TableSort } from '@ghds/react/table';

<Table
  caption="Contributors"
  columns={[
    { key: 'name', header: 'Name', sortable: true },
    { key: 'commits', header: 'Commits', sortable: true, align: 'right' },
  ]}
  rows={rows}
  sort={sort}
  onSortChange={setSort}
  selectedIds={selected}
  onSelectionChange={setSelected}
/>;
<!-- Web Components — @ghds/web-components -->
<script type="module">
  import '@ghds/web-components/table';
</script>
<gh-table id="my-table" caption="Contributors"></gh-table>
<script>
  const el = document.getElementById('my-table');
  el.columns = [
    { key: 'name', header: 'Name', sortable: true },
    { key: 'commits', header: 'Commits', sortable: true, align: 'right' },
  ];
  const data = [
    { id: '1', name: 'Ada', commits: 128 },
    { id: '2', name: 'Grace', commits: 342 },
  ];
  el.rows = data;
  el.selectedIds = [];
  el.addEventListener('sort-change', (e) => {
    el.sort = e.detail;
    // gh-table renders rows in the order given, so sort them yourself
    const { key, direction } = e.detail;
    const sign = direction === 'asc' ? 1 : -1;
    el.rows = [...data].sort((a, b) => (a[key] > b[key] ? sign : a[key] < b[key] ? -sign : 0));
  });
  el.addEventListener('selection-change', (e) => { el.selectedIds = e.detail; });
</script>
// Web Components — rich cell content via a column `render` function
import { html } from 'lit';
import '@ghds/web-components/table';

const el = document.querySelector('gh-table');
el.columns = [
  { key: 'token', header: 'Token', render: (value) => html`<code>${value}</code>` },
  { key: 'description', header: 'Description' },
];
el.rows = [{ id: 'bg', token: 'sys.color.bg.primary', description: 'Page background' }];
// React Native — @ghds/react-native
import { Table } from '@ghds/react-native/table';

<Table
  caption="Contributors"
  columns={[{ key: 'name', header: 'Name', sortable: true }]}
  rows={[{ id: '1', name: 'Ada' }]}
  sort={sort}
  onSortChange={setSort}
/>;

Live Demo

React @ghds/react

Web Components @ghds/web-components