# Tabs

Tab strips: the fill strip, or the segmented look for a view switch, and the fill strip as links between pages.

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

```tsx
import { Tabs, TabsList, Tab, TabsPanel, TabsNav, TabLink } from "@arcadia/ui";
```

## In context

Switch settings panels without leaving the page.

```tsx
import { Tab, Tabs, TabsList, TabsPanel } from "@arcadia/ui";

<Tabs defaultValue="models" rootStyle={cmp.tabsNarrow}>
  <TabsList aria-label="Workspace settings">
    <Tab value="general">General</Tab>
    <Tab value="models">Models</Tab>
    <Tab value="shortcuts">Shortcuts</Tab>
  </TabsList>
  <TabsPanel value="general">
    <div {...sx("cmp-panel-body", cmp.panelBody)}>Choose the workspace theme and language.</div>
  </TabsPanel>
  <TabsPanel value="models">
    <div {...sx("cmp-panel-body", cmp.panelBody)}>Aster and Rowan are available in this workspace.</div>
  </TabsPanel>
  <TabsPanel value="shortcuts">
    <div {...sx("cmp-panel-body", cmp.panelBody)}>Press Control K to open search.</div>
  </TabsPanel>
</Tabs>
```

## When to use

- Switch between panels for the same object.
- Use a small strip in a narrow side panel.

## When not to use

- Use `SegmentedControl` for a choice without its own panel.
- Use `TabsNav` and `TabLink` for linked pages.

## Usage

```tsx
import { Tab, Tabs, TabsList, TabsPanel } from "@arcadia/ui";

<Tabs defaultValue="changes">
  <TabsList size="sm">
    <Tab value="changes" icon="file-diff">Changes</Tab>
    <Tab value="todos" icon="todo">To-dos</Tab>
  </TabsList>
  <TabsPanel value="changes"><Changes /></TabsPanel>
  <TabsPanel value="todos"><Todos /></TabsPanel>
</Tabs>
```

Use `fill` for page sections and `segmented` for compact panel switches.

Use `density="compact"` on `TabsList` for Body labels.

Use `TabsNav` and `TabLink` when each view has an address.

## Examples

### Panel header

Add icons to a compact strip above a side panel.

```tsx
import { Tab, Tabs, TabsList, TabsPanel } from "@arcadia/ui";

<Tabs defaultValue="changes" rootStyle={cmp.tabs}>
  <TabsList density="compact">
    <Tab value="changes" icon="file-diff">Changes</Tab>
    <Tab value="todos" icon="todo">To-dos</Tab>
    <Tab value="terminal" icon="terminal">Terminal</Tab>
  </TabsList>
  <TabsPanel value="changes"><div {...sx("cmp-panel-body", cmp.panelBody)}>3 files changed.</div></TabsPanel>
  <TabsPanel value="todos"><div {...sx("cmp-panel-body", cmp.panelBody)}>4 of 4 to-dos done.</div></TabsPanel>
  <TabsPanel value="terminal"><div {...sx("cmp-panel-body", cmp.panelBody)}>No running processes.</div></TabsPanel>
</Tabs>
```

### Tabs that are links

Use links when each tab leads to its own address.

```tsx
import { TabLink, Tabs, TabsNav } from "@arcadia/ui";

<TabsNav label="Settings components">
  <TabLink href="/components/settings-group">Settings group</TabLink>
  <TabLink href="/components/switch">Switch</TabLink>
  <TabLink href="/components/tabs#tabs-that-are-links" current>Tabs</TabLink>
</TabsNav>
```

### More pages than its place holds

Scroll the strip to reach more sections.

```tsx
import { TabLink, Tabs, TabsNav } from "@arcadia/ui";

<div {...sx(null, stack.snap)}>
  <TabsNav label="Components" data-site="cmp-tabs-overflow">
    {[
      ["tabs#more-pages-than-its-place-holds", "Tabs"],
      ["notification-list", "Notifications"],
      ["profile-head", "Profile"],
      ["integration-settings", "Integrations"],
      ["auth-surface", "Sign-in"],
      ["permission-prompt", "Permissions"],
      ["limit-notice", "Limits"],
    ].map(([path, label], index) => (
      <TabLink key={path} href={"/components/" + path} current={index === 0} aria-label={label}>
        <span {...sx(null, stack.snapLabel)}>{label}</span>
      </TabLink>
    ))}
  </TabsNav>
</div>
```

### Counts

Add a quiet, tabular count without changing the label’s weight.

