mirror of
https://github.com/Sea-Haven-Industries/open-swe.git
synced 2026-10-04 23:02:12 +00:00
fix: prevent theme flash during page load (#688)
* Apply patch [skip ci] * Apply patch [skip ci] * Apply patch [skip ci] * Apply patch [skip ci] * Empty commit to trigger CI --------- Co-authored-by: open-swe[bot] <open-swe@users.noreply.github.com> Co-authored-by: Brace Sproul <braceasproul@gmail.com>
This commit is contained in:
parent
8e461d7040
commit
164794d11f
3 changed files with 37 additions and 7 deletions
|
|
@ -31,6 +31,32 @@ export default function RootLayout({
|
||||||
lang="en"
|
lang="en"
|
||||||
suppressHydrationWarning
|
suppressHydrationWarning
|
||||||
>
|
>
|
||||||
|
<head>
|
||||||
|
<script
|
||||||
|
dangerouslySetInnerHTML={{
|
||||||
|
__html: `
|
||||||
|
(function() {
|
||||||
|
try {
|
||||||
|
var theme = localStorage.getItem('theme');
|
||||||
|
var root = document.documentElement;
|
||||||
|
|
||||||
|
if (theme === 'dark') {
|
||||||
|
root.classList.add('dark');
|
||||||
|
} else if (theme === 'light') {
|
||||||
|
root.classList.add('light');
|
||||||
|
} else if (theme === 'system' || !theme) {
|
||||||
|
var systemTheme = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
|
||||||
|
root.classList.add(systemTheme);
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
// Fallback to light theme if there's any error
|
||||||
|
document.documentElement.classList.add('light');
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
`,
|
||||||
|
}}
|
||||||
|
/>
|
||||||
|
</head>
|
||||||
<body className={inter.className}>
|
<body className={inter.className}>
|
||||||
<ThemeProvider
|
<ThemeProvider
|
||||||
defaultTheme="system"
|
defaultTheme="system"
|
||||||
|
|
|
||||||
|
|
@ -40,16 +40,20 @@ export function ThemeProvider({
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const root = window.document.documentElement;
|
const root = window.document.documentElement;
|
||||||
|
|
||||||
root.classList.remove("light", "dark");
|
// Determine the target theme
|
||||||
|
let targetTheme: string;
|
||||||
if (theme === "system") {
|
if (theme === "system") {
|
||||||
const systemTheme = window.matchMedia("(prefers-color-scheme: dark)")
|
targetTheme = window.matchMedia("(prefers-color-scheme: dark)").matches
|
||||||
.matches
|
|
||||||
? "dark"
|
? "dark"
|
||||||
: "light";
|
: "light";
|
||||||
root.classList.add(systemTheme);
|
|
||||||
} else {
|
} else {
|
||||||
root.classList.add(theme);
|
targetTheme = theme;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only apply theme if it's not already present
|
||||||
|
if (!root.classList.contains(targetTheme)) {
|
||||||
|
root.classList.remove("light", "dark");
|
||||||
|
root.classList.add(targetTheme);
|
||||||
}
|
}
|
||||||
}, [theme]);
|
}, [theme]);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
import { useTheme } from "next-themes";
|
import { useTheme } from "@/components/theme-provider";
|
||||||
import { Toaster as Sonner, ToasterProps } from "sonner";
|
import { Toaster as Sonner, ToasterProps } from "sonner";
|
||||||
|
|
||||||
const Toaster = ({ ...props }: ToasterProps) => {
|
const Toaster = ({ ...props }: ToasterProps) => {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue