$npx @dinachi/cli@latest add table
| Item | Qty | Price | Amount |
|---|---|---|---|
| Merino jumperCharcoal, M | 1 × | £118.00 | £118.00 |
| Linen shirtSand, L | 2 × | £64.00 | £128.00 |
| Wool scarfRust | 1 × | £38.00 | £38.00 |
| Total | £284.00 | ||
densitydividersstackinvoice.tsx
import {
Table,
TableBody,
TableCaption,
TableCell,
TableFooter,
TableHead,
TableHeader,
TableRow,
} from "@/components/ui/table"
export function Invoice({ lines }: { lines: Line[] }) {
return (
<Table stack>
<TableCaption>Invoice 2048</TableCaption>
<TableHeader>
<TableRow>
<TableHead>Item</TableHead>
<TableHead numeric>Qty</TableHead>
<TableHead numeric>Price</TableHead>
<TableHead numeric>Amount</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{lines.map((l) => (
<TableRow key={l.item}>
<TableCell stack="title">{l.item}</TableCell>
<TableCell numeric stack="meta">{l.qty}</TableCell>
<TableCell numeric stack="meta">{money(l.price)}</TableCell>
<TableCell numeric stack="value">{money(l.qty * l.price)}</TableCell>
</TableRow>
))}
</TableBody>
<TableFooter>
<TableRow>
<TableCell colSpan={3}>Total</TableCell>
<TableCell numeric>{money(total(lines))}</TableCell>
</TableRow>
</TableFooter>
</Table>
)
}Usage
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table"
export function Orders({ orders }: { orders: { id: string; customer: string; total: string }[] }) {
return (
<Table>
<TableHeader>
<TableRow>
<TableHead>Order</TableHead>
<TableHead>Customer</TableHead>
<TableHead numeric>Total</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{orders.map((o) => (
<TableRow key={o.id}>
<TableCell>{o.id}</TableCell>
<TableCell>{o.customer}</TableCell>
<TableCell numeric>{o.total}</TableCell>
</TableRow>
))}
</TableBody>
</Table>
)
}Examples
| Item | Qty | Price | Amount |
|---|---|---|---|
| Merino jumperCharcoal, M | 1 × | £118.00 | £118.00 |
| Linen shirtSand, L | 2 × | £64.00 | £128.00 |
| Wool scarfRust | 1 × | £38.00 | £38.00 |
| Socks, 3 packGrey marl | 2 × | £15.00 | £30.00 |
| Delivery | £4.95 | ||
| VAT included | £53.16 | ||
| Total | £318.95 | ||
Numeric columns, a caption, and totals in the footer. Below 768px each line stacks.
import {
Table,
TableBody,
TableCaption,
TableCell,
TableFooter,
TableHead,
TableHeader,
TableRow,
} from '@/components/ui/table';
const lines = [
{ item: 'Merino jumper', variant: 'Charcoal, M', qty: 1, price: 118 },
{ item: 'Linen shirt', variant: 'Sand, L', qty: 2, price: 64 },
{ item: 'Wool scarf', variant: 'Rust', qty: 1, price: 38 },
{ item: 'Socks, 3 pack', variant: 'Grey marl', qty: 2, price: 15 },
];
const money = (n: number) => '£' + n.toLocaleString('en-GB', { minimumFractionDigits: 2 });
export function Example() {
const total = lines.reduce((sum, l) => sum + l.qty * l.price, 0) + 4.95;
return (
<Table stack>
<TableCaption>
<span className="flex items-baseline justify-between">
<span className="text-base font-semibold text-foreground">Invoice 2048</span>
<span>Due 1 Dec 2026</span>
</span>
</TableCaption>
<TableHeader>
<TableRow>
<TableHead>Item</TableHead>
<TableHead numeric>Qty</TableHead>
<TableHead numeric>Price</TableHead>
<TableHead numeric>Amount</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{lines.map((l) => (
<TableRow key={l.item}>
<TableCell stack="title">
{l.item}
<span className="block text-xs font-normal text-muted-foreground">{l.variant}</span>
</TableCell>
<TableCell numeric stack="meta">
{l.qty}<span className="@lg:hidden"> ×</span>
</TableCell>
<TableCell numeric stack="meta">{money(l.price)}</TableCell>
<TableCell numeric stack="value">{money(l.qty * l.price)}</TableCell>
</TableRow>
))}
</TableBody>
<TableFooter>
<TableRow>
<TableCell colSpan={3} className="text-muted-foreground">Delivery</TableCell>
<TableCell numeric>{money(4.95)}</TableCell>
</TableRow>
<TableRow>
<TableCell colSpan={3} className="text-muted-foreground">VAT included</TableCell>
<TableCell numeric className="text-muted-foreground">{money(total / 6)}</TableCell>
</TableRow>
<TableRow>
<TableCell colSpan={3}>Total</TableCell>
<TableCell numeric>{money(total)}</TableCell>
</TableRow>
</TableFooter>
</Table>
);
}| Region | Requests | Errors | p95 |
|---|---|---|---|
| Frankfurt | 48,210 | 0.12% | 182 ms |
| London | 39,875 | 0.08% | 164 ms |
| Virginia | 61,402 | 0.31% | 211 ms |
| Singapore | 17,336 | 0.05% | 238 ms |
Compact rows without dividers, for dense figures where spacing groups the cells.
import { Table, TableBody, TableCaption, TableCell, TableHead, TableHeader, TableRow } from '@/components/ui/table';
export function Example({ rows }: { rows: { region: string; requests: number; errors: number; p95: number }[] }) {
return (
<Table density="compact" dividers="none">
<TableCaption>Requests by region, last 24 hours</TableCaption>
<TableHeader>
<TableRow>
<TableHead>Region</TableHead>
<TableHead numeric>Requests</TableHead>
<TableHead numeric>Errors</TableHead>
<TableHead numeric>p95</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{rows.map((r) => (
<TableRow key={r.region}>
<TableCell>{r.region}</TableCell>
<TableCell numeric>{r.requests.toLocaleString()}</TableCell>
<TableCell numeric>{r.errors.toFixed(2)}%</TableCell>
<TableCell numeric>{r.p95} ms</TableCell>
</TableRow>
))}
</TableBody>
</Table>
);
}Notes
Phone and desktop
Without stack, a wide table scrolls sideways inside its container. With it, each row reads as a line item once the table is under 512px wide, wherever it sits: the title cell and the value cell on the first line, the meta cells under them, and the footer rows as label and figure. Give every body row one title and at most one value.
Sorting, selection and large data
Table is the markup and the look. Sorting, row selection and virtualised rows build on it in their own components.
API
density"comfortable" | "compact" = "comfortable"
Row height: 48px or 36px.dividers"rows" | "none" = "rows"
Lines between body rows.stackboolean = false
When the table is under 512px wide, each row becomes a block, its cells placed by their stack role.containerClassNamestring
Classes for the scrolling container.containerRefRef<HTMLDivElement>
The scrolling container, for a virtualiser or a scroll listener.Takes the props of the element it renders.
Takes the props of the element it renders.
Takes the props of the element it renders.
Takes the props of the element it renders.
numericboolean = false
Right-aligns the name over a column of figures.stack"title" | "value" | "meta" = "title"
Where a row header goes when the table stacks.numericboolean = false
Right-aligned tabular figures, so a column reads as one figure.stack"title" | "value" | "meta" = "meta"
Where the cell goes when the table stacks. title and value share the first line; meta sits under them.Takes the props of the element it renders.
Accessibility
- The parts are native table elements, so screen readers announce rows, columns and headers.
- A stacked table sets the table roles on its parts, since browsers drop them once the parts stop displaying as a table.
- Header cells are scoped to their column, and
TableHeadin a body row to its row. - Give the table a
TableCaptionor anaria-labelso it is named.