# Switcher (`<z-switcher>`)

> Tabbed content switcher with a sliding indicator, keyboard navigation, collapsible and manual modes, and animated panel transitions.

- **Kind:** controller
- **Category:** navigation
- **Inherits:** `Controller`, `Base`

## Attributes

| Attribute | Type | Default | From | Description |
| --- | --- | --- | --- | --- |
| `active` | `number` | — | — | Index of the active tab, kept in sync with the shown panel. Set `-1` to collapse all when `collapsible` is on; an invalid or disabled index falls back to the first enabled tab. |
| `connect` | `string` | `''` | — | CSS selector for external panel containers, matched against the whole document. When empty, panels are taken from `.z-switcher` containers inside the host or from a following `.z-switcher` sibling. |
| `no-connect` | `boolean` | `false` | — | Disables panel switching: the tablist still manages active state and keyboard focus, but no panels are shown or hidden. |
| `manual` | `boolean` | `false` | — | Keyboard navigation moves focus without activating tabs; press Enter or Space to show the focused tab. |
| `collapsible` | `boolean` | `false` | — | Allows the active tab to be closed by clicking it again (or pressing Enter/Space on it), leaving no panel open and `active` at `-1`. |
| `orientation` | `'' \| 'horizontal' \| 'vertical'` | `''` | — | Overrides the tablist orientation used for keyboard navigation and `aria-orientation`. When empty, `.z-tab-left`, `.z-tab-right`, and `.z-nav` (without `.z-subnav`) infer vertical; everything else is horizontal. |
| `media` | `string` | `''` | — | Media query that gates a left/right tablist's vertical side layout: while it matches the side class is kept, and when it stops matching the side class is removed. |
| `animation` | `string` | `''` | — | Animation preset applied to a panel as it becomes active. A comma-separated pair uses the first class when moving forward and the second when moving backward. |
| `duration` | `number` | `150` | — | Panel animation duration in milliseconds; non-finite or negative values fall back to 150. |
| `no-indicator` | `boolean` | `false` | — | Disables the sliding indicator on `.z-tab` and `.z-tab-alt` tablists, leaving the active tab's static border or background in place. |

### Inherited from `Base`

| Attribute | Type | Default | From | Description |
| --- | --- | --- | --- | --- |
| `cls` | `string` | `''` | `Base` | Adds classes to named parts as `key: classes` pairs; a plain string targets the default part. |
| `stl` | `string` | `''` | `Base` | Adds inline styles to named parts as `key: styles` pairs; a plain string targets the default part. |

## Styling Parts (`cls` / `stl`)

Default `cls` target: `active`. Default `stl` target: `host-inner`.

| Key | Default class | cls | stl | Description |
| --- | --- | --- | --- | --- |
| `active` | `z-active` | ✓ | — | Class applied to the active tab item, its trigger, and the active panel; the default `cls` target for this component. |

## Events

| Event | Cancelable | Detail | Description |
| --- | --- | --- | --- |
| `z-switcher:before-change` | ✓ | `{ index: number; previousIndex: number; item: HTMLElement \| null; trigger: HTMLElement \| null; value: string \| null }` | Fires before the active tab changes; `preventDefault()` cancels the switch. Detail carries the target `index`, `previousIndex`, `item`, `trigger`, and `value`. |
| `z-switcher:change` | — | `{ index: number; previousIndex: number; item: HTMLElement \| null; trigger: HTMLElement \| null; value: string \| null }` | Fires after the active tab changes, with the same detail as `before-change`. |
| `z-switcher:mount` | — | `Record<string, unknown>` | Fires when deferred template content is mounted into the container. |

## Methods

| Method | Params | Returns | Description |
| --- | --- | --- | --- |
| `index()` | — | `number` | Returns the index of the active tab, or `-1` when all tabs are collapsed. |
| `refresh()` | — | `void` | Re-scans the tablist and panels, rebinds listeners, and re-measures the indicator after the DOM changes. |
| `show()` | `target: number \| 'next' \| 'previous' \| string` | `boolean` | Shows a tab by index, by `next`/`previous` (wrapping over enabled tabs), or by a value/href string; returns `true` when the tab is shown. A negative index collapses all when `collapsible` is on. |

