# Tabs

Crate: `libero`
Import: `use libero::components::{OptionLabel, Options, Tabs};`
Feature: `tabs` (off by default, turns on `use-id`, `use-icon`, `visually-hidden`; [features.md](features.md))
Source: <https://github.com/tdymel/libero/tree/main/libero/src/components/navigation/tabs>
Index: [index.md](index.md) lists every other page
Description: One strip of tabs over an enum, with only the selected tab's panel built.

A strip of tabs over an enum, with the selected tab's panel below it.
`#[derive(Options)]` lists and names the tabs. `panel` matches on the same enum,
so a missing tab does not compile. Only the selected panel is built.

## Usage

The enum is the tab strip. `#[option(label = ..)]` renames a variant.

```rust
use dioxus::prelude::*;
use libero::components::{Options, Tabs};

#[derive(Clone, PartialEq, Options)]
enum Section {
    Account,
    #[option(label = "Admin area")]
    Admin,
    Billing,
}

#[component]
fn Demo() -> Element {
    let mut section = use_signal(|| Section::Account);

    rsx! {
        Tabs {
            aria_label: "Settings",
            value: section(),
            onchange: move |next| section.set(next),
            panel: |section: Section| match section {
                Section::Account => rsx! { "Account settings" },
                Section::Admin => rsx! { "Admin area" },
                Section::Billing => rsx! { "Billing details" },
            },
        }
    }
}
```

## Props

### Tabs

| Prop | Type | Default | Description |
|---|---|---|---|
| `value` | `T` | required | The selected tab. Pair it with `onchange`. |
| `onchange` | `EventHandler<T>` | - | Called with the tab that should become selected. Without it the selection never changes, and `Tabs` warns. |
| `panel` | `Callback<T, Element>` | - | The body of the selected tab. Only the selected panel is built. Without it nothing shows below the strip, and `Tabs` warns. |
| `options` | `OptionSource<T>` | `T::options()` | The tabs to show. A `Vec<T>` converts, and an `OptionList<T>` can disable single tabs. Groups draw flattened. A source still loading draws no tabs. |
| `option_label` | `Callback<T, OptionLabel>` | `T::label()` | Overrides a tab's label. Runs during render, so the strip follows a locale signal. `OptionLabel::with_icon` puts an icon before the name, `OptionLabel::rich` draws a tab as rsx and takes a text name for screen readers. |
| `size` | `Responsive<Size>` | `md` | Tab strip size. On a touch screen a tab is at least 44px tall, so `xs` and `sm` tabs draw taller there. Or one per breakpoint, `size: responsive(Size::Sm).md(Size::Md)`. |
| `color` | `ThemeAwareValue` | `primary` | Indicator and selected-label color. |
| `full_width` | `bool` | `false` | Tabs grow to fill the row, never below their label. A crowded strip still scrolls. Not themed. |
| `activation` | `TabsActivation` | `automatic` | `Automatic` selects as the arrows move. `Manual` moves only the focus, and Enter or Space selects. Use it for slow panels. |
| `placement` | `Responsive<TabsPlacement>` | `top` | Vertical tabs: `start` or `end` put the strip in a column beside the panel, moved by Up and Down. `start` is the inline start side (left in left-to-right text), `end` the inline end side. The tablist has `aria-orientation="vertical"`, and the selected tab's indicator sits on the edge facing the panel. Or one per breakpoint, `placement: responsive(TabsPlacement::Top).md(TabsPlacement::Start)`: the layout switches with the window from the first paint, while the arrow keys and `aria-orientation` follow after mount when the page was rendered on a server. |
| `panel_focusable` | `bool` | `true` | Whether the selected panel is a tab stop, as the APG asks when it holds no focusable content. Turn it off for a panel with its own controls. Not themed. |
| `parts` | `Parts<TabsPart>` | - | Styles for the inner parts in the Style API tab, under `sx`. |

`Tabs` also takes the shared props `sx`, `class`, `states`, `hidden_from`, `visible_from`, and any extra HTML
attributes.

### OptionLabel

| Field | Type | Default | Description |
|---|---|---|---|
| `name` | `String` | required | The tab's visible text and accessible name. |
| `content` | `Element` | - | Drawn in place of the name, via `OptionLabel::rich`, or an icon and the name via `OptionLabel::with_icon`. `name` still names the tab. Keep it inline: an `Icon` fits, a `Flex` does not. |

`OptionLabel` is a value, not a component, and takes no shared props. A string
converts into one (`"Konto".into()`).

## Style API

