Data display
Table
Base table parts — sortable columns, selectable rows, header 40 on subtle, rows 48, horizontal rules only.
react-aria-componentslucide-react
Sortable and selectable
| Company | Owner | Stage | Value |
|---|---|---|---|
| Ledgerly | Jordan Blake | Negotiation | $48,000 |
| Fathom Data | Priya Raman | Proposal | $36,500 |
| Northpeak | Leah Moss | Discovery | $22,000 |
| Plinth | Samuel Ortiz | Discovery | $12,000 |
"use client";
import { useMemo, useState } from "react";
import type { SortDescriptor } from "react-aria-components";
import { Badge } from "@/components/ui/badge";
import { Cell, Column, Row, Table, TableBody, TableContainer, TableHeader } from "@/components/ui/table";
const rows = [
{ id: "1", company: "Ledgerly", owner: "Jordan Blake", stage: "Negotiation", value: 48000 },
{ id: "2", company: "Northpeak", owner: "Leah Moss", stage: "Discovery", value: 22000 },
{ id: "3", company: "Fathom Data", owner: "Priya Raman", stage: "Proposal", value: 36500 },
{ id: "4", company: "Plinth", owner: "Samuel Ortiz", stage: "Discovery", value: 12000 },
];
export default function Example() {
const [sort, setSort] = useState<SortDescriptor>({ column: "value", direction: "descending" });
const sorted = useMemo(() => {
const k = sort.column as keyof (typeof rows)[number];
const dir = sort.direction === "ascending" ? 1 : -1;
return [...rows].sort((a, b) => (a[k] > b[k] ? 1 : -1) * dir);
}, [sort]);
return (
<TableContainer className="w-full">
<Table aria-label="Deals" selectionMode="multiple" sortDescriptor={sort} onSortChange={setSort} className="min-w-[520px]">
<TableHeader>
<Column id="company" isRowHeader allowsSorting>Company</Column>
<Column id="owner" allowsSorting>Owner</Column>
<Column id="stage">Stage</Column>
<Column id="value" allowsSorting className="text-right">Value</Column>
</TableHeader>
<TableBody items={sorted}>
{(r) => (
<Row id={r.id}>
<Cell className="font-medium">{r.company}</Cell>
<Cell className="text-fg-secondary">{r.owner}</Cell>
<Cell>
<Badge size="sm" tone={r.stage === "Negotiation" ? "success" : r.stage === "Proposal" ? "accent" : "neutral"}>{r.stage}</Badge>
</Cell>
<Cell className="text-right tabular">${r.value.toLocaleString("en-US")}</Cell>
</Row>
)}
</TableBody>
</Table>
</TableContainer>
);
}Installation
Components are copied into your project — no runtime package.
Install the following dependencies:
terminalnpm install react-aria-components lucide-reactCreate a
table.tsxfile and paste the following code into it.src/components/ui/table.tsx"use client"; import { ArrowDown, ArrowUp, ChevronsUpDown } from "lucide-react"; import type { ReactNode } from "react"; import { Cell as AriaCell, type CellProps, Column as AriaColumn, type ColumnProps, Row as AriaRow, type RowProps, Table as AriaTable, TableBody as AriaTableBody, type TableBodyProps, TableHeader as AriaTableHeader, type TableHeaderProps, type TableProps, } from "react-aria-components"; import { cx } from "@/utils/cx"; /** * Base table, in the same language as every Vantu table: a rounded bg-subtle * header pill, 12px row padding and hairline separators between rows. * Wrap in <TableContainer> for horizontal scroll (and an optional panel). */ export function TableContainer({ className, children }: { className?: string; children: ReactNode }) { return ( <div className={cx("overflow-hidden", className)}> {/* `relative` contains React Aria's absolutely positioned hidden inputs. */} <div className="relative overflow-x-auto">{children}</div> </div> ); } export function Table({ className, ...props }: Omit<TableProps, "className"> & { className?: string }) { return <AriaTable {...props} className={cx("w-full border-separate border-spacing-0 text-base", className)} />; } export function TableHeader<T extends object>({ className, ...props }: Omit<TableHeaderProps<T>, "className"> & { className?: string }) { return <AriaTableHeader {...props} className={cx("bg-subtle", className)} />; } export function Column({ className, children, ...props }: Omit<ColumnProps, "className" | "children"> & { className?: string; children: ReactNode }) { return ( <AriaColumn {...props} className={cx( "h-10 px-3 text-left text-sm font-normal text-fg-muted outline-none first:rounded-l-[10px] last:rounded-r-[10px] data-focus-visible:shadow-[inset_0_0_0_2px_var(--color-accent)]", props.allowsSorting && "cursor-default data-hovered:text-fg", className, )} > {({ allowsSorting, sortDirection }) => ( <span className="inline-flex items-center gap-1"> {children} {allowsSorting && (sortDirection === "ascending" ? ( <ArrowUp className="size-3 text-fg" aria-hidden /> ) : sortDirection === "descending" ? ( <ArrowDown className="size-3 text-fg" aria-hidden /> ) : ( <ChevronsUpDown className="size-3 text-fg-disabled" aria-hidden /> ))} </span> )} </AriaColumn> ); } export function TableBody<T extends object>({ className, ...props }: Omit<TableBodyProps<T>, "className"> & { className?: string }) { return <AriaTableBody {...props} className={className} />; } export function Row<T extends object>({ className, ...props }: Omit<RowProps<T>, "className"> & { className?: string }) { return ( <AriaRow {...props} className={cx( "outline-none data-hovered:bg-subtle data-selected:bg-accent-soft/60 data-focus-visible:shadow-[inset_0_0_0_2px_var(--color-accent)] [&>td]:border-b [&>td]:border-line-subtle [&:last-child>td]:border-0", className, )} /> ); } export function Cell({ className, ...props }: Omit<CellProps, "className"> & { className?: string }) { return <AriaCell {...props} className={cx("px-3 py-3 text-fg outline-none data-focus-visible:shadow-[inset_0_0_0_2px_var(--color-accent)]", className)} />; }Add the theme tokens and the
cxhelper from Installation if you haven’t yet, then update the import paths to match your project setup.