← Gallery

Table

UI primitive
table

Compares records across the same columns: orders, invoices, team members.

Compares records across the same columns: orders, invoices, team members. Use a table when people scan down a column or across a row to compare; use a list when each item is read on its own.

Live examples

Caption, numeric column, selected row
Invoices this month
InvoiceStatusAmount (USD)
INV-1042Paid1,250.00
INV-1043Due980.50
INV-1044Paid12,400.00

Anatomy

  • Table — a horizontally scrollable container and the <table>.
  • TableHeader, TableBody, TableFooter — row groups.
  • TableRow — one record.
  • TableHead — a column header cell.
  • TableCell — a data cell.
  • TableCaption — the table's title or summary, below the table.

From the code

Tokens consumed
foregroundmutedmuted-foreground
Radius
none set
Focus
none set
Motion
transition-colors

States

StateLooksNote
Row hovermuted at 50%.Pointer only.
Row selectedmuted background with data-state="selected".Pair with a checkbox column.
OverflowThe container scrolls horizontally.Cells do not wrap (whitespace-nowrap).

Do

  • Give every table a caption or an aria-label.
  • Right-align numbers and use tabular figures (text-right tabular-nums) so digits line up (DESIGN-LANGUAGE §22).
  • Keep one unit per column and say it in the header.

Don't

  • Don't use a table for layout.
  • Don't put interactive controls in every cell; give the row one clear action.
  • Don't highlight rows on hover in consumer-service screens, where DESIGN-LANGUAGE §24 forbids hover highlights on list rows.

Known gaps

  • There is no numeric-column helper; every consumer adds text-right tabular-nums by hand.
  • Row hover is always on, which conflicts with DESIGN-LANGUAGE §24 in the consumer-service recipe.
  • All cells use whitespace-nowrap, so long text forces horizontal scrolling on phones.

Sources: DESIGN-LANGUAGE §22 Number formatting; §24 Interaction rules

Size
2.4 KB
Exports
8
Imports
2
Type
ui

Exports

TableTableHeaderTableBodyTableFooterTableHeadTableRowTableCellTableCaption

Imports

  • react
  • ./utils
"use client";

import * as React from "react";

import { cn } from "./utils";

function Table({ className, ...props }: React.ComponentProps<"table">) {
  return (
    <div
      data-slot="table-container"
      className="relative w-full overflow-x-auto"
    >
      <table
        data-slot="table"
        className={cn("w-full caption-bottom text-sm", className)}
        {...props}
      />
    </div>
  );
}

function TableHeader({ className, ...props }: React.ComponentProps<"thead">) {
  return (
    <thead
      data-slot="table-header"
      className={cn("[&_tr]:border-b", className)}
      {...props}
    />
  );
}

function TableBody({ className, ...props }: React.ComponentProps<"tbody">) {
  return (
    <tbody
      data-slot="table-body"
      className={cn("[&_tr:last-child]:border-0", className)}
      {...props}
    />
  );
}

function TableFooter({ className, ...props }: React.ComponentProps<"tfoot">) {
  return (
    <tfoot
      data-slot="table-footer"
      className={cn(
        "bg-muted/50 border-t font-medium [&>tr]:last:border-b-0",
        className,
      )}
      {...props}
    />
  );
}

function TableRow({ className, ...props }: React.ComponentProps<"tr">) {
  return (
    <tr
      data-slot="table-row"
      className={cn(
        "hover:bg-muted/50 data-[state=selected]:bg-muted border-b transition-colors",
        className,
      )}
      {...props}
    />
  );
}

function TableHead({ className, ...props }: React.ComponentProps<"th">) {
  return (
    <th
      data-slot="table-head"
      className={cn(
        "text-foreground h-10 px-2 text-left align-middle font-medium whitespace-nowrap [&:has([role=checkbox])]:pr-0 [&>[role=checkbox]]:translate-y-[2px]",
        className,
      )}
      {...props}
    />
  );
}

function TableCell({ className, ...props }: React.ComponentProps<"td">) {
  return (
    <td
      data-slot="table-cell"
      className={cn(
        "p-2 align-middle whitespace-nowrap [&:has([role=checkbox])]:pr-0 [&>[role=checkbox]]:translate-y-[2px]",
        className,
      )}
      {...props}
    />
  );
}

function TableCaption({
  className,
  ...props
}: React.ComponentProps<"caption">) {
  return (
    <caption
      data-slot="table-caption"
      className={cn("text-muted-foreground mt-4 text-sm", className)}
      {...props}
    />
  );
}

export {
  Table,
  TableHeader,
  TableBody,
  TableFooter,
  TableHead,
  TableRow,
  TableCell,
  TableCaption,
};