# Table

> Renders semantic table markup with Table, Thead, Tbody, Tfoot, Tr, Th, Td, and Caption. The size prop cascades from the table to every cell, and a cell or row can override it.

Source: https://vaneui.com/docs/layout-components/table

Import: `import { Table, Thead, Tbody, Tfoot, Tr, Th, Td, Caption, Badge, Col, Stack, Text, ThemeProvider } from "@vaneui/ui"`

Use Table for data that readers compare across rows and down columns, such as a price list, a schedule or an order summary. To place cards or tiles in columns, use [Grid](/docs/layout-components/grid); for a single run of items, one per line, use [List](/docs/typography-components/list).

## Basic usage

Compose a table from `Table`, `Thead`, `Tbody`, `Tr`, `Th`, and `Td`. Header cells (`Th`) render semibold with muted text, and every cell gets a bottom row rule. `Table` defaults to `sm`, `wFull`, `noBorder`, and `fontSans`. Add `scope="col"` to header cells so assistive technology associates each column with its data.

```tsx demo
<Table>
  <Thead>
    <Tr>
      <Th scope="col">Service</Th>
      <Th scope="col">Region</Th>
      <Th scope="col" textRight>Latency</Th>
    </Tr>
  </Thead>
  <Tbody>
    <Tr>
      <Td>api-gateway</Td>
      <Td>europe-west</Td>
      <Td textRight>42ms</Td>
    </Tr>
    <Tr>
      <Td>auth</Td>
      <Td>us-east</Td>
      <Td textRight>88ms</Td>
    </Tr>
  </Tbody>
</Table>
```

## Sizes

The size prop scales both text and cell padding, and it cascades from the table to every cell. Set it once on `Table` to size the whole table: `xs`, `sm` (default), `md`, `lg`, `xl`. The default has 14px text.

```tsx demo
<Stack>
  <Table xs>
    <Thead><Tr><Th scope="col">Size</Th><Th scope="col" textRight>Value</Th></Tr></Thead>
    <Tbody><Tr><Td>xs table</Td><Td textRight>tightest</Td></Tr></Tbody>
  </Table>
  <Table>
    <Thead><Tr><Th scope="col">Size</Th><Th scope="col" textRight>Value</Th></Tr></Thead>
    <Tbody><Tr><Td>sm table</Td><Td textRight>default</Td></Tr></Tbody>
  </Table>
  <Table md>
    <Thead><Tr><Th scope="col">Size</Th><Th scope="col" textRight>Value</Th></Tr></Thead>
    <Tbody><Tr><Td>md table</Td><Td textRight>roomy</Td></Tr></Tbody>
  </Table>
  <Table lg>
    <Thead><Tr><Th scope="col">Size</Th><Th scope="col" textRight>Value</Th></Tr></Thead>
    <Tbody><Tr><Td>lg table</Td><Td textRight>spacious</Td></Tr></Tbody>
  </Table>
  <Table xl>
    <Thead><Tr><Th scope="col">Size</Th><Th scope="col" textRight>Value</Th></Tr></Thead>
    <Tbody><Tr><Td>xl table</Td><Td textRight>widest</Td></Tr></Tbody>
  </Table>
</Stack>
```

## Appearances and variants

An appearance on `Table` cascades to its rows and cells. `outline` (the default) colours the borders. `filled` fills the surface and draws the row lines in a translucent divider colour that reads on any fill. A cell with its own appearance, such as `<Td danger>`, keeps it. This needs `@vaneui/ui` 1.6.0 or later: before that, an appearance or variant on the Table did not reach the cells. The row lines of a filled Table need 1.6.2. An appearance without a variant on a `Tr`, `Thead`, `Tbody` or `Tfoot`, such as `<Tr danger>`, and a filled cell, such as `<Td success filled>`, need 1.7.2.

