Data table

A shadcn/ui data table on TanStack Table: sortable columns, search and faceted filters, pagination, row selection with bulk actions, column visibility, expandable rows, inline editing, a sticky header and pinned columns.

npx shadcn@latest add https://dresscodeui.com/r/dresscode/data-table.json

6 examples below; each installs the component with it. They wear whichever DressCode theme you install.

Sorting and pagination

Sort menus on every header, money right-aligned with totals, page size and page controls, and loading rows.

Search and filters

Search across columns, faceted status and client filters with counts, a reset button and a column visibility menu.

Row selection and bulk actions

Checkboxes with select all and shift-click ranges, a bulk bar to remind, export or delete, and a menu on each row.

Expandable rows

Rows that open in place to show an invoice's line items, totals and payment details.

Editable cells

Edit an email, terms or rate in place, with save and cancel, validation messages and undo.

Sticky header and pinned columns

Twenty-one columns that scroll both ways inside the table, with the header, first column and actions held in place.

The component

What shadcn@latest add https://dresscodeui.com/r/dresscode/data-table.json writes. It uses only shadcn/ui's variables, so it follows the theme.

components/ui/data-table-column-header.tsx
// Data table column header: the column's title as a menu button to sort it ascending or descending,
// clear the sort, or hide the column. The arrow shows the current sort; the header cell carries
// aria-sort (DataTable sets it). Right-aligned columns (meta.align) open the menu from the right.
// Built for shadcn/ui; reads only the theme's variables.
"use client"

import * as React from "react"
import type { Column } from "@tanstack/react-table"
import { ArrowDownIcon, ArrowUpIcon, ChevronsUpDownIcon, EyeOffIcon, XIcon } from "lucide-react"

import { Button } from "@/components/ui/button"
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuGroup,
  DropdownMenuItem,
  DropdownMenuRadioGroup,
  DropdownMenuRadioItem,
  DropdownMenuSeparator,
  DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu"
import { cn } from "@/lib/utils"

type DataTableColumnHeaderProps<TData, TValue> = React.ComponentProps<"div"> & {
  column: Column<TData, TValue>
  title: string
  /** Words for the two directions, e.g. { asc: "Oldest first", desc: "Newest first" }. */
  sortLabels?: { asc: string; desc: string }
}

function DataTableColumnHeader<TData, TValue>({
  column,
  title,
  sortLabels = { asc: "Ascending", desc: "Descending" },
  className,
  ...props
}: DataTableColumnHeaderProps<TData, TValue>) {
  const align = column.columnDef.meta?.align
  const canSort = column.getCanSort()
  const canHide = column.getCanHide()
  const sorted = column.getIsSorted()
  const justify = align === "right" ? "justify-end" : align === "center" ? "justify-center" : undefined

  if (!canSort && !canHide) {
    return (
      <div data-slot="data-table-column-header" className={cn("flex items-center", justify, className)} {...props}>
        {title}
      </div>
    )
  }

  return (
    <div data-slot="data-table-column-header" className={cn("flex items-center", justify, className)} {...props}>
      <DropdownMenu>
        <DropdownMenuTrigger
          render={
            <Button
              variant="ghost"
              size="sm"
              className={cn(
                "h-7 gap-1 px-2 font-medium text-muted-foreground hover:text-foreground data-popup-open:text-foreground",
                sorted && "text-foreground",
                align === "right" ? "-mr-2 flex-row-reverse" : "-ml-2",
              )}
            />
          }
        >
          <span>{title}</span>
          {sorted === "desc" ? (
            <ArrowDownIcon aria-hidden="true" />
          ) : sorted === "asc" ? (
            <ArrowUpIcon aria-hidden="true" />
          ) : (
            <ChevronsUpDownIcon aria-hidden="true" className="opacity-60" />
          )}
          <span className="sr-only">
            {sorted ? `, sorted ${(sorted === "asc" ? sortLabels.asc : sortLabels.desc).toLowerCase()}. ` : ". "}Column options
          </span>
        </DropdownMenuTrigger>
        <DropdownMenuContent align={align === "right" ? "end" : "start"} className="w-auto min-w-40">
          {canSort ? (
            <DropdownMenuGroup>
              <DropdownMenuRadioGroup
                value={sorted || ""}
                onValueChange={(value) => (value === "asc" || value === "desc" ? column.toggleSorting(value === "desc") : column.clearSorting())}
              >
                <DropdownMenuRadioItem value="asc" closeOnClick>
                  <ArrowUpIcon className="text-muted-foreground" />
                  {sortLabels.asc}
                </DropdownMenuRadioItem>
                <DropdownMenuRadioItem value="desc" closeOnClick>
                  <ArrowDownIcon className="text-muted-foreground" />
                  {sortLabels.desc}
                </DropdownMenuRadioItem>
              </DropdownMenuRadioGroup>
              {sorted ? (
                <DropdownMenuItem onClick={() => column.clearSorting()}>
                  <XIcon className="text-muted-foreground" />
                  Clear sort
                </DropdownMenuItem>
              ) : null}
            </DropdownMenuGroup>
          ) : null}
          {canSort && canHide ? <DropdownMenuSeparator /> : null}
          {canHide ? (
            <DropdownMenuItem onClick={() => column.toggleVisibility(false)}>
              <EyeOffIcon className="text-muted-foreground" />
              Hide column
            </DropdownMenuItem>
          ) : null}
        </DropdownMenuContent>
      </DropdownMenu>
    </div>
  )
}

