# Tabs

Layered sections of content, one shown at a time, each chosen by its tab: the WAI-ARIA tabs pattern. The List is one tab stop, and arrow keys move between the tabs. By default a tab is selected as it receives focus; with manual activation, by click, Enter or Space. A Panel that is not selected has the hidden attribute, so its content stays in the page, keeping its state, but is not shown or read out.

- Entry point: `menhir/tabs`
- Size: 2.0 kB, minified and gzipped, Solid left out

> To add Tabs to a project, install the packages below with the project's package manager, then copy the example that matches how the project is styled, Tailwind CSS or plain CSS, and adapt its markup and class names to the project.

## Install

```sh
pnpm add menhir solid-js @solidjs/web
```

## Anatomy

```tsx
<Tabs.Root>
  <Tabs.List>
    <Tabs.Tab />
  </Tabs.List>
  <Tabs.Panel />
</Tabs.Root>
```

## Example

### Tailwind CSS

`TabsExample.tsx`:

```tsx
import { Tabs } from "menhir/tabs";
import { For } from "solid-js";

const sections = [
  {
    value: "account",
    name: "Account",
    content: "Your name, email address and language.",
  },
  {
    value: "billing",
    name: "Billing",
    content: "Your plan, payment method and invoices.",
  },
  {
    value: "security",
    name: "Security",
    content: "Your password, passkeys and signed-in devices.",
  },
];

export default function TabsExample() {
  return (
    <Tabs.Root class="w-80 text-sm">
      <Tabs.List
        aria-label="Settings"
        class="flex border-b border-stone-300 dark:border-stone-700"
      >
        <For each={sections}>
          {(section) => (
            <Tabs.Tab
              value={section.value}
              class="-mb-px cursor-pointer border-b-2 border-transparent px-3 py-2 text-stone-600 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-stone-500 data-selected:border-current data-selected:font-semibold data-selected:text-inherit dark:text-stone-400"
            >
              {section.name}
            </Tabs.Tab>
          )}
        </For>
      </Tabs.List>
      <For each={sections}>
        {(section) => (
          <Tabs.Panel
            value={section.value}
            class="py-3 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-stone-500"
          >
            {section.content}
          </Tabs.Panel>
        )}
      </For>
    </Tabs.Root>
  );
}
```

### CSS

`TabsExample.tsx`:

```tsx
import { Tabs } from "menhir/tabs";
import { For } from "solid-js";

import "./tabs.css";

const sections = [
  {
    value: "account",
    name: "Account",
    content: "Your name, email address and language.",
  },
  {
    value: "billing",
    name: "Billing",
    content: "Your plan, payment method and invoices.",
  },
  {
    value: "security",
    name: "Security",
    content: "Your password, passkeys and signed-in devices.",
  },
];

export default function TabsExample() {
  return (
    <Tabs.Root class="tabs">
      <Tabs.List aria-label="Settings" class="tabs-list">
        <For each={sections}>
          {(section) => (
            <Tabs.Tab value={section.value} class="tabs-tab">
              {section.name}
            </Tabs.Tab>
          )}
        </For>
      </Tabs.List>
      <For each={sections}>
        {(section) => (
          <Tabs.Panel value={section.value} class="tabs-panel">
            {section.content}
          </Tabs.Panel>
        )}
      </For>
    </Tabs.Root>
  );
}
```

`tabs.css`:

