GooseUI

Components

Theme Toggle

New

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

npx shadcn@latest add @gooseui/theme-toggle
pnpm dlx shadcn@latest add @gooseui/theme-toggle
yarn dlx shadcn@latest add @gooseui/theme-toggle
bunx --bun shadcn@latest add @gooseui/theme-toggle

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

PropTypeDefaultDescription
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.
sizenumber24Icon size in pixels.
classNamestring-Additional CSS classes. Other Button props are passed to the button.