Table
Rows and columns for comparing things. The same parts as shadcn's Table in a rounded, soft-headed container that scrolls sideways on small screens. For sorting, search, selection and paging, see Data Table.
Install
npx shadcn@latest add @nelumbo/tableInvoices
A header surface and faint row dividers do the work of borders. Rows light up on hover, and the selected row is filled in (data-state="selected"). Numbers are right-aligned with tabular figures.
| Invoice | Client | Status | Method | Amount |
|---|---|---|---|---|
| INV-2041 | Northwind Studio | Card | $2,450.00 | |
| INV-2042 | Fernhill Coffee | Bank transfer | $380.50 | |
| INV-2043 | Lumen Architects | Card | $12,900.00 | |
| INV-2044 | Okafor and Daughters | Bank transfer | $1,175.25 | |
| INV-2045 | Tidewater Books | PayPal | $640.00 | |
| INV-2046 | Paper Moon Press | Card | $89.99 | |
| Total | $17,635.74 | |||
Compact
density="compact" tightens rows from 56px to 40px, for tables that carry a lot of data.
| Invoice | Client | Status | Method | Amount |
|---|---|---|---|---|
| INV-2041 | Northwind Studio | Card | $2,450.00 | |
| INV-2042 | Fernhill Coffee | Bank transfer | $380.50 | |
| INV-2043 | Lumen Architects | Card | $12,900.00 | |
| INV-2044 | Okafor and Daughters | Bank transfer | $1,175.25 | |
| INV-2045 | Tidewater Books | PayPal | $640.00 | |
| INV-2046 | Paper Moon Press | Card | $89.99 | |
| Total | $17,635.74 | |||
Sticky header
stickyHeader keeps the header in view while the body scrolls. Give the container a height with containerClassName.
| Invoice | Client | Status | Method | Amount |
|---|---|---|---|---|
| INV-2041 | Northwind Studio | Card | $2,450.00 | |
| INV-2042 | Fernhill Coffee | Bank transfer | $380.50 | |
| INV-2043 | Lumen Architects | Card | $12,900.00 | |
| INV-2044 | Okafor and Daughters | Bank transfer | $1,175.25 | |
| INV-2045 | Tidewater Books | PayPal | $640.00 | |
| INV-2046 | Paper Moon Press | Card | $89.99 | |
| Total | $17,635.74 | |||
Usage
import { Table, TableBody, TableCaption, TableCell, TableFooter, TableHead, TableHeader, TableRow,} from "@/components/ui/table"<Table density="compact"> <TableCaption>Invoices issued in May.</TableCaption> <TableHeader> <TableRow> <TableHead>Invoice</TableHead> <TableHead className="text-end">Amount</TableHead> </TableRow> </TableHeader> <TableBody> {invoices.map((i) => ( <TableRow key={i.id} data-state={i.id === selected ? "selected" : undefined}> <TableCell className="font-medium">{i.id}</TableCell> <TableCell className="text-end tabular-nums">{money.format(i.amount)}</TableCell> </TableRow> ))} </TableBody></Table>Accessibility
Real table elements, so screen readers announce rows and columns. Use TableCaption (or aria-label) to name the table. While the table overflows its container, the container can take focus, so keyboard users can scroll it with the arrow keys. For sortable columns, set aria-sort on the TableHead; Data Table does this for you.
Props
| Prop | Type | Default | Notes |
|---|---|---|---|
Table density | "comfortable" | "compact" | "comfortable" | Row height: 56px or 40px |
Table stickyHeader | boolean | false | Header stays while the body scrolls; needs a container height |
Table containerClassName | string | Classes for the scroll container, such as max-h-96 | |
Table className | string | Classes for the <table>, such as min-w-max | |
TableRow data-state | "selected" | Fills the row | |
TableHead / TableCell | th / td props | Padding follows the density; checkbox cells narrow themselves | |
TableHeader, TableBody, TableFooter, TableCaption | As in shadcn |