Skip to the content
human-kit design
53 — Data display

Table

Rows of data in columns, sorted, resized and chosen with the pointer or the keyboard.

Patient Status Cycle day Doctor
AG Ana Gómez Stimulation 12 Dr. Ruiz
LP Lucía Pérez Transfer 3 Dr. Vega
MD Marta Díaz Pregnant 21 Dr. Ruiz
Selection unavailable for this row.

Installation

pnpm dlx @human-kit/design add table
npx @human-kit/design add table
yarn dlx @human-kit/design add table
bunx @human-kit/design add table

The command copies the files to src/lib/components/ui/table. It also installs the packages that the files import.

Usage

<script lang="ts">
	import * as Table from '$lib/components/ui/table';
</script>

<Table.Root aria-label="Patients">
	<Table.Header>
		<Table.Row>
			<Table.Head id="name" rowHeader>Patient</Table.Head>
			<Table.Head id="doctor">Doctor</Table.Head>
		</Table.Row>
	</Table.Header>
	<Table.Body>
		{#each patients as patient (patient.id)}
			<Table.Row id={patient.id}>
				<Table.Cell>{patient.name}</Table.Cell>
				<Table.Cell>{patient.doctor}</Table.Cell>
			</Table.Row>
		{/each}
	</Table.Body>
</Table.Root>

Examples

A patient list

A list as a product screen draws it: a search above, the patient pinned at the start while the rest scrolls sideways, the header on top while the rows scroll under it. Each head that orders the list is sortable, the patient and the treatment can be resized, and a long treatment ends in an ellipsis. Choose rows and a bar rises over the foot of the list with what is done to them. The next visit is printed in the time zone of the clinic, written out.

12 patients
Treatment Actions
AM Ana María Gómez Stimulation 8 IVF with ICSI, own eggs Dr. Ruiz $8,450.00
DS Daniela Suárez Stimulation 10 IVF with ICSI and embryo glue Dr. Ruiz $9,100.00
VC Valentina Castro Stimulation 6 Egg freezing, two cycles Dr. Vega $6,300.00
LP Lucía Pérez Transfer 19 Frozen embryo transfer, natural cycle Dr. Vega $3,200.00
LF Laura Fernández Transfer 17 Fresh embryo transfer after ICSI Dr. Ruiz $4,100.00
MD Marta Díaz Ocampo Beta pending 27 IVF with donor eggs and PGT-A on five embryos Dr. Ruiz $12,900.00
NV Natalia Vargas Stimulation 4 Egg freezing, one cycle Dr. Luna $5,400.00
IM Isabella Moreno Beta pending 25 Frozen embryo transfer, medicated cycle Dr. Luna $3,600.00
PH Paula Herrera Cancelled 9 IVF with ICSI, poor response to stimulation Dr. Vega $2,750.00
SR Sofía Romero Pregnant 41 IUI, stimulated cycle Dr. Luna $1,850.00
GT Gabriela Torres Pregnant 52 IVF with donor eggs Dr. Vega $11,200.00
CR Camila Rojas On hold 2 IVF with ICSI, own eggs Dr. Luna $0.00
Selection unavailable for this row.

Badges, avatars and dates

A state reads as a Badge in its tone, a person as a small Avatar beside the name, both on the 20px scale that fits a 32px row. An instant is printed in the zone of the clinic, set in Intl.DateTimeFormat and named in the header, and a number takes tabular-nums at the end of its cell.

Patient Beta test Doctor Transfer (Bogotá)
AM Ana María Gómez Positive DR Dr. Ruiz
LP Lucía Pérez Pending DV Dr. Vega
MD Marta Díaz Ocampo Negative DL Dr. Luna
Selection unavailable for this row.

Sorting

A sortable head turns its name into a button that sets sortDescriptor; the table does not reorder the rows, your code sorts them from it. The arrow shows once a column sorts, and a faint pair under the pointer before. In a column aligned to the end, the arrow leads, so the name ends where the numbers end.

Marta Díaz Dr. Ruiz 21 2
Ana Gómez Dr. Ruiz 12 4
Sofía Romero Dr. Luna 8 6
Lucía Pérez Dr. Vega 3 0
Selection unavailable for this row.

Search and filters

The search and the filters sit above the table and filter the rows your code gives it. When nothing is left, Table.Empty says why.

Patient Doctor Stage
Ana Gómez Dr. Ruiz Stimulation
Lucía Pérez Dr. Vega Transfer
Marta Díaz Dr. Ruiz Beta pending
Valentina Castro Dr. Vega Stimulation
Sofía Romero Dr. Luna Transfer
Selection unavailable for this row.

Selection

With selectionMode="multiple", a Table.Checkbox in a column of width={40} with the check look of tableStyles chooses a row, and the one in the header chooses all, or shows a dash while some are. A chosen row takes the grey of the pointer and keeps its ink. disabledKeys names the rows that cannot be chosen.

Embryo Stage Grade
EMB-2041 Blastocyst, day 5 4AA
EMB-2042 Blastocyst, day 5 4AB
EMB-2043 Blastocyst, day 6 3BB
EMB-2044 Morula, day 4 Arrested
Selection unavailable for this row.

1 of 3 chosen to thaw

Row actions

onRowAction runs when a row is pressed or Enter is pressed on it: to open its record. The rest goes in a menu in a Table.InteractiveCell with the actions look: its button takes the pointer and the keys, and does not choose or open the row.

Time Patient Visit Actions
08:30 Ana Gómez Follicle scan
10:00 Lucía Pérez Embryo transfer
11:45 Marta Díaz Consultation
Selection unavailable for this row.

Press a row to open it

Column visibility

hiddenColumns names the columns to leave out: they leave the layout, the keyboard and the column count, and keep their width for when they come back. Here a checkbox group in a popover sets it. The column that names the row always stays.

Patient Doctor Stage Cycle day
Ana Gómez Dr. Ruiz Stimulation 8
Lucía Pérez Dr. Vega Transfer 19
Marta Díaz Dr. Ruiz Beta pending 27
Selection unavailable for this row.

Column widths

width fixes a column, defaultWidth starts it, and resizable adds a grip at its end, by pointer or by keyboard, within minWidth and maxWidth. A cell with truncate ends its text in an ellipsis; put the whole text in its title.

Date Author Note
2 Dec Dr. Ruiz Eleven follicles over 14 mm on the left ovary, nine on the right. Trigger tonight at 22:00.
30 Nov Nurse Ortega Patient reports mild bloating, no pain. Reminded her of the fluids.
28 Nov Dr. Ruiz Estradiol at 1,840 pg/mL. Keep the dose and scan again in two days.
Selection unavailable for this row.

Pinned columns

pin="left" or pin="right" keeps a column at an edge while the frame scrolls sideways, with a rule on the side the rest passes under. The header always stays on top: give the frame a height with frameClass and it scrolls under it.

Embryo D1 D2 D3 D4 D5 D6 Outcome
EMB-2041 2-cell4-cell3AB4AA8AM Frozen
EMB-2042 2-cell4-cell4AA8AM3AB Transferred
EMB-2043 2-cell4-cell8AM3AB4AA Discarded
EMB-2044 2-cell4-cellM3AB4AA8A Frozen
EMB-2045 2-cell4-cell3AB4AA8AM Transferred
EMB-2046 2-cell4-cell4AA8AM3AB Discarded
EMB-2047 2-cell4-cell8AM3AB4AA Frozen
EMB-2048 2-cell4-cellM3AB4AA8A Transferred
EMB-2049 2-cell4-cell3AB4AA8AM Discarded
EMB-2050 2-cell4-cell4AA8AM3AB Frozen
Selection unavailable for this row.

Groups

A Table.SectionRow heads each group with its count and its total. Its onAction runs on a press, Enter or Space, to fold the group; say so with aria-expanded. The rows of a folded group are left out of the body.

Patient Stage Balance
Dr. Ruiz 3 $30,450.00
Ana María Gómez Stimulation $8,450.00
Marta Díaz Ocampo Beta pending $12,900.00
Daniela Suárez Stimulation $9,100.00
Dr. Vega 2 $9,500.00
Lucía Pérez Transfer $3,200.00
Valentina Castro Stimulation $6,300.00
Selection unavailable for this row.

Expandable rows

A chevron button in a Table.InteractiveCell opens a row, and its detail follows as rows of their own in the same columns, a step in and a step smaller: the embryos of a cycle under it. The button carries aria-expanded.

Embryos Cycle Patient Embryos
IVF-318 Ana María Gómez 3
IVF-318-1 Day 5, transferred 4AA
IVF-318-2 Day 5, frozen 4AB
IVF-318-3 Day 6, frozen 3BB
IVF-322 Lucía Pérez 1
IVF-327 Marta Díaz Ocampo 0
Selection unavailable for this row.

Sections, footer and totals

Table.SectionRow is a heading across every column, between groups of rows. It is not a data row: it has no checkbox and is never chosen. Table.Footer closes the table with a total over a rule.

Time Patient Visit
Monday 24
08:30 Ana Gómez Scan
10:00 Lucía Pérez Consultation
Tuesday 25
09:15 Marta Díaz Retrieval
Total 3 visits
Selection unavailable for this row.

Pages

Slice the rows to the page and put Pagination under the table, with the range in words beside it. Every page keeps the same number of rows, so the pager stays where it is.

Invoice Patient Total
FE-4810 Ana Gómez $180.00
FE-4811 Lucía Romero $317.00
FE-4812 Marta Luna $454.00
FE-4813 Sofía Pérez $591.00
FE-4814 Valentina Ruiz $728.00
Selection unavailable for this row.

1–5 of 47

Loading

While the rows load, the body has none, and Table.Empty shows a Spinner with what it waits for. The kit draws no skeletons. Mark the table aria-busy meanwhile.

Test Value Taken
Estradiol 1,840 pg/mL 07:10
Progesterone 0.9 ng/mL 07:10
LH 4.2 mIU/mL 07:10
Selection unavailable for this row.

Empty

Put Table.Empty in the body: it shows while the body has no rows, across every column, and says why the list is empty.

Invoice Patient Total
No invoices wait to be sent.
Selection unavailable for this row.

A long table

For thousands of rows, give the body the array as items, a snippet that draws one row, and virtualizer={{ rowHeight: 32 }}: only the rows near the viewport are drawn. The frame is the scroller, so give it a height with frameClass, and keep every row 32px. keyboardNavigation="row" makes one focus stop per row, not per cell, which is cheaper while rows come and go. Give the table a least width with class, so a phone scrolls it sideways.

Record Patient Cycle day Stage
HC-00001 Ana Gómez 1 Stimulation
HC-00002 Lucía Castro 12 Culture
HC-00003 Marta Vega 23 Beta test
HC-00004 Sofía Romero 6 Retrieval
HC-00005 Valentina Pérez 17 Transfer
HC-00006 Camila Rojas 28 Stimulation
HC-00007 Laura Luna 11 Culture
HC-00008 Paula Ruiz 22 Beta test
HC-00009 Ana Díaz 5 Retrieval
HC-00010 Lucía Gómez 16 Transfer
HC-00011 Marta Castro 27 Stimulation
HC-00012 Sofía Vega 10 Culture
HC-00013 Valentina Romero 21 Beta test
HC-00014 Camila Pérez 4 Retrieval
HC-00015 Laura Rojas 15 Transfer
HC-00016 Paula Luna 26 Stimulation
HC-00017 Ana Ruiz 9 Culture
HC-00018 Lucía Díaz 20 Beta test
HC-00019 Marta Gómez 3 Retrieval
HC-00020 Sofía Castro 14 Transfer
HC-00021 Valentina Vega 25 Stimulation
HC-00022 Camila Romero 8 Culture
HC-00023 Laura Pérez 19 Beta test
HC-00024 Paula Rojas 2 Retrieval
HC-00025 Ana Luna 13 Transfer
HC-00026 Lucía Ruiz 24 Stimulation
HC-00027 Marta Díaz 7 Culture
HC-00028 Sofía Gómez 18 Beta test
HC-00029 Valentina Castro 1 Retrieval
HC-00030 Camila Vega 12 Transfer
Selection unavailable for this row.

Guidelines

  • Use a table for records the user compares across the same fields. For a list read one item at a time, use a list.
  • Use outline on the page, where the table is a card of its own, and ghost inside a card or a panel. A ghost table on another surface sets --table-surface to it, which the sticky header and the pinned columns paint.
  • Mark the column that names each row with rowHeader, as the patient or the invoice number.
  • Align numbers to the end, with text-end tabular-nums, so their digits line up.
  • Make a column sortable when its order helps the task; then sort the rows from sortDescriptor.
  • Use onRowAction for a row that opens its record, and selectionMode for rows the user acts on together. What is done to the chosen rows goes in a bar that shows while there are any.
  • Print an instant with its time zone written out, as timeZone in Intl.DateTimeFormat: without it, the reader guesses the hour.
  • Pin the column that names the row on a table wider than its frame, so each row stays recognisable while it scrolls.

Accessibility

  • The table is an interactive grid over native table markup. Name it with aria-label or aria-labelledby.
  • One tab stop enters the table; the arrows move from cell to cell, header included. keyboardNavigation="row" moves by row instead, and "none" leaves the body out.
  • On a Table.InteractiveCell the arrows move as on any cell; once the focus is on a control inside it, that control takes the keys and the pointer.
  • Space chooses the focused row. Enter runs its action, or chooses it when there is no action. Disabled rows are skipped.
  • A sort changes aria-sort on its head, and a polite live region announces the new order.
  • A resize grip is a separator: Enter starts the resize, the horizontal arrows change the width, Home and End go to the least and to the width of the content, and Enter again ends it.
  • Cells of a rowHeader column are rowheader cells, so a screen reader names each row by them.

API reference

Table.Root

PropTypeDefault
variant'outline' | 'filled' | 'ghost'

A card of its own around the rows, a soft grey, or the rows on the surface below.

'outline'
frameClassstring

The class of the box around the table, which scrolls. Give it a height and the header stays on top of the rows.

—
selectionMode'none' | 'single' | 'multiple'

How many rows the user can choose.

'none'
selectionBehavior'toggle' | 'replace' | 'checkbox'

What a press on a row does to the choice. With checkbox, only the box, Space or select-all change it.

'toggle'
selectedKeysIterable<TableSelectionKey>

The chosen rows, with onSelectionChange.

—
defaultSelectedKeysIterable<TableSelectionKey>

The chosen rows at the start.

—
onSelectionChange(keys: Set<TableSelectionKey>) => void

Called when the choice changes.

—
disallowEmptySelectionboolean

Keeps at least one row chosen.

false
disabledKeysIterable<TableSelectionKey>

The rows the user cannot choose. Under disabledBehavior="all" they take no focus or press either.

—
disabledBehavior'selection' | 'all'

Whether a disabled row only refuses the choice, or every press.

'all'
sortDescriptorTableSortDescriptor

The column and the direction of the sort. Bindable.

—
defaultSortDescriptorTableSortDescriptor

The sort at the start.

—
onSortChange(descriptor: TableSortDescriptor | undefined) => void

Called when a sortable head is pressed.

—
onRowAction(id: TableSelectionKey) => void

Called when a row is pressed, or Enter is pressed on it: to open it.

—
keyboardNavigation'grid' | 'row' | 'none'

How far the focus goes into the body: each cell, each row, or none.

'grid'

Plus every attribute of a <table>, and hiddenColumns, columnWidths, defaultColumnWidths, onColumnWidthsChange, ssrMinTableWidth and context of the primitive.

Head

PropTypeDefault
idstring

The key of the column, the one sortDescriptor.column holds. Required.

—
rowHeaderboolean

Its cells name their rows.

—
sortableboolean

Its name is a button that sorts the table by it.

false
resizableboolean

A grip at its end changes its width, by pointer or by keyboard.

false
widthnumber | `${number}px` | `${number}%` | `${number}fr`

A width it keeps, also with a grip.

—
defaultWidthnumber | `${number}px` | `${number}%` | `${number}fr`

The width at the start, which a grip can change.

—
minWidthnumber

The least width a resize leaves it, in px.

—
maxWidthnumber

The most width a resize gives it, in px.

—
pin'left' | 'right'

Keeps it at an edge while the table scrolls sideways.

—
textValuestring

The name read aloud for the column, when the content is not plain text.

—

The column and its header cell in one part.

Row

PropTypeDefault
idTableSelectionKey

The key of the row, for the choice and for onRowAction.

—
disabledboolean

It cannot be chosen, and the focus skips it.

—
textValuestring

The text that names the row.

—

Plus every attribute of a <tr>.

Cell

PropTypeDefault
columnIndexnumber

The column it belongs to, when the row skips columns, as after a colspan.

—

Plus every attribute of a <td>. InteractiveCell takes the same, for a cell with controls of its own.

SectionRow

PropTypeDefault
idTableSelectionKey

The key of the group it heads.

—
onAction() => void

Called on a press, Enter or Space: to fold or unfold the group.

—

Plus every attribute of a <tr>. It draws its one cell across every column.

Checkbox.Root

PropTypeDefault
aria-labelstring

Its name, in place of the one the table gives it.

—

Plus every attribute of a <span>. In the header it chooses every row, and shows only with selectionMode="multiple".

Body

PropTypeDefault
itemsreadonly T[]

The rows as data, drawn by the children snippet, for a virtualized body.

—
virtualizerTableBodyVirtualizer<T>

Draws only the rows in view: rowHeight, and overscan for the rows around them.

—
isSectionItem(item: T) => boolean

Which items are section rows, for a virtualized body.

—

Header, Body and Footer take every attribute of their element. Empty takes a class and its content.