From 3bfd4ef4d20a9a8bca90b191274080cc87d11738 Mon Sep 17 00:00:00 2001 From: hamster1963 <1410514192@qq.com> Date: Sun, 28 Dec 2025 16:33:57 +0800 Subject: [PATCH] fix: prevent color fade during theme transitions --- src/components/ThemeProvider.tsx | 10 +++++++++- src/index.css | 9 +++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/components/ThemeProvider.tsx b/src/components/ThemeProvider.tsx index 000d8f6..8eed477 100644 --- a/src/components/ThemeProvider.tsx +++ b/src/components/ThemeProvider.tsx @@ -26,6 +26,7 @@ export function ThemeProvider({ children, storageKey = "vite-ui-theme" }: ThemeP useEffect(() => { const root = window.document.documentElement + root.classList.add("disable-transitions") root.classList.remove("light", "dark") if (theme === "system") { @@ -34,12 +35,19 @@ export function ThemeProvider({ children, storageKey = "vite-ui-theme" }: ThemeP root.classList.add(systemTheme) const themeColor = systemTheme === "dark" ? "hsl(30 15% 8%)" : "hsl(0 0% 98%)" document.querySelector('meta[name="theme-color"]')?.setAttribute("content", themeColor) - return + const timeoutId = window.setTimeout(() => { + root.classList.remove("disable-transitions") + }, 0) + return () => window.clearTimeout(timeoutId) } root.classList.add(theme) const themeColor = theme === "dark" ? "hsl(30 15% 8%)" : "hsl(0 0% 98%)" document.querySelector('meta[name="theme-color"]')?.setAttribute("content", themeColor) + const timeoutId = window.setTimeout(() => { + root.classList.remove("disable-transitions") + }, 0) + return () => window.clearTimeout(timeoutId) }, [theme]) const value = { diff --git a/src/index.css b/src/index.css index f961429..18ee2e1 100644 --- a/src/index.css +++ b/src/index.css @@ -201,6 +201,15 @@ } } +@layer base { + /* Avoid color fade when toggling themes. */ + html.disable-transitions *, + html.disable-transitions *::before, + html.disable-transitions *::after { + transition: none !important; + } +} + @media (max-width: 640px) { .container { @apply px-4;