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)
| Preserve | Jars | Sealed |
|---|---|---|
| Damson jam | 48 | In August |
| Green tomato chutney | 31 | Last week |
| Seville marmalade | 12 | In 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>;}
.desk { display: grid; gap: 0.5em; width: min(31rem, 100%); font-family: 'Shantell Sans', system-ui, sans-serif; font-size: var(--step--1); line-height: 1.5; color: var(--ink);}.grid { display: table; width: 100%; margin: 0 0 0.4em; border: 1px solid var(--ink); border-collapse: separate; font-size: inherit; border-radius: 6px; border-spacing: 0; overflow: clip;}.grid:focus-visible { outline: 2px solid var(--ink); outline-offset: 4px;}.caption { padding-block-end: 0.5em; font-family: 'Joy Elia', system-ui, sans-serif; font-size: var(--step-0); text-align: start;}.head,.cell { padding: 0.55em 0.8em; border: 0; font-family: 'Shantell Sans', system-ui, sans-serif; font-size: inherit; font-weight: 400; letter-spacing: normal; text-align: start; text-transform: none; vertical-align: middle; opacity: 1;}.head { border-block-end: 1px solid var(--ink);}.sorts { cursor: pointer; transition: background 140ms ease;}.sorts:hover { background: color-mix(in srgb, var(--purple) 16%, var(--cream)); html.dark & { --ink: var(--on-accent); color: var(--on-accent); }}.sorts::after { content: ''; margin-inline-start: 0.4em;}.sorts[aria-sort='descending']::after { content: '↓';}.sorts[aria-sort='ascending']::after { content: '↑';}.sorts:focus-visible { outline: 2px solid var(--ink); outline-offset: -4px;}.row { cursor: pointer; transition: background 140ms ease;}.row:hover { background: color-mix(in srgb, var(--purple) 16%, var(--cream)); html.dark & { --ink: var(--on-accent); color: var(--on-accent); }}.row[ui-selected] { background: color-mix(in srgb, var(--purple) 26%, var(--cream)); html.dark & { --ink: var(--on-accent); color: var(--on-accent); }}.grid[ui-disabled] .row { opacity: 0.55; cursor: not-allowed;}.row + .row .cell { border-block-start: 1px solid var(--ink);}.cell:focus-visible { outline: 2px solid var(--ink); outline-offset: -4px;}.readout { margin: 0;}.readout-value { color: var(--ink); font-family: var(--font-mono);}@media (prefers-reduced-motion: reduce) { .sorts, .row { transition: none; }}
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 atable.coltrigger, and that column can ask for a sort. - Write
value,onChangeormultipleon the root, and rows can be picked. The table then becomesrole="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
Tabular data where the columns are load-bearing: header association, per-column sort, and a person who navigates in two directions.
table.rootis the<table>. It holds which rows are picked and which column is sorted.table.itemis one body row, a<tr>. Itsvaluenames the row, and it is required.table.itemcontentis one cell, a<td>. Addrowheaderand it renders a<th>that names its row.table.coltriggeris a column header that asks for a sort, a<th>. Itsvaluenames the column.table.itemfieldis 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 thevalueeachtable.itemwas written with. Writing it at all is what makes the rows selectable.multiplebooleanSeveral rows can be picked at once, and the table says so witharia-multiselectable.Defaultfalse.disabledbooleanNo row can be reached or picked, and the table drops out of the tab order.Defaultfalse.sortTableSortWhich column the rows are already sorted by. Omit it and every sortable header readsaria-sort="none"; the family never sorts anything itself.onChange(value: readonly string[]) => voidCalled with the rows that are picked whenever that changes. Writing it is the other way to make the rows selectable.onSortChange(column: string) => voidCalled with the column atable.coltriggerwas 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
valuestringRequired.This column's name, as it appears insort.columnand inonSortChange.
table.item
valuestringRequired.This row's identity in the picked set. Two rows must not share one.disabledbooleanThis row cannot be reached by the walk or picked.Defaultfalse.
table.itemcontent
rowheaderbooleanThis cell is what names its row: it renders<th scope="row">withrole="rowheader"instead of a<td>. A reader announces it when it reaches the row.Defaultfalse.
table.itemfield
No props of its own.