export { DataTableColumnHeader }
export type { DataTableColumnHeaderProps }
components/ui/data-table-faceted-filter.tsx
// Data table faceted filter: a dashed button that opens a searchable list of a column's values, each
// with a checkbox and how many rows have it (counted with the other filters applied). Pick several;
// the button shows what is picked. Give the column `filterFn: facetedFilterFn`.
// Built for shadcn/ui; reads only the theme's variables.
"use client"

import * as React from "react"
import type { Column, Row } from "@tanstack/react-table"
import { CheckIcon, PlusCircleIcon } from "lucide-react"

import { Badge } from "@/components/ui/badge"
import { Button } from "@/components/ui/button"
import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList, CommandSeparator } from "@/components/ui/command"
import { Popover, PopoverContent, PopoverTrigger } from "@/components/ui/popover"
import { cn } from "@/lib/utils"

type FacetOption = {
  label: string
  value: string
  icon?: React.ComponentType<{ className?: string }>
}

type DataTableFacetedFilterProps<TData, TValue> = {
  column?: Column<TData, TValue>
  title: string
  options: FacetOption[]
  /** Show the search box at the top of the list. On by default when there are more than 6 options. */
  searchable?: boolean
  className?: string
}

/** Keeps rows whose value is one of the picked values. Use as the column's filterFn. */
function facetedFilterFn<TData>(row: Row<TData>, columnId: string, value: string[] | undefined) {
  return !value?.length || value.includes(String(row.getValue(columnId)))
}
facetedFilterFn.autoRemove = (value: unknown) => !Array.isArray(value) || value.length === 0

function DataTableFacetedFilter<TData, TValue>({ column, title, options, searchable, className }: DataTableFacetedFilterProps<TData, TValue>) {
  const facets = column?.getFacetedUniqueValues()
  const selected = new Set((column?.getFilterValue() as string[] | undefined) ?? [])
  const picked = options.filter((o) => selected.has(o.value))
  const withSearch = searchable ?? options.length > 6

  const toggle = (value: string) => {
    const next = new Set(selected)
    if (next.has(value)) next.delete(value)
    else next.add(value)
    column?.setFilterValue(next.size ? options.map((o) => o.value).filter((v) => next.has(v)) : undefined)
  }

  return (
    <Popover>
      <PopoverTrigger
        render={<Button variant="outline" size="sm" className={cn("border-dashed data-popup-open:bg-muted", selected.size > 0 && "border-solid", className)} />}
      >
        <PlusCircleIcon className="text-muted-foreground" />
        {title}
        {picked.length > 0 ? (
          <>
            <span aria-hidden="true" className="mx-0.5 h-4 w-px bg-border" />
            <Badge variant="secondary" className="rounded-sm px-1.5 font-normal tabular-nums lg:hidden">
              {picked.length}
              <span className="sr-only"> selected</span>
            </Badge>
            <span className="hidden gap-1 lg:flex">
              {picked.length > 2 ? (
                <Badge variant="secondary" className="rounded-sm px-1.5 font-normal">
                  {picked.length} selected
                </Badge>
              ) : (
                picked.map((o) => (
                  <Badge key={o.value} variant="secondary" className="max-w-32 rounded-sm px-1.5 font-normal">
                    <span className="truncate">{o.label}</span>
                  </Badge>
                ))
              )}
            </span>
          </>
        ) : null}
      </PopoverTrigger>
      <PopoverContent align="start" className="w-60 gap-0 p-0">
        <Command>
          {withSearch ? <CommandInput placeholder={`Search ${title.toLowerCase()}`} aria-label={`Search ${title.toLowerCase()}`} /> : null}
          <CommandList aria-label={title}>
            <CommandEmpty>No matches.</CommandEmpty>
            <CommandGroup>
              {options.map((option) => {
                const isSelected = selected.has(option.value)
                const count = facets?.get(option.value) ?? 0
                return (
                  <CommandItem
                    key={option.value}
                    value={option.label}
                    onSelect={() => toggle(option.value)}
                    aria-checked={isSelected}
                    className="[&>svg:last-child]:hidden"
                  >
                    <span
                      aria-hidden="true"
                      className={cn(
                        "flex size-4 shrink-0 items-center justify-center rounded-[4px] border border-input",
                        isSelected && "border-primary bg-primary text-primary-foreground",
                      )}
                    >
                      {isSelected ? <CheckIcon className="size-3" /> : null}
                    </span>
                    {option.icon ? <option.icon className="text-muted-foreground" /> : null}
                    <span className="truncate">{option.label}</span>
                    <span className={cn("ml-auto text-xs tabular-nums text-muted-foreground", count === 0 && "opacity-60")}>
                      {count}
                      <span className="sr-only"> {count === 1 ? "row" : "rows"}</span>
                    </span>
                  </CommandItem>
                )
              })}
            </CommandGroup>
            {selected.size > 0 ? (
              <>
                <CommandSeparator />
                <CommandGroup>
                  <CommandItem value="__clear" onSelect={() => column?.setFilterValue(undefined)} className="justify-center [&>svg:last-child]:hidden">
                    Clear filter
                  </CommandItem>
                </CommandGroup>
              </>
            ) : null}
          </CommandList>
        </Command>
      </PopoverContent>
    </Popover>
  )
}

