Components
Theme Toggle
A sun and moon toggle for light and dark.
<ThemeToggle />Installation
npx shadcn@latest add https://ui.zjkm.tech/r/theme-toggle.jsonimport { ThemeToggle } from "@/components/theme-toggle"Using JavaScript? Set tsx: false in components.json and the CLI converts it.
New to shadcn? Run npx shadcn@latest init first. The CLI needs components.json and the @/ import alias.
Usage
A sun that turns into a moon. It reports the choice through onChange and leaves applying the theme to you, so it works with any theming setup.
import { ThemeToggle } from "@/components/theme-toggle";
import { useTheme } from "next-themes";
export function Header() {
const { resolvedTheme, setTheme } = useTheme();
return <ThemeToggle dark={resolvedTheme === "dark"} onChange={(dark) => setTheme(dark ? "dark" : "light")} />;
}Props
| Prop | Type | Default | Description |
|---|---|---|---|
dark | boolean | Controls the state. | |
defaultDark | boolean | false | Starting state. |
onChange | (dark: boolean) => void | Called when it flips. | |
className | string | Classes for the button. |
With reduced motion, the sun and moon swap without animating.