Pattern
Theme toggle
Controlled theme controls with a three-value switch, binary switch, cycle button, and dropdown.
Composition
Three-way choice
- <ThemeSegmentedSwitch />
Binary choice
- <ThemeSwitch>
- <Switch />
Cycle button
- <ThemeToggle>
- <Button />
Dropdown choice
- <ThemeDropdown>
- <DropdownMenu>
- <DropdownMenuTrigger />
- <DropdownMenuContent>
- <DropdownMenuRadioGroup>
- <DropdownMenuLabel />
- <DropdownMenuRadioItem />
- <DropdownMenuRadioGroup>
- <DropdownMenu>
Installation
npx shadcn@latest add https://control-ui.dev/r/theme-toggle-block.jsonUsage
"use client";
import { type ThemeMode, ThemeSegmentedSwitch } from "@/components/control-ui/blocks/theme-toggle";
type ThemeAdapter = { theme: string | undefined; setTheme: (value: string) => void;};
function toThemeMode(value: string | undefined): ThemeMode { if (value === "light" || value === "dark" || value === "system") return value; return "system";}
export function AppearanceControl({ theme, setTheme }: ThemeAdapter) { return <ThemeSegmentedSwitch value={toThemeMode(theme)} onValueChange={(value) => setTheme(value)} />;}
Library dependencies
Owned source
This template’s recipe and private support files. Public dependencies stay linked above.
Block recipe
src/registry/blocks/control-ui/theme-toggle.tsx"use client";
import { ChevronDownIcon, MonitorIcon, MoonIcon, SunIcon } from "lucide-react";import type { ComponentProps, ComponentType, CSSProperties } from "react";import { useId } from "react";import { cn } from "@/components/control-ui/lib/cn";import { Button } from "@/components/control-ui/ui/button";import { DropdownMenu, DropdownMenuContent, DropdownMenuLabel, DropdownMenuRadioGroup, DropdownMenuRadioItem, DropdownMenuTrigger,} from "@/components/control-ui/ui/dropdown-menu";import { Switch } from "@/components/control-ui/ui/switch";import { Text } from "@/components/control-ui/ui/typography";
export type ThemeMode = "light" | "dark" | "system";
export type ThemeToggleOption = { value: ThemeMode; label: string; icon: ComponentType<{ className?: string; "aria-hidden"?: boolean }>; disabled?: boolean;};
type ThemeControlProps = { value: ThemeMode; onValueChange: (value: ThemeMode) => void; options?: readonly ThemeToggleOption[];};
export type ThemeToggleProps = Omit<ComponentProps<typeof Button>, "value" | "onChange"> & ThemeControlProps & { showLabel?: boolean; };
export type ThemeSwitchProps = Omit<ComponentProps<typeof Switch>, "checked" | "defaultChecked" | "onCheckedChange" | "value"> & { value: ThemeMode; onValueChange: (value: ThemeMode) => void; onValue?: ThemeMode; offValue?: ThemeMode;};
export type ThemeSegmentedSwitchProps = Omit<ComponentProps<"div">, "onChange"> & ThemeControlProps & { name?: string; showLabels?: boolean; disabled?: boolean; };
export type ThemeDropdownProps = Omit<ComponentProps<typeof DropdownMenuTrigger>, "children" | "value" | "onChange"> & ThemeControlProps & { label?: string; };
const systemThemeOption = { value: "system", label: "System", icon: MonitorIcon } satisfies ThemeToggleOption;
const defaultThemeOptions: readonly ThemeToggleOption[] = [ { value: "light", label: "Light", icon: SunIcon }, { value: "dark", label: "Dark", icon: MoonIcon }, systemThemeOption,];
const defaultSwitchCheckedIcon = <MoonIcon />;const defaultSwitchUncheckedIcon = <SunIcon />;
function enabledOptions(options: readonly ThemeToggleOption[]) { return options.filter((option) => !option.disabled);}
function controlOptions(options: readonly ThemeToggleOption[]) { return options.length > 0 ? options : defaultThemeOptions;}
function currentThemeOption(value: ThemeMode, options: readonly ThemeToggleOption[]) { return ( options.find((option) => option.value === value) ?? defaultThemeOptions.find((option) => option.value === value) ?? systemThemeOption );}
function effectiveThemeValue(value: ThemeMode, options: readonly ThemeToggleOption[]) { if (options.some((option) => option.value === value)) return value; return enabledOptions(options)[0]?.value ?? options[0]?.value ?? "system";}
function nextThemeValue(value: ThemeMode, options: readonly ThemeToggleOption[]) { const available = enabledOptions(options); if (available.length === 0) return value;
const currentIndex = available.findIndex((option) => option.value === value); return available[(currentIndex + 1) % available.length]?.value ?? available[0]?.value ?? value;}
export function ThemeSwitch({ value, onValueChange, onValue = "dark", offValue = "light", checkedIcon, uncheckedIcon, "aria-label": ariaLabel, ...props}: ThemeSwitchProps) { return ( <Switch checked={value === onValue} checkedIcon={checkedIcon ?? defaultSwitchCheckedIcon} uncheckedIcon={uncheckedIcon ?? defaultSwitchUncheckedIcon} onCheckedChange={(checked) => onValueChange(checked ? onValue : offValue)} aria-label={ariaLabel ?? `${currentThemeOption(onValue, defaultThemeOptions).label} mode`} {...props} /> );}
export function ThemeSegmentedSwitch({ value, onValueChange, options = defaultThemeOptions, showLabels = false, disabled = false, name, className, style, "aria-label": ariaLabel = "Theme", ...props}: ThemeSegmentedSwitchProps) { const generatedName = useId(); const choices = controlOptions(options).map((option) => (disabled ? { ...option, disabled: true } : option)); const currentValue = effectiveThemeValue(value, choices); const activeIndex = Math.max( 0, choices.findIndex((option) => option.value === currentValue), ); const indicatorStyle = { "--_theme-toggle-index": activeIndex, width: `calc((100% - var(--_theme-toggle-padding) * 2) / ${choices.length})`, } satisfies CSSProperties & Record<"--_theme-toggle-index", number>;
return ( <div role="radiogroup" aria-label={ariaLabel} aria-disabled={disabled || undefined} data-control-ui="theme-toggle" data-control-family="theme-toggle" data-slot="root" className={cn("relative isolate inline-grid w-fit shrink-0 auto-cols-fr grid-flow-col", className)} style={style} {...props} > <span aria-hidden data-control-ui="theme-toggle" data-control-family="theme-toggle" data-slot="indicator" className="pointer-events-none absolute inset-y-(--_theme-toggle-padding) start-(--_theme-toggle-padding) translate-x-[calc(var(--_theme-toggle-index)*100%)] rtl:translate-x-[calc(var(--_theme-toggle-index)*-100%)]" style={indicatorStyle} /> {choices.map((option) => { const selected = option.value === currentValue; const Icon = option.icon;
return ( <label key={option.value} data-control-ui="theme-toggle" data-control-family="theme-toggle" data-slot="option" data-control="true" data-selected={selected ? "true" : undefined} data-disabled={option.disabled ? "true" : undefined} className={cn( "relative z-1 inline-flex items-center justify-center gap-1.5 px-1.5", showLabels ? "min-w-20" : "w-(--cui-theme-toggle-height)", )} > <input type="radio" name={name ?? generatedName} value={option.value} checked={selected} disabled={option.disabled} aria-label={option.label} className="sr-only" onChange={(event) => { if (event.currentTarget.checked) onValueChange(option.value); }} /> <Icon className="size-3.5" aria-hidden /> {showLabels ? ( <Text size="label" weight="medium"> {option.label} </Text> ) : null} </label> ); })} </div> );}
export function ThemeToggle({ value, onValueChange, options = defaultThemeOptions, showLabel = false, className, onClick, "aria-label": ariaLabel, ...props}: ThemeToggleProps) { const choices = controlOptions(options); const current = currentThemeOption(value, choices); const Icon = current.icon;
return ( <Button variant="surface" size="sm" className={cn("gap-2", className)} aria-label={ariaLabel ?? `Theme: ${current.label}`} onClick={(event) => { onClick?.(event); if (!event.defaultPrevented) onValueChange(nextThemeValue(value, choices)); }} {...props} > <Icon aria-hidden /> {showLabel ? <span>{current.label}</span> : null} </Button> );}
export function ThemeDropdown({ value, onValueChange, options = defaultThemeOptions, label = "Theme", className, "aria-label": ariaLabel, ...props}: ThemeDropdownProps) { const choices = controlOptions(options); const current = currentThemeOption(value, choices); const CurrentIcon = current.icon;
return ( <DropdownMenu> <DropdownMenuTrigger aria-label={ariaLabel ?? `${label}: ${current.label}`} className={cn("min-w-36 gap-2", className)} {...props}> <CurrentIcon aria-hidden /> <span className="min-w-0 truncate">{current.label}</span> <ChevronDownIcon className="text-muted-foreground" aria-hidden /> </DropdownMenuTrigger> <DropdownMenuContent className="min-w-40"> <DropdownMenuRadioGroup value={value} onValueChange={onValueChange}> <DropdownMenuLabel>{label}</DropdownMenuLabel> {choices.map((option) => { const Icon = option.icon;
return ( <DropdownMenuRadioItem key={option.value} value={option.value} disabled={option.disabled}> <Icon className="size-3.5" aria-hidden /> <span className="min-w-0 flex-1 truncate">{option.label}</span> </DropdownMenuRadioItem> ); })} </DropdownMenuRadioGroup> </DropdownMenuContent> </DropdownMenu> );}