export { DataTableFacetedFilter, facetedFilterFn }
export type { DataTableFacetedFilterProps, FacetOption }
components/ui/data-table-pagination.tsx
// Data table pagination: which rows are showing (or how many are selected), rows per page, page n of m
// and first / previous / next / last. On phones the first and last buttons and the label fold away.
// Built for shadcn/ui; reads only the theme's variables.
"use client"

import * as React from "react"
import type { Table } from "@tanstack/react-table"
import { ChevronLeftIcon, ChevronRightIcon, ChevronsLeftIcon, ChevronsRightIcon } from "lucide-react"

import { Button } from "@/components/ui/button"
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"
import { cn } from "@/lib/utils"

type DataTablePaginationProps<TData> = React.ComponentProps<"div"> & {
  table: Table<TData>
  pageSizeOptions?: number[]
  /** What a row is called: "Showing 1–10 of 48 invoices". */
  rowLabel?: { singular: string; plural: string }
}

function DataTablePagination<TData>({
  table,
  pageSizeOptions = [10, 20, 50],
  rowLabel = { singular: "row", plural: "rows" },
  className,
  ...props
}: DataTablePaginationProps<TData>) {
  const labelId = React.useId()
  const { pageIndex, pageSize } = table.getState().pagination
  const total = table.getFilteredRowModel().rows.length
  const pageCount = Math.max(table.getPageCount(), 1)
  const selectable = Boolean(table.options.enableRowSelection)
  const selected = selectable ? table.getFilteredSelectedRowModel().rows.length : 0
  const from = total ? pageIndex * pageSize + 1 : 0
  const to = Math.min(total, (pageIndex + 1) * pageSize)
  const noun = total === 1 ? rowLabel.singular : rowLabel.plural
  const sizes = pageSizeOptions.includes(pageSize) ? pageSizeOptions : [...pageSizeOptions, pageSize].sort((a, b) => a - b)
  const items = sizes.map((s) => ({ label: String(s), value: String(s) }))

  return (
    <div
      data-slot="data-table-pagination"
      className={cn("flex flex-wrap items-center justify-between gap-x-6 gap-y-3 px-1 text-sm", className)}
      {...props}
    >
      <p className="text-muted-foreground tabular-nums" aria-live="polite">
        {selected > 0 ? (
          <>
            <span className="font-medium text-foreground">{selected}</span> of {total} {noun} selected
          </>
        ) : total ? (
          <>
            Showing {from}–{to} of {total} {noun}
          </>
        ) : (
          <>No {rowLabel.plural}</>
        )}
      </p>
      <div className="flex items-center gap-3 sm:gap-6">
        <div className="flex items-center gap-2">
          <span id={labelId} className="hidden font-medium sm:inline">
            Rows per page
          </span>
          <Select items={items} value={String(pageSize)} onValueChange={(value) => value != null && table.setPageSize(Number(value))}>
            <SelectTrigger size="sm" aria-labelledby={labelId} className="w-[4.5rem] tabular-nums">
              <SelectValue />
            </SelectTrigger>
            <SelectContent side="top">
              {items.map((item) => (
                <SelectItem key={item.value} value={item.value} className="tabular-nums">
                  {item.label}
                </SelectItem>
              ))}
            </SelectContent>
          </Select>
        </div>
        <span className="font-medium whitespace-nowrap tabular-nums">
          Page {Math.min(pageIndex + 1, pageCount)} of {pageCount}
        </span>
        <div className="flex items-center gap-1">
          <Button
            variant="outline"
            size="icon-sm"
            className="hidden sm:inline-flex"
            onClick={() => table.firstPage()}
            disabled={!table.getCanPreviousPage()}
            aria-label="Go to first page"
          >
            <ChevronsLeftIcon />
          </Button>
          <Button variant="outline" size="icon-sm" onClick={() => table.previousPage()} disabled={!table.getCanPreviousPage()} aria-label="Go to previous page">
            <ChevronLeftIcon />
          </Button>
          <Button variant="outline" size="icon-sm" onClick={() => table.nextPage()} disabled={!table.getCanNextPage()} aria-label="Go to next page">
            <ChevronRightIcon />
          </Button>
          <Button
            variant="outline"
            size="icon-sm"
            className="hidden sm:inline-flex"
            onClick={() => table.lastPage()}
            disabled={!table.getCanNextPage()}
            aria-label="Go to last page"
          >
            <ChevronsRightIcon />
          </Button>
        </div>
      </div>
    </div>
  )
}

