|
| 1 | +import { useState, useEffect, HTMLAttributes } from 'react'; |
| 2 | +import { Button } from "@/components/ui/button"; |
| 3 | +import { |
| 4 | + DropdownMenu, |
| 5 | + DropdownMenuContent, |
| 6 | + DropdownMenuItem, |
| 7 | + DropdownMenuTrigger, |
| 8 | +} from "@/components/ui/dropdown-menu"; |
| 9 | + |
| 10 | +type Appearance = 'light' | 'dark' | 'system'; |
| 11 | + |
| 12 | +declare global { |
| 13 | + interface Window { |
| 14 | + appearance: Appearance; |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +interface AppearanceToggleProps extends HTMLAttributes<HTMLDivElement> { } |
| 19 | + |
| 20 | +export default function AppearanceToggle({ className = '', ...props }: AppearanceToggleProps) { |
| 21 | + const [appearance, setAppearance] = useState<Appearance>('system'); |
| 22 | + |
| 23 | + // Check if user prefers dark mode |
| 24 | + const prefersDark = () => |
| 25 | + window.matchMedia('(prefers-color-scheme: dark)').matches; |
| 26 | + |
| 27 | + // Apply theme to document body |
| 28 | + const applyTheme = (mode: Appearance) => { |
| 29 | + const isDark = mode === 'dark' || (mode === 'system' && prefersDark()); |
| 30 | + document.body.classList.toggle('dark', isDark); |
| 31 | + }; |
| 32 | + |
| 33 | + // Update appearance state and sync with window/localStorage |
| 34 | + const updateAppearance = (mode: Appearance) => { |
| 35 | + setAppearance(mode); |
| 36 | + window.appearance = mode; |
| 37 | + localStorage.setItem('appearance', mode); |
| 38 | + applyTheme(mode); |
| 39 | + }; |
| 40 | + |
| 41 | + // Initialize theme from localStorage or default to system |
| 42 | + useEffect(() => { |
| 43 | + const savedAppearance = localStorage.getItem('appearance') as Appearance; |
| 44 | + const initialAppearance = savedAppearance || 'system'; |
| 45 | + updateAppearance(initialAppearance); |
| 46 | + |
| 47 | + // Listen for system theme changes |
| 48 | + const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)'); |
| 49 | + const handleChange = () => { |
| 50 | + if (appearance === 'system') { |
| 51 | + applyTheme('system'); |
| 52 | + } |
| 53 | + }; |
| 54 | + |
| 55 | + mediaQuery.addEventListener('change', handleChange); |
| 56 | + return () => mediaQuery.removeEventListener('change', handleChange); |
| 57 | + }, []); |
| 58 | + |
| 59 | + return ( |
| 60 | + <div className={className} {...props}> |
| 61 | + <DropdownMenu> |
| 62 | + <DropdownMenuTrigger asChild> |
| 63 | + <Button variant="ghost" size="icon" className="h-9 w-9 rounded-md"> |
| 64 | + {appearance === 'dark' ? ( |
| 65 | + <svg xmlns="http://www.w3.org/2000/svg" className="h-5 w-5" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-moon"><path d="M12 3a6 6 0 0 0 9 9 9 9 0 1 1-9-9Z"/></svg> |
| 66 | + ) : appearance === 'light' ? ( |
| 67 | + <svg xmlns="http://www.w3.org/2000/svg" className="h-5 w-5" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-sun-medium"><circle cx="12" cy="12" r="4"/><path d="M12 3v1"/><path d="M12 20v1"/><path d="M3 12h1"/><path d="M20 12h1"/><path d="m18.364 5.636-.707.707"/><path d="m6.343 17.657-.707.707"/><path d="m5.636 5.636.707.707"/><path d="m17.657 17.657.707.707"/></svg> |
| 68 | + ) : ( |
| 69 | + <svg xmlns="http://www.w3.org/2000/svg" className="h-5 w-5" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-monitor"><rect width="20" height="14" x="2" y="3" rx="2"/><line x1="8" x2="16" y1="21" y2="21"/><line x1="12" x2="12" y1="17" y2="21"/></svg> |
| 70 | + )} |
| 71 | + <span className="sr-only">Toggle theme</span> |
| 72 | + </Button> |
| 73 | + </DropdownMenuTrigger> |
| 74 | + <DropdownMenuContent align="end" rounded="xl"> |
| 75 | + <DropdownMenuItem onClick={() => updateAppearance('light')}> |
| 76 | + <span className="flex items-center gap-2"> |
| 77 | + <svg xmlns="http://www.w3.org/2000/svg" className="h-5 w-5" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-sun-medium"><circle cx="12" cy="12" r="4"/><path d="M12 3v1"/><path d="M12 20v1"/><path d="M3 12h1"/><path d="M20 12h1"/><path d="m18.364 5.636-.707.707"/><path d="m6.343 17.657-.707.707"/><path d="m5.636 5.636.707.707"/><path d="m17.657 17.657.707.707"/></svg> |
| 78 | + Light |
| 79 | + </span> |
| 80 | + </DropdownMenuItem> |
| 81 | + <DropdownMenuItem onClick={() => updateAppearance('dark')}> |
| 82 | + <span className="flex items-center gap-2"> |
| 83 | + <svg xmlns="http://www.w3.org/2000/svg" className="h-5 w-5" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-moon"><path d="M12 3a6 6 0 0 0 9 9 9 9 0 1 1-9-9Z"/></svg> |
| 84 | + Dark |
| 85 | + </span> |
| 86 | + </DropdownMenuItem> |
| 87 | + <DropdownMenuItem onClick={() => updateAppearance('system')}> |
| 88 | + <span className="flex items-center gap-2"> |
| 89 | + <svg xmlns="http://www.w3.org/2000/svg" className="h-5 w-5" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-monitor"><rect width="20" height="14" x="2" y="3" rx="2"/><line x1="8" x2="16" y1="21" y2="21"/><line x1="12" x2="12" y1="17" y2="21"/></svg> |
| 90 | + System |
| 91 | + </span> |
| 92 | + </DropdownMenuItem> |
| 93 | + </DropdownMenuContent> |
| 94 | + </DropdownMenu> |
| 95 | + </div> |
| 96 | + ); |
| 97 | +} |
0 commit comments