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

Props

PropTypeDefaultDescription
theme"light" | "dark"requiredCurrent active theme
onClickReact.MouseEventHandler<HTMLButtonElement>Called when the button is clicked
lightIconReact.ReactNodeBuilt-in sun SVGIcon shown in light mode
darkIconReact.ReactNodeBuilt-in moon SVGIcon shown in dark mode
classNamestringAdditional CSS classes
Last updated on