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);
|
|
|
|
const [visible, setVisible] = useState(true);
|
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
|
|
|
}
|
|
|
|
|
|
|
|
let lastScroll = 0;
|
|
|
|
const handleScroll = () => {
|
|
|
|
const currentScroll = window.scrollY;
|
|
|
|
setVisible(currentScroll <= lastScroll || currentScroll < 50);
|
|
|
|
lastScroll = currentScroll;
|
|
|
|
};
|
|
|
|
|
2024-12-04 02:35:06 +08:00
|
|
|
window.addEventListener("scroll", handleScroll);
|
|
|
|
return () => window.removeEventListener("scroll", handleScroll);
|
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}
|
|
|
|
className={`p-2 rounded-lg transition-all duration-300 transform ${
|
2024-12-04 02:35:06 +08:00
|
|
|
visible ? "translate-y-0 opacity-100" : "-translate-y-full opacity-0"
|
2024-12-03 01:37:02 +08:00
|
|
|
}`}
|
|
|
|
aria-label="Toggle theme"
|
|
|
|
>
|
|
|
|
{isDark ? (
|
2024-12-04 02:35:06 +08:00
|
|
|
<SunIcon width="24" height="24" />
|
2024-12-03 01:37:02 +08:00
|
|
|
) : (
|
2024-12-04 02:35:06 +08:00
|
|
|
<MoonIcon width="24" height="24" />
|
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 };
|
|
|
|
};
|