File tree Expand file tree Collapse file tree 1 file changed +13
-5
lines changed Expand file tree Collapse file tree 1 file changed +13
-5
lines changed Original file line number Diff line number Diff line change 46
46
}
47
47
48
48
function isInitialThemeDark ( ) {
49
- return (
50
- localStorage . getItem ( "theme" ) === "dark" || prefersDarkColorScheme . matches
51
- ) ;
49
+ return loadTheme ( ) === "dark" || prefersDarkColorScheme . matches ;
52
50
}
53
51
54
52
function onThemeChange ( ) {
55
53
if ( this . checked ) {
56
54
delay ( function ( ) {
57
55
htmlElement . classList . add ( "dark-theme" ) ;
58
56
} , 100 ) ;
59
- localStorage . setItem ( "theme" , "dark" ) ;
57
+ saveTheme ( "dark" ) ;
60
58
} else {
61
59
delay ( function ( ) {
62
60
htmlElement . classList . remove ( "dark-theme" ) ;
63
61
} , 100 ) ;
64
- localStorage . setItem ( "theme" , "light" ) ;
62
+ saveTheme ( "light" ) ;
63
+ }
64
+ }
65
+
66
+ function saveTheme ( theme ) {
67
+ if ( localStorage ) {
68
+ localStorage . setItem ( "theme" , theme ) ;
65
69
}
66
70
}
71
+
72
+ function loadTheme ( ) {
73
+ return localStorage !== null ? localStorage . getItem ( "theme" ) : null ;
74
+ }
67
75
} ) ( ) ;
You can’t perform that action at this time.
0 commit comments