Skip to content

Data display

ThemeToggle

tsx
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

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

size"sm" | "md"

Control size. Defaults to "md".

labelsPartial<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".

aria-labelstring

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

HookValues
.cherry-theme-toggleRoot element
data-valuesystem, light, dark (the current preference)
data-sizesm, 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.