Theme Toggle
Theme Toggle
Switch between a sun and crescent moon inside one circular button.
Dependencies
React
Installation
Add this component with your preferred package manager.
pnpm dlx shadcn@4.21.5 add @cavemio/theme-toggleFirst install? Configure @cavemio once
Run npx shadcn@4.21.5 init if shadcn is not set up, then merge this entry into the registries field in your existing components.json. Keep your other settings and registries.
components.json
{
"registries": {
"@cavemio": "https://registry.cavemio.dev/r/cavemio-{name}.json"
}
}Usage
The reference demo synchronizes icon, pill, and segmented controls on its own card, including a circular reveal. Selecting System follows the operating system. The website theme stays independent. Existing theme/onThemeChange/pageTransition integrations remain supported.
"use client";
import { useState } from "react";
import { ThemeToggle } from "@/components/theme-toggle";
// Your CSS must define light/dark colors for this attribute.
export function AppearanceToggle() {
const [theme, setTheme] = useState<"light" | "dark">("light");
return (
<ThemeToggle
theme={theme}
pageTransition="circular"
onThemeChange={(next) => {
document.documentElement.dataset.theme = next;
setTheme(next);
}}
/>
);
}Props
| Prop | Type | Description |
|---|---|---|
| theme | "light" | "dark" | Controlled appearance; pair with onThemeChange. |
| defaultTheme | "light" | "dark" | Initial uncontrolled appearance. Defaults to light. |
| onThemeChange | (theme: "light" | "dark") => void | Receives the next appearance when activated. The host owns document theme and persistence. |
| pageTransition | "none" | "circular" | Defaults to none. Circular opts into a 620ms document reveal and keeps button scale stable. onThemeChange must apply theme styles synchronously; reduced motion or unavailable transitions switch immediately. |
| size | number | Button diameter in pixels. Defaults to 48, with a minimum 44px hit area. |
| disabled | boolean | Disables pointer and keyboard activation. |
| className / style | string / CSSProperties | Customize the button surface and dimensions. |
| aria-label | string | Override the default destination label. Native button props are forwarded. |
| nonce | string | Optional CSP nonce for the temporary transition stylesheet. Use your server-generated allowed style nonce. |
| variant | "icon" | "pill" | "segmented" | Opt into the reference control API and appearance. Defaults to icon when the reference API is selected. |
| value / defaultValue | "light" | "dark" | "system" | Controlled or initial appearance for the reference variants. Pair value with onValueChange. |
| onValueChange | (theme: "light" | "dark" | "system") => void | Receives the reference variant selection. A controlled host applies the theme. |
| storageKey / attribute / reveal | string / "class" | "data-theme" / boolean | Reference variants: uncontrolled persistence key, document theme marker, and optional circular reveal. See the reference variant documentation for defaults. |
Accessibility & motion
Native button with a destination label, pressed state, keyboard activation, visible focus, disabled behavior, and live reduced-motion support.
pnpm dlx shadcn@4.21.5 add @cavemio/theme-toggle