ThemeToggle
import { ThemeToggle } from "@cherry-design-system/react";
export function Header() {
return (
<header>
<ThemeToggle />
</header>
);
}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 for the init script that applies the mode before the first paint.
Examples
Small
import { ThemeToggle } from "@cherry-design-system/react";
export function CompactToggle() {
return <ThemeToggle size="sm" />;
}Translated labels
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.
"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
Control size. Defaults to "md".
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".
Group label. Defaults to "Color mode".
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.