import * as React from "react"; import { Moon, Sun } from "lucide-react"; import { useTheme } from "next-themes"; import { Button } from "@/components/ui/button"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, } from "@/components/ui/dropdown-menu"; import { cn } from "@/lib/utils"; const ThemeToggle = ({ children }: { children?: React.ReactNode }) => { const { theme, setTheme, systemTheme } = useTheme(); const [mounted, setMounted] = React.useState(false); // 确保组件挂载后再渲染 React.useEffect(() => { setMounted(true); }, []); // 在客户端渲染之前返回null if (!mounted) { return null; } // 获取当前实际主题 const currentTheme = theme === "system" ? systemTheme : theme; return ( {!children ? ( ) : ( children )} setTheme("light")}> Light setTheme("dark")}> Dark setTheme("system")}> System ); }; export default ThemeToggle;