1
1
function changeTheme ( ) {
2
- const element = document . documentElement
3
- const theme = element . classList . contains ( "dark" ) ? "light" : "dark"
2
+ const element = document . documentElement ;
3
+ const theme = element . classList . contains ( "dark" ) ? "light" : "dark" ;
4
4
5
- const css = document . createElement ( "style" )
5
+ const css = document . createElement ( "style" ) ;
6
6
7
7
css . appendChild (
8
8
document . createTextNode (
@@ -14,56 +14,58 @@ function changeTheme() {
14
14
transition: none !important;
15
15
}` ,
16
16
) ,
17
- )
17
+ ) ;
18
18
19
- document . head . appendChild ( css )
19
+ document . head . appendChild ( css ) ;
20
20
21
21
if ( theme === "dark" ) {
22
- element . classList . add ( "dark" )
22
+ element . classList . add ( "dark" ) ;
23
23
} else {
24
- element . classList . remove ( "dark" )
24
+ element . classList . remove ( "dark" ) ;
25
25
}
26
26
27
- window . getComputedStyle ( css ) . opacity
27
+ window . getComputedStyle ( css ) . opacity ;
28
28
29
- document . head . removeChild ( css )
30
- localStorage . theme = theme
29
+ document . head . removeChild ( css ) ;
30
+ localStorage . theme = theme ;
31
31
}
32
32
33
33
function preloadTheme ( ) {
34
34
const theme = ( ( ) => {
35
- const userTheme = localStorage . theme
35
+ const userTheme = localStorage . theme ;
36
36
37
37
if ( userTheme === "light" || userTheme === "dark" ) {
38
- return userTheme
38
+ return userTheme ;
39
39
} else {
40
- return window . matchMedia ( "(prefers-color-scheme: dark)" ) . matches ? "dark" : "light"
40
+ return window . matchMedia ( "(prefers-color-scheme: dark)" ) . matches
41
+ ? "dark"
42
+ : "light" ;
41
43
}
42
- } ) ( )
44
+ } ) ( ) ;
43
45
44
- const element = document . documentElement
46
+ const element = document . documentElement ;
45
47
46
48
if ( theme === "dark" ) {
47
- element . classList . add ( "dark" )
49
+ element . classList . add ( "dark" ) ;
48
50
} else {
49
- element . classList . remove ( "dark" )
51
+ element . classList . remove ( "dark" ) ;
50
52
}
51
53
52
- localStorage . theme = theme
54
+ localStorage . theme = theme ;
53
55
}
54
56
55
57
window . onload = ( ) => {
56
58
function initializeThemeButtons ( ) {
57
- const headerThemeButton = document . getElementById ( "header-theme-button" )
58
- const drawerThemeButton = document . getElementById ( "drawer-theme-button" )
59
- headerThemeButton ?. addEventListener ( "click" , changeTheme )
60
- drawerThemeButton ?. addEventListener ( "click" , changeTheme )
59
+ const headerThemeButton = document . getElementById ( "header-theme-button" ) ;
60
+ const drawerThemeButton = document . getElementById ( "drawer-theme-button" ) ;
61
+ headerThemeButton ?. addEventListener ( "click" , changeTheme ) ;
62
+ drawerThemeButton ?. addEventListener ( "click" , changeTheme ) ;
61
63
}
62
64
63
- document . addEventListener ( "astro:after-swap" , initializeThemeButtons )
64
- initializeThemeButtons ( )
65
- }
65
+ document . addEventListener ( "astro:after-swap" , initializeThemeButtons ) ;
66
+ initializeThemeButtons ( ) ;
67
+ } ;
66
68
67
- document . addEventListener ( "astro:after-swap" , preloadTheme )
69
+ document . addEventListener ( "astro:after-swap" , preloadTheme ) ;
68
70
69
- preloadTheme ( )
71
+ preloadTheme ( ) ;
0 commit comments