|
1 | 1 | import React from "react"; |
2 | | -import { DarkThemeKey, ThemeSetting } from "./src/theme/app/ThemeManager.js"; |
| 2 | +import { DarkThemeKey, ThemeSetting } from "./src/theme/app/ThemeManager"; |
3 | 3 | import lighttheme, { darktheme } from "./src/theme/app/themeStyles"; |
4 | 4 |
|
5 | | -const themes = { light: lighttheme, dark: darktheme }; |
| 5 | +const themes = { |
| 6 | + light: lighttheme, |
| 7 | + dark: darktheme, |
| 8 | +}; |
| 9 | + |
| 10 | +const MagicScriptTag = ({ theme }) => { |
| 11 | + const themeJSON = JSON.stringify(theme); |
6 | 12 |
|
7 | | -const MagicScriptTag = (props) => { |
8 | | - // Injects CSS variables before first paint. The React hydration mismatch |
9 | | - // is resolved downstream in ThemeManager.js using window.__theme. |
| 13 | + // Injects CSS variables and theme state before the first paint to prevent FOUC. |
10 | 14 | const codeToRunOnClient = ` |
11 | | - (function() { |
12 | | - // 1. Keeps SYSTEM as the priority preference |
13 | | - const themeFromLocalStorage = localStorage.getItem('${DarkThemeKey}') || '${ThemeSetting.SYSTEM}'; |
14 | | -
|
15 | | - // 2. We change the check to look for LIGHT mode explicitly |
16 | | - const systemLightModeSetting = () => window.matchMedia ? window.matchMedia('(prefers-color-scheme: light)') : null; |
17 | | - |
18 | | - const isLightModeActive = () => { |
19 | | - return !!systemLightModeSetting()?.matches; |
20 | | - }; |
21 | | -
|
22 | | - let colorMode; |
23 | | - switch (themeFromLocalStorage) { |
24 | | - case '${ThemeSetting.SYSTEM}': |
25 | | - // LOGIC CHANGE: If Light is active -> Light. Otherwise (Dark, No Preference, or Error) -> Dark. |
26 | | - colorMode = isLightModeActive() ? '${ThemeSetting.LIGHT}' : '${ThemeSetting.DARK}' |
27 | | - break |
28 | | - case '${ThemeSetting.DARK}': |
29 | | - case '${ThemeSetting.LIGHT}': |
30 | | - colorMode = themeFromLocalStorage |
31 | | - break |
32 | | - default: |
33 | | - // 3. Fallback to DARK in case of error |
34 | | - colorMode = '${ThemeSetting.DARK}' |
35 | | - } |
36 | | -
|
37 | | - const root = document.documentElement; |
38 | | - const iterate = (obj) => { |
39 | | - if (!obj) return; |
40 | | - Object.keys(obj).forEach(key => { |
41 | | - if (typeof obj[key] === 'object') { |
42 | | - iterate(obj[key]) |
43 | | - } else { |
44 | | - root.style.setProperty("--" + key, obj[key]) |
45 | | - } |
46 | | - }) |
47 | | - } |
48 | | - const parsedTheme = JSON.parse('${JSON.stringify(props.theme)}') |
49 | | - const theme = parsedTheme[colorMode] |
50 | | - iterate(theme) |
51 | | - root.style.setProperty('--initial-color-mode', colorMode); |
52 | | - window.__theme = colorMode; |
53 | | - })() |
54 | | - `; |
| 15 | +(function() { |
| 16 | + // 1. Keeps SYSTEM as the priority preference |
| 17 | + try { |
| 18 | + var themeFromLocalStorage = localStorage.getItem('${DarkThemeKey}') || '${ThemeSetting.SYSTEM}'; |
| 19 | +
|
| 20 | + // 2. We change the check to look for LIGHT mode explicitly |
| 21 | + var systemLightModeSetting = function() { |
| 22 | + return window.matchMedia ? window.matchMedia('(prefers-color-scheme: light)') : null; |
| 23 | + }; |
| 24 | + var isLightModeActive = function() { |
| 25 | + var mql = systemLightModeSetting(); |
| 26 | + return mql ? mql.matches : false; |
| 27 | + }; |
| 28 | +
|
| 29 | + var colorMode; |
| 30 | + switch (themeFromLocalStorage) { |
| 31 | + case '${ThemeSetting.SYSTEM}': |
| 32 | + // LOGIC CHANGE: If Light is active -> Light. Otherwise (Dark, No Preference, or Error) -> Dark. |
| 33 | + colorMode = isLightModeActive() ? '${ThemeSetting.LIGHT}' : '${ThemeSetting.DARK}'; |
| 34 | + break; |
| 35 | + case '${ThemeSetting.DARK}': |
| 36 | + case '${ThemeSetting.LIGHT}': |
| 37 | + colorMode = themeFromLocalStorage; |
| 38 | + break; |
| 39 | + default: |
| 40 | + // 3. Fallback to DARK in case of error |
| 41 | + colorMode = '${ThemeSetting.DARK}'; |
| 42 | + } |
| 43 | +
|
| 44 | + var root = document.documentElement; |
| 45 | + var parsedTheme = ${themeJSON}; |
| 46 | + var selectedTheme = parsedTheme[colorMode]; |
| 47 | +
|
| 48 | + var iterate = function(obj) { |
| 49 | + if (!obj) return; |
| 50 | + Object.keys(obj).forEach(function(key) { |
| 51 | + if (typeof obj[key] === 'object' && obj[key] !== null) { |
| 52 | + iterate(obj[key]); |
| 53 | + } else { |
| 54 | + root.style.setProperty('--' + key, obj[key]); |
| 55 | + } |
| 56 | + }); |
| 57 | + }; |
| 58 | +
|
| 59 | + iterate(selectedTheme); |
| 60 | + root.style.setProperty('--initial-color-mode', colorMode); |
| 61 | +
|
| 62 | + // FIX: Setting data-theme is required for global CSS styles to apply correctly before React hydration. |
| 63 | + root.setAttribute('data-theme', colorMode); |
| 64 | +
|
| 65 | + // Sync the calculated theme globally so ThemeManager can pick it up immediately during hydration. |
| 66 | + window.__theme = colorMode; |
| 67 | +
|
| 68 | + } catch (e) {} |
| 69 | +})(); |
| 70 | +`; |
| 71 | + |
55 | 72 | return <script dangerouslySetInnerHTML={{ __html: codeToRunOnClient }} />; |
56 | 73 | }; |
57 | 74 |
|
58 | | -export const onRenderBody = ( { setPreBodyComponents }) => { |
59 | | - setPreBodyComponents(<MagicScriptTag key="theme-injection" theme={themes} />); |
| 75 | +// FIX: Using setHeadComponents instead of setPreBodyComponents ensures the script runs |
| 76 | +// in the <head>, blocking the first paint until the theme is applied and completely eliminating FOUC. |
| 77 | +export const onRenderBody = ({ setHeadComponents }) => { |
| 78 | + setHeadComponents([ |
| 79 | + <MagicScriptTag key="theme-initializer" theme={themes} />, |
| 80 | + ]); |
60 | 81 | }; |
0 commit comments