# Data table

> A table whose rows travel: sort and every row glides to its rank, live data re-ranks itself, and selection turns the toolbar into its actions.

Most data tables redraw in a jump: sort and the rows are simply somewhere else, filter and they vanish, delete and they are gone. Here rows travel. Click a header and every row glides to its new rank, top first, while the rows that didn't move stay put and the arrow flips with a slight overshoot. When new data arrives the table re-sorts itself, so a customer whose seats go up glides past the ones above it while the figure rolls, and a new row fades in as the rows below make room. Search folds away what stops matching while the count rolls down. Tick a row and the toolbar becomes the selection, its count rolling; Shift ticks a range one box after another. Delete folds the rows out and holds "3 customers deleted · Undo" over a draining hairline before it commits. Rows open into their detail by measured height, a cell edits in place and saves with a drawn check (or shakes back), pages slide in from the side you went, and a narrow container gets one card per row.

## Install

```bash
npx shadcn@latest add @hairline/data-table
```

Needs React 19, Tailwind CSS v4 and shadcn/ui theme variables (any style, light and dark). Icons from lucide-react.

- npm: `lucide-react`
- Hairline UI components it needs (the CLI installs them too): `number-roll`, `text-morph`, `status-button`
- Docs and live preview: https://hairlineui.com/ui/data-table
- Registry item (source): https://hairlineui.com/r/data-table.json

## Usage

```tsx
import { DataTable, type DataTableColumn } from "@/components/data-table";

const columns: DataTableColumn<Customer>[] = [
  { key: "company", header: "Customer", sortable: true, searchable: true },
  { key: "mrr", header: "MRR", align: "right", sortable: true },
];

<DataTable columns={columns} rows={customers} noun={["customer", "customers"]} selectable />
```

## An admin table on your API

Rows are yours: pass them in, and update them from your server or a subscription; the table glides whatever changed. Actions get the selected rows; one with removes takes them out at once and only runs once the chance to undo is over.

```tsx
"use client";
import { useState } from "react";
import { DataTable, type DataTableColumn } from "@/components/data-table";
import { NumberRoll } from "@/components/number-roll";

type Customer = { id: string; company: string; email: string; seats: number; mrr: number; status: string };

const columns: DataTableColumn<Customer>[] = [
  { key: "company", header: "Customer", width: "minmax(0,1.6fr)", sortable: true, searchable: true, card: "title" },
  { key: "seats", header: "Seats", width: "80px", align: "right", sortable: true, editable: true, card: "subtitle" },
  { key: "mrr", header: "MRR", width: "100px", align: "right", sortable: true, card: "trailing",
    cell: (c) => <NumberRoll value={c.mrr} format={{ style: "currency", currency: "USD", maximumFractionDigits: 0 }} /> },
  { key: "status", header: "Status", width: "110px", sortable: true, card: "trailing" },
];

export function CustomersTable({ initial }: { initial: Customer[] }) {
  const [rows, setRows] = useState(initial);
  return (
    <DataTable
      columns={columns}
      rows={rows}
      noun={["customer", "customers"]}
      defaultSort={{ key: "mrr", dir: "desc" }}
      selectable
      actions={[
        { id: "export", label: "Export", progress: ["Exporting", "Exported"], run: (chosen) => exportCsv(chosen) },
        {
          id: "delete", label: "Delete", progress: ["Deleting", "Deleted"], tone: "destructive", removes: true,
          run: async (chosen) => {
            await fetch("/api/customers", { method: "DELETE", body: JSON.stringify(chosen.map((c) => c.id)) });
            setRows((list) => list.filter((c) => !chosen.includes(c)));
          },
        },
      ]}
      onEdit={async (row, key, value) => {
        const res = await fetch(`/api/customers/${row.id}`, { method: "PATCH", body: JSON.stringify({ [key]: Number(value) }) });
        if (!res.ok) throw new Error("Not saved");
        const saved: Customer = await res.json();
        setRows((list) => list.map((c) => (c.id === row.id ? saved : c)));
      }}
      renderDetail={(c) => <CustomerDetail customer={c} />}
    />
  );
}
```

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `columns` | `DataTableColumn<T>[]` | – | { key, header, value?, cell?, align?, width?, sortable?, searchable?, editable?, card? }. value is what sorts, searches and edits (defaults to row[key]); cell is what shows. |
| `rows` | `T[]` | – | Your rows. Change them any time (new data, a subscription) and the table glides whatever moved. |
| `getRowId` | `(row: T) => string` | `row.id` | A stable id per row: it's what lets a row glide instead of jumping. |
| `noun` | `[string, string]` | `["row", "rows"]` | What a row is called in the count, the toolbar and announcements. |
| `sort / defaultSort / onSortChange` | `{ key, dir } \| null` | – | Controlled or starting sort. |
| `searchable` | `boolean` | `true` | Shows the search field; it looks in columns marked searchable, or all of them. |
| `selectable` | `boolean` | `false` | A drawn checkbox per row and in the header; Shift-click selects a range. |
| `selected / defaultSelected / onSelectedChange` | `string[]` | – | Selected row ids, controlled or not. |
| `actions` | `DataTableAction<T>[]` | – | { id, label, progress?, tone?, removes?, run }. Shown in the toolbar while rows are selected; run gets the selected rows. A removing action takes them out at once and runs after undoFor. |
| `undoFor` | `number` | `5000` | How long Undo is offered after a removing action, ms. |
| `onEdit` | `(row, key, value) => Promise<void>` | – | Saves an edited cell (columns with editable). Throw to put the old value back. |
| `renderDetail` | `(row: T) => ReactNode` | – | Rows open into this, with a chevron at the end of each. |
| `pageSize` | `number` | `10` | Rows per page. |
| `label` | `string` | `"Table"` | Accessible name of the grid. |