export { DataTablePagination }
export type { DataTablePaginationProps }
components/ui/data-table-view-options.tsx
// Data table view options: a menu of checkboxes to show and hide columns, named by each column's
// meta.label. The last visible column cannot be hidden; "Reset columns" brings back the defaults.
// Built for shadcn/ui; reads only the theme's variables.
"use client"

import type { Table } from "@tanstack/react-table"
import { RotateCcwIcon, Settings2Icon } from "lucide-react"

import { Button } from "@/components/ui/button"
import {
  DropdownMenu,
  DropdownMenuCheckboxItem,
  DropdownMenuContent,
  DropdownMenuGroup,
  DropdownMenuItem,
  DropdownMenuLabel,
  DropdownMenuSeparator,
  DropdownMenuTrigger,
} from "@/components/ui/dropdown-menu"
import { cn } from "@/lib/utils"

type DataTableViewOptionsProps<TData> = {
  table: Table<TData>
  className?: string
}

function DataTableViewOptions<TData>({ table, className }: DataTableViewOptionsProps<TData>) {
  const columns = table.getAllLeafColumns().filter((c) => c.accessorFn !== undefined && c.getCanHide())
  const visible = columns.filter((c) => c.getIsVisible())
  const initial = table.initialState.columnVisibility ?? {}
  const isDefault = columns.every((c) => c.getIsVisible() === (initial[c.id] ?? true))

  return (
    <DropdownMenu>
      <DropdownMenuTrigger render={<Button variant="outline" size="sm" className={cn("data-popup-open:bg-muted", className)} />}>
        <Settings2Icon />
        <span className="max-sm:sr-only">View</span>
        {visible.length < columns.length ? (
          <span className="text-muted-foreground tabular-nums max-sm:hidden">
            {visible.length}/{columns.length}
          </span>
        ) : null}
      </DropdownMenuTrigger>
      <DropdownMenuContent align="end" className="w-52">
        <DropdownMenuGroup>
          <DropdownMenuLabel>Show columns</DropdownMenuLabel>
          {columns.map((column) => {
            const only = column.getIsVisible() && visible.length === 1
            return (
              <DropdownMenuCheckboxItem
                key={column.id}
                checked={column.getIsVisible()}
                disabled={only}
                onCheckedChange={(value) => column.toggleVisibility(value)}
              >
                {column.columnDef.meta?.label ?? column.id}
              </DropdownMenuCheckboxItem>
            )
          })}
        </DropdownMenuGroup>
        <DropdownMenuSeparator />
        <DropdownMenuItem disabled={isDefault} onClick={() => table.resetColumnVisibility()}>
          <RotateCcwIcon className="text-muted-foreground" />
          Reset columns
        </DropdownMenuItem>
      </DropdownMenuContent>
    </DropdownMenu>
  )
}

export { DataTableViewOptions }
export type { DataTableViewOptionsProps }
components/ui/data-table.tsx
// Data table: rows of records on @tanstack/react-table and the stock shadcn/ui Table. Sorting,
// search and column filters, pagination, row selection (shift-click selects a range), column
// visibility, expandable rows, a totals footer, a sticky header and pinned columns.
// Pass columns and data; the toolbar slot gets the table instance for search, filters and bulk
// actions. Column meta (label, align, classes) is typed below. Built for shadcn/ui; reads only the
// theme's variables. The frame's background is var(--data-table-bg), the page background by default:
// set it to var(--card) when the table sits in a card, so pinned cells match.
"use client"

