Something went wrong. Try again.
Fork of backnotprop/plannotator with reduced surface area
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111import { useState, useRef, useEffect } from 'react';
type Theme = 'dark' | 'light' | 'system';
function getCookie(key: string): string | null { const match = document.cookie.match(new RegExp(`(?:^|; )${key}=([^;]*)`)); return match ? decodeURIComponent(match[1]) : null;}
function setCookie(key: string, value: string) { document.cookie = `${key}=${encodeURIComponent(value)};path=/;max-age=31536000;SameSite=Lax`;}
function applyTheme(theme: Theme) { const root = document.documentElement; root.classList.remove('light'); let effective = theme; if (theme === 'system') { effective = window.matchMedia('(prefers-color-scheme: light)').matches ? 'light' : 'dark'; } if (effective === 'light') { root.classList.add('light'); }}
export default function ModeToggleIsland() { const [theme, setTheme] = useState<Theme>(() => (getCookie('plannotator-theme') as Theme) || 'dark' ); const [isOpen, setIsOpen] = useState(false); const ref = useRef<HTMLDivElement>(null);
useEffect(() => { applyTheme(theme); }, [theme]);
useEffect(() => { if (theme !== 'system') return; const mq = window.matchMedia('(prefers-color-scheme: light)'); const handler = () => applyTheme('system'); mq.addEventListener('change', handler); return () => mq.removeEventListener('change', handler); }, [theme]);
useEffect(() => { const handler = (e: MouseEvent) => { if (ref.current && !ref.current.contains(e.target as Node)) { setIsOpen(false); } }; document.addEventListener('mousedown', handler); return () => document.removeEventListener('mousedown', handler); }, []);
const handleSelect = (t: Theme) => { setCookie('plannotator-theme', t); setTheme(t); setIsOpen(false); };
return ( <div className="relative" ref={ref}> <button onClick={() => setIsOpen(!isOpen)} className="p-1.5 rounded-md text-muted-foreground hover:text-foreground hover:bg-muted transition-colors" title="Toggle theme" > {/* Sun - visible in light mode */} <svg className="w-4 h-4 hidden light:block" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2} > <circle cx="12" cy="12" r="4" /> <path d="M12 2v2M12 20v2M4.93 4.93l1.41 1.41M17.66 17.66l1.41 1.41M2 12h2M20 12h2M6.34 17.66l-1.41 1.41M19.07 4.93l-1.41 1.41" /> </svg> {/* Moon - visible in dark mode */} <svg className="w-4 h-4 light:hidden" fill="none" viewBox="0 0 24 24" stroke="currentColor" strokeWidth={2} > <path d="M12 3a6 6 0 0 0 9 9 9 9 0 1 1-9-9Z" /> </svg> </button>
{isOpen && ( <div className="absolute right-0 mt-1 w-32 rounded-lg border border-border bg-popover shadow-xl z-50 overflow-hidden py-1"> {(['light', 'dark', 'system'] as const).map((t) => ( <button key={t} onClick={() => handleSelect(t)} className={`w-full px-3 py-1.5 text-left text-xs capitalize transition-colors ${ theme === t ? 'text-primary bg-primary/10 font-medium' : 'text-popover-foreground hover:bg-muted' }`} > {t} </button> ))} </div> )} </div> );}