# ThemeSwitcher

Crate: `libero`
Import: `use libero::components::ThemeSwitcher;`
Feature: `theme-switcher` (off by default, turns on `menu`, `action-icon`, `use-color-scheme`; [features.md](features.md))
Source: <https://github.com/tdymel/libero/tree/main/libero/src/components/buttons/theme_switcher.rs>
Index: [index.md](index.md) lists every other page
Description: An icon button that flips the colour scheme, optionally through the system's too, with an optional theme picker beside it.

An icon button that switches the app's colour scheme. It starts on the
system's scheme, and each press flips to the other one. Flipping back to the
system's scheme follows the system again, so the app never stays pinned. The
icon shows where the next press goes: a sun for light, a moon for dark.

## Usage

```rust
use dioxus::prelude::*;
use libero::components::ThemeSwitcher;

#[component]
fn Demo() -> Element {
    rsx! {
        ThemeSwitcher {}
    }
}
```

## Props

| Prop | Type | Default | Description |
|---|---|---|---|
| `variant` | `Responsive<Variant>` | `outlined` | Visual style, as on `ActionIcon`. Or one per breakpoint, `variant: responsive(Variant::Outlined).md(Variant::Filled)`. |
| `color` | `ThemeAwareValue` | `muted` | Accent color. A theme color name or any CSS color. |
| `size` | `Responsive<ThemeAwareValue>` | `md` | Button size. The icon takes 55% of it. Or one per breakpoint, `size: responsive(Size::Sm).md(Size::Lg)`. |
| `radius` | `Responsive<ThemeAwareValue>` | `sm` | Corner radius, independent of `size`. Or one per breakpoint, `radius: responsive(Size::Sm).md(Size::Xl)`. |
| `themes` | `&'static [&'static ThemeSet]` | - | Adds the theme picker, a second button that opens a menu of these sets. The app persists the picked set: Libero stores only the scheme and the direction. `class`, `sx` and extra attributes then land on the group around both. |
| `with_system` | `bool` | `false` | Adds following the system to the cycle, as a step of its own with a half-filled disc: system, the scheme the system is not showing, the one it is, and back. Following, an OS change applies at once; a picked scheme stays until the next press. Off, a press flips between light and dark, and flipping to the system's own scheme follows the system again. For your own control, such as a menu of all three choices, build on `use_color_scheme()`: it reads the setting and what it resolves to (a `libero::theme::ColorScheme`), and `set`, `toggle` and `cycle` change it. |
| `label` | `Callback<ColorSchemeSetting, String>` | - | Replaces the three built-in button names. Gets the scheme a press shows next, always `Light` or `Dark` without `with_system` (even when the press stores `System`), and returns what the press does. |
| `disabled` | `bool` | `false` | Disables and dims the toggle and, with `themes`, the picker and its menu. |
| `parts` | `Parts<ThemeSwitcherPart>` | - | Styles for the inner parts in the Style API tab, under `sx`. `Toggle`, `Picker` and `Chevron` exist with `themes` only. |
| `menu_parts` | `Parts<MenuPart>` | - | The theme-set menu's `parts`, the `Menu` page's Style API table. The menu opens in a portal, out of `sx` and `parts`. |

Like every component, `ThemeSwitcher` also takes the shared props `sx`,
`class`, `style`, `states`, `hidden_from`, `visible_from`, and any extra HTML attributes.

## Style API

Without `themes` the toggle is the root itself, styled by `sx`; `Toggle`, `Picker` and `Chevron` exist only with `themes`. The theme-set menu is portaled, out of `parts`' reach: style it with `menu_parts`, which takes the [`Menu`](menu.md) parts.

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 |
|---|---|---|
| `ThemeSwitcherPart::Icon` | `icon` | The sun, moon or system glyph. |
| `ThemeSwitcherPart::Toggle` | `toggle` | The scheme button beside the picker. |
| `ThemeSwitcherPart::Picker` | `picker` | The button that opens the theme-set menu. |
| `ThemeSwitcherPart::Chevron` | `chevron` | The picker's chevron. |

## Accessibility

### Libero handles

- The button's name says what a press does, from the localization's
  `ThemeSwitcherLabels`: `to_light` and `to_dark`, plus `to_system` with `with_system`.
- With `themes`, both buttons sit in a `role="group"` named by `group`. The
  picker is named by `picker` and opens a [`Menu`](menu.md), with its keys; the
  sets are radio items in a group named by `themes`.

### You must

- With `label`, return what the press does, not the current scheme.

### Example

A `ThemeSwitcher {}` in the header while the page is light: its name says what
a press does, switch to dark, not that the page is light now.

## Theme defaults

`ThemeSwitcherDefaults` on the theme, as `theme_switcher`.

| Field | Type | Description |
|---|---|---|
| `variant` | `Variant` | Default `variant` when the prop is omitted (`outlined`). |
| `color` | `Color` | Default `color` when the prop is omitted (`muted`). |

The names are `ThemeSwitcherLabels` in the localization: `to_light`,
`to_dark`, `to_system` (the toggle's names), `group`, `picker` and `themes`
(the picker's).

## CSS variables

None of its own; it renders an `ActionIcon`, whose variables apply.

## Data attributes

`ActionIcon`'s, on the root's `data-state`.