## States

- **sort**: A header click sorts (numbers and dates descending first, text ascending) and every row that changed rank glides there in 480ms, 16ms after the row above it (up to 200ms). The arrow turns with a slight overshoot.
- **live**: Whenever rows change, each row glides from where it was in the layout to where it is, as an added offset, so a change landing mid-glide never restarts one. New rows fade in through a blur 90ms later, as the rows below glide clear.
- **search**: Rows that stop matching leave a copy that folds out where it stood (280ms) while the rest close up; the count rolls and the list's height eases to fit. With nothing left, "No customers match …" takes the place of the rows.
- **select**: A tick draws itself; the toolbar's search slides up and out as "1 selected" and the actions slide in, its tint shifting to the primary. Shift-click ticks every row between, 28ms apart, starting from the last one you clicked.
- **remove**: A removing action folds the rows out at once and the toolbar reads "3 customers deleted · Undo" over a hairline that drains in 5s (paused while you hover it). Undo brings them back in place; when it runs out, the action runs, and if it throws they come back with a note.
- **edit**: Double-click an editable cell, or Enter on it, and it becomes a field in place. Enter saves: the new value morphs in with a spinner that turns into a drawn check; if onEdit throws the cell shakes and the old value returns.
- **detail**: The chevron (or Enter on the row) opens the row into its detail by grid rows 0fr → 1fr in 420ms; the chevron turns with a slight overshoot and the content fades in 120ms later.
- **pages**: The next page slides in 28px from the side you went toward through a blur; "9–16 of 24" rolls.

## What it handles

Most data tables redraw in a jump: sort and the rows are somewhere else, delete and they are gone. This one keeps people oriented as the data changes, and covers sorting, search, selection, editing, detail rows and paging.

- **Sort and search**: Click a header and every row glides to its rank. Search folds away what stops matching while the count rolls.
- **Live data**: When your rows change, the table re-sorts itself and rows glide past each other instead of jumping.
- **Bulk actions with Undo**: Tick rows (Shift for a range) and the toolbar becomes their actions. A delete holds an Undo for five seconds before it runs.
- **Edit in place**: Double-click a cell to edit it. A save draws a check, and a failure puts the old value back.
- **Detail rows and pages**: A row opens into its detail by measured height, and pages slide in from the side you went.
- **Cards on narrow screens**: Below 34rem of container width each row becomes a card.

## Replaces

column sorting, search, row selection, a bulk-action bar, undoable delete, pagination

## Where it fits

- **Admin panels**: Customers, orders and users that people sort and search all day.
- **Live dashboards**: Ranked tables that update as new data arrives.
- **Bulk-action screens**: Select rows and archive or delete them, with a chance to undo.
- **Responsive lists**: A table that turns into one card per row on a narrow container.

## Notes

- The table is a grid: arrow keys move between rows and into cells, Home and End jump, Space selects the row (Shift for a range), Enter edits an editable cell or opens the row, Page Up and Page Down change page. Headers carry aria-sort.
- Sorting, the number of matches, selection, removals, Undo, saves and failures are announced in a status region; the drawing checkboxes, rolling counts and sliding toolbar are what you see.
- Rows move with transforms only. Their places are layout offsets kept after every render and whenever the list changes size, so a glide always starts from where the row really was, even mid-glide.
- Below 34rem of container width each row becomes a card: columns marked card: "title", "subtitle" and "trailing" fill it, and sorting moves to a native select in the toolbar.
- Reduced motion changes rows, pages and the toolbar in place; colours, ticks and the announcements still say what happened.

License: MIT (https://hairlineui.com/license#components)
