# Tabs

Switches between views of the same content; SegmentedControl switches a small set of options inline.

- Family: navigation
- Import: `import { Tabs, TabList, Tab, TabPanel, SegmentedControl } from "@senvenli/react/navigation";`
- Page: https://design.senvenli.com/docs/components/tabs
- JSON: https://design.senvenli.com/docs/components/tabs.json

## Example: Views of one record

Short nouns; a count can follow a label.

```tsx
import { Text } from "@senvenli/react/display";
import { Tabs } from "@senvenli/react/navigation";

export default function Example() {
  return (
    <Tabs
      aria-label="Customer"
      items={[
        { id: "overview", label: "Overview", panel: <Text>Ada Lovelace has been a customer since March 2024.</Text> },
        { id: "orders", label: "Orders", count: 12, panel: <Text>12 orders, the latest on March 18.</Text> },
        { id: "invoices", label: "Invoices", count: 2, panel: <Text>2 invoices are awaiting payment.</Text> },
        { id: "notes", label: "Notes", panel: <Text>Prefers delivery before noon.</Text> },
      ]}
    />
  );
}
```

## Examples

### Composed from parts

`TabList`, `Tab` and `TabPanel` as children: an icon in a label, panels built from your own components.

```tsx
import { Icon, Text } from "@senvenli/react/display";
import { Tab, TabList, TabPanel, Tabs } from "@senvenli/react/navigation";

export default function Example() {
  return (
    <Tabs>
      <TabList aria-label="Settings">
        <Tab id="profile">
          <Icon name="user" size="small" />
          Profile
        </Tab>
        <Tab id="billing" count={1}>
          <Icon name="credit-card" size="small" />
          Billing
        </Tab>
        <Tab id="team" isDisabled>
          <Icon name="users" size="small" />
          Team
        </Tab>
      </TabList>
      <TabPanel id="profile">
        <Text>Your name, photo and the language the app speaks to you in.</Text>
      </TabPanel>
      <TabPanel id="billing">
        <Text>One invoice is waiting for payment.</Text>
      </TabPanel>
      <TabPanel id="team">
        <Text>Invite people once your plan includes seats.</Text>
      </TabPanel>
    </Tabs>
  );
}
```

### Variants and sizes

`underline` (default), `pills` and `outline`; `size` small, medium or large.

```tsx
import { Text } from "@senvenli/react/display";
import { Stack } from "@senvenli/react/layout";
import { Tabs } from "@senvenli/react/navigation";

const items = (name: string) => [
  { id: "overview", label: "Overview", panel: <Text>{name}: the summary of this customer.</Text> },
  { id: "orders", label: "Orders", count: 12, panel: <Text>{name}: twelve orders.</Text> },
  { id: "notes", label: "Notes", panel: <Text>{name}: prefers delivery before noon.</Text> },
];

export default function Example() {
  return (
    <Stack space="space-400">
      <Tabs aria-label="Underline" items={items("Underline")} />
      <Tabs aria-label="Pills" variant="pills" items={items("Pills")} />
      <Tabs aria-label="Outline" variant="outline" size="small" items={items("Outline, small")} />
    </Stack>
  );
}
```

### Vertical

`orientation="vertical"`: the tabs in a column beside the panel; arrows go up and down.

```tsx
import { Text } from "@senvenli/react/display";
import { Tabs } from "@senvenli/react/navigation";

export default function Example() {
  return (
    <Tabs
      aria-label="Settings"
      orientation="vertical"
      variant="pills"
      items={[
        { id: "profile", label: "Profile", panel: <Text>Your name, photo and language.</Text> },
        { id: "billing", label: "Billing", panel: <Text>Plan, invoices and payment method.</Text> },
        { id: "team", label: "Team", panel: <Text>People and their roles.</Text> },
      ]}
    />
  );
}
```

### Controlled

The app owns the selected tab, for example to keep it in the URL.

```tsx
import { Button } from "@senvenli/react/action";
import { Text } from "@senvenli/react/display";
import { Stack } from "@senvenli/react/layout";
import { Tabs } from "@senvenli/react/navigation";
import { useState } from "react";

export default function Example() {
  const [tab, setTab] = useState<string | number>("open");
  return (
    <Stack space="space-200" align="start">
      <div className="w-full">
        <Tabs
          aria-label="Invoices"
          selectedKey={tab}
          onSelectionChange={setTab}
          items={[
            { id: "open", label: "Open", panel: <Text>18 invoices are open.</Text> },
            { id: "paid", label: "Paid", panel: <Text>1,186 invoices are paid.</Text> },
            { id: "overdue", label: "Overdue", panel: <Text>3 invoices are overdue.</Text> },
          ]}
        />
      </div>
      <Button variant="subtle" isDisabled={tab === "overdue"} onPress={() => setTab("overdue")}>
        Show overdue invoices
      </Button>
    </Stack>
  );
}
```

