# Table

A table for reading: rows of facts under named columns, one line per row and a quiet second line where a cell needs one.

Live variants: https://design.arcadia.is/components/table

```tsx
import { Table } from "@arcadia/ui";
```

## In context

Read what an agent found on a sign-up page.

Elements on a sign-up page

```tsx
import { Table } from "@arcadia/ui";

<Table
  aria-label="Elements on the page"
  columns={[
    { key: "name", label: "Name", grow: true, rowHeader: true },
    { key: "role", label: "Role" },
    { key: "state", label: "State" },
    { key: "ref", label: "Ref", tone: "quiet" },
  ]}
  rows={[
    { key: "e1", cells: { name: "Sign up", role: "heading", ref: "@e1" } },
    { key: "e2", cells: { name: "Email", role: "textbox", state: "focused", ref: "@e2" } },
    { key: "e3", cells: { name: "Password", role: "textbox", state: "required", ref: "@e3" } },
    { key: "e4", cells: { name: <>Stay signed in</>, role: "checkbox", state: "checked", ref: "@e4" } },
    { key: "e5", cells: { name: "Create", role: "button", ref: "@e5" } },
  ]}
/>
```

## When to use

Compare the same facts across records, or total a numeric column.

## When not to use

Use `Facts` for one record and `ListRow` for rows of actions.

## Usage

```tsx
import { Table } from "@arcadia/ui";

<Table
  aria-label="Files"
  columns={[{ key: "name", label: "Name" }, { key: "size", label: "Size", align: "end" }]}
  rows={[{ key: "readme", cells: { name: "README.md", size: "2.4 kB" } }]}
/>
```

Give every column and row a `key`. Map `cells` by column key.

Cells wrap to fit their column. Use `nowrap` when the table should keep each value on one line.

Use `tone="quiet"` for secondary values such as refs or IDs.

## Examples

### A heading that stays in view

Use `stickyHeader` in a scroll container with no top fade. Sort the rows before passing them to the table.

```tsx
import { useState } from "react";
import { ScrollArea, Switch, Table } from "@arcadia/ui";

function Sticky() {
  const [largestFirst, setLargestFirst] = useState(false);
  const folders = ["App", "Docs", "Images", "Scripts", "Tests", "Tools"].map((folder, i) => ({
    key: folder, cells: { folder, files: 4 + i * 2 },
  }));
  return (
    <div {...stylex.props(s.frame)}>
      <Switch label="Most files first" checked={largestFirst} onCheckedChange={setLargestFirst} />
      <ScrollArea label="Project folders" scrollbar="shown" rootStyle={s.scroll}>
        <Table
          caption="Project folders"
          stickyHeader
          columns={[{ key: "folder", label: "Folder", grow: true, rowHeader: true }, { key: "files", label: "Files", align: "end" }]}
          rows={largestFirst ? folders.reverse() : folders}
        />
      </ScrollArea>
    </div>
  );
}
```

### Merged cells

Key `spans` by the starting column. Omit the cells it covers.

```tsx
import { Table } from "@arcadia/ui";

<Table
  caption="Run schedule"
  headerless
  columns={[{ key: "day", rowHeader: true }, { key: "morning" }, { key: "afternoon" }]}
  rows={[
    { key: "mon", cells: { day: "Monday", morning: "Build and test" }, spans: { morning: { colSpan: 2 } } },
    { key: "tue", cells: { day: "Tuesday", morning: "Review", afternoon: "Publish" }, spans: { day: { rowSpan: 2 } } },
    { key: "next", cells: { morning: "Check", afternoon: "Close" } },
  ]}
/>
```

### An empty column

Turn off `hideEmpty` to keep columns before their values arrive.

```tsx
import { useState } from "react";
import { Switch, Table } from "@arcadia/ui";

function Empty() {
  const [hide, setHide] = useState(true);
  return (
    <div {...stylex.props(s.stack)}>
      <Switch label="Leave out empty columns" checked={hide} onCheckedChange={setHide} />
      <Table
        aria-label="Buttons and links"
        hideEmpty={hide}
        columns={[
          { key: "name", label: "Name" },
          { key: "role", label: "Role" },
          { key: "value", label: "Value" },
          { key: "ref", label: "Ref", tone: "quiet" },
        ]}
        rows={[
          { key: "e5", cells: { name: "Create account", role: "button", ref: "@e5" } },
          { key: "e6", cells: { name: "Sign in instead", role: "link", ref: "@e6" } },
          { key: "e7", cells: { name: "Terms", role: "link", ref: "@e7" } },
        ]}
      />
    </div>
  );
}
```

