import { useState, useRef, useEffect } from "react"; import { Sun, Moon, Monitor } from "lucide-react"; import { useTheme } from "../lib/use-theme"; export function ThemeToggle() { const { theme, setTheme } = useTheme(); const [expanded, setExpanded] = useState(false); const ref = useRef(null); useEffect(() => { if (!expanded) return; const handler = (e: MouseEvent) => { if (ref.current && !ref.current.contains(e.target as Node)) { setExpanded(false); } }; document.addEventListener("mousedown", handler); return () => document.removeEventListener("mousedown", handler); }, [expanded]); const options = [ { value: "light" as const, icon: Sun, label: "浅色" }, { value: "dark" as const, icon: Moon, label: "深色" }, { value: "system" as const, icon: Monitor, label: "自动" }, ]; if (!expanded) { const CurrentIcon = options.find(o => o.value === theme)!.icon; return (
); } return (
{options.map(({ value, icon: Icon, label }) => ( ))}
); }