Skip to Content
zjkm-ui
Components

Theme Toggle

A sun and moon toggle for light and dark.

Props
<ThemeToggle />

Installation

npx shadcn@latest add https://ui.zjkm.tech/r/theme-toggle.json
import { 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

PropTypeDefaultDescription
darkbooleanControls the state.
defaultDarkbooleanfalseStarting state.
onChange(dark: boolean) => voidCalled when it flips.
classNamestringClasses for the button.

With reduced motion, the sun and moon swap without animating.

On this page