File tree Expand file tree Collapse file tree 1 file changed +34
-9
lines changed Expand file tree Collapse file tree 1 file changed +34
-9
lines changed Original file line number Diff line number Diff line change 33 < head >
44 < script >
55 ( function ( ) {
6- const savedTheme = localStorage . getItem ( 'theme' ) || 'light' ;
7- if (
8- savedTheme . includes ( 'dark' ) ||
9- ( savedTheme === 'system' &&
10- window . matchMedia &&
11- window . matchMedia ( '(prefers-color-scheme: dark)' ) . matches )
12- ) {
13- document . documentElement . style . backgroundColor = '#2d2c33' ;
14- }
6+ try {
7+ const savedTheme = localStorage . getItem ( 'theme' ) || 'light' ;
8+ const isDarkThemePreferred = window ?. matchMedia ?. (
9+ '(prefers-color-scheme:dark)' ,
10+ ) ?. matches ;
11+
12+ let theme ;
13+ if ( ! savedTheme || savedTheme === 'system' ) {
14+ theme = isDarkThemePreferred ? 'dark' : 'light' ;
15+ } else {
16+ theme = savedTheme ;
17+ }
18+
19+ const observer = new MutationObserver ( ( mutations , obs ) => {
20+ if ( document . body ) {
21+ try {
22+ const themeClass = [ ...document . body . classList ] . find ( ( cl ) =>
23+ cl . startsWith ( 'g-root_theme' ) ,
24+ ) ;
25+ if ( themeClass ) {
26+ document . body . classList . remove ( themeClass ) ;
27+ }
28+ document . body . classList . add ( `g-root_theme_${ theme } ` ) ;
29+ } catch ( e ) {
30+ } finally {
31+ obs . disconnect ( ) ;
32+ }
33+ }
34+ } ) ;
35+
36+ observer . observe ( document . documentElement , {
37+ childList : true ,
38+ } ) ;
39+ } catch ( e ) { }
1540 } ) ( ) ;
1641 </ script >
1742 < meta charset ="utf-8 " />
You can’t perform that action at this time.
0 commit comments