```tsx demo
<Col>
  <Table success>
    <Thead><Tr><Th scope="col">Check</Th><Th scope="col" textRight>Result</Th></Tr></Thead>
    <Tbody><Tr><Td>outline success</Td><Td textRight>borders take the colour</Td></Tr></Tbody>
  </Table>
  <Table filled secondary>
    <Thead><Tr><Th scope="col">Check</Th><Th scope="col" textRight>Result</Th></Tr></Thead>
    <Tbody>
      <Tr><Td>filled secondary</Td><Td textRight>surface takes the colour</Td></Tr>
      <Tr><Td>row lines</Td><Td textRight>translucent on the fill</Td></Tr>
    </Tbody>
  </Table>
</Col>
```

## Striped and hoverable rows

`striped` tints every other body row, which helps the eye follow a wide row across the table. `hoverable` tints the row under the pointer. Both tints are a mix of the row's own text color, so they read on any appearance, in both color schemes, and a row or cell with its own fill keeps it. These props need `@vaneui/ui` 1.8.0 or later.

```tsx demo
<Table striped hoverable>
  <Thead><Tr><Th scope="col">Region</Th><Th scope="col" textRight>Latency</Th></Tr></Thead>
  <Tbody>
    <Tr><Td>Europe</Td><Td textRight>24ms</Td></Tr>
    <Tr><Td>North America</Td><Td textRight>61ms</Td></Tr>
    <Tr><Td>Asia Pacific</Td><Td textRight>142ms</Td></Tr>
    <Tr><Td>South America</Td><Td textRight>118ms</Td></Tr>
  </Tbody>
</Table>
```

## Per-cell and per-row size

A size on a `Th`, `Td`, or `Tr` overrides the table for that cell or row. Here an `xs` table keeps one `xl` cell and one `lg` row. An explicit `md` cell also holds `md` against a differently sized table.

```tsx demo
<Table xs>
  <Thead><Tr><Th scope="col">Metric</Th><Th scope="col" xl textRight>xl cell</Th></Tr></Thead>
  <Tbody>
    <Tr><Td>tiny row</Td><Td xl textRight>large</Td></Tr>
    <Tr lg><Td>lg row</Td><Td textRight>lg row</Td></Tr>
  </Tbody>
</Table>
```

## Column alignment

Set `textLeft` (default), `textCenter`, or `textRight` per cell. Right-align numeric columns so the digits line up.

```tsx demo
<Table>
  <Thead>
    <Tr>
      <Th scope="col">Plan</Th>
      <Th scope="col" textCenter>Seats</Th>
      <Th scope="col" textRight>Price</Th>
    </Tr>
  </Thead>
  <Tbody>
    <Tr><Td>Starter</Td><Td textCenter>3</Td><Td textRight>$0</Td></Tr>
    <Tr><Td>Team</Td><Td textCenter>25</Td><Td textRight>$99</Td></Tr>
    <Tr><Td>Business</Td><Td textCenter>100</Td><Td textRight>$499</Td></Tr>
  </Tbody>
</Table>
```

A `Text` inside a right-aligned cell is its own fit-width box, so give it `wFull textRight` to line up with the column.

```tsx demo
<Table>
  <Tbody>
    <Tr><Td>Trail Runner 2</Td><Td textRight><Text wFull textRight>$89.00</Text></Td></Tr>
    <Tr><Td>Merino socks (3 pairs)</Td><Td textRight><Text wFull textRight>$48.00</Text></Td></Tr>
  </Tbody>
</Table>
```

Text in a cell wraps between words, never inside one, so a name or a price stays whole in a narrow column.

## Footer row

Add a `Tfoot` for totals or summary rows. Header cells (`Th`) carry the semibold treatment, so they read well for a total.

```tsx demo
<Table>
  <Thead>
    <Tr><Th scope="col">Item</Th><Th scope="col" textRight>Amount</Th></Tr>
  </Thead>
  <Tbody>
    <Tr><Td>Compute</Td><Td textRight>$120</Td></Tr>
    <Tr><Td>Storage</Td><Td textRight>$30</Td></Tr>
  </Tbody>
  <Tfoot>
    <Tr><Th scope="row">Total</Th><Th textRight>$150</Th></Tr>
  </Tfoot>
</Table>
```

