# Theme switcher

Lets people choose system, light or dark theme, stored per device and applied before first paint.

- Family: action
- Import: `import { ThemeSwitcher } from "@senvenli/react/action";`
- Page: https://design.senvenli.com/docs/components/theme-switcher
- JSON: https://design.senvenli.com/docs/components/theme-switcher.json

## Example: In a site header

System, light or dark. It stores the choice and applies it to the page, in step with every other toggle.

```tsx
import { ThemeSwitcher } from "@senvenli/react/action";

export default function Example() {
  return <ThemeSwitcher />;
}
```

## Examples

### As a list

The same choice as three rows with a check, for a phone menu.

```tsx
import { ThemeSwitcher } from "@senvenli/react/action";

export default function Example() {
  return (
    <div className="max-w-xs">
      <ThemeSwitcher variant="list" />
    </div>
  );
}
```

### Controlled

With `value`, the parent owns the theme: the toggle stores and applies nothing.

```tsx
import { ThemeSwitcher, type ThemeChoice } from "@senvenli/react/action";
import { Text } from "@senvenli/react/display";
import { Stack } from "@senvenli/react/layout";
import { useState } from "react";

export default function Example() {
  const [choice, setChoice] = useState<ThemeChoice>("system");
  return (
    <Stack direction="horizontal" space="space-150">
      <ThemeSwitcher value={choice} onChange={setChoice} />
      <Text tone="subtle">Chosen: {choice}</Text>
    </Stack>
  );
}
```

## Guidelines

### When to use it

- An app or site offers light and dark themes.

### Use something else when

- [switch](https://design.senvenli.com/docs/components/switch.md): Only two themes exist and the system option is not offered.
- [menu](https://design.senvenli.com/docs/components/menu.md): The theme choice sits among other account settings.

## Accessibility

Keyboard:

- Arrow keys: Moves between and selects the options.

Built in:

- One tab stop; a radio group named "Theme".
- System says which theme is in effect now.

## API reference

### ThemeSwitcher

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| value | `ThemeChoice` |  | no | Controlled choice; leave out to let the toggle manage, store and apply it. |
| onChange | `(choice: ThemeChoice) => void` |  | no |  |
| labels | `Partial<ThemeLabels>` |  | no | Localized names; the locale's built-in strings by default. |
| storageKey | `string` |  | no |  |
| variant | `"list" \| "menu" \| "segmented"` | `"segmented"` | no | `segmented`: three icon buttons. `menu`: one icon button (the theme in effect) that opens System / Light / Dark, the compact form for a busy header. `list`: a three-option list with a check, for the phone menu. |

### Tokens it reads

`--ds-background-neutral`, `--ds-background-neutral-bold`, `--ds-background-neutral-pressed`, `--ds-background-neutral-subtle-hovered`, `--ds-background-segment-selected`, `--ds-border-focused`, `--ds-icon`, `--ds-icon-subtle`, `--ds-radius-control-medium`, `--ds-radius-large`, `--ds-radius-medium`, `--ds-radius-small`, `--ds-shadow-overlay`, `--ds-shadow-raised`, `--ds-size-control`, `--ds-space-050`, `--ds-space-100`, `--ds-space-150`, `--ds-surface-overlay`, `--ds-text`, `--ds-text-inverse`, `--ds-text-subtle`