import * as React from "react"
import {
  flexRender,
  getCoreRowModel,
  getExpandedRowModel,
  getFacetedRowModel,
  getFacetedUniqueValues,
  getFilteredRowModel,
  getPaginationRowModel,
  getSortedRowModel,
  useReactTable,
  type Column,
  type ColumnDef,
  type FilterFnOption,
  type InitialTableState,
  type Row,
  type RowData,
  type Table as TanstackTable,
  type TableOptions,
} from "@tanstack/react-table"
import { ChevronDownIcon, ChevronRightIcon, ChevronsDownUpIcon, ChevronsUpDownIcon, MinusIcon } from "lucide-react"

import { Button } from "@/components/ui/button"
import { Checkbox } from "@/components/ui/checkbox"
import { Skeleton } from "@/components/ui/skeleton"
import { Table, TableBody, TableCaption, TableCell, TableFooter, TableHead, TableHeader, TableRow } from "@/components/ui/table"
import { cn } from "@/lib/utils"
import { DataTablePagination } from "@/components/ui/data-table-pagination"

declare module "@tanstack/react-table" {
  // TData and TValue must stay: the declaration merges with TanStack's own.
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
  interface ColumnMeta<TData extends RowData, TValue> {
    /** The column's name in the view menu and in screen-reader labels. Defaults to its id. */
    label?: string
    /** Right-align numbers and money; the header follows. */
    align?: "left" | "center" | "right"
    /** Extra classes for this column's header cell. */
    headerClassName?: string
    /** Extra classes for this column's body and footer cells. */
    cellClassName?: string
  }
}

type RowLabel = { singular: string; plural: string }

type DataTableProps<TData, TValue> = {
  columns: ColumnDef<TData, TValue>[]
  data: TData[]
  /** Rendered above the table with the table instance: search, filters, view options, bulk actions. */
  toolbar?: (table: TanstackTable<TData>) => React.ReactNode
  /** A caption for screen readers, e.g. "Invoices". */
  caption?: string
  /** A stable id per row (an invoice number), so selection and expansion survive sorting and edits. */
  getRowId?: (row: TData, index: number) => string
  /** Lets rows be selected; on by default when the columns include selectColumn(). A function decides per row. */
  enableRowSelection?: boolean | ((row: Row<TData>) => boolean)
  /** What an expanded row shows under itself; pair it with expandColumn(). */
  renderSubRow?: (row: Row<TData>) => React.ReactNode
  /** Which rows can expand. Defaults to every row when renderSubRow is set. */
  getRowCanExpand?: (row: Row<TData>) => boolean
  /** Starting sort, filters, page size, hidden and pinned columns, selection. */
  initialState?: InitialTableState
  /** Pages of rows with a footer to move between them. Off shows every row. */
  pagination?: boolean
  pageSizeOptions?: number[]
  /** What the pagination footer calls a row: "Showing 1–10 of 48 invoices". */
  rowLabel?: RowLabel
  /** Shows skeleton rows instead of data. */
  loading?: boolean
  /** Shown in place of rows when there are none (or none match the filters); a function gets the table. */
  empty?: React.ReactNode | ((table: TanstackTable<TData>) => React.ReactNode)
  /** Scroll the rows inside a box this tall; the header row sticks to its top. */
  maxHeight?: number | string
  /**
   * Go back to page 1 when the rows change. By default the table does so when the search, filters or
   * sort change, but stays on its page when the data does (an edit, a delete, a status change).
   */
  autoResetPageIndex?: boolean
  /** How the search box (table.setGlobalFilter) matches a row. Defaults to "contains" on text and number columns. */
  globalFilterFn?: FilterFnOption<TData>
  /** Anything else TanStack Table takes (meta, onRowSelectionChange, manual pagination...). */
  options?: Partial<TableOptions<TData>>
  className?: string
}

