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) return savedTheme; const isDark = window.matchMedia("(prefers-color-scheme: dark)").matches; const theme = isDark ? "dark" : "light"; localStorage.setItem("${THEME_KEY}", theme); return theme; } document.documentElement.className = getInitialTheme(); })() `; export const ThemeScript = () => { return