Table
Data displayA table of data: <Table.Root> (in a container that scrolls sideways when it is too wide) holds a Table.Header, a Table.Body and an optional Table.Footer, each of Table.Rows of Table.Head (column headings) and Table.Cells, and a Table.Caption that names it. Rows highlight on hover; data-state="selected" on a row marks it selected. A Table.Head with sort (ascending, descending or none) is a sort button with aria-sort that calls onSort. It is the native table, so screen readers read it as one; the app sorts, selects and pages the rows.
import { Table } from '@limerdev/zest'; Examples
Basic
A caption names the table; Table.Footer holds the totals.
| Invoice | Customer | Status | Amount |
|---|---|---|---|
| 1042 | Acme AB | Paid | 4 200 kr |
| 1043 | Globex | Sent | 12 800 kr |
| 1044 | Initech | Overdue | 1 650 kr |
| 1045 | Umbrella | Draft | 7 300 kr |
| Total | 25 950 kr | ||
<script lang="ts">
import { Badge, Table, type BadgeVariant } from '@limerdev/zest';
type Invoice = {
number: string;
customer: string;
status: string;
tone: BadgeVariant;
amount: number;
};
const invoices: Invoice[] = [
{ number: '1042', customer: 'Acme AB', status: 'Paid', tone: 'success', amount: 4200 },
{ number: '1043', customer: 'Globex', status: 'Sent', tone: 'info', amount: 12800 },
{ number: '1044', customer: 'Initech', status: 'Overdue', tone: 'error', amount: 1650 },
{ number: '1045', customer: 'Umbrella', status: 'Draft', tone: 'outline', amount: 7300 }
];
const total: number = invoices.reduce((sum, invoice) => sum + invoice.amount, 0);
const kr = new Intl.NumberFormat('sv-SE', {
style: 'currency',
currency: 'SEK',
maximumFractionDigits: 0
});
</script>
<Table.Root>
<Table.Caption>Invoices this month</Table.Caption>
<Table.Header>
<Table.Row>
<Table.Head scope="col">Invoice</Table.Head>
<Table.Head scope="col">Customer</Table.Head>
<Table.Head scope="col">Status</Table.Head>
<Table.Head scope="col" class="text-end">Amount</Table.Head>
</Table.Row>
</Table.Header>
<Table.Body>
{#each invoices as invoice (invoice.number)}
<Table.Row>
<Table.Cell class="font-medium">{invoice.number}</Table.Cell>
<Table.Cell>{invoice.customer}</Table.Cell>
<Table.Cell><Badge variant={invoice.tone}>{invoice.status}</Badge></Table.Cell>
<Table.Cell class="text-end tabular-nums">{kr.format(invoice.amount)}</Table.Cell>
</Table.Row>
{/each}
</Table.Body>
<Table.Footer>
<Table.Row>
<Table.Cell colspan={3}>Total</Table.Cell>
<Table.Cell class="text-end tabular-nums">{kr.format(total)}</Table.Cell>
</Table.Row>
</Table.Footer>
</Table.Root> Selection
Rows with a Checkbox: data-state="selected" marks a chosen row. The heading's Checkbox selects all, and is indeterminate while only some are.
| Name | ||
|---|---|---|
| Anna Berg | anna@example.com | |
| Erik Lund | erik@example.com | |
| Sara Ek | sara@example.com |
<script lang="ts">
import { Checkbox, Table } from '@limerdev/zest';
type User = { id: string; name: string; email: string };
const users: User[] = [
{ id: 'u1', name: 'Anna Berg', email: 'anna@example.com' },
{ id: 'u2', name: 'Erik Lund', email: 'erik@example.com' },
{ id: 'u3', name: 'Sara Ek', email: 'sara@example.com' }
];
let selected: string[] = $state([]);
function toggle(id: string, checked: boolean): void {
selected = checked ? [...selected, id] : selected.filter((other) => other !== id);
}
function toggleAll(checked: boolean): void {
selected = checked ? users.map((user) => user.id) : [];
}
</script>
<Table.Root>
<Table.Caption>{selected.length} of {users.length} users selected</Table.Caption>
<Table.Header>
<Table.Row>
<Table.Head scope="col">
<Checkbox
aria-label="Select all users"
checked={selected.length === users.length}
indeterminate={selected.length > 0 && selected.length < users.length}
onCheckedChange={toggleAll}
/>
</Table.Head>
<Table.Head scope="col">Name</Table.Head>
<Table.Head scope="col">Email</Table.Head>
</Table.Row>
</Table.Header>
<Table.Body>
{#each users as user (user.id)}
<Table.Row data-state={selected.includes(user.id) ? 'selected' : undefined}>
<Table.Cell>
<Checkbox
aria-label="Select {user.name}"
checked={selected.includes(user.id)}
onCheckedChange={(checked) => toggle(user.id, checked)}
/>
</Table.Cell>
<Table.Cell class="font-medium">{user.name}</Table.Cell>
<Table.Cell class="text-muted">{user.email}</Table.Cell>
</Table.Row>
{/each}
</Table.Body>
</Table.Root> Sorting
sort on Table.Head makes the heading a button with aria-sort; onSort is yours, and so is the sorting.
| Invoice | ||
|---|---|---|
| 1044 | Ekgården | 12000 kr |
| 1042 | Nordlys AB | 4200 kr |
| 1043 | Bergström & Co | 900 kr |
<script lang="ts">
import { Table } from '@limerdev/zest';
type Invoice = { id: string; client: string; amount: number };
type Column = 'client' | 'amount';
const invoices: Invoice[] = [
{ id: '1042', client: 'Nordlys AB', amount: 4200 },
{ id: '1043', client: 'Bergström & Co', amount: 900 },
{ id: '1044', client: 'Ekgården', amount: 12000 }
];
let column: Column = $state('amount');
let direction: Table.SortDirection = $state('descending');
const rows: Invoice[] = $derived(
[...invoices].sort((a, b) => {
const order =
column === 'amount' ? a.amount - b.amount : a.client.localeCompare(b.client, 'sv');
return direction === 'ascending' ? order : -order;
})
);
function sortBy(next: Column): void {
if (column === next) {
direction = direction === 'ascending' ? 'descending' : 'ascending';
return;
}
column = next;
direction = 'ascending';
}
function sortOf(of: Column): Table.SortDirection {
return column === of ? direction : 'none';
}
</script>
<Table.Root>
<Table.Caption>Invoices, sorted by {column}</Table.Caption>
<Table.Header>
<Table.Row>
<Table.Head scope="col">Invoice</Table.Head>
<Table.Head scope="col" sort={sortOf('client')} onSort={() => sortBy('client')}>
Client
</Table.Head>
<Table.Head
scope="col"
class="text-end"
sort={sortOf('amount')}
onSort={() => sortBy('amount')}
>
Amount
</Table.Head>
</Table.Row>
</Table.Header>
<Table.Body>
{#each rows as invoice (invoice.id)}
<Table.Row>
<Table.Cell>{invoice.id}</Table.Cell>
<Table.Cell>{invoice.client}</Table.Cell>
<Table.Cell class="text-end tabular-nums">{invoice.amount} kr</Table.Cell>
</Table.Row>
{/each}
</Table.Body>
</Table.Root> Props
Every part also takes class (merged over its defaults), ref (bindable: the element), children, and child (a snippet that
renders your own element, with props to spread on it).
Table.Root
No props of its own beyond the common ones.
Table.Header
No props of its own beyond the common ones.
Table.Body
No props of its own beyond the common ones.
Table.Footer
No props of its own beyond the common ones.
Table.Row
No props of its own beyond the common ones.
Table.Head
| Prop | Type | Default | Description |
|---|---|---|---|
| onSort | (() => void) | undefined | Called when the sort button is clicked; the app sets the next sort. |
| sort | "none" | "ascending" | "descending" | undefined | Makes the column sortable: the heading becomes a button with an arrow, and the cell gets aria-sort. none is sortable but not sorted now. Left unset, the column isn't sortable. The app sorts the rows. |
Table.Cell
No props of its own beyond the common ones.
Table.Caption
No props of its own beyond the common ones.