diff --git a/src/components/ThemeToggle.tsx b/src/components/ThemeToggle.tsx index 11e139c..36238ec 100644 --- a/src/components/ThemeToggle.tsx +++ b/src/components/ThemeToggle.tsx @@ -4,55 +4,57 @@ import { useTheme } from "../lib/use-theme"; export function ThemeToggle() { const { theme, setTheme } = useTheme(); - const [open, setOpen] = useState(false); + const [expanded, setExpanded] = useState(false); const ref = useRef(null); useEffect(() => { - if (!open) return; + if (!expanded) return; const handler = (e: MouseEvent) => { if (ref.current && !ref.current.contains(e.target as Node)) { - setOpen(false); + setExpanded(false); } }; document.addEventListener("mousedown", handler); return () => document.removeEventListener("mousedown", handler); - }, [open]); + }, [expanded]); - const iconMap = { light: Sun, dark: Moon, system: Monitor }; - const CurrentIcon = iconMap[theme]; + 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 ( -
- - - {open && ( -
- {([ - { value: "light" as const, icon: Sun, label: "浅色" }, - { value: "dark" as const, icon: Moon, label: "深色" }, - { value: "system" as const, icon: Monitor, label: "自动" }, - ]).map(({ value, icon: Icon, label }) => ( - - ))} -
- )} +
+ {options.map(({ value, icon: Icon, label }) => ( + + ))}
); }