Table
An HTML table for reading data in rows and columns.
| Invoice | Status | Method | Amount |
|---|---|---|---|
| INV001 | Paid | Credit card | $250.00 |
| INV002 | Pending | PayPal | $150.00 |
| INV003 | Unpaid | Bank transfer | $350.00 |
| INV004 | Paid | Credit card | $450.00 |
| Total | $1,200.00 | ||
Installation
npx shadcn@latest add @oakoss/tableUsage
import {
Table,
TableBody,
TableCaption,
TableCell,
TableHead,
TableHeader,
TableRow,
} from '#/components/ui/data/table';
<Table>
<TableCaption>Recent invoices</TableCaption>
<TableHeader>
<TableRow>
<TableHead>Invoice</TableHead>
<TableHead align="end">Amount</TableHead>
</TableRow>
</TableHeader>
<TableBody>
<TableRow>
<TableHead>INV001</TableHead>
<TableCell align="end">$250.00</TableCell>
</TableRow>
</TableBody>
</Table>;Table is a plain HTML table for reading data. Sorting, selection, filtering and pagination belong to a data table built on it.
Examples
Sticky header and column
stickyHeader keeps the header rows in view while the body scrolls, and
stickyColumn keeps the first column in view while the rest scrolls sideways;
a first cell that spans several columns, such as a footer's total label,
scrolls with the rest rather than cover them. Below a first cell that spans
rows, the next rows' first cells sit in the second column, so set
isSticky={false} on them. Give the container a height
with containerClassName for the header to stick to. A shadow marks each
edge once content has scrolled beneath it. Sticky cells paint --table-bg,
the page color by default, so on another surface set it once on that surface,
such as a Card with className="[--table-bg:var(--color-card)]".
| Region | Jan | Feb | Mar | Apr | May | Jun | Jul | Aug |
|---|---|---|---|---|---|---|---|---|
| North | 777 | 888 | 99 | 210 | 321 | 432 | 543 | 654 |
| South | 136 | 284 | 432 | 580 | 728 | 876 | 124 | 272 |
| East | 395 | 580 | 765 | 50 | 235 | 420 | 605 | 790 |
| West | 654 | 876 | 198 | 420 | 642 | 864 | 186 | 408 |
| Central | 13 | 272 | 531 | 790 | 149 | 408 | 667 | 26 |
| Coastal | 272 | 568 | 864 | 260 | 556 | 852 | 248 | 544 |
Compact and striped
size="sm" tightens rows for dense data, and striped tints every other body
row to help the eye follow a long row.
| Commit | Status | Duration |
|---|---|---|
| a3f9c1e | Succeeded | 1m 12s |
| b81d4f0 | Failed | 2m 40s |
| c02e7aa | Succeeded | 1m 05s |
| d5c9b13 | Succeeded | 58s |
| e7a0f62 | Cancelled | 3m 21s |
Selected rows
aria-selected on a row tints it. Pair it with a control that says what's
selected, such as a checkbox, since screen readers don't announce selection
on a plain table's rows. interactive adds a hover tint, for rows that act
when clicked; a read-only table leaves it off so its rows don't look
clickable.
| Name | Role | |
|---|---|---|
| Ada Lovelace | ada@example.com | Owner |
| Grace Hopper | grace@example.com | Admin |
| Alan Turing | alan@example.com | Member |
Linked rows
aria-current="true" on a row or a cell marks the one a link points to, such
as ?invoice=INV014 in the URL, with a warm tint over any other. Mark one at a
time, and only for a link: selection stays aria-selected. Once the row has
rendered, scroll it in with scrollIntoView({ block: 'center' }), which keeps
the table's header in view too. The container pads scrolling by the sticky
header and first column, so the row lands clear of them. Then move focus to
its row header (tabIndex={-1} and focus({ preventScroll: true })), so a
screen reader reads the linked row rather than starting at the top of the page.
| Invoice | Status | Amount |
|---|---|---|
| INV001 | Paid | $50.00 |
| INV002 | Pending | $100.00 |
| INV003 | Unpaid | $150.00 |
| INV004 | Paid | $200.00 |
| INV005 | Pending | $250.00 |
| INV006 | Unpaid | $300.00 |
| INV007 | Paid | $350.00 |
| INV008 | Pending | $400.00 |
| INV009 | Unpaid | $450.00 |
| INV010 | Paid | $500.00 |
| INV011 | Pending | $550.00 |
| INV012 | Unpaid | $600.00 |
| INV013 | Paid | $650.00 |
| INV014 | Pending | $700.00 |
| INV015 | Unpaid | $750.00 |
| INV016 | Paid | $800.00 |
| INV017 | Pending | $850.00 |
| INV018 | Unpaid | $900.00 |
| INV019 | Paid | $950.00 |
| INV020 | Pending | $1000.00 |
Empty state
With no rows, one cell spans every column (colSpan) and says why.
| Invoice | Status | Amount |
|---|---|---|
| No invoices yet. | ||
Truncated text
Table cells grow to fit their text and ignore max-width, so truncate a
span inside the cell and give it the full text as a title. Screen readers
read the whole text either way, but keyboard and touch users can't open a
title, so truncate only text that's repeated elsewhere, such as on a detail
page the row links to.
| Invoice | Note |
|---|---|
| INV001 | Paid in two installments after the first reminder |
| INV002 | Waiting on the purchase order number from finance |
Accessibility
Table renders native table elements, following the
WAI-ARIA table pattern, so
screen readers navigate it by row and column and announce the headers of each
cell. TableCaption names the table;
without one, give Table an aria-label or aria-labelledby. A TableHead
in the header heads its column (scope="col"), and one in the body or footer
heads its row (scope="row"), so make each row's first cell a TableHead
when it identifies the row. A scope you set wins.
A table wider or taller than its space scrolls, and only then is its container focusable, so keyboard users can tab to it and scroll with the arrow keys. It also becomes a region named like the table, so screen readers announce what the focused area holds; a table that fits is neither a tab stop nor a landmark. Its focus ring sits inside its edge. React Aria adds no keys.
Digits have equal width throughout, so numbers line up, and align="end"
aligns a column to the end of the reading direction. In Windows High Contrast
the row tints give way to the system colors: a selected row takes the
selection colors, a linked row or cell the mark colors, and lines between rows
stay.
API
Table
A table in a scrolling div.
Props
Prop
Type
Also accepts table props. className styles the table and
containerClassName the scrolling container.
Data attributes
| Attribute | Present when |
|---|---|
data-slot | Always: table, and table-container on the container |
CSS variables
| Variable | Sets |
|---|---|
--table-bg | The surface sticky cells paint, set on any ancestor; the page color when nothing sets it |
TableHead and TableCell
A th and a td.
Props
Prop
Type
Also accept th or td props, except HTML's align. TableHead also takes
scope (col, colgroup, row or rowgroup), which defaults to col in
the header and row elsewhere.
Data attributes
| Attribute | Present when |
|---|---|
data-slot | Always: table-head or table-cell |
TableHeader, TableBody, TableFooter, TableRow and TableCaption
A thead, tbody, tfoot, tr and caption, accepting their elements'
props. Set aria-selected on a TableRow to mark it selected, and
aria-current on a TableRow, TableHead or TableCell to mark it linked.
Data attributes
| Attribute | Present when |
|---|---|
data-slot | Always: table-header, table-body, table-footer, table-row or table-caption |