@@ -7,23 +7,31 @@ const themes = { light: lighttheme, dark: darktheme };
77const 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
4755export const onRenderBody = ( { setPreBodyComponents } ) => {
4856 setPreBodyComponents ( < MagicScriptTag key = "theme-injection" theme = { themes } /> ) ;
49- } ;
57+ } ;
0 commit comments