## Caption

`Caption` renders a `<caption>` element that titles the table for screen readers. Its text follows the table size and accepts its own size to override.

```tsx demo
<Table>
  <Caption>Monthly active users by region</Caption>
  <Thead>
    <Tr><Th scope="col">Region</Th><Th scope="col" textRight>Users</Th></Tr>
  </Thead>
  <Tbody>
    <Tr><Td>Europe</Td><Td textRight>18,400</Td></Tr>
    <Tr><Td>North America</Td><Td textRight>22,100</Td></Tr>
  </Tbody>
</Table>
```

## Outer border

`Table` has no border by default. Add `border` for an appearance-colored outer border. The table uses collapsed borders, so the corners stay square and there is no shape prop.

```tsx demo
<Table border>
  <Thead><Tr><Th scope="col">Name</Th><Th scope="col">Role</Th></Tr></Thead>
  <Tbody>
    <Tr><Td>Ada</Td><Td>Engineer</Td></Tr>
    <Tr><Td>Grace</Td><Td>Architect</Td></Tr>
  </Tbody>
</Table>
```

## Components in cells

Cells accept any VaneUI component. Drop a `Badge` into a status column.

```tsx demo
<Table>
  <Thead>
    <Tr><Th scope="col">Service</Th><Th scope="col">Region</Th><Th scope="col">Status</Th></Tr>
  </Thead>
  <Tbody>
    <Tr>
      <Td>api-gateway</Td>
      <Td>europe-west</Td>
      <Td><Badge success filled>Healthy</Badge></Td>
    </Tr>
    <Tr>
      <Td>worker</Td>
      <Td>us-east</Td>
      <Td><Badge warning filled>Degraded</Badge></Td>
    </Tr>
    <Tr>
      <Td>scheduler</Td>
      <Td>us-east</Td>
      <Td><Badge danger filled>Down</Badge></Td>
    </Tr>
  </Tbody>
</Table>
```

## Font family

Cells render in the sans font (`fontSans`, the default), whatever font the page around them uses. Pass `fontMono` for machine output such as ids and hashes, or `fontSerif` / `fontHeading`.

```tsx demo
<Table fontMono>
  <Thead><Tr><Th scope="col">Commit</Th><Th scope="col">Message</Th></Tr></Thead>
  <Tbody>
    <Tr><Td>6aba224</Td><Td>fix: make components consistent</Td></Tr>
    <Tr><Td>899e2fd</Td><Td>chore: release 1.4.3</Td></Tr>
  </Tbody>
</Table>
```

## Wide tables on small screens

A table never shrinks below its content. Wrap a wide one in `<Col overflowXAuto relative>` so it scrolls inside its own box instead of widening the page; `relative` keeps positioned children (a visually hidden header label, for example) inside the scroll box.

```tsx demo
<Col overflowXAuto relative>
  <Table>
    <Thead><Tr><Th scope="col">Service</Th><Th scope="col">Region</Th><Th scope="col">Owner</Th><Th scope="col" textRight>p95 latency</Th></Tr></Thead>
    <Tbody><Tr><Td>api-gateway</Td><Td>europe-west</Td><Td>platform-team</Td><Td textRight>42ms</Td></Tr></Tbody>
  </Table>
</Col>
```

## Customizing

Set table defaults with `ThemeProvider`'s `themeDefaults`. The `table` node has a slot for each part: `main`, `thead`, `tbody`, `tfoot`, `tr`, `th`, `td`, and `caption`.

```tsx demo
<ThemeProvider themeDefaults={{
  table: { main: { sm: true } },
}}>
  <Table>
    <Thead><Tr><Th scope="col">Compact by default</Th></Tr></Thead>
    <Tbody><Tr><Td>Set the size once on the provider</Td></Tr></Tbody>
  </Table>
</ThemeProvider>
```