function DataTable<TData, TValue>({
  columns,
  data,
  toolbar,
  caption,
  getRowId,
  enableRowSelection,
  renderSubRow,
  getRowCanExpand,
  initialState,
  pagination = true,
  pageSizeOptions = [10, 20, 50],
  rowLabel,
  loading = false,
  empty,
  maxHeight,
  autoResetPageIndex,
  globalFilterFn = "auto",
  options,
  className,
}: DataTableProps<TData, TValue>) {
  "use no memo" // TanStack Table mutates its instance; keep the React Compiler from caching renders.

  // A render with new data skips TanStack's page reset, so editing a row on page 3 stays on page 3.
  const [seenData, setSeenData] = React.useState(data)
  const dataChanged = seenData !== data
  if (dataChanged) setSeenData(data)

  // eslint-disable-next-line react-hooks/incompatible-library -- opted out of the compiler above
  const table = useReactTable<TData>({
    data,
    columns,
    getRowId,
    enableRowSelection: enableRowSelection ?? columns.some((c) => c.id === "select"),
    getRowCanExpand: getRowCanExpand ?? (renderSubRow ? () => true : undefined),
    globalFilterFn,
    autoResetPageIndex: autoResetPageIndex ?? !dataChanged,
    initialState: {
      ...initialState,
      pagination: { pageIndex: 0, pageSize: pageSizeOptions[0] ?? 10, ...initialState?.pagination },
    },
    getCoreRowModel: getCoreRowModel(),
    getSortedRowModel: getSortedRowModel(),
    getFilteredRowModel: getFilteredRowModel(),
    getFacetedRowModel: getFacetedRowModel(),
    getFacetedUniqueValues: getFacetedUniqueValues(),
    getExpandedRowModel: getExpandedRowModel(),
    ...(pagination ? { getPaginationRowModel: getPaginationRowModel() } : {}),
    ...options,
  })

  // After a delete the current page can be past the end: step back to the last page.
  const { pageIndex } = table.getState().pagination
  const pageCount = table.getPageCount()
  React.useEffect(() => {
    if (pagination && pageIndex > 0 && pageIndex >= pageCount) table.setPageIndex(Math.max(pageCount - 1, 0))
  }, [pagination, pageIndex, pageCount, table])

  const sticky = maxHeight !== undefined
  const rows = table.getRowModel().rows
  const columnCount = table.getVisibleLeafColumns().length
  const footerGroups = table.getFooterGroups()
  const hasFooter = footerGroups.some((g) => g.headers.some((h) => h.column.columnDef.footer !== undefined))
  const skeletonRows = Math.min(table.getState().pagination.pageSize, 8)

  return (
    <div data-slot="data-table" className={cn("flex min-w-0 flex-col gap-3", className)}>
      {toolbar?.(table)}
      <div
        data-slot="data-table-frame"
        className={cn(
          "@container/data-table overflow-hidden rounded-lg border bg-(--data-table-bg,var(--background))",
          sticky && "*:data-[slot=table-container]:max-h-(--data-table-max-h) *:data-[slot=table-container]:overscroll-x-contain",
        )}
        style={sticky ? ({ "--data-table-max-h": typeof maxHeight === "number" ? `${maxHeight}px` : maxHeight } as React.CSSProperties) : undefined}
      >
        <Table aria-busy={loading || undefined}>
          {caption ? <TableCaption className="sr-only">{caption}</TableCaption> : null}
          <TableHeader className={cn(sticky && "[&_tr]:border-b-0")}>
            {table.getHeaderGroups().map((group) => (
              <TableRow key={group.id} className="hover:bg-transparent">
                {group.headers.map((header) => (
                  <TableHead
                    key={header.id}
                    colSpan={header.colSpan}
                    aria-sort={ariaSort(header.column)}
                    style={columnStyle(header.column)}
                    className={cn(
                      "px-3 text-muted-foreground first:pl-4 last:pr-4",
                      alignClass(header.column),
                      edgeClasses(header.column, sticky ? "sticky-head" : "head"),
                      header.column.columnDef.meta?.headerClassName,
                    )}
                  >
                    {header.isPlaceholder ? null : flexRender(header.column.columnDef.header, header.getContext())}
                  </TableHead>
                ))}
              </TableRow>
            ))}
          </TableHeader>
          <TableBody>
            {loading ? (
              Array.from({ length: skeletonRows }, (_, i) => (
                <TableRow key={i} className="hover:bg-transparent">
                  {table.getVisibleLeafColumns().map((column) => (
                    <TableCell key={column.id} style={columnStyle(column)} className={cn("h-12 px-3 first:pl-4 last:pr-4", edgeClasses(column, "cell"))}>
                      <Skeleton className={cn("h-4", column.id === "select" || column.id === "expand" ? "w-4" : "w-full max-w-32", column.columnDef.meta?.align === "right" && "ml-auto")} />
                    </TableCell>
                  ))}
                </TableRow>
              ))
            ) : rows.length ? (
              rows.map((row) => (
                <React.Fragment key={row.id}>
                  <TableRow data-state={row.getIsSelected() ? "selected" : undefined} data-expanded={row.getIsExpanded() || undefined}>
                    {row.getVisibleCells().map((cell) => (
                      <TableCell
                        key={cell.id}
                        style={columnStyle(cell.column)}
                        className={cn("h-12 px-3 first:pl-4 last:pr-4", alignClass(cell.column), edgeClasses(cell.column, "cell"), cell.column.columnDef.meta?.cellClassName)}
                      >
                        {flexRender(cell.column.columnDef.cell, cell.getContext())}
                      </TableCell>
                    ))}
                  </TableRow>
                  {renderSubRow && row.getIsExpanded() ? (
                    <TableRow data-slot="data-table-sub-row" className="bg-muted/30 hover:bg-muted/30">
                      <TableCell colSpan={columnCount} className="p-0 whitespace-normal">
                        {/* As wide as the visible frame and held at its left edge, so on a narrow screen the
                            details stay in view while the wide row above scrolls sideways. */}
                        <div className="sticky left-0 w-[100cqw]">{renderSubRow(row)}</div>
                      </TableCell>
                    </TableRow>
                  ) : null}
                </React.Fragment>
              ))
            ) : (
              <TableRow className="hover:bg-transparent">
                <TableCell colSpan={columnCount} className="h-40 px-4 text-center whitespace-normal text-muted-foreground">
                  {(typeof empty === "function" ? empty(table) : empty) ?? "No results."}
                </TableCell>
              </TableRow>
            )}
          </TableBody>
          {hasFooter && !loading && rows.length ? (
            <TableFooter>
              {footerGroups.slice(0, 1).map((group) => (
                <TableRow key={group.id} className="hover:bg-transparent">
                  {group.headers.map((header) => (
                    <TableCell
                      key={header.id}
                      colSpan={header.colSpan}
                      style={columnStyle(header.column)}
                      className={cn("h-11 px-3 first:pl-4 last:pr-4", alignClass(header.column), edgeClasses(header.column, "foot"), header.column.columnDef.meta?.cellClassName)}
                    >
                      {header.isPlaceholder ? null : flexRender(header.column.columnDef.footer, header.getContext())}
                    </TableCell>
                  ))}
                </TableRow>
              ))}
            </TableFooter>
          ) : null}
        </Table>
      </div>
      {pagination ? <DataTablePagination table={table} pageSizeOptions={pageSizeOptions} rowLabel={rowLabel} /> : null}
    </div>
  )
}