## Data Hooks

| Attribute | Values | Applies to | Role | Description |
| --- | --- | --- | --- | --- |
| `data-fn` | `template` | `descendant` | `config` | Marks a deferred `<template>` child whose content is mounted into the container on demand. |
| `data-open` | — | `descendant` | `config` | Triggers carrying `data-open` are skipped during tab detection; the attribute belongs to the dialog toggle system, not the switcher. |
| `data-switcher-ignore` | — | `descendant` | `config` | Excludes a tablist child from tab detection; `.z-nav-header` and `.z-nav-divider` children are ignored automatically. |
| `data-switcher-item` | `next`, `previous` | `descendant` | `trigger` | Turns a descendant element into a remote control: clicking it shows the tab whose index, value, or href matches the attribute, or steps through enabled tabs with `next`/`previous`. |
| `data-toggle` | — | `descendant` | `config` | Triggers carrying `data-toggle` are skipped during tab detection; the attribute belongs to the dialog toggle system, not the switcher. |
| `data-value` | — | `descendant` | `read-only` | Identifies a tab for `show()` and `active` targeting; read from the trigger or its item, falling back to the trigger href or the tab index. |

## CSS Classes

| Class | Role | Applies to | Description |
| --- | --- | --- | --- |
| `.z-active` | `state` | `.z-switcher` | State class marking the active tab item, its trigger, and the active panel. |
| `.z-switcher` | `root` | `z-switcher` | Panel container: its direct children are the panels, and every child without `.z-active` is hidden. |

## Keyboard

| Keys | Action | When |
| --- | --- | --- |
| `ArrowRight` / `ArrowLeft` | Move focus to the next / previous enabled tab, wrapping at the ends; reversed in RTL. Also activates the tab unless `manual` is set. | Focus is on a tab in a horizontal tablist |
| `ArrowDown` / `ArrowUp` | Move focus to the next / previous enabled tab, wrapping at the ends. Also activates the tab unless `manual` is set. | Focus is on a tab in a vertical tablist |
| `Home` / `End` | Move focus to the first / last enabled tab. | Focus is on a tab |
| `Enter` / ` ` | Show the focused tab, or collapse it when it is already active and `collapsible` is set. | Focus is on a tab |

## Accessibility

> The tablist gets `role="tablist"` and `aria-orientation`; each trigger gets `role="tab"`, `aria-selected`, `aria-controls`, and a roving `tabindex` (0 on the focused tab, -1 on the rest). Disabled triggers get `aria-disabled`.

> Panels get `role="tabpanel"` and `aria-labelledby` pointing at their trigger; inactive panels are `hidden`. Tab item wrappers that have no role get `role="presentation"`.

## Notes

- The tablist is resolved from `[data-part="tablist"]` or `[role="tablist"]`, then `.z-tab`, `.z-tab-alt`, `.z-subnav`, or `.z-nav`, then the first layout child. Tabs are its direct children that are links, buttons, or `role="tab"` elements, or whose first direct child is one.
- The sliding indicator is driven by the `--z-tab-indicator-*` variables on `.z-tab` and `.z-tab-alt` tablists (see the Tab component) and re-measures on resize.
- Clicking a tab whose href is a real URL navigates normally when no panel containers are connected; hash links and connected panels always switch in place.

## Guides

- [Styling with cls and stl](https://next.0build.dev/docs/guides/styling.md)
- [Translating Components](https://next.0build.dev/docs/guides/i18n.md)
- [Icon Resolution](https://next.0build.dev/docs/guides/icons.md)
- [The data-source Convention](https://next.0build.dev/docs/guides/data-source.md)
- [Openers & Triggers](https://next.0build.dev/docs/guides/triggers.md)
- [Dialog Behavior](https://next.0build.dev/docs/guides/dialogs.md)
- [Utilities](https://next.0build.dev/docs/guides/utilities.md)
- [Runtime](https://next.0build.dev/docs/guides/runtime.md)

## Related

- [Tab](https://next.0build.dev/docs/components/tab.md)
