Skip to content
Thrum

Table

A styled native table for rows of records: invoices, members, results. It is made of parts, written like the HTML, and scrolls sideways in its frame when the columns do not fit. For sorting, selection and pages use the data table, which is built on it.

Install

npx shadcn@latest add https://thrumui.dev/r/table.json

Adds components/ui/table.tsx. Nothing else in your project changes.

import { Table } from "@/components/ui/table";

Props

PropTypeDefaultDescription
Table: density"compact" | "normal"Default "normal"compact rows are 32 pixels tall, normal rows 40.
Table: stripedbooleanDefault falseShade every second body row.
Table: stickyHeaderbooleanDefault falseKeep the header row at the top of the frame while the body scrolls. The frame needs a height: set one with containerClassName.
Table: containerClassNamestring–Classes for the scrolling frame: max-h-72 for a sticky header, a width, a border.
Table: aria-labelstring–A name for the table and its frame when there is no TableCaption.
Table: classNamestring–Extra classes for the table element, merged last.
TableHeader, TableBody, TableFooterthead, tbody and tfoot props–The row groups. Body rows shade under the pointer; the footer is shaded and medium weight, for totals.
TableRow: selectedboolean–Shade the row in the accent colour and set aria-selected. Leave it out in a table without selection.
TableHead: numericbooleanDefault falseAlign the heading to the end, over a column of numbers.
TableHead: scope"col" | "row" | "colgroup" | "rowgroup"Default "col"Which cells the heading names. Use row for a heading at the start of a row.
TableCell: numericbooleanDefault falseAlign the value to the end, in figures of equal width, so the digits line up.
TableCaptionComponentProps<"caption">–What the table holds, above it. It names the table and the scrolling frame.
classNamestring–On every part: extra classes, merged last.

Motion and sound

Motion

  • A body row's shade fades in over 150 ms under the pointer.
  • Reduced motion: the shade changes at once.

Sound

Silent. This component makes no sound.

Accessibility

  • It is a native table, so screen readers announce its size and read each cell with its column heading. TableHead sets scope="col" unless you set another scope.
  • The frame around it is a region that can take the focus, named by the TableCaption, so a keyboard reader can scroll a wide table. Without a caption, give the table an aria-label.
  • A selected row carries aria-selected and the accent fill. In a table where rows can be selected, a checkbox in the row says so in words as well.
  • The frame is a panel surface. Under a glass material it becomes translucent, and its tint keeps text at 4.5 to 1.
  • Long cell text wraps. In a container narrower than the columns, the frame scrolls sideways instead of squeezing them.
KeysAction
TabFocus the table's frame.
← / → / ↑ / ↓Scroll the frame, when the table is wider or taller than it.

Source

components/ui/table.tsx
"use client";

import { createContext, useContext, useId, type ComponentProps } from "react";

import { cn } from "@/lib/utils";

export type TableDensity = "compact" | "normal";

export type TableProps = ComponentProps<"table"> & {
  /** `compact` rows are 32 pixels tall, `normal` rows 40. */
  density?: TableDensity;
  /** Shade every second body row. */
  striped?: boolean;
  /** Keep the header row in view while the body scrolls. Give the frame a height with `containerClassName`, such as `max-h-80`. */
  stickyHeader?: boolean;
  /** Classes for the frame around the table: its height, its width, its border. */
  containerClassName?: string;
};

/** The caption's id, so the scrolling frame can take the caption as its name. */
const CaptionId = createContext<string | undefined>(undefined);

/**
 * A data table: a native `<table>` in a bordered frame that scrolls sideways when the columns do not fit.
 * The frame is a focusable region named by the `TableCaption`, so a keyboard reader can scroll it with the arrow keys.
 * Build it from the parts below, as you would write the HTML.
 */
export function Table({ density = "normal", striped = false, stickyHeader = false, containerClassName, className, "aria-label": label, ...props }: TableProps) {
  const id = useId();
  return (
    <CaptionId.Provider value={`${id}-caption`}>
      <div
        data-slot="table-container"
        data-surface="panel"
        role="region"
        // Named by the caption, or by an aria-label given to the table.
        aria-label={label}
        aria-labelledby={label ? undefined : `${id}-caption`}
        tabIndex={0}
        className={cn(
          "relative w-full overflow-auto overscroll-x-contain rounded-lg border bg-card text-card-foreground",
          "focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-ring",
          containerClassName,
        )}
      >
        <table
          data-slot="table"
          data-density={density}
          data-striped={striped ? "" : undefined}
          data-sticky={stickyHeader ? "" : undefined}
          aria-label={label}
          className={cn("group/table w-full caption-top border-collapse text-sm", className)}
          {...props}
        />
      </div>
    </CaptionId.Provider>
  );
}

