Skip to content

Table

Shows records in rows and columns that people can scan, compare and act on.

OrderSupplierSiteStatusTotal
PO-2417BulloviaBergamoAwaiting approval€18,420
PO-2416Ferrolva AcciaiBresciaIn transit€52,300
PO-2415PlastrenaPadovaReceived€7,860
PO-2414CuscinalBolognaReceived€12,950
Purchase orders opened this week.

Installation

pnpm dlx shadcn@latest add @silkware/table

To copy the component by hand, install its dependencies:

pnpm add react-aria-components cn

Then copy the source into components/ui/table.tsx:

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

Use TableFooter for totals that belong to the whole table.

ItemQuantityUnit priceLine total
Steel bar, 40 mm120€48.50€5,820
Bearing 6204-2RS600€3.20€1,920
Gasket kit GK-12250€6.80€1,700
Total970€9,440
Three lines, one delivery.

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.

SupplierContactOpen ordersActions
BulloviaLuca3
Ferrolva AcciaiGiulia1
PlastrenaMarco0

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.

OrderSupplierTotal
PO-2417Bullovia€18,420
PO-2416Ferrolva Acciai€52,300
PO-2415Plastrena€7,860
PO-2414Cuscinal€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.

OrderSupplierTotal
PO-2417Bullovia€18,420
PO-2416Ferrolva Acciai€52,300
PO-2415Plastrena€7,860
PO-2414Cuscinal€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.

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

OrderSupplierSiteStatusTotal
PO-2417BulloviaBergamoAwaiting approval€18,420
PO-2416Ferrolva AcciaiBresciaIn transit€52,300
PO-2415PlastrenaPadovaReceived€7,860
PO-2414CuscinalBolognaReceived€12,950
Purchase orders opened this week.

Accessibility

KeyAction
TabMoves 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.
ArrowDownMoves focus to the next row, or to the cell below when a cell has focus.
ArrowUpMoves focus to the previous row, or to the cell above. From the first row it reaches the column headers.
ArrowRightMoves focus from a row into its first cell, then to the next cell.
ArrowLeftMoves focus to the previous cell in the row.
HomeMoves focus to the first row.
EndMoves focus to the last row.
SpaceToggles the selection of the focused row, or sorts by the focused column header when it allows sorting.
EnterSorts by the focused column header when it allows sorting.
ShiftArrowDownExtends the selection to the next row. Shift and Arrow Up extends it upward.
CtrlASelects every row when selection is multiple. On macOS the shortcut is Cmd and A.
EscapeClears the selection.

React Aria provides:

  • The grid role with row, column header, row header and cell roles, and aria-sort on 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-label or aria-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-nums so 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

ItemQuantityUnit price
Steel bar, 40 mm120€48.50
Bearing 6204-2RS600€3.20
Gasket kit GK-12250€6.80
Do

Right-align numbers and their headers so digits line up.

ItemQuantityUnit price
Steel bar, 40 mm120€48.50
Bearing 6204-2RS600€3.20
Gasket kit GK-12250€6.80
Don’t

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.

PropTypeDefaultDescription
aria-label?stringNoneThe 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>NoneThe selected rows, for a controlled table.
defaultSelectedKeys?"all" | Iterable<Key>NoneThe initially selected rows, for an uncontrolled table.
onSelectionChange?(keys: Selection) => voidNoneCalled when the selection changes.
sortDescriptor?SortDescriptorNoneThe sorted column and its direction.
onSortChange?(descriptor: SortDescriptor) => voidNoneCalled when a sortable column is pressed.
className?stringNoneClasses 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.

PropTypeDefaultDescription
renderEmptyState?() => ReactNodeNoneContent shown when the body has no rows.
items?Iterable<T>NoneThe 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.

PropTypeDefaultDescription
id?KeyNoneA stable key for the row. Required for selection.
href?stringNoneMakes the row a link to another page.

TableHead

Renders a column header through React Aria's Column.

PropTypeDefaultDescription
id?KeyNoneA stable key for the column. Required for sorting.
isRowHeader?booleanfalseMarks the column whose cells name their row.
allowsSorting?booleanfalseLets people sort by this column.
children?ReactNode | (renderProps) => ReactNodeNoneThe 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.