table

table is a plain HTML table that can do more. It adds arrow keys from cell to cell, rows a person can pick, and headers that ask for a sort.

onChange called 0 time(s)

What the pantry put up
PreserveJarsSealed
Damson jam48In August
Green tomato chutney31Last week
Seville marmalade12In March

Picked: jam

Sort asked for: nothing yet

import { computed, state } from '@markless/core';import { table } from '@markless/ui';export default function Pantry() @{	let picked = state<readonly string[]>(['jam']);	let asked = state('nothing yet');	const shown = computed(() => (picked.length === 0 ? 'nothing' : picked.join(', ')));	<div class="desk">		<table.root			class="grid"			value={picked}			sort={{ column: 'jars', direction: 'descending' }}			onChange={(next: readonly string[]) => {				picked = next;			}}			onSortChange={(column: string) => {				asked = column;			}}		>			<caption class="caption">What the pantry put up</caption>			<thead>				<tr>					<th class="head">Preserve</th>					<table.coltrigger class="head sorts" value="jars">Jars</table.coltrigger>					<table.coltrigger class="head sorts" value="sealed">Sealed</table.coltrigger>				</tr>			</thead>			<tbody>				<table.item class="row" value="jam">					<table.itemcontent class="cell" rowheader>Damson jam</table.itemcontent>					<table.itemcontent class="cell">48</table.itemcontent>					<table.itemcontent class="cell">In August</table.itemcontent>				</table.item>				<table.item class="row" value="chutney">					<table.itemcontent class="cell" rowheader>Green tomato chutney</table.itemcontent>					<table.itemcontent class="cell">31</table.itemcontent>					<table.itemcontent class="cell">Last week</table.itemcontent>				</table.item>				<table.item class="row" value="marmalade">					<table.itemcontent class="cell" rowheader>Seville marmalade</table.itemcontent>					<table.itemcontent class="cell">12</table.itemcontent>					<table.itemcontent class="cell">In March</table.itemcontent>				</table.item>			</tbody>		</table.root>		<p class="readout">			Picked: <span class="readout-value picked">{shown}</span>		</p>		<p class="readout">			Sort asked for: <span class="readout-value asked">{asked}</span>		</p>	</div>;}

Click a cell, then use the arrow keys. Press Space to pick the row you are on. Turn on Multiple, then hold Shift and press Down to pick a run of rows. Press a column header, and the line under the table names it. The rows stay put.

You add the extras one at a time

A bare table.root around your own <thead> and <tbody> is a plain HTML table. It writes no role and no Tab stop. Each part you add turns on one extra:

  • Wrap cells in table.itemcontent, and the arrow keys move from cell to cell.
  • Swap a <th> for a table.coltrigger, and that column can ask for a sort.
  • Write value, onChange or multiple on the root, and rows can be picked. The table then becomes role="grid".

The family does no data work. It does not sort, filter or page your rows. It tells you which header was pressed and which rows are picked. Your own code puts the rows in order.

When to use it

Use table for data in rows and columns, where each column means something. For a list of cards or rich items, use gridlist instead.

Parts

table anatomy
Anatomy perspectiveFlat
Layers
Project ↓StatusBrand studioIn progressWebsiteReady
table.root

Tabular data where the columns are load-bearing: header association, per-column sort, and a person who navigates in two directions.

  • table.root is the <table>. It holds which rows are picked and which column is sorted.
  • table.item is one body row, a <tr>. Its value names the row, and it is required.
  • table.itemcontent is one cell, a <td>. Add rowheader and it renders a <th> that names its row.
  • table.coltrigger is a column header that asks for a sort, a <th>. Its value names the column.
  • table.itemfield is a hidden checkbox that sends a picked row to a form. Put it inside a cell, because a <tr> can only hold cells.

<caption>, <thead>, <tbody>, <tfoot>, the header row and a plain <th> need no parts. Write them as normal HTML.

Sort a column

Pressing a table.coltrigger calls onSortChange with that column's value. It does not pick a direction. Your code decides the next direction, sorts the rows, and passes the result back as sort:

<table.root sort={{ column: 'jars', direction: 'descending' }} onSortChange={turn}>

sort.direction is ascending or descending, the same words aria-sort uses. Every table.coltrigger writes aria-sort. A column that is not the sorted one says none.

Pick rows

Write value, onChange or multiple, and rows can be picked. value is the list of picked row values. onChange receives the whole new list each time it changes. With multiple, a person can pick more than one row.

<table.root multiple value={picked} onChange={keep}>

Styling hooks

Attribute On Present when
ui-selectable root Rows can be picked.
ui-multiple root multiple is set.
ui-disabled root, item The table or this row is disabled.
ui-value item, coltrigger Always. Its value is the row's or column's value.
ui-selected item This row is picked.
ui-sorted coltrigger The rows are sorted by this column.

A table whose rows can be picked gets user-select: none from a rule in @layer markless. Without it, a Shift click paints the page's own text selection across the rows. One rule of yours outside a layer beats it.

Keyboard

The family's tests check each of these rows. They work once the table has table.itemcontent cells or rows that can be picked. Without cells, the arrows move whole rows.

Key What it does
Left arrow or Right arrow Moves to the cell before or after. It stops at the ends of the row.
Down arrow or Up arrow Moves to the same column in the next or the previous row.
Home or End Moves to the first or the last cell of the row.
Ctrl or Cmd with Home or End Moves to the first cell of the table, or the last.
Space Picks the row, or lets go of it.
Shift and Down arrow or Up arrow Picks the run of rows from where you started. Needs multiple.
Ctrl or Cmd and A Picks every row. Needs multiple.
Escape Lets go of every picked row.
Letters Moves to the next row whose text starts with what you typed.
Enter or Space on a table.coltrigger Calls onSortChange for that column.

The table is one Tab stop. When focus comes back, it lands on the cell it left. Each table.coltrigger is a Tab stop of its own, outside the arrow walk. The walk skips a disabled row.

Gotchas

API reference

table.root

valuereadonly string[]
The rows that are picked, named by the value each table.item was written with. Writing it at all is what makes the rows selectable.
multipleboolean
Several rows can be picked at once, and the table says so with aria-multiselectable. Default false.
disabledboolean
No row can be reached or picked, and the table drops out of the tab order. Default false.
sortTableSort
Which column the rows are already sorted by. Omit it and every sortable header reads aria-sort="none"; the family never sorts anything itself.
onChange(value: readonly string[]) => void
Called with the rows that are picked whenever that changes. Writing it is the other way to make the rows selectable.
onSortChange(column: string) => void
Called with the column a table.coltrigger was activated for. It reports the press, not a direction: what the next sort should be depends on the consumer's own policy, and a family that computed it would pick a fight with every data library that computes it differently.

table.coltrigger

valuestring
Required. This column's name, as it appears in sort.column and in onSortChange.

table.item

valuestring
Required. This row's identity in the picked set. Two rows must not share one.
disabledboolean
This row cannot be reached by the walk or picked. Default false.

table.itemcontent

rowheaderboolean
This cell is what names its row: it renders <th scope="row"> with role="rowheader" instead of a <td>. A reader announces it when it reaches the row. Default false.

table.itemfield

No props of its own.