function ariaSort<TData>(column: Column<TData, unknown>): React.AriaAttributes["aria-sort"] {
  if (!column.getCanSort()) return undefined
  const sorted = column.getIsSorted()
  return sorted === "asc" ? "ascending" : sorted === "desc" ? "descending" : "none"
}

function alignClass<TData>(column: Column<TData, unknown>) {
  const align = column.columnDef.meta?.align
  return align === "right" ? "text-right" : align === "center" ? "text-center" : undefined
}

/**
 * Pinned columns get a fixed width (the column's `size`, 150px unless set) and their offset, so
 * several can stick side by side. Other columns size to their content.
 */
function columnStyle<TData>(column: Column<TData, unknown>): React.CSSProperties | undefined {
  const pinned = column.getIsPinned()
  if (!pinned) return undefined
  const size = column.getSize()
  return {
    width: size,
    minWidth: size,
    maxWidth: size,
    ...(pinned === "left" ? { left: column.getStart("left") } : { right: column.getAfter("right") }),
  }
}

/**
 * Sticky header cells and pinned columns need an opaque background (the frame's) and draw their
 * borders as inset shadows, because collapsed table borders do not travel with sticky cells.
 */
function edgeClasses<TData>(column: Column<TData, unknown>, part: "head" | "sticky-head" | "cell" | "foot") {
  const pinned = column.getIsPinned()
  const lastLeft = pinned === "left" && column.getIsLastColumn("left")
  const firstRight = pinned === "right" && column.getIsFirstColumn("right")
  const bg = "bg-(--data-table-bg,var(--background))"
  if (part === "sticky-head") {
    return cn(
      "sticky top-0",
      bg,
      pinned ? "z-30" : "z-20",
      lastLeft ? "shadow-[inset_-1px_-1px_0_var(--border)]" : firstRight ? "shadow-[inset_1px_-1px_0_var(--border)]" : "shadow-[inset_0_-1px_0_var(--border)]",
    )
  }
  if (!pinned) return undefined
  return cn(
    "sticky z-10",
    part === "foot" ? "bg-[color-mix(in_srgb,var(--muted)_50%,var(--data-table-bg,var(--background)))]" : bg,
    part === "cell" &&
      "[tr:hover>&]:bg-[color-mix(in_srgb,var(--muted)_50%,var(--data-table-bg,var(--background)))] [tr[data-state=selected]>&]:bg-muted",
    lastLeft && "shadow-[inset_-1px_0_0_var(--border)]",
    firstRight && "shadow-[inset_1px_0_0_var(--border)]",
  )
}

