File tree 1 file changed +15
-4
lines changed
1 file changed +15
-4
lines changed Original file line number Diff line number Diff line change 1
- let lightMode = true ;
2
- const lightModeSelectorContainer = document . getElementById ( 'light-mode-selector-container' ) ;
3
- lightModeSelectorContainer . addEventListener ( 'click' , ( ) => {
1
+ let lightMode ;
2
+
3
+ if ( window . matchMedia && window . matchMedia ( '(prefers-color-scheme: dark)' ) . matches ) {
4
+ lightMode = false ;
5
+ } else {
6
+ lightMode = true ;
7
+ }
8
+
9
+ function changeLightMode ( ) {
4
10
const styledElements = [ ...document . getElementsByClassName ( lightMode ? 'light' : 'dark' ) ] ;
5
11
for ( const element of styledElements ) {
6
12
element . classList . remove ( lightMode ? 'light' : 'dark' ) ;
7
13
element . classList . add ( lightMode ? 'dark' : 'light' ) ;
8
14
}
9
15
lightMode = ! lightMode ;
10
- } ) ;
16
+ }
17
+
18
+ changeLightMode ( ) ;
19
+
20
+ const lightModeSelectorContainer = document . getElementById ( 'light-mode-selector-container' ) ;
21
+ lightModeSelectorContainer . addEventListener ( 'click' , changeLightMode ) ;
You can’t perform that action at this time.
0 commit comments