```tsx
import { Tab, TabLink, Tabs, TabsList, TabsNav, TabsPanel } from "@arcadia/ui";

<div {...stylex.props(stack.strips)}>
  <Tabs defaultValue="members" rootStyle={cmp.tabs}>
    <TabsList>
      <Tab value="members" count={12}>Members</Tab>
      <Tab value="agents" count={3}>Agents</Tab>
      <Tab value="invites" count={0}>Invites</Tab>
    </TabsList>
    <TabsPanel value="members"><div {...sx("cmp-panel-body", cmp.panelBody)}>12 people can see this board.</div></TabsPanel>
    <TabsPanel value="agents"><div {...sx("cmp-panel-body", cmp.panelBody)}>3 agents work in this team.</div></TabsPanel>
    <TabsPanel value="invites"><div {...sx("cmp-panel-body", cmp.panelBody)}>No invitation is waiting.</div></TabsPanel>
  </Tabs>
  <TabsNav label="Components" rootStyle={cmp.tabs}>
    <TabLink href="/components/list-row" count={4}>Rows</TabLink>
    <TabLink href="/components/tabs#counts" count={12} current>Tabs</TabLink>
    <TabLink href="/components/kbd" count={9}>Shortcuts</TabLink>
  </TabsNav>
</div>
```

### Tabs that close

Close tabs with the close button, middle click or Delete.

```tsx
import { useState } from "react";
import { Tab, Tabs, TabsList } from "@arcadia/ui";

function Closing() {
  const [open, setOpen] = useState(FILES);
  const [current, setCurrent] = useState("session.ts");
  return (
    <Tabs value={current} onValueChange={setCurrent} rootStyle={cmp.tabs}>
      <TabsList size="sm" aria-label="Open files" onClose={(file) => setOpen((files) => files.filter((f) => f !== file))}>
        {open.map((file) => <Tab key={file} value={file} icon="file">{file}</Tab>)}
      </TabsList>
      {open.length === 0 && <div {...sx("cmp-panel-body", cmp.panelBody)}>Every file is closed.</div>}
    </Tabs>
  );
}
```

### Tabs that move

Drag to reorder, or use Alt+Shift with an arrow key.

```tsx
import { useState } from "react";
import { Tab, Tabs, TabsList } from "@arcadia/ui";

function Reordering() {
  const [open, setOpen] = useState(FILES);
  return (
    <Tabs defaultValue="router.ts" rootStyle={cmp.tabs}>
      <TabsList size="sm" aria-label="Open files" onReorder={setOpen}>
        {open.map((file) => <Tab key={file} value={file} icon="file">{file}</Tab>)}
      </TabsList>
    </Tabs>
  );
}
```

### Unsaved changes

Mark unsaved files with a dot in the close button’s place.

```tsx
import { useState } from "react";
import { Tab, Tabs, TabsList } from "@arcadia/ui";

function Unsaved() {
  const [open, setOpen] = useState(FILES);
  return (
    <Tabs defaultValue="session.ts" rootStyle={cmp.tabs}>
      <TabsList size="sm" aria-label="Open files" onClose={(file) => setOpen((files) => files.filter((f) => f !== file))}>
        {open.map((file) => <Tab key={file} value={file} icon="file" dirty={file === "session.ts" || file === "billing.ts"}>{file}</Tab>)}
      </TabsList>
    </Tabs>
  );
}
```

### More tabs than its place holds

Scroll to more files without losing sight of the active tab.

```tsx
import { useState } from "react";
import { Tab, Tabs, TabsList } from "@arcadia/ui";

function ManyFiles() {
  const [open, setOpen] = useState(MANY);
  return (
    <Tabs defaultValue="deploy.yml" rootStyle={cmp.tabsNarrow}>
      <TabsList size="sm" aria-label="Open files" onClose={(file) => setOpen((files) => files.filter((f) => f !== file))} onReorder={setOpen}>
        {open.map((file) => <Tab key={file} value={file} icon="file" dirty={file === "webhooks.ts"}>{file}</Tab>)}
      </TabsList>
    </Tabs>
  );
}
```

### Segmented

Use the segmented look when each option opens its own panel.

```tsx
import { Tab, Tabs, TabsList } from "@arcadia/ui";

<Tabs defaultValue="preview">
  <TabsList appearance="segmented">
    <Tab value="preview">Preview</Tab>
    <Tab value="code">Code</Tab>
    <Tab value="diff">Diff</Tab>
  </TabsList>
</Tabs>
```

### Long names

Hover or focus a shortened name to read it in full.

```tsx
import { FadeText, Tab, Tabs, TabsList } from "@arcadia/ui";

<Tabs defaultValue="plan" rootStyle={cmp.tabsNarrow}>
  <TabsList appearance="segmented" stretch aria-label="Open pages">
    <Tab value="plan"><FadeText>Quarterly planning for the platform team</FadeText></Tab>
    <Tab value="notes"><FadeText>Release notes for the November build</FadeText></Tab>
    <Tab value="review"><FadeText>Design review of the settings pages</FadeText></Tab>
  </TabsList>
</Tabs>
```

