Table

Rows of data, scrolling sideways when wide.

Plain table elements with the system’s spacing and rules. Mark a selected row with aria-selected.

Preview
basic.tsx
import { Badge } from "~/components/ui/badge"
import {
  Table,
  TableBody,
  TableCaption,
  TableCell,
  TableHead,
  TableHeader,
  TableRow,
} from "~/components/ui/table"
import { Text } from "~/components/ui/text"

const pages = [
  { page: "A1", section: "News", stories: 4, status: "2 errors" },
  { page: "A2", section: "News", stories: 3, status: "Ready" },
  { page: "B1", section: "Sports", stories: 5, status: "1 warning" },
  { page: "C1", section: "Opinion", stories: 2, status: "Ready" },
]

export default function Example() {
  return (
    <Table>
      <TableCaption>Weekend edition, pages with checks run.</TableCaption>
      <TableHeader>
        <TableRow>
          <TableHead>Page</TableHead>
          <TableHead>Section</TableHead>
          <TableHead className="text-right">Stories</TableHead>
          <TableHead className="text-right">Checks</TableHead>
        </TableRow>
      </TableHeader>
      <TableBody>
        {pages.map((row) => (
          <TableRow key={row.page}>
            <TableCell>
              <Text render={<span />} weight="medium">
                {row.page}
              </Text>
            </TableCell>
            <TableCell>{row.section}</TableCell>
            <TableCell className="text-right">
              <Text render={<span />} tabular>
                {row.stories}
              </Text>
            </TableCell>
            <TableCell className="text-right">
              <Badge
                variant={
                  row.status === "Ready"
                    ? "success"
                    : row.status.includes("error")
                      ? "destructive"
                      : "warning"
                }
              >
                {row.status}
              </Badge>
            </TableCell>
          </TableRow>
        ))}
      </TableBody>
    </Table>
  )
}

Dependencies

What it's built from and what's built from it in Base, read from the imports in its source.

UsesScroll AreaprimitiveTextprimitive
Used byNothing yet.
Packages None.

Installation

Set the project up for Base once first, as Getting started shows. Then add it with the CLI, or by hand.

With the CLI

terminal
npx shadcn@latest add https://design.zepez.dev/r/base/table.json

Manually

Add the components it uses first, listed under Dependencies.

Then copy the source:

components/ui/table.tsx
import type * as React from "react"

import { ScrollArea } from "~/components/ui/scroll-area"
import { textVariants } from "~/components/ui/text"
import { cn } from "~/lib/utils"

/** A data table that scrolls sideways when it's wider than its container. */
function Table({ className, ...props }: React.ComponentProps<"table">) {
  return (
    <ScrollArea orientation="horizontal" data-slot="table-container" className="w-full">
      <table
        data-slot="table"
        className={cn(textVariants({ size: "sm" }), "w-full caption-bottom", className)}
        {...props}
      />
    </ScrollArea>
  )
}

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

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

/** Totals or notes under the body. */
function TableFooter({ className, ...props }: React.ComponentProps<"tfoot">) {
  return (
    <tfoot
      data-slot="table-footer"
      className={cn(
        textVariants({ weight: "medium" }),
        "border-t border-border bg-muted/50 [&>tr]:last:border-b-0",
        className,
      )}
      {...props}
    />
  )
}

/** A row. Set `aria-selected` to mark it selected. */
function TableRow({ className, ...props }: React.ComponentProps<"tr">) {
  return (
    <tr
      data-slot="table-row"
      className={cn(
        "border-b border-border transition-colors hover:bg-muted/50 aria-selected:bg-muted",
        className,
      )}
      {...props}
    />
  )
}

/** A column heading, or with `scope="row"`, a row's heading, sized like the cells beside it. */
function TableHead({ className, ...props }: React.ComponentProps<"th">) {
  return (
    <th
      data-slot="table-head"
      className={cn(
        textVariants({ weight: "medium" }),
        "h-10 px-sm text-left align-middle whitespace-nowrap text-muted-foreground [&:has([role=checkbox])]:pr-0",
        "[&[scope=row]]:h-auto [&[scope=row]]:py-sm",
        className,
      )}
      {...props}
    />
  )
}

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

/** Describes the table, under it. */
function TableCaption({ className, ...props }: React.ComponentProps<"caption">) {
  return (
    <caption
      data-slot="table-caption"
      className={cn(textVariants({ size: "sm", tone: "muted" }), "mt-lg", className)}
      {...props}
    />
  )
}

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

API reference

Generated from Base's types. Props marked ours are added here; the rest come from the ui-sdk part it wraps. Every part also accepts its element's native attributes.

Table

A data table that scrolls sideways when it's wider than its container.

Takes the props of a native div.

TableBody

The body row group.

Takes the props of a native div.

TableCaption

Describes the table, under it.

Takes the props of a native div.

TableCell

A cell.

Takes the props of a native div.

TableFooter

Totals or notes under the body.

Takes the props of a native div.

TableHead

A column heading, or with scope="row", a row's heading, sized like the cells beside it.

Takes the props of a native div.

TableHeader

The header row group.

Takes the props of a native div.

TableRow

A row. Set aria-selected to mark it selected.

Takes the props of a native div.