Table
Rows of things with the same columns: hostnames, clients, projects to pick, usage per model. A compound component on a CSS grid with the roles of a table, so a cell holds any control and a column can leave when the table gets narrow. For the facts of one thing, use a KeyValueList; for a list without columns, a ListRow.
import { Table } from '@adecore/ui';import { IconButton, Menu, Switch, Table } from '@adecore/ui';
import { Ellipsis } from 'lucide-react';
import { useState } from 'react';
const GROUPS = [
{
label: 'Production',
hosts: [
{ name: 'shop.example.com', target: 'localhost:3000' },
{ name: 'api.example.com', target: 'localhost:8080' }
]
},
{
label: 'Staging',
hosts: [{ name: 'staging.example.com', target: 'localhost:3001' }]
}
];
const COLUMNS = [{ width: 'minmax(0,2fr)' }, { width: 'minmax(0,1fr)', hideBelow: 'md' }, { width: 'auto' }, { width: 'auto' }] as const;
export default function TableDemo() {
const [live, setLive] = useState<Record<string, boolean>>({ 'shop.example.com': true, 'api.example.com': true });
return (
<div className="w-full max-w-xl rounded-xl border border-border bg-surface">
<Table.Root label="Hostnames" columns={COLUMNS}>
<Table.Head>
<Table.Column>Hostname</Table.Column>
<Table.Column>Points to</Table.Column>
<Table.Column>Live</Table.Column>
<Table.Column>
<span className="sr-only">Actions</span>
</Table.Column>
</Table.Head>
{GROUPS.map((group) => (
<Table.Group key={group.label} label={group.label}>
{group.hosts.map((host) => (
<Table.Row key={host.name}>
<Table.Cell>{host.name}</Table.Cell>
<Table.Cell mono>{host.target}</Table.Cell>
<Table.Cell>
<Switch
label={`Serve ${host.name}`}
checked={live[host.name] ?? false}
onCheckedChange={(checked) => setLive((current) => ({ ...current, [host.name]: checked }))}
/>
</Table.Cell>
<Table.Cell>
<Menu.Root>
<IconButton icon={Ellipsis} label={`Actions for ${host.name}`} size="xs" render={<Menu.Trigger />} />
<Menu.Popup align="end">
<Menu.Item>Copy address</Menu.Item>
<Menu.Item>Remove</Menu.Item>
</Menu.Popup>
</Menu.Root>
</Table.Cell>
</Table.Row>
))}
</Table.Group>
))}
</Table.Root>
</div>
);
}<Table.Root
label="Hostnames"
columns={[
{ width: 'minmax(0,2fr)' },
{ width: 'auto', align: 'end' }
]}
>
<Table.Head>
<Table.Column>Hostname</Table.Column>
<Table.Column>Requests</Table.Column>
</Table.Head>
<Table.Row>
<Table.Cell>shop.example.com</Table.Cell>
<Table.Cell>1,204</Table.Cell>
</Table.Row>
</Table.Root>Parts
| Part | What it is |
|---|---|
Table.Root | The table. label is its accessible name and columns lays out the columns. |
Table.Head | The row of column headers. |
Table.Column | One header. sort and onSort make it a button that sorts. |
Table.Row | One row. selected tints it in the accent, muted mutes its text, and onClick makes it a button; focusable={false} keeps that row out of the tab order. |
Table.Cell | One cell. Its text ends in an ellipsis; truncate={false} wraps it. mono sets it in the monospace face, muted. |
Table.Group | Rows under a label that spans every column. |
Table.Foot | A row of totals under the body, behind a stronger line. |
Table.Empty | The message of a table without rows, across every column. |
Table.Loading | Rows of bars while the rows load. rows says how many. |
Every part but Table.Root and Table.Loading takes the props of its element, plus render to draw another one.
Columns
Each entry of columns is one column: width is a grid track (minmax(0,2fr), auto, 120px), align: 'end' puts its content at the end in tabular figures, and hideBelow drops it while the table is narrower than a breakpoint: sm (24rem), md (28rem), lg (32rem), xl (36rem) or 2xl (42rem). The table measures its own width, not the window's, so a table in a narrow dialog drops its columns too.
A header or a cell takes the alignment and the hiding of its column from its place in the row, so a row's cells are its direct children, one per column.
Frame and density
The table is bare by default, to sit inside a card that already draws a border. framed gives it a border and rounded corners of its own. density="compact" makes the rows shorter and the text smaller, for a long list or a dialog.
Rich content
A cell holds any content: two lines, a Pill, a Switch, an IconButton that opens a Menu. Only text truncates, so a control keeps its own size; a block of two lines adds truncate to its lines. muted on a row reads as a row a person cannot change.
import { Pill, Table } from '@adecore/ui';
const CLIENTS = [
{ name: 'Build server', key: 'ci-runner-7f3a', scopes: ['read', 'write'], muted: false },
{ name: 'Phone', key: 'phone-2c91', scopes: ['read'], muted: false },
{ name: 'Old laptop', key: 'laptop-0b44', scopes: ['revoked'], muted: true }
];
const COLUMNS = [{ width: 'minmax(0,1fr)' }, { width: 'auto', hideBelow: 'sm' }] as const;
export default function TableRichDemo() {
return (
<Table.Root label="Clients" columns={COLUMNS} framed className="w-full max-w-md">
<Table.Head>
<Table.Column>Client</Table.Column>
<Table.Column>Access</Table.Column>
</Table.Head>
{CLIENTS.map((client) => (
<Table.Row key={client.key} muted={client.muted}>
<Table.Cell>
<div className="min-w-0">
<div className="truncate">{client.name}</div>
<div className="truncate font-mono text-code text-text-muted">{client.key}</div>
</div>
</Table.Cell>
<Table.Cell>
{client.scopes.map((scope) => (
<Pill key={scope} size="sm" tone={scope === 'revoked' ? 'error' : 'muted'}>
{scope}
</Pill>
))}
</Table.Cell>
</Table.Row>
))}
</Table.Root>
);
}Picking rows
A row with onClick is a tab stop that Enter and Space press, with a hover color. A click on a control inside the row, such as a checkbox, stays the control's. When that control does what the row's click does, focusable={false} keeps the row out of the tab order: the pointer still picks the whole row, and the keyboard reaches only the control. With maxHeight the body scrolls under a head that stays at the top.
import { Checkbox, Table } from '@adecore/ui';
import { useState } from 'react';
const PROJECTS = ['Atlas', 'Beacon', 'Cobalt', 'Delta', 'Ember', 'Fjord', 'Granite', 'Harbor', 'Iris', 'Juniper'].map((name) => ({
name,
folder: `~/Projects/${name.toLowerCase()}`
}));
const COLUMNS = [{ width: 'auto' }, { width: 'minmax(0,1fr)' }, { width: 'minmax(0,2fr)', hideBelow: 'sm' }] as const;
export default function TableSelectionDemo() {
const [picked, setPicked] = useState<ReadonlySet<string>>(new Set(['Beacon']));
const toggle = (name: string) => {
setPicked((current) => {
const next = new Set(current);
if (!next.delete(name)) {
next.add(name);
}
return next;
});
};
const all = picked.size === PROJECTS.length;
return (
<Table.Root label="Projects" columns={COLUMNS} density="compact" framed maxHeight={220} className="w-full max-w-md">
<Table.Head>
<Table.Column>
<Checkbox
label="Pick every project"
checked={all}
indeterminate={picked.size > 0 && !all}
onCheckedChange={(checked) => setPicked(new Set(checked ? PROJECTS.map((project) => project.name) : []))}
/>
</Table.Column>
<Table.Column>Project</Table.Column>
<Table.Column>Folder</Table.Column>
</Table.Head>
{PROJECTS.map((project) => (
<Table.Row key={project.name} selected={picked.has(project.name)} focusable={false} onClick={() => toggle(project.name)}>
<Table.Cell>
<Checkbox label={`Pick ${project.name}`} checked={picked.has(project.name)} onCheckedChange={() => toggle(project.name)} />
</Table.Cell>
<Table.Cell>{project.name}</Table.Cell>
<Table.Cell mono>{project.folder}</Table.Cell>
</Table.Row>
))}
</Table.Root>
);
}Sorting and totals
sort is none, asc or desc, and the header carries aria-sort. A click calls onSort with the direction it asks for; the caller sorts the rows. Table.Foot holds the totals.
import { Table, type TableSort } from '@adecore/ui';
import { formatNumber } from '@adecore/ui/format';
import { useState } from 'react';
const MODELS = [
{ name: 'Large', requests: 1204, tokens: 3_840_000 },
{ name: 'Medium', requests: 4410, tokens: 2_115_500 },
{ name: 'Small', requests: 9870, tokens: 905_300 }
];
type Key = 'requests' | 'tokens';
const COLUMNS = [{ width: 'minmax(0,1fr)' }, { width: 'auto', align: 'end' }, { width: 'auto', align: 'end' }] as const;
export default function TableSortableDemo() {
const [order, setOrder] = useState<{ key: Key; direction: 'asc' | 'desc' }>({ key: 'tokens', direction: 'desc' });
const rows = MODELS.toSorted((left, right) => (order.direction === 'asc' ? 1 : -1) * (left[order.key] - right[order.key]));
const sortOf = (key: Key): TableSort => (order.key === key ? order.direction : 'none');
return (
<Table.Root label="Usage per model" columns={COLUMNS} framed className="w-full max-w-md">
<Table.Head>
<Table.Column>Model</Table.Column>
<Table.Column sort={sortOf('requests')} onSort={(direction) => setOrder({ key: 'requests', direction })}>
Requests
</Table.Column>
<Table.Column sort={sortOf('tokens')} onSort={(direction) => setOrder({ key: 'tokens', direction })}>
Tokens
</Table.Column>
</Table.Head>
{rows.map((model) => (
<Table.Row key={model.name}>
<Table.Cell>{model.name}</Table.Cell>
<Table.Cell>{formatNumber(model.requests)}</Table.Cell>
<Table.Cell>{formatNumber(model.tokens)}</Table.Cell>
</Table.Row>
))}
<Table.Foot>
<Table.Cell>Total</Table.Cell>
<Table.Cell>{formatNumber(MODELS.reduce((sum, model) => sum + model.requests, 0))}</Table.Cell>
<Table.Cell>{formatNumber(MODELS.reduce((sum, model) => sum + model.tokens, 0))}</Table.Cell>
</Table.Foot>
</Table.Root>
);
}Empty and loading
import { Table } from '@adecore/ui';
const COLUMNS = [{ width: 'minmax(0,2fr)' }, { width: 'minmax(0,1fr)' }, { width: 'auto', align: 'end' }] as const;
function Head() {
return (
<Table.Head>
<Table.Column>Name</Table.Column>
<Table.Column>Owner</Table.Column>
<Table.Column>Size</Table.Column>
</Table.Head>
);
}
export default function TableStatesDemo() {
return (
<div className="grid w-full max-w-xl gap-4 sm:grid-cols-2">
<Table.Root label="Shared folders" columns={COLUMNS} framed>
<Head />
<Table.Empty>Nothing shared yet</Table.Empty>
</Table.Root>
<Table.Root label="Shared folders" columns={COLUMNS} framed>
<Head />
<Table.Loading rows={3} />
</Table.Root>
</div>
);
}TableRootProps, TableHeadProps, TableColumnProps, TableRowProps, TableCellProps, TableGroupProps, TableFootProps, TableEmptyProps, TableLoadingProps, TableColumnLayout, TableBreakpoint, TableSort and TableDensity are exported types.