### A row that opens

Use `open` to make the name cell link to the record.

```tsx
import { Table } from "@arcadia/ui";

<Table
  aria-label="Boards"
  columns={[
    { key: "name", label: "Board", grow: true },
    { key: "cards", label: "Cards", align: "end" },
    { key: "done", label: "Done", align: "end" },
  ]}
  rows={[
    { key: "app", cells: { name: "App", cards: "38", done: "24" }, open: { href: "/app/app-frame" } },
    { key: "api", cells: { name: "Kit", cards: "52", done: "41" }, open: { href: "/components/button" } },
    { key: "docs", cells: { name: "Docs", cards: "12", done: "12" }, open: { href: "/" } },
  ]}
/>
```

### Numbers and totals

Align figures to the end and put totals in the footer.

```tsx
import { Table } from "@arcadia/ui";

<Table
  aria-label="Requests"
  columns={[
    { key: "file", label: "File", grow: true },
    { key: "type", label: "Type" },
    { key: "size", label: "Size", align: "end" },
    { key: "time", label: "Time", align: "end" },
  ]}
  rows={[
    { key: "doc", cells: { file: "signup", type: "document", size: "14.2 kB", time: "182 ms" } },
    { key: "js", cells: { file: "app.js", type: "script", size: "214 kB", time: "1,312 ms" } },
    { key: "css", cells: { file: "app.css", type: "stylesheet", size: "38.6 kB", time: "96 ms" } },
    { key: "api", cells: { file: "session", type: "fetch", size: "1.2 kB", time: "84 ms" } },
  ]}
  footer={{ file: "4 requests", size: "268 kB", time: "1,674 ms" }}
/>
```

### A second line

Use `notes` for a quiet second line under a value.

```tsx
import { ActorName, Table } from "@arcadia/ui";

<Table
  aria-label="People and their agents"
  columns={[{ key: "who", label: "Name" }, { key: "role", label: "Role" }, { key: "acted", label: "Last seen", align: "end" }]}
  rows={[
    { key: "ada", cells: { who: <ActorName actor={ada} />, role: "Owner", acted: "Now" } },
    { key: "scout", cells: { who: <ActorName actor={scout} />, role: "Agent", acted: "2 min ago" } },
    {
      key: "atlas", cells: { who: <ActorName actor={atlas} />, role: "Agent", acted: "3 h ago" },
      notes: { who: "Offline since 14:02" },
    },
  ]}
/>
```

### Row headers and longer values

Use `rowHeader` to name each row. Longer values wrap inside their column.

```tsx
import { Table } from "@arcadia/ui";

<Table
  aria-label="What is kept and for how long"
  stack
  columns={[
    { key: "what", label: "What", rowHeader: true },
    { key: "kept", label: "Kept for" },
    { key: "why", label: "Why" },
  ]}
  rows={[
    { key: "messages", cells: { what: "Messages", kept: "Until you delete them", why: <>Your conversations, so you can read them again on any device you sign in on.</> } },
    { key: "logs", cells: { what: "Sign-in records", kept: "90 days", why: <>The time, device and place of each sign-in, so you can see who used your account and end a session you do not know.</> } },
    { key: "files", cells: { what: "Uploads", kept: "Until you delete them", why: <>What you attach to a message.</> } },
  ]}
/>
```

### Groups

Give each set of rows its own heading with `groups`.

```tsx
import { Table } from "@arcadia/ui";

<Table
  aria-label="Milestones by roadmap"
  columns={[{ key: "name", label: "Name" }, { key: "due", label: "Due" }, { key: "state", label: "State" }]}
  rows={[]}
  groups={[
    { key: "compiler", label: "Compiler", rows: [{ key: "parser", cells: { name: "Parser", due: "Dec 11", state: "Begun" } }, { key: "emitter", cells: { name: "Emitter", due: "Dec 4", state: "Not begun" } }] },
    { key: "billing", label: "Billing", rows: [{ key: "invoices", cells: { name: "Invoices", due: "Dec 18", state: "Not begun" } }] },
  ]}
/>
```

### Short of room

Drag the slider to see columns share the available width.

