Theme Toggle is a button that switches between light and dark themes with a sun and moon animation. The component knows nothing about a theme provider: it takes the current theme in theme and reports a change through onThemeChange. That lets you wire it to next-themes or any store of your own. It is built on Button, the icon size comes from the size prop, and the animation turns off under prefers-reduced-motion.
Preview
Installation
Usage
import { useTheme } from "next-themes"
import { ThemeToggle } from "@/components/ui/theme-toggle"
const { resolvedTheme, setTheme } = useTheme()
<ThemeToggle
theme={resolvedTheme === "dark" ? "dark" : "light"}
onThemeChange={setTheme}
/>Examples
Basic
const { resolvedTheme, setTheme } = useTheme()
const theme = resolvedTheme === "dark" ? "dark" : "light"
<ThemeToggle theme={theme} onThemeChange={setTheme} />With a label
Light
<div className="flex items-center gap-2">
<ThemeToggle theme={theme} onThemeChange={setTheme} />
<span className="text-sm">{theme === "dark" ? "Dark" : "Light"}</span>
</div>Props
| Prop | Type | Default | Description |
|---|---|---|---|
| theme | "light" | "dark" | undefined | - | Current theme. You can omit it before mount: the icon then follows the .dark class on an ancestor, with no animation. |
| onThemeChange | (theme: "light" | "dark") => void | - | Called on click with the theme to switch to. Required. |
| size | number | 24 | Icon size in pixels. |
| className | string | - | Additional CSS classes. Other Button props are passed to the button. |