Skip to content

Commit e16317a

Browse files
authored
Merge branch 'master' into M-DEV-1/replace-images-on-colors-page
2 parents 83a93c4 + 6365678 commit e16317a

3 files changed

Lines changed: 67 additions & 18 deletions

File tree

src/html.js

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import React from "react";
22
import PropTypes from "prop-types";
33

4-
54
export default function HTML(props) {
65
return (
76
<html lang="en" {...props.htmlAttributes}>
@@ -24,20 +23,45 @@ export default function HTML(props) {
2423
{props.headComponents}
2524
</head>
2625
<body {...props.bodyAttributes}>
27-
<script dangerouslySetInnerHTML={{
28-
__html:
29-
`(function() {
30-
try {
31-
var banner = sessionStorage.getItem('banner');
32-
if (banner === null)
33-
document.body.classList.add('banner1');
34-
else
35-
document.body.classList.add('banner' + banner);
36-
} catch (e) {
37-
return;
38-
}
39-
})();`,
40-
}}
26+
{/* Script for theme initialization - needs to run before React renders to prevent flicker */}
27+
<script
28+
dangerouslySetInnerHTML={{
29+
__html: `
30+
(function() {
31+
try {
32+
// Theme initialization
33+
const darkThemeKey = 'theme';
34+
let initialTheme = 'system';
35+
try {
36+
initialTheme = localStorage.getItem(darkThemeKey) || 'system';
37+
} catch (e) {}
38+
39+
// Determine initial dark mode
40+
let isDarkMode = false;
41+
if (initialTheme === 'dark') {
42+
isDarkMode = true;
43+
} else if (initialTheme === 'system') {
44+
isDarkMode = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
45+
}
46+
47+
// Set initial color mode
48+
document.documentElement.style.setProperty(
49+
'--initial-color-mode',
50+
isDarkMode ? 'dark' : 'light'
51+
);
52+
53+
// Banner initialization
54+
var banner = sessionStorage.getItem('banner');
55+
if (banner === null)
56+
document.body.classList.add('banner1');
57+
else
58+
document.body.classList.add('banner' + banner);
59+
} catch (e) {
60+
console.error('Error in theme initialization:', e);
61+
}
62+
})();
63+
`,
64+
}}
4165
/>
4266
{props.preBodyComponents}
4367
<div

src/theme/app/StyledThemeProvider.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,18 @@ import React, { useContext } from "react";
55
import { ThemeProvider } from "styled-components";
66
import { ThemeManagerContext } from "./ThemeManager";
77

8+
// Safe check for browser environment
9+
const isBrowser = typeof window !== "undefined";
10+
811
export const StyledThemeProvider = (props) => {
912
const { children, darkTheme, lightTheme } = props;
1013
const { isDark, didLoad } = useContext(ThemeManagerContext);
14+
15+
// For SSR, we need to provide a consistent theme initially
16+
// This ensures the server and client render the same thing initially
1117
const currentTheme = isDark ? darkTheme : lightTheme;
1218
const theme = {
13-
...(didLoad ? currentTheme : transformTheme(currentTheme)),
19+
...(didLoad || !isBrowser ? currentTheme : transformTheme(currentTheme)),
1420
};
1521

1622
return (

src/theme/app/ThemeManager.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@ const defaultState = {
2222

2323
export const ThemeManagerContext = createContext(defaultState);
2424

25+
// Safe check for browser environment
26+
const isBrowser = typeof window !== "undefined";
27+
2528
const systemDarkModeSetting = () =>
26-
window.matchMedia ? window.matchMedia("(prefers-color-scheme: dark)") : null;
29+
isBrowser && window.matchMedia ? window.matchMedia("(prefers-color-scheme: dark)") : null;
2730
const isDarkModeActive = () => {
2831
// Assume that dark mode is not active if there's no system dark mode setting available
2932
return !!systemDarkModeSetting()?.matches;
@@ -36,15 +39,29 @@ export const ThemeManagerProvider = (props) => {
3639
const [isDark, setIsDark] = useState(false);
3740

3841
useEffect(() => {
42+
if (!isBrowser) return;
43+
3944
const root = window.document.documentElement;
4045
const initialColorValue = root.style.getPropertyValue(
4146
"--initial-color-mode"
4247
);
4348
setIsDark(initialColorValue === ThemeSetting.DARK);
4449
setDidLoad(true);
45-
}, []);
50+
51+
// Add listener for system color scheme changes
52+
const darkModeMediaQuery = systemDarkModeSetting();
53+
if (darkModeMediaQuery && themeSetting === ThemeSetting.SYSTEM) {
54+
const handleChange = (e) => {
55+
setIsDark(e.matches);
56+
};
57+
darkModeMediaQuery.addEventListener("change", handleChange);
58+
return () => darkModeMediaQuery.removeEventListener("change", handleChange);
59+
}
60+
}, [themeSetting]);
4661

4762
const toggleDark = (value) => {
63+
if (!isBrowser) return;
64+
4865
const newIsDark = value ?? !isDark;
4966
const theme = newIsDark ? ThemeSetting.DARK : ThemeSetting.LIGHT;
5067
setIsDark(newIsDark);
@@ -53,6 +70,8 @@ export const ThemeManagerProvider = (props) => {
5370
};
5471

5572
const changeThemeSetting = (setting) => {
73+
if (!isBrowser) return;
74+
5675
switch (setting) {
5776
case ThemeSetting.SYSTEM: {
5877
setIsDark(isDarkModeActive());

0 commit comments

Comments
 (0)