```tsx
import { useState } from "react";
import { Slider, Table } from "@arcadia/ui";

function Narrow() {
  const [w, setW] = useState(100);
  return (
    <div {...stylex.props(s.frame)}>
      <div style={{ width: `${w}%` }}>
        <Table
          aria-label="Fields on the page"
          columns={[
            { key: "name", label: "Name" },
            { key: "role", label: "Role" },
            { key: "value", label: "Value" },
            { key: "ref", label: "Ref", tone: "quiet" },
          ]}
          rows={[
            { key: "e2", cells: { name: "Email address for receipts", role: "textbox", value: "ada.lovelace@analytical-engines.example", ref: "@e2" } },
            { key: "e4", cells: { name: "Keep me signed in on this device", role: "checkbox", value: "on", ref: "@e4" } },
            { key: "e5", cells: { name: "Create account", role: "button", ref: "@e5" } },
          ]}
        />
      </div>
      <Slider label="Room for the table" min={30} value={w} onValueChange={setW} valueText={(value) => `${value}%`} rootStyle={cmp.scrubSlider} />
    </div>
  );
}
```

### A narrow pane

`stack` puts each value beneath its label in a narrow pane and uses the row heading as the title.

Stacked in a 420px pane

```tsx
import { Table } from "@arcadia/ui";

<div {...stylex.props(s.narrow)}>
  <Table
    stack
    aria-label="Elements on the page"
    columns={[
      { key: "name", label: "Name" },
      { key: "role", label: "Role" },
      { key: "ref", label: "Ref", tone: "quiet" },
    ]}
    rows={[
      { key: "e2", cells: { name: "Email address for receipts", role: "textbox", ref: "@e2" } },
      { key: "e4", cells: { name: "Keep me signed in on this device", role: "checkbox", ref: "@e4" } },
      { key: "e5", cells: { name: "Create account", role: "button", ref: "@e5" } },
    ]}
  />
</div>
```

### A grow column short of room

A `grow` column takes spare space without shrinking to nothing.

```tsx
import { Link, Table } from "@arcadia/ui";

<Table
  aria-label="Requests with a note"
  columns={[
    { key: "file", label: "File", rowHeader: true },
    { key: "note", label: "Note", grow: true },
    { key: "size", label: "Size", align: "end" },
  ]}
  rows={[
    { key: "doc", cells: { file: "signup", size: "14.2 kB", note: "Loaded before the first paint" } },
    { key: "js", cells: { file: "app.js", size: "214 kB", note: "Cached for a year, checked on every load" } },
    { key: "css", cells: { file: "ui.css", size: "38.6 kB", note: <>See <code>ui.css</code> for <strong {...stylex.props(s.emphasis)}>base</strong> <Link href="/foundations/typography">styles</Link>.</> } },
  ]}
/>
```

## Customising

Set cell padding, row height and header paint through `rootStyle`.

- `--table-px` (`Ref<Space>`, default `var(--space-3)`)
- `--table-row-h` (`Ref<Control>`, default `var(--control-lg)`)
- `--table-head-bg` (`Paint`, default `var(--bg-card)`)

## Accessibility

The table is the platform's: `table`, `thead`, `tbody` and `tfoot`, `tr`, a `th` scoped to its column, a `th` scoped to its row in a `rowHeader` column, and `td`, laid out by the kit's grid, each restating its role, so a screen reader counts its rows and columns and reads each cell with its column's name, and a copy pastes into a sheet as rows and columns. Name it with `aria-label` or `aria-labelledby`, as every example here does, or with `caption` where the heading above it already says the name: the caption is heard and not shown. A column `hideEmpty` leaves out is gone from the reading too, and wrapping a value does not change how it is read.

The table reads rows in the order you pass them. It does not manage sorting, selection or arrow-key movement between cells. A link or a button you put in a cell takes Tab like any other. A row with `open` puts one link in its name cell: that link is the row's Tab stop, its Enter and its modified click, while a pointer anywhere else on the row follows the same link. A button or another link in the row keeps its own press. A quiet column differs from the rest by its ink alone, so it should never be the only place a fact is said. A cell's second line is read in the same cell, after its value.

## API reference

### Table

- `columns` (`readonly TableColumn[]`, required)
- `rows` (`readonly TableRow[]`, required)
- `groups` (`readonly TableGroup[]`)
- `footer` (`Readonly<Record<string, ReactNode>>`)
- `hideEmpty` (`boolean`, default `true`)
- `headerless` (`boolean`, default `false`)
- `stickyHeader` (`boolean`, default `false`)
- `stack` (`boolean`)
- `nowrap` (`boolean`, default `false`)
- `caption` (`string`)
- `rootStyle` (`RootStyle<TableKnobs>`)
- `aria-label` (`string`): Names the table for a screen reader.

Rest props go to `<table>` props.