/** The header rows. With `stickyHeader` on the table they stay at the top of the frame. */
export function TableHeader({ className, ...props }: ComponentProps<"thead">) {
  return (
    <thead
      data-slot="table-header"
      className={cn("[&_tr]:border-b group-data-sticky/table:[&_th]:sticky group-data-sticky/table:[&_th]:top-0 group-data-sticky/table:[&_th]:z-10 group-data-sticky/table:[&_th]:bg-card", className)}
      {...props}
    />
  );
}

export function TableBody({ className, ...props }: ComponentProps<"tbody">) {
  return <tbody data-slot="table-body" className={cn("[&_tr:last-child]:border-0 group-data-striped/table:[&>tr:nth-child(even)]:bg-muted/60", className)} {...props} />;
}

/** Totals under the body, in the same columns. */
export function TableFooter({ className, ...props }: ComponentProps<"tfoot">) {
  return <tfoot data-slot="table-footer" className={cn("border-t bg-muted/60 font-medium [&>tr:last-child]:border-b-0", className)} {...props} />;
}

export type TableRowProps = ComponentProps<"tr"> & {
  /** The row is selected: it is shaded, and `aria-selected` says so. Leave it out for a table without selection. */
  selected?: boolean;
};

/** One row. Body rows shade under the pointer. */
export function TableRow({ selected, className, ...props }: TableRowProps) {
  return (
    <tr
      data-slot="table-row"
      data-selected={selected ? "" : undefined}
      aria-selected={selected}
      className={cn(
        "border-b transition-[background-color] duration-150 ease-out motion-reduce:transition-none",
        "[tbody>&]:hover:bg-muted data-selected:bg-accent! data-selected:text-accent-foreground",
        // A bar at the start of a selected row, so selection is a shape as well as a shade.
        "data-selected:[&>:first-child]:shadow-[inset_2px_0_0_var(--foreground)] rtl:data-selected:[&>:first-child]:shadow-[inset_-2px_0_0_var(--foreground)]",
        className,
      )}
      {...props}
    />
  );
}

export type TableCellProps = {
  /** A column of numbers: aligned to the end, in figures of equal width. */
  numeric?: boolean;
};

const CELL = "px-3 align-middle group-data-[density=compact]/table:py-1.5 group-data-[density=normal]/table:py-2.5 [&:has([role=checkbox])]:w-10 [&:has([role=checkbox])]:pe-0";

/** A column or row heading. `scope` is `col` unless you set it, so each cell is read with its heading. */
export function TableHead({ numeric = false, scope = "col", className, ...props }: ComponentProps<"th"> & TableCellProps) {
  return (
    <th
      data-slot="table-head"
      scope={scope}
      className={cn(CELL, "h-8 text-start text-xs font-medium whitespace-nowrap text-muted-foreground", numeric && "text-end tabular-nums", className)}
      {...props}
    />
  );
}

export function TableCell({ numeric = false, className, ...props }: ComponentProps<"td"> & TableCellProps) {
  return <td data-slot="table-cell" className={cn(CELL, numeric && "text-end tabular-nums", className)} {...props} />;
}

/** What the table holds, above it. It names the table and its scrolling frame. */
export function TableCaption({ className, ...props }: ComponentProps<"caption">) {
  const id = useContext(CaptionId);
  return <caption id={id} data-slot="table-caption" className={cn("px-3 pt-3 pb-1 text-start text-sm font-medium text-foreground", className)} {...props} />;
}

Files and dependencies

Adds
components/ui/table.tsx
Packages
None
Brings along
Nothing

Changelog

  1. 1.0.0 · 2026-12-03First release.

Loading index…

Keyboard shortcuts

/Ctrl KSearch components, docs and actions
?Show this list
AAppearance studio
SSound on or off
MFull or reduced motion
TTheme: auto, light, dark
GSurface: solid or frosted
BBackdrop photograph (under glass)
CCorner radius: square to round
RReplay the demo, on a component page
DDraw new data, on a component page
←→HomeEndMove the cursor in a focused chart
EscClose a dialog, or leave a chart cursor