Containment
Table
Rows and columns, with no borders: tinted rows, a hover pill that follows the pointer, sort arrows that flip, and a glow on new rows.
Examples and variants
Sortable
Sort by projects; add a row to see the glow.
| Name | Role | |
|---|---|---|
| Alex Lin | Engineer | 8 |
| Piper Phan | Designer | 12 |
| Mo Kim | Product | 15 |
function TableSortable() {
const [sort, setSort] = useState<"ascending" | "descending">("ascending")
const [rows, setRows] = useState(people)
const [fresh, setFresh] = useState("")
const sorted = [...rows].sort((a, b) => (sort === "ascending" ? a.projects - b.projects : b.projects - a.projects))
return (
<div className="flex w-full flex-col gap-3">
<Table>
<TableHeader>
<TableRow>
<TableHead>Name</TableHead>
<TableHead>Role</TableHead>
<TableHead sort={sort} onSort={() => setSort((s) => (s === "ascending" ? "descending" : "ascending"))}>
Projects
</TableHead>
</TableRow>
</TableHeader>
<TableBody>
{sorted.map((row) => (
<TableRow key={row.name} fresh={row.name === fresh}>
<TableCell className="font-medium">{row.name}</TableCell>
<TableCell className="text-muted-foreground">{row.role}</TableCell>
<TableCell className="tabular-nums">{row.projects}</TableCell>
</TableRow>
))}
</TableBody>
</Table>
<Button
size="sm"
variant="secondary"
className="self-center"
onClick={() => {
const name = `New person ${rows.length - 2}`
setRows((all) => [...all, { name, role: "Guest", projects: rows.length }])
setFresh(name)
}}
>
Add a row
</Button>
</div>
)
}Spec drawing
Measured from the live component at default density. Each code maps to the token that sets it; change the token and the drawing follows.
Install
With the CLI
The shadcn CLI only delivers files: it copies our code from the Isotopi registry into your project and installs the packages it uses. Add the foundation once, then the component.
npx shadcn@latest add https://isotopi.com/r/minimal.json
npx shadcn@latest add https://isotopi.com/r/table.jsonBy hand
No CLI needed. Add the foundation once (see Getting started), then follow these steps. They include every component this one builds on.
Install the packages it uses.
Terminalnpm install cn lucide-reactCopy this file to the same paths in your project. Imports use the
@/alias for your project root.components/ui/table.tsxtable
components/ui/table.tsx"use client" import * as React from "react" import { ArrowUpIcon } from "lucide-react" import { cn } from "cn" /* Table: rows and columns of comparable data. Minimal has no borders: rows alternate between canvas and a faint tint, a hover pill follows the pointer down the table, sort arrows flip with a bounce, and new rows arrive with a short highlight. Real table markup. */ function Table({ className, children, ...props }: React.ComponentProps<"table">) { const wrapRef = React.useRef<HTMLDivElement>(null) const pillRef = React.useRef<HTMLSpanElement>(null) function track(event: React.PointerEvent<HTMLDivElement>) { const row = (event.target as HTMLElement).closest<HTMLTableRowElement>("tbody tr") const wrap = wrapRef.current const pill = pillRef.current if (!row || !wrap || !pill) return const top = row.getBoundingClientRect().top - wrap.getBoundingClientRect().top + wrap.scrollTop pill.style.top = `${top}px` pill.style.height = `${row.offsetHeight}px` pill.dataset.visible = "" } return ( <div ref={wrapRef} data-slot="table-container" className="relative w-full overflow-x-auto rounded-(--table-corner) bg-(--table-container)" onPointerOver={track} onPointerLeave={() => pillRef.current?.removeAttribute("data-visible")} > <span ref={pillRef} aria-hidden className="table-pill pointer-events-none absolute inset-x-1 rounded-(--table-row-corner) bg-(--table-row-hover)" /> <table data-slot="table" className={cn("relative w-full caption-bottom border-collapse text-sm", className)} {...props}> {children} </table> </div> ) } function TableHeader(props: React.ComponentProps<"thead">) { return <thead data-slot="table-header" {...props} /> } function TableBody({ className, ...props }: React.ComponentProps<"tbody">) { return <tbody data-slot="table-body" className={cn("[&>tr:nth-child(odd)>td]:bg-(--table-row-alt)", className)} {...props} /> } function TableRow({ className, fresh = false, ...props }: React.ComponentProps<"tr"> & { fresh?: boolean }) { return <tr data-slot="table-row" data-fresh={fresh || undefined} className={cn("table-row", className)} {...props} /> } function TableHead({ className, sort, onSort, children, ...props }: React.ComponentProps<"th"> & { sort?: "ascending" | "descending" | "none"; onSort?: () => void }) { return ( <th data-slot="table-head" scope="col" aria-sort={sort} className={cn("h-(--table-head-height) px-(--table-cell-padding-x) text-start align-middle font-mono text-caption font-normal tracking-[0.08em] text-ink-subtle uppercase", className)} {...props} > {onSort ? ( <button type="button" onClick={onSort} className="hit-target group/sort -mx-1 inline-flex items-center gap-1 rounded-(--shape-sm) px-1 uppercase outline-none hover:text-foreground focus-visible:shadow-(--focus-ring)" > {children} <ArrowUpIcon aria-hidden data-sort={sort ?? "none"} className="table-sort size-(--size-icon-sm)" /> </button> ) : ( children )} </th> ) } function TableCell({ className, ...props }: React.ComponentProps<"td">) { return <td data-slot="table-cell" className={cn("h-(--table-row-height) px-(--table-cell-padding-x) align-middle first:rounded-s-(--table-row-corner) last:rounded-e-(--table-row-corner)", className)} {...props} /> } function TableCaption({ className, ...props }: React.ComponentProps<"caption">) { return <caption data-slot="table-caption" className={cn("py-3 text-sm text-muted-foreground", className)} {...props} /> } export { Table, TableHeader, TableBody, TableRow, TableHead, TableCell, TableCaption }
Imports
What the examples on this page import:
import { CopyIcon, FileIcon, MoonIcon, PlusIcon, SettingsIcon, TrashIcon } from "lucide-react"
import { Button } from "@/components/ui/button"
import { Calendar, type CalendarRange } from "@/components/ui/calendar"
import { Carousel } from "@/components/ui/carousel"
import { CommandDialog, type CommandItem } from "@/components/ui/command"
import { ContextMenu, ContextMenuContent, ContextMenuTrigger } from "@/components/ui/context-menu"
import { DatePicker } from "@/components/ui/date-picker"
import { DropdownMenuContent, DropdownMenuItem, DropdownMenuSeparator, DropdownMenuShortcut } from "@/components/ui/dropdown-menu"
import { Kbd, KbdGroup } from "@/components/ui/kbd"
import { Menubar, MenubarMenu, MenubarTrigger } from "@/components/ui/menubar"
import { Search } from "@/components/ui/search"
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table"
import { TimePicker } from "@/components/ui/time-picker"Anatomy
- 1Container · --table-container, --table-corner; scrolls sideways when narrow.
- 2Header cell · --table-head-height; optional sort button.
- 3Row · --table-row-height; odd rows --table-row-alt; new rows --table-fresh.
- 4Hover pill · --table-row-hover.
Props
| Prop | Type | Default | Description |
|---|---|---|---|
TableHead sort | "ascending" | "descending" | "none" | — | Sets aria-sort and the arrow. |
TableHead onSort | () => void | — | Makes the header a sort button. |
TableRow fresh | boolean | — | Plays the new-row glow. |
Tokens
Every visual spec is a component token in styles/tokens/components.css, pointing at a semantic token. Change a token to restyle the component; themes change the semantic layer.
| Token | Points to | Light | Dark |
|---|---|---|---|
--table-container | var(--surface-container-lowest) | oklch(1 0 0) | oklch(0.14 0 0) |
--table-corner | var(--shape-md) | 14px | same |
--table-row-alt | var(--color-bg-row-alt) | color-mix(in oklch, var(--color-fg-default) 4%, transparent) | color-mix(in oklch, var(--color-fg-default) 6%, transparent) |
--table-row-hover | var(--color-bg-hover) | oklch(0.965 0.005 95) | oklch(0.22 0 0) |
--table-row-corner | var(--shape-sm) | 8px | same |
--table-row-height | var(--size-row) | 40px | same |
--table-head-height | var(--size-row) | 40px | same |
--table-cell-padding-x | var(--space-inset-item-x) | 12px | same |
--table-fresh | var(--color-bg-selected) | color-mix(in oklch, var(--ref-select) 12%, transparent) | color-mix(in oklch, var(--ref-select) 16%, transparent) |
Motion
| Trigger | What happens | Tokens |
|---|---|---|
| Hover | A pill glides to the hovered row. | --spring-snappy |
| Sort | The arrow flips with a bounce. | --duration-medium, --ease-bounce |
| New row | A soft highlight fades over a second. | --duration-pulse |
Calm keeps the motion without overshoot. Still (and reduced motion) keeps a designed frame with no movement.
Accessibility
- Real table markup with scope on headers and aria-sort.
- Sort buttons say what they sort.
Usage
Do
- Use for comparable data.
Don’t
- Don't use for layout.
Code
"use client"
import * as React from "react"
import { ArrowUpIcon } from "lucide-react"
import { cn } from "cn"
/*
Table: rows and columns of comparable data. Minimal has no borders: rows
alternate between canvas and a faint tint, a hover pill follows the pointer
down the table, sort arrows flip with a bounce, and new rows arrive with a
short highlight. Real table markup.
*/
function Table({ className, children, ...props }: React.ComponentProps<"table">) {
const wrapRef = React.useRef<HTMLDivElement>(null)
const pillRef = React.useRef<HTMLSpanElement>(null)
function track(event: React.PointerEvent<HTMLDivElement>) {
const row = (event.target as HTMLElement).closest<HTMLTableRowElement>("tbody tr")
const wrap = wrapRef.current
const pill = pillRef.current
if (!row || !wrap || !pill) return
const top = row.getBoundingClientRect().top - wrap.getBoundingClientRect().top + wrap.scrollTop
pill.style.top = `${top}px`
pill.style.height = `${row.offsetHeight}px`
pill.dataset.visible = ""
}
return (
<div
ref={wrapRef}
data-slot="table-container"
className="relative w-full overflow-x-auto rounded-(--table-corner) bg-(--table-container)"
onPointerOver={track}
onPointerLeave={() => pillRef.current?.removeAttribute("data-visible")}
>
<span ref={pillRef} aria-hidden className="table-pill pointer-events-none absolute inset-x-1 rounded-(--table-row-corner) bg-(--table-row-hover)" />
<table data-slot="table" className={cn("relative w-full caption-bottom border-collapse text-sm", className)} {...props}>
{children}
</table>
</div>
)
}
function TableHeader(props: React.ComponentProps<"thead">) {
return <thead data-slot="table-header" {...props} />
}
function TableBody({ className, ...props }: React.ComponentProps<"tbody">) {
return <tbody data-slot="table-body" className={cn("[&>tr:nth-child(odd)>td]:bg-(--table-row-alt)", className)} {...props} />
}
function TableRow({ className, fresh = false, ...props }: React.ComponentProps<"tr"> & { fresh?: boolean }) {
return <tr data-slot="table-row" data-fresh={fresh || undefined} className={cn("table-row", className)} {...props} />
}
function TableHead({
className,
sort,
onSort,
children,
...props
}: React.ComponentProps<"th"> & { sort?: "ascending" | "descending" | "none"; onSort?: () => void }) {
return (
<th
data-slot="table-head"
scope="col"
aria-sort={sort}
className={cn("h-(--table-head-height) px-(--table-cell-padding-x) text-start align-middle font-mono text-caption font-normal tracking-[0.08em] text-ink-subtle uppercase", className)}
{...props}
>
{onSort ? (
<button
type="button"
onClick={onSort}
className="hit-target group/sort -mx-1 inline-flex items-center gap-1 rounded-(--shape-sm) px-1 uppercase outline-none hover:text-foreground focus-visible:shadow-(--focus-ring)"
>
{children}
<ArrowUpIcon
aria-hidden
data-sort={sort ?? "none"}
className="table-sort size-(--size-icon-sm)"
/>
</button>
) : (
children
)}
</th>
)
}
function TableCell({ className, ...props }: React.ComponentProps<"td">) {
return <td data-slot="table-cell" className={cn("h-(--table-row-height) px-(--table-cell-padding-x) align-middle first:rounded-s-(--table-row-corner) last:rounded-e-(--table-row-corner)", className)} {...props} />
}
function TableCaption({ className, ...props }: React.ComponentProps<"caption">) {
return <caption data-slot="table-caption" className={cn("py-3 text-sm text-muted-foreground", className)} {...props} />
}
export { Table, TableHeader, TableBody, TableRow, TableHead, TableCell, TableCaption }