'use client' import { useEffect, useRef, useState } from 'react' import { Check, Monitor, Moon, Sun } from 'lucide-react' import { useTheme, type ThemePref } from '@/lib/theme' import { cn } from '@/lib/utils' const OPTIONS: { value: ThemePref; label: string; icon: typeof Sun }[] = [ { value: 'system', label: 'Sistema', icon: Monitor }, { value: 'light', label: 'Chiaro', icon: Sun }, { value: 'dark', label: 'Scuro', icon: Moon }, ] /** Header theme switcher — Sistema / Chiaro / Scuro. Defaults to Sistema * (follows the OS). Dropdown closes on outside-click + Esc. */ export default function ThemeToggle() { const { pref, resolved, setTheme } = useTheme() const [open, setOpen] = useState(false) const ref = useRef(null) useEffect(() => { if (!open) return const onDocClick = (e: MouseEvent) => { if (ref.current && !ref.current.contains(e.target as Node)) setOpen(false) } const onEsc = (e: KeyboardEvent) => { if (e.key === 'Escape') setOpen(false) } document.addEventListener('mousedown', onDocClick) document.addEventListener('keydown', onEsc) return () => { document.removeEventListener('mousedown', onDocClick) document.removeEventListener('keydown', onEsc) } }, [open]) const TriggerIcon = pref === 'system' ? Monitor : resolved === 'dark' ? Moon : Sun return (
{open && (
{OPTIONS.map((o) => { const Icon = o.icon const active = o.value === pref return ( ) })}
)}
) }