Skip to content

Commit 357553e

Browse files
authored
Change theme logic to prioritize light mode
Updated color mode logic to prioritize light mode when active. Signed-off-by: Lee Calcote <leecalcote@gmail.com>
1 parent 4ecf25d commit 357553e

1 file changed

Lines changed: 14 additions & 6 deletions

File tree

onRenderBody.js

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,31 @@ const themes = { light: lighttheme, dark: darktheme };
77
const MagicScriptTag = (props) => {
88
const codeToRunOnClient = `
99
(function() {
10+
// 1. Keeps SYSTEM as the priority preference
1011
const themeFromLocalStorage = localStorage.getItem('${DarkThemeKey}') || '${ThemeSetting.SYSTEM}';
11-
const systemDarkModeSetting = () => window.matchMedia ? window.matchMedia('(prefers-color-scheme: dark)') : null;
12-
const isDarkModeActive = () => {
13-
return !!systemDarkModeSetting()?.matches;
12+
13+
// 2. We change the check to look for LIGHT mode explicitly
14+
const systemLightModeSetting = () => window.matchMedia ? window.matchMedia('(prefers-color-scheme: light)') : null;
15+
16+
const isLightModeActive = () => {
17+
return !!systemLightModeSetting()?.matches;
1418
};
19+
1520
let colorMode;
1621
switch (themeFromLocalStorage) {
1722
case '${ThemeSetting.SYSTEM}':
18-
colorMode = isDarkModeActive() ? '${ThemeSetting.DARK}' : '${ThemeSetting.LIGHT}'
23+
// LOGIC CHANGE: If Light is active -> Light. Otherwise (Dark, No Preference, or Error) -> Dark.
24+
colorMode = isLightModeActive() ? '${ThemeSetting.LIGHT}' : '${ThemeSetting.DARK}'
1925
break
2026
case '${ThemeSetting.DARK}':
2127
case '${ThemeSetting.LIGHT}':
2228
colorMode = themeFromLocalStorage
2329
break
2430
default:
25-
colorMode = '${ThemeSetting.LIGHT}'
31+
// 3. Fallback to DARK in case of error
32+
colorMode = '${ThemeSetting.DARK}'
2633
}
34+
2735
const root = document.documentElement;
2836
const iterate = (obj) => {
2937
if (!obj) return;
@@ -46,4 +54,4 @@ const MagicScriptTag = (props) => {
4654

4755
export const onRenderBody = ( { setPreBodyComponents }) => {
4856
setPreBodyComponents(<MagicScriptTag key="theme-injection" theme={themes} />);
49-
};
57+
};

0 commit comments

Comments
 (0)