Skip to content

Commit 73d936b

Browse files
committed
I think this work
1 parent 628c5ad commit 73d936b

File tree

1 file changed

+18
-31
lines changed

1 file changed

+18
-31
lines changed

reflex/.templates/web/utils/react-theme.js

Lines changed: 18 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
} from "react";
1010

1111
import { isDevMode, defaultColorMode } from "$/utils/context";
12+
import { resolve } from "path";
1213

1314
const ThemeContext = createContext({
1415
theme: defaultColorMode,
@@ -17,28 +18,13 @@ const ThemeContext = createContext({
1718
});
1819

1920
export function ThemeProvider({ children, defaultTheme = "system" }) {
20-
// Read the actual saved theme immediately during initialization to avoid double theme changes
21-
const getInitialTheme = () => {
22-
if (typeof window === "undefined") return defaultTheme;
23-
return localStorage.getItem("theme") || defaultTheme;
24-
};
25-
26-
const [theme, setTheme] = useState(getInitialTheme);
27-
28-
// Detect system preference synchronously during initialization
29-
const getInitialSystemTheme = () => {
30-
if (defaultTheme !== "system") return defaultTheme;
31-
if (typeof window === "undefined") return "light";
32-
return window.matchMedia("(prefers-color-scheme: dark)").matches
33-
? "dark"
34-
: "light";
35-
};
36-
37-
const [systemTheme, setSystemTheme] = useState(getInitialSystemTheme);
38-
const [isInitialized, setIsInitialized] = useState(false);
21+
const [theme, setTheme] = useState(defaultTheme);
22+
const [systemTheme, setSystemTheme] = useState(
23+
defaultTheme !== "system" ? defaultTheme : "light",
24+
);
25+
const [isInitialized, setIsInitialized] = useState(false); // ← Add this flag
3926

4027
const firstRender = useRef(true);
41-
4228
useEffect(() => {
4329
if (!firstRender.current) {
4430
return;
@@ -55,14 +41,12 @@ export function ThemeProvider({ children, defaultTheme = "system" }) {
5541
}
5642
}
5743

44+
// Load saved theme from localStorage
45+
const savedTheme = localStorage.getItem("theme") || defaultTheme;
46+
setTheme(savedTheme);
5847
setIsInitialized(true);
5948
});
6049

61-
const resolvedTheme = useMemo(
62-
() => (theme === "system" ? systemTheme : theme),
63-
[theme, systemTheme],
64-
);
65-
6650
useEffect(() => {
6751
// Set up media query for system preference detection
6852
const mediaQuery = window.matchMedia("(prefers-color-scheme: dark)");
@@ -80,17 +64,20 @@ export function ThemeProvider({ children, defaultTheme = "system" }) {
8064
mediaQuery.removeEventListener("change", handleChange);
8165
};
8266
});
67+
const resolvedTheme = useMemo(
68+
() => (theme === "system" ? systemTheme : theme),
69+
[theme, systemTheme],
70+
);
71+
console.log(theme, defaultTheme, resolvedTheme);
8372

84-
// Save theme to localStorage whenever it changes (but not on initial mount)
73+
// Save theme to localStorage whenever it changes
8574
useEffect(() => {
86-
if (isInitialized) {
87-
localStorage.setItem("theme", theme);
88-
}
89-
}, [theme, isInitialized]);
75+
localStorage.setItem("theme", theme);
76+
}, [theme]);
9077

9178
useEffect(() => {
79+
if (!isInitialized) return;
9280
const root = window.document.documentElement;
93-
9481
root.classList.remove("light", "dark");
9582
root.classList.add(resolvedTheme);
9683
root.style.colorScheme = resolvedTheme;

0 commit comments

Comments
 (0)