# ThemeToggle

> A System, Light and Dark segmented switch that sets and stores the color mode preference.

Source: https://docs.cherry.al/components/theme-toggle

> For the complete documentation index, see [llms.txt](https://docs.cherry.al/llms.txt).

# ThemeToggle

<Preview bleed>
<iframe className="light-only" src="https://v2.cherry.al/preview/theme-toggle?theme=light" title="ThemeToggle preview" loading="lazy" style={{ width: "100%", height: "240px" }} />
<iframe className="dark-only" src="https://v2.cherry.al/preview/theme-toggle?theme=dark" title="ThemeToggle preview" loading="lazy" style={{ width: "100%", height: "240px" }} />

```tsx
import { ThemeToggle } from "@cherry-design-system/react";

export function Header() {
  return (
    <header>
      <ThemeToggle />
    </header>
  );
}
```
</Preview>

ThemeToggle is a three-way switch between System, Light and Dark color modes. It reads and sets the same preference as `useColorMode()`, which is stored in `localStorage` and mirrored to a cookie. See [Dark mode](/dark-mode) for the init script that applies the mode before the first paint.

## Examples

### Small

```tsx
import { ThemeToggle } from "@cherry-design-system/react";

export function CompactToggle() {
  return <ThemeToggle size="sm" />;
}
```

### Translated labels

```tsx
import { ThemeToggle } from "@cherry-design-system/react";

export function GermanToggle() {
  return (
    <ThemeToggle
      aria-label="Farbmodus"
      labels={{ system: "System", light: "Hell", dark: "Dunkel" }}
    />
  );
}
```

### Your own control

To build a different control, read and set the preference with the hook.

```tsx
"use client";

import { Button, useColorMode } from "@cherry-design-system/react";

export function ModeButton() {
  const { mode, toggle } = useColorMode();
  return (
    <Button variant="ghost" onClick={toggle}>
      {mode === "dark" ? "Light mode" : "Dark mode"}
    </Button>
  );
}
```

## Properties

<Field value="size" type='"sm" | "md"'>
  Control size. Defaults to `"md"`.
</Field>

<Field value="labels" type="Partial<Record<ColorModePreference, string>>">
  Names of the three options (`system`, `light`, `dark`). The options show icons, so these are their accessible names and tooltips. Translate here. Defaults to "System", "Light" and "Dark".
</Field>

<Field value="aria-label" type="string">
  Group label. Defaults to `"Color mode"`.
</Field>

ThemeToggle renders a `<div role="radiogroup">` with three option buttons, forwards its ref to the group and passes every other `<div>` attribute through except `onChange`. It is a client component.

## Styling

| Hook | Values |
| --- | --- |
| `.cherry-theme-toggle` | Root element |
| `data-value` | `system`, `light`, `dark` (the current preference) |
| `data-size` | `sm`, `md` |
| Parts | `.cherry-theme-toggle__indicator`, `.cherry-theme-toggle__option` |

The track reads `--cherry-bg-segment`, the selected indicator `--cherry-bg-segment-selected` and `--cherry-elevation-xs`.

## Accessibility

A radiogroup with one tab stop: arrow keys move and change the mode. Each option is labelled (System, Light, Dark); override the words with `labels`.

<Button href="https://github.com/cherry-design-system/react/tree/main/src/components/theme-toggle" icon="code" iconPosition="left">
  View Source
</Button>
