import React, { useState, useEffect } from "react"; import { MoonIcon, SunIcon } from "@radix-ui/react-icons"; import { Button } from "@radix-ui/themes"; const THEME_KEY = "theme-preference"; // 添加这个脚本来预先设置主题,避免闪烁 const themeScript = ` (function() { function getInitialTheme() { const savedTheme = localStorage.getItem("${THEME_KEY}"); if (savedTheme) { document.documentElement.className = savedTheme; return savedTheme; } const isDark = window.matchMedia("(prefers-color-scheme: dark)").matches; const theme = isDark ? "dark" : "light"; document.documentElement.className = theme; localStorage.setItem("${THEME_KEY}", theme); return theme; } // 确保在 DOM 内容加载前执行 if (document.documentElement) { getInitialTheme(); } else { document.addEventListener('DOMContentLoaded', getInitialTheme); } })() `; export const ThemeScript = () => { return