Theme Toggle
NewLight, dark and system switch as a segmented control or one animated icon button. Works with any theme setup.
Preview: System (light)
Installation
$ pnpm dlx shadcn@latest add @synccn/theme-toggleFirst time in this project? Connect synccn with one command.
Usage
import { ThemeToggle, useResolvedTheme } from "@/components/ui/theme-toggle";<ThemeToggle value={theme} onValueChange={setTheme} />API reference
Generated from the component's TypeScript types, so it always matches the code.
ThemeToggle
Light, dark and system switch, as a three-way segmented control or a single animated icon button. Works with any theme setup; with `apply` it sets the `dark` class itself.
- value
"light" | "dark" | "system"Controlled theme.
- defaultValue
"light" | "dark" | "system"Default
"system"Theme at first, when uncontrolled.
- onValueChange
(theme: "light" | "dark" | "system") => voidCalled with the chosen theme.
- variant
"segmented" | "icon"Default
"segmented""segmented" shows light, dark and system; "icon" is one button that flips between light and dark.
- apply
booleanDefault
falseToggles the `dark` class on <html> to match. Turn off when your app applies the theme itself (next-themes and friends).
- className
stringExtra classes for the root element.
useResolvedTheme
Resolves "system" to light or dark using the operating system's setting.
Takes no props.
Dependencies
Read from the component's imports when the site builds. The CLI installs all of these for you.
Helpers
npm packages
- clsx for cn()
- tailwind-merge for cn()
- lucide-react for Monitor, Moon, Sun
Keyboard
- ←or→Pick light, dark or system
Accessibility
- The segmented version is a radiogroup with one tab stop; the icon button says which theme it switches to.
From the source
Read from this component's code when the site builds, so it can't drift.
- ARIA roles: radio, radiogroup
- ARIA attributes: aria-checked, aria-label
- Keys handled: ArrowUp, ArrowDown, ArrowLeft, ArrowRight
- Shows a visible focus ring for keyboard users