Skip to Content
ComponentsThemeToggle

ThemeToggle

A controlled button for switching between light and dark themes. Animates between sun and moon icons with a crossfade, and respects prefers-reduced-motion.

Import

import { ThemeToggle } from "chunks-ui";

Basic Usage

ThemeToggle is fully controlled — pass the current theme and an onClick handler to toggle it:

function App() { const [theme, setTheme] = useState<"light" | "dark">("light"); return ( <ThemeToggle theme={theme} onClick={() => setTheme(theme === "light" ? "dark" : "light")} /> ); }
Current: light

Custom Icons

Replace the default sun/moon icons via lightIcon and darkIcon:

<ThemeToggle theme={theme} onClick={() => setTheme(theme === "light" ? "dark" : "light")} lightIcon={<SunIcon />} darkIcon={<MoonIcon />} />
Custom icons

With a Tooltip

ThemeToggle forwards its ref and props to the <button>, so it works as a Tooltip.Trigger render target:

<Tooltip.Root> <Tooltip.Trigger render={<ThemeToggle theme={theme} onClick={() => setTheme(theme === "light" ? "dark" : "light")} />} /> <Tooltip.Portal> <Tooltip.Positioner> <Tooltip.Popup>Toggle theme</Tooltip.Popup> </Tooltip.Positioner> </Tooltip.Portal> </Tooltip.Root>

Props

NameTypeDefault
theme"light" | "dark"

Current active theme.

onClickMouseEventHandler<HTMLButtonElement>

Called when the toggle button is clicked.

lightIconReactNode

Custom icon shown when the theme is light.

darkIconReactNode

Custom icon shown when the theme is dark.

labelsPartial<ThemeToggleLabels>

Replaces the built-in English strings, e.g. to translate them. Keys you leave out keep the default from DEFAULT_THEME_TOGGLE_LABELS. aria-label takes precedence.

All other <button> props, including ref, are forwarded to the underlying <button> element. Pass aria-label to replace the default name (“Switch to dark mode” / “Switch to light mode”) in both themes, or labels to replace each one.

Labels

labels replaces the built-in English text, for example to translate it. Keys you leave out keep their default from DEFAULT_THEME_TOGGLE_LABELS. aria-label takes precedence. See Labels.

NameTypeDefault
switchToDarkstring

Name of the button while the theme is light.

"Switch to dark mode"
switchToLightstring

Name of the button while the theme is dark.

"Switch to light mode"
Last updated on