# Tabs

Sections of content shown one at a time, chosen from a list of tabs.

_Diagram: Tabs are a row of labels under a heading in the content area, with the panel of the selected tab right below them._

## Live example

### Automatic activation

```html
<dui-tabs>
  <nav aria-label="Account settings">
    <ul>
      <li><a href="#tab-profile">Profile</a></li>
      <li><a href="#tab-security">Security</a></li>
      <li><a href="#tab-notifications">Notifications</a></li>
    </ul>
  </nav>
  <section id="tab-profile">
    <h3>Profile</h3>
    <p>Your name and photo appear on comments and shared projects.</p>
  </section>
  <section id="tab-security">
    <h3>Security</h3>
    <div data-field>
      <label for="tab-security-code">Recovery code</label>
      <input id="tab-security-code" name="recovery" type="text" autocomplete="off">
    </div>
  </section>
  <section id="tab-notifications">
    <h3>Notifications</h3>
    <p>Email alerts are on. You get a weekly digest on Mondays.</p>
  </section>
</dui-tabs>
```

### Manual activation

```html
<dui-tabs activation="manual">
  <nav aria-label="Report period">
    <ul>
      <li><a href="#tab-week">Week</a></li>
      <li><a href="#tab-month" aria-current="true">Month</a></li>
      <li><a href="#tab-year">Year</a></li>
    </ul>
  </nav>
  <section id="tab-week">
    <h3>Week</h3>
    <p>42 orders this week.</p>
  </section>
  <section id="tab-month">
    <h3>Month</h3>
    <p>180 orders this month.</p>
  </section>
  <section id="tab-year">
    <h3>Year</h3>
    <p>2,140 orders this year.</p>
  </section>
</dui-tabs>
```

### Right to left

```html
<div dir="rtl" lang="ar">
  <dui-tabs>
    <nav aria-label="الإعدادات">
      <ul>
        <li><a href="#tab-rtl-general">عام</a></li>
        <li><a href="#tab-rtl-privacy">الخصوصية</a></li>
        <li><a href="#tab-rtl-help">مساعدة</a></li>
      </ul>
    </nav>
    <section id="tab-rtl-general">
      <h3>عام</h3>
      <p>الإعدادات العامة للحساب.</p>
    </section>
    <section id="tab-rtl-privacy">
      <h3>الخصوصية</h3>
      <p>من يمكنه رؤية ملفك الشخصي.</p>
    </section>
    <section id="tab-rtl-help">
      <h3>مساعدة</h3>
      <p>الأسئلة الشائعة والدعم.</p>
    </section>
  </dui-tabs>
</div>
```

## Attributes

| Attribute or value | Where | Effect |
| --- | --- | --- |
| `<dui-tabs>` |  | wraps a `<nav aria-label>` with a list of `<a href="#panel-id">` links, then the panels |
| `activation="manual"` | `<dui-tabs>` | arrows move focus, Enter or Space selects (default: automatic) |
| `aria-current="true"` | a link | the tab selected on load (else the tab named in the URL fragment, else the first) |
| `role="tablist" \| "tab" \| "tabpanel"` |  | added by the component; styled here |

### Keyboard

| Key | Action |
| --- | --- |
| <kbd>Tab</kbd> | moves into the tab list (to the selected tab), then to the panel |
| <kbd>Right Arrow</kbd> / <kbd>Left Arrow</kbd> | next or previous tab, wrapping (swapped in right-to-left) |
| <kbd>Home</kbd> / <kbd>End</kbd> | first or last tab |
| <kbd>Enter</kbd> / <kbd>Space</kbd> | selects the focused tab (manual activation) |

## Accessibility

- The selected tab has a bar and full-strength text, not color alone ([WCAG 1.4.11](https://www.w3.org/WAI/WCAG22/Understanding/non-text-contrast.html)).
- Without JavaScript, every section shows and the links work as a table of contents.
- **Our choice:** automatic activation, as the [APG recommends](https://www.w3.org/WAI/ARIA/apg/patterns/tabs/#deciding-when-to-make-selection-automatically-follow-focus) for fast panels. `activation="manual"` otherwise.

## Specifications

- [APG Tabs pattern](https://www.w3.org/WAI/ARIA/apg/patterns/tabs/)
- [MDN `tab` role](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Reference/Roles/tab_role)
- [MDN `tabpanel` role](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Reference/Roles/tabpanel_role)
- [W3C el nav](https://www.w3.org/TR/html-aria/#el-nav)

## See also

- [Molecules](https://domestic-ui.odonckers.dev/molecules/index.md)
- Source: [`css/components/molecules/tabs.css`](https://github.com/odonckers/domestic-ui/blob/main/css/components/molecules/tabs.css), [`examples/molecules/tabs.html`](https://github.com/odonckers/domestic-ui/blob/main/examples/molecules/tabs.html)

## How other systems do it

For comparison only; these are not the standard this system follows.

- [Apple Human Interface Guidelines: Tab views](https://developer.apple.com/design/human-interface-guidelines/tab-views)
- [Material Design 3: Tabs](https://m3.material.io/components/tabs/overview)
- [Vercel Geist: Tabs](https://vercel.com/geist/tabs)
