修复了暗色情况下和刷新后图标和风格显示不一样的情况
Some checks failed
Build Test / release (18.x) (push) Has been cancelled
Docker Image CI / build (push) Has been cancelled

(cherry picked from commit 1fcf08f0e11fc4085e7d1cb4727ad1f334e9688c)
This commit is contained in:
GreenDreamer 2025-02-27 21:08:08 +08:00 committed by Deng Junhai
parent e5d28b808d
commit 9c03dee238

View File

@ -1,11 +1,11 @@
import { createContext, useContext, useEffect, useState } from "react"; import { createContext, useContext, useEffect, useState } from "react";
import { Moon, Sun } from "lucide-react"; import { Moon, Sun, Monitor } from "lucide-react";
import { Button } from "./ui/button"; import { Button } from "./ui/button";
import { getMemory, setMemory } from "@/utils/memory.ts"; import { getMemory, setMemory } from "@/utils/memory.ts";
import { themeEvent } from "@/events/theme.ts"; import { themeEvent } from "@/events/theme.ts";
const defaultTheme: Theme = "dark"; const defaultTheme: Theme = "system";
export type Theme = "dark" | "light" | "system"; export type Theme = "dark" | "light" | "system";
@ -15,7 +15,6 @@ type ThemeProviderProps = {
type ThemeProviderState = { type ThemeProviderState = {
theme: Theme; theme: Theme;
setTheme: (theme: Theme) => void;
toggleTheme?: () => void; toggleTheme?: () => void;
}; };
@ -23,13 +22,15 @@ export function activeTheme(theme: Theme) {
const root = window.document.documentElement; const root = window.document.documentElement;
root.classList.remove("light", "dark"); root.classList.remove("light", "dark");
if (theme === "system")
if (theme === "system") {
theme = window.matchMedia("(prefers-color-scheme: dark)").matches theme = window.matchMedia("(prefers-color-scheme: dark)").matches
? "dark" ? "dark"
: "light"; : "light";
}
root.classList.add(`${theme}`);
root.classList.add(theme);
setMemory("theme", theme);
themeEvent.emit(theme); themeEvent.emit(theme);
} }
@ -38,52 +39,58 @@ export function getTheme() {
} }
const initialState: ThemeProviderState = { const initialState: ThemeProviderState = {
theme: "system", theme: defaultTheme,
setTheme: (theme: Theme) => {
activeTheme(theme);
},
toggleTheme: () => { toggleTheme: () => {
const key = getMemory("theme"); const currentTheme = getMemory("theme");
const theme = (key.length > 0 ? key : defaultTheme) as Theme; let newTheme: Theme;
const root = window.document.documentElement;
root.classList.remove("dark", "light","system");
// dark -> light -> system -> dark
if (currentTheme === "dark") {
newTheme = "light";
root.classList.add(`${newTheme}`);
} else if (currentTheme === "light") {
newTheme = "system";
} else {
newTheme = "dark";
root.classList.add(`${newTheme}`);
}
activeTheme(newTheme);
setMemory("theme", newTheme);
activeTheme(theme === "dark" ? "light" : "dark");
}, },
}; };
const ThemeProviderContext = createContext<ThemeProviderState>(initialState); const ThemeProviderContext = createContext<ThemeProviderState>(initialState);
export function ThemeProvider({ export function ThemeProvider({
defaultTheme = "dark", defaultTheme = "system",
...props ...props
}: ThemeProviderProps) { }: ThemeProviderProps) {
const [theme, setTheme] = useState<Theme>( const { theme } = useTheme();
() => (getMemory("theme") as Theme) || defaultTheme,
);
useEffect(() => { useEffect(() => {
const root = window.document.documentElement;
root.classList.remove("light", "dark"); const savedTheme = getTheme();
if (theme === "system") { activeTheme(savedTheme);
const systemTheme = window.matchMedia("(prefers-color-scheme: dark)")
.matches
? "dark"
: "light";
root.classList.add(systemTheme);
return;
}
root.classList.add(theme);
}, [theme]); }, [theme]);
const value = { const value = {
theme, theme,
setTheme: (theme: Theme) => { setTheme: (theme: Theme) => {
setMemory("theme", theme); setMemory("theme", theme);
setTheme(theme);
}, },
toggleTheme: initialState.toggleTheme,
}; };
return <ThemeProviderContext.Provider {...props} value={value} />; return <ThemeProviderContext.Provider {...props} value={value} />;
@ -99,16 +106,61 @@ export const useTheme = () => {
}; };
export function ModeToggle() { export function ModeToggle() {
const { toggleTheme } = useTheme(); const { theme, toggleTheme } = useTheme();
const [isAnimating, setIsAnimating] = useState(false);
const [currentIconIndex, setCurrentIconIndex] = useState(0);
const icons = [
<Moon key="dark" className="h-[1.2rem] w-[1.2rem]" />,
<Sun key="light" className="h-[1.2rem] w-[1.2rem]" />,
<Monitor key="system" className="h-[1.2rem] w-[1.2rem]" />,
];
useEffect(() => {
const savedTheme = getTheme();
const index = icons.findIndex(icon => icon.key === savedTheme);
setCurrentIconIndex(index);
}, [theme]);
const handleClick = () => {
if (isAnimating) return; // 防止重复点击
setIsAnimating(true);
toggleTheme?.();
setTimeout(() => {
const nextIconIndex = (currentIconIndex + 1) % icons.length;
setCurrentIconIndex(nextIconIndex);
setIsAnimating(false);
}, 500);
};
return ( return (
<Button variant="outline" size="icon" onClick={() => toggleTheme?.()}> <Button
<Sun variant="outline"
className={`relative dark:absolute h-[1.2rem] w-[1.2rem] rotate-0 scale-100 transition-all dark:-rotate-90 dark:scale-0`} size="icon"
/> onClick={handleClick}
<Moon style={{
className={`absolute dark:relative h-[1.2rem] w-[1.2rem] rotate-90 scale-0 transition-all dark:rotate-0 dark:scale-100`} transition: "background-color 0.5s ease",
/> position: "relative",
overflow: "hidden",
}}
>
<div
style={{
position: "absolute",
transform: isAnimating ? "scale(0)" : "scale(1)",
transition: "transform 0.5s ease, opacity 0.5s ease",
}}
>
{icons[currentIconIndex]}
</div>
</Button> </Button>
); );
} }