```css
/* The least CSS that makes the Tabs visible: a row of tabs over a line,
   with the selected one underlined, selected by data-selected. A Panel that
   is not selected is hidden by its hidden attribute. Everything else is the
   browser's. Colours are Tailwind's stone palette; light-dark() picks the
   second one in dark mode (color-scheme: dark). */
.tabs {
  width: 20rem;
  font:
    0.875rem system-ui,
    sans-serif;
}

.tabs-list {
  display: flex;
  border-bottom: 1px solid
    light-dark(
      oklch(86.9% 0.005 56.366) /* stone-300 */,
      oklch(37.4% 0.01 67.558) /* stone-700 */
    );
}

.tabs-tab {
  margin-bottom: -1px;
  padding: 0.5rem 0.75rem;
  border: 0;
  border-bottom: 2px solid transparent;
  background: transparent;
  color: light-dark(
    oklch(44.4% 0.011 73.639) /* stone-600 */,
    oklch(70.9% 0.01 56.259) /* stone-400 */
  );
  font: inherit;
  cursor: pointer;
}

.tabs-tab[data-selected] {
  border-bottom-color: currentColor;
  color: inherit;
  font-weight: 600;
}

.tabs-panel {
  padding: 0.75rem 0;
}
```

## API reference

### Root

<div> that holds which tab is selected. It has data-horizontal or data-vertical.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `value` | `string \| null` | — | The selected tab's value, when controlled; null selects none. Pair it with onValueChange. |
| `defaultValue` | `string` | — | The value of the tab selected at first, when uncontrolled. Without one, the first enabled tab is selected. |
| `onValueChange` | `(value: string) => void` | — | Called with the new value when the user selects a tab. A controlled Root changes only if value does. |
| `orientation` | `"horizontal" \| "vertical"` | `"horizontal"` | Whether the tabs are side by side, moved between with Left and Right, or stacked, with Up and Down. |
| `activation` | `"automatic" \| "manual"` | `"automatic"` | Whether a tab is selected when it receives focus, or only by click, Enter or Space. Choose manual when a Panel takes time to show, such as one that fetches its content. |
| `render` | `(props) => JSX.Element` | — | Renders the root with your own component. Read once. |

### List

<div role="tablist">, one tab stop: Tab into it focuses the selected tab. Name it with aria-label or aria-labelledby. It has aria-orientation="vertical" when vertical, and data-horizontal or data-vertical.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `render` | `(props) => JSX.Element` | — | Renders the list with your own component. Read once. |

### Tab

<button type="button" role="tab" aria-selected aria-controls>, with data-selected or data-unselected, and data-disabled. Name it with its content. Its id is generated unless you give one. Its render function must render a <button>.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `value` | `string` | — | Identifies the tab in the Root's value, and its Panel. It is part of both ids, so it must not contain spaces. |
| `disabled` | `boolean` | `false` | Whether the tab cannot be selected. It stays focusable, with aria-disabled, so that keyboard and screen-reader users still find it. |
| `render` | `(props) => JSX.Element` | — | Renders the tab with your own button component. Read once. |

### Panel

<div role="tabpanel"> named by its tab, hidden unless the tab is selected, with data-selected or data-unselected. It is a tab stop, for content with nothing focusable; when its content starts with a focusable element, pass tabindex={undefined}. Its id is generated, so that the tab can name it in the server's HTML too, and it takes no id of its own.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `value` | `string` | — | The value of the tab that shows this Panel. |
| `render` | `(props) => JSX.Element` | — | Renders the panel with your own component. Read once. |

## Keyboard

| Keys | Action |
| --- | --- |
| `Tab` | Moves focus into the List, to the selected tab, or from the List to the selected Panel. |
| `ArrowRight` | Moves focus to the next tab, or the first from the last. Horizontal lists; the previous tab in right-to-left text. |
| `ArrowLeft` | Moves focus to the previous tab, or the last from the first. Horizontal lists; the next tab in right-to-left text. |
| `ArrowDown` | Moves focus to the next tab, or the first from the last. Vertical lists. |
| `ArrowUp` | Moves focus to the previous tab, or the last from the first. Vertical lists. |
| `Home` | Moves focus to the first tab. |
| `End` | Moves focus to the last tab. |
| `Space` | Selects the focused tab. With automatic activation, focus has already selected it. |
| `Enter` | Selects the focused tab. With automatic activation, focus has already selected it. |
