2024-12-03 01:37:02 +08:00
|
|
|
import React, { useState, useEffect } from "react";
|
2024-12-04 02:35:06 +08:00
|
|
|
import { MoonIcon, SunIcon } from "@radix-ui/react-icons";
|
2024-12-03 01:37:02 +08:00
|
|
|
import { Button } from "@radix-ui/themes";
|
|
|
|
|
|
|
|
const THEME_KEY = "theme-preference";
|
|
|
|
|
|
|
|
export const ThemeModeToggle: React.FC = () => {
|
|
|
|
const [mounted, setMounted] = useState(false);
|
2024-12-04 02:35:06 +08:00
|
|
|
const [isDark, setIsDark] = useState(false);
|
2024-12-03 01:37:02 +08:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setMounted(true);
|
|
|
|
const saved = localStorage.getItem(THEME_KEY);
|
2024-12-04 02:35:06 +08:00
|
|
|
const initialTheme =
|
|
|
|
saved ||
|
|
|
|
(window.matchMedia("(prefers-color-scheme: dark)").matches
|
|
|
|
? "dark"
|
|
|
|
: "light");
|
|
|
|
setIsDark(initialTheme === "dark");
|
2024-12-03 01:37:02 +08:00
|
|
|
if (saved) {
|
2024-12-04 02:35:06 +08:00
|
|
|
document.documentElement.className = saved;
|
2024-12-03 01:37:02 +08:00
|
|
|
} else {
|
2024-12-04 02:35:06 +08:00
|
|
|
document.documentElement.className = initialTheme;
|
2024-12-03 01:37:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const toggleTheme = () => {
|
|
|
|
const newIsDark = !isDark;
|
|
|
|
setIsDark(newIsDark);
|
2024-12-04 02:35:06 +08:00
|
|
|
const newTheme = newIsDark ? "dark" : "light";
|
|
|
|
document.documentElement.className = newTheme;
|
|
|
|
localStorage.setItem(THEME_KEY, newTheme);
|
2024-12-03 01:37:02 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
if (!mounted) return null;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Button
|
|
|
|
variant="ghost"
|
|
|
|
onClick={toggleTheme}
|
2024-12-04 19:57:59 +08:00
|
|
|
className="w-full h-full p-0 rounded-lg transition-all duration-300 transform"
|
2024-12-03 01:37:02 +08:00
|
|
|
aria-label="Toggle theme"
|
|
|
|
>
|
|
|
|
{isDark ? (
|
2024-12-04 19:57:59 +08:00
|
|
|
<SunIcon className="w-full h-full"/>
|
2024-12-03 01:37:02 +08:00
|
|
|
) : (
|
2024-12-04 19:57:59 +08:00
|
|
|
<MoonIcon className="w-full h-full" />
|
2024-12-03 01:37:02 +08:00
|
|
|
)}
|
|
|
|
</Button>
|
|
|
|
);
|
|
|
|
};
|
2024-12-04 02:35:06 +08:00
|
|
|
|
|
|
|
export const useThemeMode = () => {
|
|
|
|
const [mode, setMode] = useState<"light" | "dark">("light");
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (typeof window !== "undefined") {
|
|
|
|
const saved = localStorage.getItem(THEME_KEY);
|
|
|
|
if (saved) {
|
|
|
|
setMode(saved as "light" | "dark");
|
|
|
|
} else {
|
|
|
|
const isDark = window.matchMedia(
|
|
|
|
"(prefers-color-scheme: dark)",
|
|
|
|
).matches;
|
|
|
|
setMode(isDark ? "dark" : "light");
|
|
|
|
}
|
|
|
|
|
|
|
|
// 监听主题变化事件
|
|
|
|
const handleThemeChange = (e: CustomEvent) => {
|
|
|
|
setMode(e.detail.theme);
|
|
|
|
};
|
|
|
|
|
|
|
|
window.addEventListener(
|
|
|
|
"theme-change",
|
|
|
|
handleThemeChange as EventListener,
|
|
|
|
);
|
|
|
|
return () =>
|
|
|
|
window.removeEventListener(
|
|
|
|
"theme-change",
|
|
|
|
handleThemeChange as EventListener,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return { mode };
|
|
|
|
};
|