// ---------------------------------------------------------------------------------------------
// Column helpers: a checkbox column for selection and a chevron column for expandable rows.

/** The row last clicked in each table, and whether shift is down, for shift-click range selection. */
const selectionAnchors = new WeakMap<object, string>()
const shiftHeld = new WeakMap<object, boolean>()

/**
 * A checkbox column: the header checkbox selects every row on the page (and shows a dash when only
 * some are), each row's checkbox selects that row, and shift-click selects the rows in between.
 * (The dash fill is bg-primary! so it also beats the stock checkbox's dark-mode background.)
 */
function selectColumn<TData>({ label }: { label?: (row: Row<TData>) => string } = {}): ColumnDef<TData> {
  return {
    id: "select",
    size: 44,
    enableSorting: false,
    enableHiding: false,
    enableGlobalFilter: false,
    meta: { label: "Select", headerClassName: "w-11 pr-0", cellClassName: "w-11 pr-0" },
    header: ({ table }) => {
      const some = table.getIsSomePageRowsSelected()
      return (
        <span className="relative flex w-fit">
          <Checkbox
            checked={table.getIsAllPageRowsSelected()}
            indeterminate={some}
            onCheckedChange={(value) => table.toggleAllPageRowsSelected(value)}
            aria-label="Select all rows on this page"
            className="data-indeterminate:border-primary data-indeterminate:bg-primary! data-indeterminate:[&_[data-slot=checkbox-indicator]]:hidden"
          />
          {some ? <MinusIcon aria-hidden="true" className="pointer-events-none absolute inset-0 m-auto size-3 text-primary-foreground" /> : null}
        </span>
      )
    },
    cell: ({ row, table }) => (
      <Checkbox
        checked={row.getIsSelected()}
        disabled={!row.getCanSelect()}
        aria-label={`Select ${label ? label(row) : `row ${row.index + 1}`}`}
        // The change event comes from a hidden input, so read shift from the press itself.
        onPointerDown={(event) => shiftHeld.set(table, event.shiftKey)}
        onKeyDown={(event) => shiftHeld.set(table, event.shiftKey)}
        onCheckedChange={(value) => {
          const anchor = selectionAnchors.get(table)
          const shift = shiftHeld.get(table) ?? false
          selectionAnchors.set(table, row.id)
          shiftHeld.delete(table)
          if (!shift || !anchor || anchor === row.id) return row.toggleSelected(value)
          const pageRows = table.getRowModel().rows
          const a = pageRows.findIndex((r) => r.id === anchor)
          const b = pageRows.findIndex((r) => r.id === row.id)
          if (a === -1) return row.toggleSelected(value)
          const range = pageRows.slice(Math.min(a, b), Math.max(a, b) + 1).filter((r) => r.getCanSelect())
          table.setRowSelection((prev) => {
            const next = { ...prev }
            for (const r of range) {
              if (value) next[r.id] = true
              else delete next[r.id]
            }
            return next
          })
        }}
      />
    ),
  }
}

/**
 * A chevron column for rows that expand (DataTable's renderSubRow). The header button opens or
 * closes every row at once.
 */
function expandColumn<TData>({ label }: { label?: (row: Row<TData>) => string } = {}): ColumnDef<TData> {
  return {
    id: "expand",
    size: 48,
    enableSorting: false,
    enableHiding: false,
    enableGlobalFilter: false,
    meta: { label: "Details", headerClassName: "w-12 pr-0", cellClassName: "w-12 pr-0" },
    header: ({ table }) => {
      const all = table.getIsAllRowsExpanded()
      return (
        <Button
          variant="ghost"
          size="icon-sm"
          className="-ml-1.5 text-muted-foreground"
          onClick={() => table.toggleAllRowsExpanded(!all)}
          aria-label={all ? "Collapse all rows" : "Expand all rows"}
        >
          {all ? <ChevronsDownUpIcon /> : <ChevronsUpDownIcon />}
        </Button>
      )
    },
    cell: ({ row }) =>
      row.getCanExpand() ? (
        <Button
          variant="ghost"
          size="icon-sm"
          className="-ml-1.5"
          onClick={row.getToggleExpandedHandler()}
          aria-expanded={row.getIsExpanded()}
          aria-label={`${row.getIsExpanded() ? "Hide" : "Show"} details for ${label ? label(row) : `row ${row.index + 1}`}`}
        >
          {row.getIsExpanded() ? <ChevronDownIcon /> : <ChevronRightIcon />}
        </Button>
      ) : null,
  }
}

export { DataTable, expandColumn, selectColumn }
export type { DataTableProps }