## Matrix

Appearance and size

Rows: Fill, Segmented. Columns: Medium, Small, Hover and focus.

## Customising

`rootStyle` takes where the strip sits and the `--tab-*` knobs below. [Theming](/foundations/theming) says what else a product may change.

```tsx
import * as stylex from "@stylexjs/stylex";

const styles = stylex.create({
  roomy: { "--tab-px": "var(--space-3)", "--tabs-gap": "var(--space-1)" },
});

<TabsList rootStyle={styles.roomy}>
  <Tab value="changes">Changes</Tab>
</TabsList>
```

- `--tab-h` (`Ref<Control>`, default `var(--control-sm)`)
- `--tab-px` (`Ref<Space>`, default `var(--space-2)`)
- `--tabs-gap` (`Ref<Space>`, default `var(--space-0-5)`)
- `--tab-fg` (`Paint`, default `var(--text-3)`)
- `--tab-fg-hover` (`Paint`, default `var(--text-2)`)
- `--tab-fg-active` (`Paint`, default `var(--text-1)`)
- `--tab-fg-disabled` (`Paint`, default `var(--text-4)`)
- `--tab-edge` (`Paint`)

The knobs go on `TabsList` and every tab inherits them. A disabled tab drops to `--tab-fg-disabled`; the current tab keeps its colour. A segmented strip takes the Segmented Control's knobs instead.

## Accessibility

Tabs follow the APG pattern: a `tablist` with one tab stop, and panels labelled by their tabs. Focus never drops to the page when a tab is activated. A disabled tab stays in place at text-4 and is skipped by the arrow keys. Under forced colours, which drop every fill, the current tab stands on the system's highlight, its label in the highlight's text. A tab's height is a least height, so a person's own text spacing grows it instead of clipping the label.

A `TabsNav` is a `nav` named by its `label` ("Settings components"), with no tab roles: each `TabLink` is a link and a Tab stop of its own, Enter follows it, and the current one is marked `aria-current="page"`.

A `count` joins the tab's name, so a reader hears "Members, 12", and its figure stays out of the label's own weight so choosing the tab moves nothing.

A strip that closes or moves its tabs names those keys on each tab (`aria-keyshortcuts`). A close button is named for its tab ("Close router.ts") and takes no Tab stop of its own: Delete closes the focused tab, and focus goes to the tab that takes its place. An unsaved tab is heard "session.ts, unsaved", and a moved tab its new place, "router.ts, moved to position 2 of 4".

- ← + →: Move focus between tabs
- Enter + Space: Activate the focused tab, unless `activateOnFocus` already did
- Home + End: First or last tab
- Delete: Close the focused tab, with `onClose`; Backspace too
- Alt + Shift + ←: Move the focused tab a place toward the start, with `onReorder`; → toward the end

## API reference

### Tabs

- `value` (`string`)
- `defaultValue` (`string`)
- `onValueChange` (`(value: string, details: { event: Event }) => void`)
- `orientation` (`"horizontal" | "vertical"`, default `"horizontal"`)
- `rootStyle` (`RootStyle`): A component's typed override prop: `Layout` and its documented knobs, each the references it takes or another kit knob of its kind; anything else is a type error at the call site (STYLEX.md).

Rest props go to `<div>` props.

### TabsList

- `appearance` (`"fill" | "segmented"`, default `"fill"`)
- `size` (`"sm" | "md"`, default `"md"`)
- `density` (`"standard" | "compact"`)
- `stretch` (`boolean`, default `false`)
- `activateOnFocus` (`boolean`, default `false`)
- `onClose` (`(value: string) => void`)
- `onReorder` (`(values: string[]) => void`)
- `steady` (`boolean`, default `false`)
- `rootStyle` (`TabsListStyle`)

Rest props go to `<div>` props.

### Tab

- `value` (`string`, required)
- `disabled` (`boolean`)
- `icon` (`IconName`)
- `count` (`number`)
- `dirty` (`boolean`, default `false`)

Rest props go to `<button>` props.

### TabsPanel

- `value` (`string`, required)

Rest props go to `<div>` props.

### TabsNav

- `label` (`string`, required)
- `size` (`"sm" | "md"`, default `"md"`)
- `rootStyle` (`RootStyle<TabsKnobs>`)

Rest props go to `<nav>` props.

### TabLink

- `href` (`string`, required)
- `current` (`boolean`, default `false`)
- `icon` (`IconName`)
- `count` (`number`)

Rest props go to `<a>` props.