### SegmentedControl

A small set of options switched inline, with no panels of its own.

```tsx
import { Text } from "@senvenli/react/display";
import { Stack } from "@senvenli/react/layout";
import { SegmentedControl } from "@senvenli/react/navigation";
import { useState } from "react";

const REVENUE: Record<string, string> = { day: "$4,280", week: "$31,460", month: "$128,900" };

export default function Example() {
  const [period, setPeriod] = useState<string | number>("week");
  return (
    <Stack direction="horizontal" space="space-200">
      <SegmentedControl
        label="Period"
        value={period}
        onChange={setPeriod}
        items={[
          { id: "day", label: "Day" },
          { id: "week", label: "Week" },
          { id: "month", label: "Month" },
        ]}
      />
      <Text isNumeric>Revenue: {REVENUE[period]}</Text>
    </Stack>
  );
}
```

## Guidelines

### When to use it

- Two to seven views of one thing: Overview, Activity, Settings.

### Use something else when

- [toggle-button](https://design.senvenli.com/docs/components/toggle-button.md): Options act on content instead of switching views.

## Accessibility

Keyboard:

- Arrow keys: Moves between tabs.

Built in:

- Tabs and panels are linked; counts are announced.

You provide:

- Short nouns for tab labels.
- With children: one TabPanel per Tab, matched by the same id, and an aria-label on TabList when no heading names it.

## API reference

### Tabs

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| items | `readonly TabItem[]` |  | no | Two to six tabs as data. Leave it out and compose `TabList`, `Tab` and `TabPanel` as children instead, for a tab with an icon, a panel built elsewhere, or tabs rendered from your own components. |
| children | `React.ReactNode` |  | no | With no `items`: a `TabList` of `Tab`s followed by one `TabPanel` per tab, matched by `id`. |
| selectedKey | `Key` |  | no |  |
| defaultSelectedKey | `Key` |  | no |  |
| onSelectionChange | `(key: Key) => void` |  | no |  |
| aria-label | `string` |  | no | Names the tab list when no heading above it does (with `items`; on `TabList` otherwise). |
| variant | `TabsVariant` |  | no | `underline` (default): a line under the selected tab; `pills`: a filled tab; `outline`: a framed tab. |
| orientation | `"horizontal" \| "vertical"` |  | no | `horizontal` (default), or `vertical`: the tabs in a column beside the panels (settings pages). |
| size | `ComponentSize` |  | no | `small`, `medium` (default) or `large`; the provider default otherwise. |
| motion | `ComponentMotion` |  | no | `auto` (follow the device), `reduce` or `none`, for this component; the provider's setting otherwise. |

### TabList

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| children | `React.ReactNode` |  | yes | `Tab` elements. |
| aria-label | `string` |  | no | Names the tab list when no heading above it does. |
| aria-labelledby | `string` |  | no |  |

### Tab

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| id | `Key` |  | yes | Matches the `TabPanel` with the same `id`. |
| children | `React.ReactNode` |  | yes | The label: a noun, one or two words, optionally with an Icon before it. |
| count | `number` |  | no | A count after the label (a Badge); hidden at 0. |
| isDisabled | `boolean` |  | no |  |

### TabPanel

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| id | `Key` |  | yes | Matches the `Tab` with the same `id`. |
| children | `React.ReactNode` |  | yes |  |

### SegmentedControl

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| label | `string` |  | yes | Names the group for assistive tech ("Period"); it is not shown. |
| items | `readonly SegmentedControlItem[]` |  | yes | Two to five short options ("Day", "Week", "Month"). |
| value | `Key` |  | no |  |
| defaultValue | `Key` |  | no |  |
| onChange | `(value: Key) => void` |  | no |  |
| isDisabled | `boolean` |  | no |  |

### Tokens it reads

`--ds-background-neutral`, `--ds-background-segment-selected`, `--ds-border`, `--ds-border-bold`, `--ds-border-focused`, `--ds-border-selected`, `--ds-easing-standard`, `--ds-radius-control-medium`, `--ds-radius-medium`, `--ds-radius-small`, `--ds-shadow-raised`, `--ds-space-050`, `--ds-space-075`, `--ds-space-100`, `--ds-space-150`, `--ds-space-200`, `--ds-space-300`, `--ds-space-400`, `--ds-text`, `--ds-text-disabled`, `--ds-text-subtle`

## Related

- [toggle-button](https://design.senvenli.com/docs/components/toggle-button.md)
