# Tabs

Associate tabs and panels with native keyboard behavior. (Custom element.)

Reference: https://timeless.build/docs/components/tabs/

## Markup

```html
<ui-tabs id="package-tabs" value="install">
  <div role="tablist" aria-label="Package details" aria-orientation="horizontal">
    <button id="package-tabs-install-tab" value="install" role="tab" aria-controls="package-tabs-install-panel" aria-selected="true" tabindex="0" type="button">Install</button>
    <button id="package-tabs-usage-tab" value="usage" role="tab" aria-controls="package-tabs-usage-panel" aria-selected="false" tabindex="-1" type="button">Usage</button>
  </div>
  <section id="package-tabs-install-panel" role="tabpanel" aria-labelledby="package-tabs-install-tab" tabindex="0">
    <p>Install @timelessui/components from npm.</p>
  </section>
  <section id="package-tabs-usage-panel" role="tabpanel" aria-labelledby="package-tabs-usage-tab" tabindex="0" hidden>
    <p>Author native Light DOM anatomy.</p>
  </section>
</ui-tabs>
```

## Install

```js
import '@timelessui/components/css/tokens.css'
import '@timelessui/components/css/core/tabs.css'
import '@timelessui/components/css/themes/atmosphere/tokens.css'
import '@timelessui/components/css/themes/atmosphere/tabs.css'
import '@timelessui/components/register/ui-tabs'
```

A `register/` import is a side effect: it defines the element as the module evaluates, so it
has to run in code the browser loads. To register explicitly instead — to control the timing,
or to define into another window — call the function from the matching `define/` entry point,
which registers nothing on its own:

```js
import { defineTabsElement } from '@timelessui/components/define/ui-tabs'
defineTabsElement()
```

## Anatomy

Parts are authored in your own markup and identified by the selector below. Required parts
must be present for the component to work. Private `data-ui-internal-*` hooks are written by
the runtime and must never be authored.

| Part | Required | Selector | Purpose |
| --- | --- | --- | --- |
| `tablist` | Yes | `[role='tablist']` | Container for the tabs. |
| `tab` | Yes | `[role='tab']` | One tab. Use `<button type="button">` and give it a `value`; Timeless wires `id`, `aria-controls`, `aria-selected`, and `tabindex`. |
| `tabpanel` | Yes | `[role='tabpanel']` | One panel per tab, in the same order. Timeless wires `id`, `aria-labelledby`, and `hidden`. |

These parts also accept per-item input of their own. No stylesheet selects these
attributes — the component reads them.

| Part | Attribute | Description |
| --- | --- | --- |
| `tab` | `data-ui-value` | The value this tab selects, for a tab that cannot carry a bare `value` attribute. `value` wins over it, and the tab `id` is the last fallback. |

## Attributes

Every value below is implemented by the stylesheets this component ships. Boolean attributes
are presence-based: author the attribute with no value, or omit it.

| Attribute | Values | Default | Description |
| --- | --- | --- | --- |
| `activation` | `automatic` · `manual` | `automatic` | Whether moving focus with the arrow keys selects the tab immediately (`automatic`) or waits for Enter or Space (`manual`). Use `manual` when selecting a tab is expensive. |
| `orientation` | `horizontal` · `vertical` | `horizontal` | Arrow-key axis. Mirrored onto `aria-orientation` on the tablist during enhancement. |
| `value` | any `string` | — | The tab selected on load and after a form reset. Match a tab’s `value` attribute. Assign the `value` property for live changes. |

## Element API

Attributes above are the authoring surface. These are the DOM properties and events the
registered element adds once it upgrades.

### `<ui-tabs>`

| Property | Type | Notes |
| --- | --- | --- |
| `activation` | `'automatic' | 'manual'` | Reflects the `activation` attribute |
| `orientation` | `'horizontal' | 'vertical'` | Reflects the `orientation` attribute |
| `defaultValue` | `string` | Reflects the `value` attribute |
| `value` | `string` | Live value. Assigning it does not rewrite the authored default and does not dispatch transition events. |

| Event | Detail | Description |
| --- | --- | --- |
| `ui-before-change` | `CustomEvent<TabsChangeDetail>` | Cancelable proposal dispatched before the selected tab changes. Call `preventDefault()` to reject the transition and keep the current value. |
| `ui-change` | `CustomEvent<TabsChangeDetail>` | Dispatched after the selected tab has changed. Bubbles and is composed. |

## State

Native attributes, ARIA, and platform pseudo-classes are authoritative. Style state through
the selectors below rather than adding your own state classes.

| State | Source | How to express it |
| --- | --- | --- |
| `selected` | aria | `aria-selected="true"` on the active tab. |

## Styling

Root identity: `ui-tabs`.
Required stylesheets: `tokens.css`, `core/tabs.css`, `themes/atmosphere/tokens.css`, `themes/atmosphere/tabs.css`.

This component adds no custom properties of its own. Restyle it through the design tokens at https://timeless.build/docs/styling/theming/ or your own CSS.

Design tokens this component reads (12), global and set at the theme level: `--ui-accent`, `--ui-accent-soft`, `--ui-duration-fast`, `--ui-ease-standard`, `--ui-fg`, `--ui-fg-muted`, `--ui-focus`, `--ui-line`, `--ui-radius-md`, `--ui-space-2`, `--ui-space-4`, `--ui-space-5`.

## Accessibility

Keep the native elements, roles, and relationships shown in the markup. Timeless adds only the
state and keyboard coordination the platform does not already provide, and it never supplies
your accessible names — those depend on your content.

Follows the [Tabs pattern](https://www.w3.org/WAI/ARIA/apg/patterns/tabs/) from the ARIA Authoring Practices Guide.

| Key | Action |
| --- | --- |
| `Arrow keys` | Move focus between tabs along the `orientation` axis. |
| `Home / End` | Move focus to the first or last tab. |
| `Enter / Space` | Select the focused tab. Only needed when `activation` is `manual`; with `automatic` the arrow keys select as they move. |

The tablist is one tab stop: Tab moves into the selected tab, then out to the panel. Timeless manages roving `tabindex`, `aria-selected`, and panel `hidden`.

## Before JavaScript runs

The markup above is complete and usable on its own: native controls submit, labels associate, and authored ARIA is already correct. Registration adds state synchronisation, focus management, and keyboard coordination on top of it.
