"use client" import { useSyncExternalStore } from "react" import { LaptopIcon, MoonIcon, SunIcon } from "lucide-react" import { useTheme } from "next-themes" import { Button } from "@/components/ui/button" import { DropdownMenu, DropdownMenuContent, DropdownMenuRadioGroup, DropdownMenuRadioItem, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu" type ThemeMode = "light" | "dark" | "system" const themeOptions: Array<{ value: ThemeMode label: string icon: typeof SunIcon }> = [ { value: "light", label: "浅色模式", icon: SunIcon }, { value: "dark", label: "深色模式", icon: MoonIcon }, { value: "system", label: "跟随系统", icon: LaptopIcon }, ] export function ThemeToggle() { const { theme, setTheme } = useTheme() const mounted = useSyncExternalStore( () => () => {}, () => true, () => false ) const activeTheme = mounted ? ((theme as ThemeMode | undefined) ?? "system") : "system" const ActiveIcon = themeOptions.find((option) => option.value === activeTheme)?.icon ?? LaptopIcon return ( } aria-label="切换主题"> {/* 主题 */} setTheme(value as ThemeMode)} > {themeOptions.map((option) => { const Icon = option.icon return ( {option.label} ) })} ) }