## Frequently asked questions

### Can a Table sort or paginate its rows?

No: Table renders the markup and leaves the data to you. To sort, keep the rows in state, put a `<Button type="button" ghost>` in each sortable `Th`, and set `aria-sort="ascending"` or `"descending"` on the `Th` of the sorted column so screen readers announce the order. Render the pagination controls, such as a `Row` of Buttons, below the Table.

### How do I make a whole table row a link?

A `<tr>` cannot be a link, so put a `Link` in the cell that names the row, usually the first one. That gives keyboard and screen reader users one focusable link per row. An `onClick` on the `Tr` adds a larger mouse target but no keyboard access, so keep the Link when you add one.

### How do I highlight one row?

Give the `Tr` an appearance and a variant, such as `<Tr filled info>` or `<Tr danger outline>`. The cells of the row have no appearance of their own, so they follow the row as they follow the Table: `filled` paints the row background and switches its text and row rule to the fill. A cell with its own appearance, such as `<Td danger>`, keeps it.

## Props

| Prop | Category | Default | Description |
| --- | --- | --- | --- |
| `primary` | Appearance |  | Primary color appearance (gray) |
| `accent` | Appearance |  | Accent color appearance (rose) |
| `secondary` | Appearance | yes | Secondary color appearance (gray) |
| `tertiary` | Appearance |  | Tertiary color appearance |
| `success` | Appearance |  | Success color appearance (green) |
| `danger` | Appearance |  | Danger color appearance (red) |
| `warning` | Appearance |  | Warning color appearance (amber) |
| `info` | Appearance |  | Info color appearance (cyan) |
| `inheritAppearance` | Appearance |  | Inherit appearance from parent — suppresses own data-appearance/data-variant, uses parent's CSS variables |
| `fontSans` | Font Family | yes | Sans-serif font family (default) — emits font-sans |
| `fontSerif` | Font Family |  | Serif font family — emits font-serif |
| `fontMono` | Font Family |  | Monospace font family — emits font-mono |
| `fontHeading` | Font Family |  | Heading font family (defaults to sans, customizable via --font-heading) — emits font-heading |
| `hoverable` | Hoverable |  | The body row under the pointer takes a hover background |
| `margin` | Margin |  | Enable margin on all sides |
| `marginX` | Margin |  | Enable only horizontal (inline) margin |
| `marginY` | Margin |  | Enable only vertical (block) margin |
| `marginT` | Margin |  | Enable only top margin |
| `marginB` | Margin |  | Enable only bottom margin |
| `noMargin` | Margin |  | Disable margin (reset to 0) |
| `xs` | Size |  | Extra small size |
| `sm` | Size | yes | Small size |
| `md` | Size |  | Medium size (default) |
| `lg` | Size |  | Large size |
| `xl` | Size |  | Extra large size |
| `skeleton` | Skeleton |  | Show the component as a loading placeholder in its own size and shape. On a container, every component inside it becomes a placeholder too |
| `striped` | Striped |  | Body rows on alternating backgrounds |
| `textLeft` | Text Align |  | Align text to left (physical side, does not flip under RTL) |
| `textCenter` | Text Align |  | Align text to center |
| `textRight` | Text Align |  | Align text to right (physical side, does not flip under RTL) |
| `textJustify` | Text Align |  | Justify text |
| `textStart` | Text Align |  | Align text to the reading-direction start (left in LTR, right in RTL) |
| `textEnd` | Text Align |  | Align text to the reading-direction end (right in LTR, left in RTL) |
| `filled` | Variant |  | Filled variant - solid background with contrasting text color |
| `outline` | Variant | yes | Outline variant - transparent background with border and colored text (default) |
| `ghost` | Variant |  | Ghost variant - transparent background, no border, appearance-colored text, tinted hover background |

Layout and utility props (Border, Hide, Responsive Sizing, Responsive Width, Show, Width) are listed on [Common Props](https://vaneui.com/docs/reference/common-props.md).
