1
+ import React from "react" ;
2
+ import { DarkThemeKey , ThemeSetting } from "./src/theme/app/ThemeManager.js" ;
3
+ import lighttheme , { darktheme } from "./src/theme/app/themeStyles" ;
4
+
5
+ const themes = { light : lighttheme , dark : darktheme } ;
6
+
7
+ const MagicScriptTag = ( props ) => {
8
+ const codeToRunOnClient = `
9
+ (function() {
10
+ 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;
14
+ };
15
+ let colorMode;
16
+ switch (themeFromLocalStorage) {
17
+ case '${ ThemeSetting . SYSTEM } ':
18
+ colorMode = isDarkModeActive() ? '${ ThemeSetting . DARK } ' : '{ThemeSetting.LIGHT}'
19
+ break
20
+ case '${ ThemeSetting . DARK } ':
21
+ case '${ ThemeSetting . LIGHT } ':
22
+ colorMode = themeFromLocalStorage
23
+ break
24
+ default:
25
+ colorMode = '${ ThemeSetting . LIGHT } '
26
+ }
27
+ const root = document.documentElement;
28
+ const iterate = (obj) => {
29
+ Object.keys(obj).forEach(key => {
30
+ if (typeof obj[key] === 'object') {
31
+ iterate(obj[key])
32
+ } else {
33
+ root.style.setProperty("--" + key, obj[key])
34
+ }
35
+ })
36
+ }
37
+ const parsedTheme = JSON.parse('${ JSON . stringify ( props . theme ) } ')
38
+ const theme = parsedTheme[colorMode]
39
+ iterate(theme)
40
+ root.style.setProperty('--initial-color-mode', colorMode);
41
+ })()
42
+ ` ;
43
+ return < script dangerouslySetInnerHTML = { { __html : codeToRunOnClient } } /> ;
44
+ } ;
45
+
46
+ export const onRenderBody = ( { setPreBodyComponents } ) => {
47
+ setPreBodyComponents ( < MagicScriptTag key = "theme-injection" theme = { themes } /> ) ;
48
+ } ;
0 commit comments