← Components

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.

NameRole
Alex LinEngineer8
Piper PhanDesigner12
Mo KimProduct15
Example
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.

Terminal
npx shadcn@latest add https://isotopi.com/r/minimal.json
npx shadcn@latest add https://isotopi.com/r/table.json

By hand

No CLI needed. Add the foundation once (see Getting started), then follow these steps. They include every component this one builds on.

  1. Install the packages it uses.

    Terminal
    npm install cn lucide-react
  2. Copy 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:

Imports
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

  1. 1Container · --table-container, --table-corner; scrolls sideways when narrow.
  2. 2Header cell · --table-head-height; optional sort button.
  3. 3Row · --table-row-height; odd rows --table-row-alt; new rows --table-fresh.
  4. 4Hover pill · --table-row-hover.

Props

PropTypeDefaultDescription
TableHead sort"ascending" | "descending" | "none"—Sets aria-sort and the arrow.
TableHead onSort() => void—Makes the header a sort button.
TableRow freshboolean—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.

TokenPoints toLightDark
--table-containervar(--surface-container-lowest)oklch(1 0 0)oklch(0.14 0 0)
--table-cornervar(--shape-md)14pxsame
--table-row-altvar(--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-hovervar(--color-bg-hover)oklch(0.965 0.005 95)oklch(0.22 0 0)
--table-row-cornervar(--shape-sm)8pxsame
--table-row-heightvar(--size-row)40pxsame
--table-head-heightvar(--size-row)40pxsame
--table-cell-padding-xvar(--space-inset-item-x)12pxsame
--table-freshvar(--color-bg-selected)color-mix(in oklch, var(--ref-select) 12%, transparent)color-mix(in oklch, var(--ref-select) 16%, transparent)

Motion

TriggerWhat happensTokens
HoverA pill glides to the hovered row.--spring-snappy
SortThe arrow flips with a bounce.--duration-medium, --ease-bounce
New rowA 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

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 }