For the complete documentation index, see llms.txt. Markdown variants are available by appending .md to documentation URLs. Homepage markdown is available at /llms.md/content.md. An agent skill is available at /.well-known/agent-skills/site-skill.md.

Table

The Table component from this design system, built on Base UI.

Installation

$ pnpm dlx shadcn@latest add @cobalt/table

Options

  • size="sm" is the denser 13px text option.
  • TableRow hoverable={false} turns off the hover highlight, for header and static rows.

Source

"use client";

import { cn } from "cn";
import * as React from "react";

function Table({
  className,
  size = "default",
  ...props
}: React.ComponentProps<"table"> & { size?: "default" | "sm" }) {
  return (
    <div
      data-slot="table-container"
      className="relative w-full overflow-x-auto"
    >
      <table
        data-slot="table"
        data-size={size}
        className={cn(
          "w-full border-collapse caption-bottom text-start text-sm [&_th]:transition-[padding,font-size] [&_th]:duration-200 [&_th]:ease-[ease] [&_td]:transition-[padding,font-size] [&_td]:duration-200 [&_td]:ease-[ease] motion-reduce:[&_th]:transition-none motion-reduce:[&_td]:transition-none data-[size=sm]:[&_th]:px-2.5 data-[size=sm]:[&_th]:py-1.5 data-[size=sm]:[&_th]:text-[13px] data-[size=sm]:[&_th]:leading-[18px] data-[size=sm]:[&_td]:px-2.5 data-[size=sm]:[&_td]:py-1.5 data-[size=sm]:[&_td]:text-[13px] data-[size=sm]:[&_td]:leading-[18px]",
          className
        )}
        {...props}
      />
    </div>
  );
}

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

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

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

function TableRow({
  className,
  hoverable = true,
  ...props
}: React.ComponentProps<"tr"> & {
  /** Set to false for header and static rows that should not highlight on hover. */
  hoverable?: boolean;
}) {
  return (
    <tr
      data-slot="table-row"
      className={cn(
        // Only hover fades; selecting or unselecting a row switches at once.
        "transition-colors duration-150 motion-reduce:transition-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-inset has-aria-expanded:bg-muted/50 data-[state=selected]:bg-muted data-[state=selected]:transition-none",
        hoverable && "hover:bg-muted/50",
        className
      )}
      {...props}
    />
  );
}

function TableHead({ className, ...props }: React.ComponentProps<"th">) {
  return (
    <th
      data-slot="table-head"
      className={cn(
        "border-y border-[var(--border-subtle,var(--border))] bg-[var(--surface-sunken,var(--muted))] px-3 py-2.5 text-start align-middle text-sm leading-5 font-medium whitespace-nowrap text-[color:var(--foreground-subtle,var(--muted-foreground))] outline-none [&:has([role=checkbox])]:pr-0",
        className
      )}
      {...props}
    />
  );
}

function TableCell({ className, ...props }: React.ComponentProps<"td">) {
  return (
    <td
      data-slot="table-cell"
      className={cn(
        "px-3 py-2.5 align-middle text-sm leading-5 font-medium text-[color:var(--foreground-strong,var(--foreground))] outline-none [&:has([role=checkbox])]:pr-0",
        className
      )}
      {...props}
    />
  );
}

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

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