Skip to content

Commit ed0aa74

Browse files
authored
Refactor theme injection logic in onRenderBody.js
Refactor theme handling logic in MagicScriptTag component. Update the way themes are injected into the document and change the function signature for onRenderBody. Signed-off-by: Ankit Rewar <171806101+AnkitRewar11@users.noreply.github.com>
1 parent 21df90e commit ed0aa74

File tree

1 file changed

+51
-61
lines changed

1 file changed

+51
-61
lines changed

onRenderBody.js

Lines changed: 51 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,70 +1,60 @@
11
import React from "react";
2-
import { DarkThemeKey, ThemeSetting } from "./src/theme/app/ThemeManager";
2+
import { DarkThemeKey, ThemeSetting } from "./src/theme/app/ThemeManager.js";
33
import lighttheme, { darktheme } from "./src/theme/app/themeStyles";
44

5-
const themes = {
6-
light: lighttheme,
7-
dark: darktheme,
8-
};
9-
10-
const MagicScriptTag = ({ theme }) => {
11-
const themeJSON = JSON.stringify(theme);
5+
const themes = { light: lighttheme, dark: darktheme };
126

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.
1310
const codeToRunOnClient = `
14-
(function() {
15-
try {
16-
var themeFromLocalStorage = localStorage.getItem('${DarkThemeKey}') || '${ThemeSetting.SYSTEM}';
17-
var systemLightModeSetting = function() {
18-
return window.matchMedia ? window.matchMedia('(prefers-color-scheme: light)') : null;
19-
};
20-
var isLightModeActive = function() {
21-
var mql = systemLightModeSetting();
22-
return mql ? mql.matches : false;
23-
};
24-
25-
var colorMode;
26-
switch (themeFromLocalStorage) {
27-
case '${ThemeSetting.SYSTEM}':
28-
colorMode = isLightModeActive() ? '${ThemeSetting.LIGHT}' : '${ThemeSetting.DARK}';
29-
break;
30-
case '${ThemeSetting.DARK}':
31-
case '${ThemeSetting.LIGHT}':
32-
colorMode = themeFromLocalStorage;
33-
break;
34-
default:
35-
colorMode = '${ThemeSetting.DARK}';
36-
}
37-
38-
var root = document.documentElement;
39-
var parsedTheme = ${themeJSON};
40-
var selectedTheme = parsedTheme[colorMode];
41-
42-
var iterate = function(obj) {
43-
if (!obj) return;
44-
Object.keys(obj).forEach(function(key) {
45-
if (typeof obj[key] === 'object' && obj[key] !== null) {
46-
iterate(obj[key]);
47-
} else {
48-
root.style.setProperty('--' + key, obj[key]);
49-
}
50-
});
51-
};
52-
53-
iterate(selectedTheme);
54-
root.style.setProperty('--initial-color-mode', colorMode);
55-
root.setAttribute('data-theme', colorMode);
56-
57-
window.__theme = colorMode;
58-
59-
} catch (e) {}
60-
})();
61-
`;
62-
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+
`;
6355
return <script dangerouslySetInnerHTML={{ __html: codeToRunOnClient }} />;
6456
};
6557

66-
export const onRenderBody = ({ setHeadComponents }) => {
67-
setHeadComponents([
68-
<MagicScriptTag key="theme-initializer" theme={themes} />,
69-
]);
58+
export const onRenderBody = ( { setPreBodyComponents }) => {
59+
setPreBodyComponents(<MagicScriptTag key="theme-injection" theme={themes} />);
7060
};

0 commit comments

Comments
 (0)