oakoss/ui
Components

Table

An HTML table for reading data in rows and columns.

Source
Recent invoices
InvoiceStatusMethodAmount
INV001PaidCredit card$250.00
INV002PendingPayPal$150.00
INV003UnpaidBank transfer$350.00
INV004PaidCredit card$450.00
Total$1,200.00

Installation

npx shadcn@latest add @oakoss/table

Usage

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)]".

Monthly sales by region, in units
RegionJanFebMarAprMayJunJulAug
North77788899210321432543654
South136284432580728876124272
East39558076550235420605790
West654876198420642864186408
Central1327253179014940866726
Coastal272568864260556852248544

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.

CommitStatusDuration
a3f9c1eSucceeded1m 12s
b81d4f0Failed2m 40s
c02e7aaSucceeded1m 05s
d5c9b13Succeeded58s
e7a0f62Cancelled3m 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.

NameEmailRole
Ada Lovelaceada@example.comOwner
Grace Hoppergrace@example.comAdmin
Alan Turingalan@example.comMember

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.

InvoiceStatusAmount
INV001Paid$50.00
INV002Pending$100.00
INV003Unpaid$150.00
INV004Paid$200.00
INV005Pending$250.00
INV006Unpaid$300.00
INV007Paid$350.00
INV008Pending$400.00
INV009Unpaid$450.00
INV010Paid$500.00
INV011Pending$550.00
INV012Unpaid$600.00
INV013Paid$650.00
INV014Pending$700.00
INV015Unpaid$750.00
INV016Paid$800.00
INV017Pending$850.00
INV018Unpaid$900.00
INV019Paid$950.00
INV020Pending$1000.00

Empty state

With no rows, one cell spans every column (colSpan) and says why.

InvoiceStatusAmount
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.

InvoiceNote
INV001Paid in two installments after the first reminder
INV002Waiting 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

AttributePresent when
data-slotAlways: table, and table-container on the container

CSS variables

VariableSets
--table-bgThe 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

AttributePresent when
data-slotAlways: 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

AttributePresent when
data-slotAlways: table-header, table-body, table-footer, table-row or table-caption