Table
Shows records in rows and columns that people can scan, compare and act on.
| Order | Supplier | Site | Status | Total |
|---|---|---|---|---|
| PO-2417 | Bullovia | Bergamo | Awaiting approval | €18,420 |
| PO-2416 | Ferrolva Acciai | Brescia | In transit | €52,300 |
| PO-2415 | Plastrena | Padova | Received | €7,860 |
| PO-2414 | Cuscinal | Bologna | Received | €12,950 |
Installation
pnpm dlx shadcn@latest add @silkware/tableTo copy the component by hand, install its dependencies:
pnpm add react-aria-components cnThen copy the source into components/ui/table.tsx:
"use client"
import * as React from "react"import { cn } from "cn"import { Cell as CellPrimitive, Column as ColumnPrimitive, Row as RowPrimitive, TableBody as TableBodyPrimitive, TableFooter as TableFooterPrimitive, TableHeader as TableHeaderPrimitive, Table as TablePrimitive, type CellProps, type ColumnProps, type RowProps, type TableBodyProps, type TableFooterProps, type TableHeaderProps, type TableProps,} from "react-aria-components"
function Table({ className, ...props }: TableProps) { return ( <div data-slot="table-container" className="relative w-full overflow-x-auto" > <TablePrimitive data-slot="table" className={cn("w-full caption-bottom text-sm", className)} {...props} /> </div> )}
function TableHeader<T>({ className, ...props }: TableHeaderProps<T>) { return ( <TableHeaderPrimitive data-slot="table-header" className={cn("[&_tr]:border-b", className)} {...props} /> )}
function TableBody<T>({ className, ...props }: TableBodyProps<T>) { return ( <TableBodyPrimitive data-slot="table-body" className={cn( "data-empty:h-24 data-empty:text-center [&_tr:last-child]:border-0", className )} {...props} /> )}
function TableFooter<T>({ className, ...props }: TableFooterProps<T>) { return ( <TableFooterPrimitive data-slot="table-footer" className={cn( "border-t bg-muted/50 font-medium [&>tr]:last:border-b-0", className )} {...props} /> )}
function TableRow<T>({ className, ...props }: RowProps<T>) { return ( <RowPrimitive data-slot="table-row" className={cn( "border-b transition-colors hover:bg-muted/50 has-aria-expanded:bg-muted/50 data-[state=selected]:bg-muted data-selected:bg-muted", className )} {...props} /> )}
function TableHead({ className, ...props }: ColumnProps) { return ( <ColumnPrimitive data-slot="table-head" className={cn( "h-10 px-2 text-left align-middle font-medium whitespace-nowrap text-foreground [&:has([data-slot=checkbox])]:pr-0 [&:has([role=checkbox])]:pr-0", className )} {...props} /> )}
function TableCell({ className, ...props }: CellProps) { return ( <CellPrimitive data-slot="table-cell" className={cn( "p-2 align-middle whitespace-nowrap [&:has([data-slot=checkbox])]:pr-0 [&:has([role=checkbox])]:pr-0", className )} {...props} /> )}
function TableCaption({ className, ...props}: React.ComponentProps<"figcaption">) { return ( <figcaption data-slot="table-caption" className={cn( "mt-4 text-center text-sm text-muted-foreground", className )} {...props} /> )}
export { Table, TableHeader, TableBody, TableFooter, TableHead, TableRow, TableCell, TableCaption,}Usage
import { Table, TableBody, TableCaption, TableCell, TableFooter, TableHead, TableHeader, TableRow,} from "@/components/ui/table"<Table aria-label="Purchase orders"> <TableHeader> <TableHead isRowHeader>Order</TableHead> <TableHead>Supplier</TableHead> </TableHeader> <TableBody> <TableRow id="PO-2417"> <TableCell>PO-2417</TableCell> <TableCell>Bullovia</TableCell> </TableRow> </TableBody></Table>Two things to get right. First, TableHead is a column, so a header row has no tr of its own: place the TableHead elements straight inside TableHeader. Second, mark one column with isRowHeader so a screen reader can announce the row by its name, and give every row an id when you use selection.
TableCaption renders a figcaption, not a caption. Wrap the table and the caption in a figure, as the first example does. Name the table itself with aria-label or aria-labelledby.
Event handlers and render functions (renderEmptyState, a function as a column's children) cannot cross from a server component to a client one. Put them in a file that starts with "use client".
Examples
Footer
Use TableFooter for totals that belong to the whole table.
| Item | Quantity | Unit price | Line total |
|---|---|---|---|
| Steel bar, 40 mm | 120 | €48.50 | €5,820 |
| Bearing 6204-2RS | 600 | €3.20 | €1,920 |
| Gasket kit GK-12 | 250 | €6.80 | €1,700 |
| Total | 970 | €9,440 |
Actions
Put a row's secondary actions in a Dropdown Menu in its last column. The trigger holds only an icon, so give it an aria-label that names the row.
| Supplier | Contact | Open orders | Actions |
|---|---|---|---|
| Bullovia | Luca | 3 | |
| Ferrolva Acciai | Giulia | 1 | |
| Plastrena | Marco | 0 |
Selection
Set selectionMode="multiple" on the Table and place Checkbox slot="selection" in the header and in each row. The header checkbox selects every row. Use defaultSelectedKeys for an uncontrolled table, or selectedKeys with onSelectionChange to control it.
| Order | Supplier | Total | |
|---|---|---|---|
| PO-2417 | Bullovia | €18,420 | |
| PO-2416 | Ferrolva Acciai | €52,300 | |
| PO-2415 | Plastrena | €7,860 | |
| PO-2414 | Cuscinal | €12,950 |
Sorting
Set allowsSorting on a column that people may sort, and sortDescriptor with onSortChange on the Table. The table does not reorder rows: you sort the data and render it in the new order. A column's children can be a function that receives sortDirection, which is how the example shows the arrow.
| Order | Supplier | Total |
|---|---|---|
| PO-2417 | Bullovia | €18,420 |
| PO-2416 | Ferrolva Acciai | €52,300 |
| PO-2415 | Plastrena | €7,860 |
| PO-2414 | Cuscinal | €12,950 |
Empty
Pass renderEmptyState to TableBody so an empty result explains itself. For a larger message with an action, use an Empty inside the function.
| Order | Supplier | Total |
|---|---|---|
| No purchase orders match these filters. | ||
With status
Where a row stands in its flow is the state square next to a word: put the pair in the cell. The outcome of a check on a row, such as an inspection that passed or an import that failed, can be a status Badge with an icon and a word. Colour only the rows that need attention, and never use a status badge for a workflow step.
| Order | Supplier | Site | Status | Total |
|---|---|---|---|---|
| PO-2417 | Bullovia | Bergamo | Awaiting approval | €18,420 |
| PO-2416 | Ferrolva Acciai | Brescia | In transit | €52,300 |
| PO-2415 | Plastrena | Padova | Received | €7,860 |
| PO-2414 | Cuscinal | Bologna | Received | €12,950 |
Accessibility
| Key | Action |
|---|---|
| Tab | Moves focus into the table, onto the first row or the first selected row, then out of it. Rows, cells and headers are reached with the arrow keys. |
| ArrowDown | Moves focus to the next row, or to the cell below when a cell has focus. |
| ArrowUp | Moves focus to the previous row, or to the cell above. From the first row it reaches the column headers. |
| ArrowRight | Moves focus from a row into its first cell, then to the next cell. |
| ArrowLeft | Moves focus to the previous cell in the row. |
| Home | Moves focus to the first row. |
| End | Moves focus to the last row. |
| Space | Toggles the selection of the focused row, or sorts by the focused column header when it allows sorting. |
| Enter | Sorts by the focused column header when it allows sorting. |
| ShiftArrowDown | Extends the selection to the next row. Shift and Arrow Up extends it upward. |
| CtrlA | Selects every row when selection is multiple. On macOS the shortcut is Cmd and A. |
| Escape | Clears the selection. |
React Aria provides:
- The
gridrole with row, column header, row header and cell roles, andaria-sorton the sorted column. - Keyboard navigation between cells, with a single tab stop for the whole table.
- Selection with a header checkbox and range selection with Shift and click.
- Selection changes announced to screen readers.
You provide:
- An accessible name through
aria-labeloraria-labelledby. - One column marked
isRowHeader. - A text label on controls that hold only an icon, such as a row action menu.
- Status in words as well as in marks: a state square is never the only signal.
Guidelines
When to use
- To compare the same fields across many records: orders, suppliers, quality checks.
- When people scan down a column or look for one record among many.
When not to use
- For filtering, sorting and paging a large list with a toolbar. Build a Data Table from this component.
- For a list where each entry is a title, a description and an action. Use an Item group.
- For layout. A table is for data, not for placing things side by side. Use a grid.
- For a handful of values about one record. Use a Card or a description list.
Content
- Name a column with a noun: "Supplier", "Total". Put the unit in the header, not in every cell.
- Align text to the left and numbers to the right, and use
tabular-numsso digits line up. - Keep cells to one line. Move long text to a detail view.
- Write the empty state as a sentence that says what is missing and what to do.
Alignment
| Item | Quantity | Unit price |
|---|---|---|
| Steel bar, 40 mm | 120 | €48.50 |
| Bearing 6204-2RS | 600 | €3.20 |
| Gasket kit GK-12 | 250 | €6.80 |
Right-align numbers and their headers so digits line up.
| Item | Quantity | Unit price |
|---|---|---|
| Steel bar, 40 mm | 120 | €48.50 |
| Bearing 6204-2RS | 600 | €3.20 |
| Gasket kit GK-12 | 250 | €6.80 |
Don’t centre numbers. The eye cannot compare values that do not share an edge.
API reference
Table
Renders a scrolling container around a React Aria Table. It accepts every React Aria Table prop.
| Prop | Type | Default | Description |
|---|---|---|---|
aria-label? | string | None | The accessible name of the table. Required unless aria-labelledby is set. |
selectionMode? | "none" | "single" | "multiple" | "none" | Whether rows can be selected, and how many. |
selectedKeys? | "all" | Iterable<Key> | None | The selected rows, for a controlled table. |
defaultSelectedKeys? | "all" | Iterable<Key> | None | The initially selected rows, for an uncontrolled table. |
onSelectionChange? | (keys: Selection) => void | None | Called when the selection changes. |
sortDescriptor? | SortDescriptor | None | The sorted column and its direction. |
onSortChange? | (descriptor: SortDescriptor) => void | None | Called when a sortable column is pressed. |
className? | string | None | Classes merged onto the table element. |
TableHeader
Renders the header row through React Aria's TableHeader. Its children are TableHead columns.
TableBody
Renders the body through React Aria's TableBody. It accepts items with a render function, or static rows.
| Prop | Type | Default | Description |
|---|---|---|---|
renderEmptyState? | () => ReactNode | None | Content shown when the body has no rows. |
items? | Iterable<T> | None | The data to render, when you pass a render function as children. |
TableRow
Renders a row through React Aria's Row. Selected rows take the muted background.
| Prop | Type | Default | Description |
|---|---|---|---|
id? | Key | None | A stable key for the row. Required for selection. |
href? | string | None | Makes the row a link to another page. |
TableHead
Renders a column header through React Aria's Column.
| Prop | Type | Default | Description |
|---|---|---|---|
id? | Key | None | A stable key for the column. Required for sorting. |
isRowHeader? | boolean | false | Marks the column whose cells name their row. |
allowsSorting? | boolean | false | Lets people sort by this column. |
children? | ReactNode | (renderProps) => ReactNode | None | The header content. A function receives sortDirection and allowsSorting. |
TableCell
Renders a cell through React Aria's Cell. It accepts colSpan and className.
TableFooter
Renders a footer through React Aria's TableFooter, with a top border and a muted background.
TableCaption
Renders a figcaption under the table. Place it next to the Table inside a figure. It accepts every figcaption prop.