Style a part with the `parts` prop, or address it as `[data-slot='…']` in your
own CSS. The names are stable. [Style API in Styling](styling.md#style-api)
explains how parts work.

| Part | `data-slot` | Description |
|---|---|---|
| `TabsPart::List` | `list` | The `tablist` strip. |
| `TabsPart::Tab` | `tab` | One tab. The selected one has `aria-selected="true"`. |
| `TabsPart::Panel` | `panel` | The selected tab's panel. |

## Accessibility

### Keyboard

| Key | Action |
|---|---|
| `Tab` | Enters the strip at the selected tab, the only one in the tab order. |
| `Left` or `Right` | On a horizontal strip, moves to the previous or next tab, skipping disabled ones. With `Automatic` activation it selects the tab too; `Manual` moves only the focus. On a vertical strip they do nothing. |
| `Up` or `Down` | On a vertical strip (`placement: start` or `end`), moves to the previous or next tab, with the same activation rule. On a horizontal strip they scroll the page. |
| `Home` or `End` | Jumps to the first or last tab. With `Automatic` activation it selects the tab too; `Manual` moves only the focus. |
| `Enter` or `Space` | With `activation: TabsActivation::Manual`, where the arrows move only the focus: selects the focused tab. |

### Libero handles

- Only the selected tab is in the tab order. The selected panel is a tab stop
  too, so content that cannot take focus stays reachable by keyboard;
  `panel_focusable: false` drops it for a panel that holds focusable content.
- `aria_label` and `aria_labelledby` land on the tablist, not the root. A
  vertical tablist has `aria-orientation="vertical"`.
- In a strip too wide for its box, the selected tab scrolls into view, also
  when `value` changes from outside.

### You must

- Name the strip with `aria_label` or `aria_labelledby`. Without either it
  warns in debug builds.
- If you remove the focused tab from `options`, move the focus back to the
  strip yourself.

### Example

A settings strip, `Tabs { aria_label: "Settings" }`: Tab lands on the selected
tab, the arrows move and select, and Home and End jump to the first and last
tab.

## Theme defaults

`TabsDefaults` on the theme. Per-size values live in its `sizes` scale.

| Field | Type | Description |
|---|---|---|
| `size` | `Size` | Default `size` when the prop is omitted. |
| `color` | `Color` | Default `color` when the prop is omitted. |
| `activation` | `TabsActivation` | Default `activation` when the prop is omitted. |
| `sizes` | `Sizes<TabsSizeLevel>` | `font_size`, `padding_x`, `padding_y`, `indicator`, `icon_gap` per size. |
| `border_color` | `ColorValue` | The line the whole strip sits on. |
| `hover_color` | `Option<ColorValue>` | Background of an unselected tab while hovered. `None`, the default, takes the theme's row hover tint: the first muted step that shows at 1.15:1 on the page and the card. |

## CSS variables

| Variable | Description |
|---|---|
| `--lsx-tabs-font-size-<size>` | `font-size` for that size step. |
| `--lsx-tabs-padding-x-<size>` | Horizontal tab padding for that size step. |
| `--lsx-tabs-padding-y-<size>` | Vertical tab padding for that size step. |
| `--lsx-tabs-indicator-<size>` | Thickness of the selected tab's underline. |
| `--lsx-tabs-icon-gap-<size>` | Gap between a rich label's icon and its text. |
| `--lsx-tabs-pad-x` | The picked step's horizontal padding, resolved on the strip so each tab inherits it. |
| `--lsx-tabs-pad-y` | The picked step's vertical padding. |
| `--lsx-tabs-line` | The picked step's indicator thickness. |
| `--lsx-tabs-gap` | The picked step's icon gap. |
| `--lsx-tabs-border-color` | Color of the strip's 1px line. |
| `--lsx-tabs-hover` | Hover background of an unselected tab. |
| `--lsx-tabs-color` | Indicator and selected-label color, from the `color` prop. |

## Data attributes

State tokens on the root's `data-state`, space separated.

| Token | Condition |
|---|---|
| `size-<size>` | The `size` in effect. |
| `full-width` | `full_width` is set. |
| `vertical` | `placement` is `start` or `end`: the strip is a column beside the panel. |
| `placement-start` | `placement` is `start`. |
| `placement-end` | `placement` is `end`. |
| `<from>-<until>:placement-<placement>` | A responsive `placement`, one word per viewport range, such as `md-:placement-start`. It replaces `vertical` and `placement-*`. |

The tabs carry no `data-state`. Style them through `[aria-selected="true"]`
and `[aria-disabled="true"]`.
