File tree 1 file changed +17
-46
lines changed
1 file changed +17
-46
lines changed Original file line number Diff line number Diff line change 19
19
}
20
20
21
21
constructor ( ) {
22
- if ( this . hasMode ) {
23
- if ( this . isDarkMode ) {
24
- if ( ! this . isSysDarkPrefer ) {
25
- this . setDark ( ) ;
26
- }
27
- } else {
28
- if ( this . isSysDarkPrefer ) {
29
- this . setLight ( ) ;
30
- }
31
- }
32
- }
33
-
34
22
let self = this ;
35
23
36
24
{ % - comment - % } always follow the system prefers { % - endcomment - % }
37
25
this . sysDarkPrefers . addEventListener ( 'change' , ( ) = > {
38
26
if ( self . hasMode ) {
39
- if ( self . isDarkMode ) {
40
- if ( ! self . isSysDarkPrefer ) {
41
- self . setDark ( ) ;
42
- }
43
- } else {
44
- if ( self . isSysDarkPrefer ) {
45
- self . setLight ( ) ;
46
- }
47
- }
48
-
49
27
self . clearMode ( ) ;
50
28
}
51
-
52
29
self . notify ( ) ;
53
30
} ) ;
54
- } { % - comment - % } constructor ( ) { % - endcomment - % }
31
+
32
+ if ( ! this . hasMode ) {
33
+ return ;
34
+ }
35
+
36
+ if ( this . isDarkMode ) {
37
+ this . setDark ( ) ;
38
+ } else {
39
+ this . setLight ( ) ;
40
+ }
41
+ }
55
42
56
43
get sysDarkPrefers ( ) {
57
44
return window . matchMedia ( '(prefers-color-scheme: dark)' ) ;
58
45
}
59
46
60
- get isSysDarkPrefer ( ) {
47
+ get isPreferDark ( ) {
61
48
return this . sysDarkPrefers . matches ;
62
49
}
63
50
64
51
get isDarkMode ( ) {
65
52
return this . mode === ModeToggle . DARK_MODE ;
66
53
}
67
54
68
- get isLightMode ( ) {
69
- return this . mode === ModeToggle . LIGHT_MODE ;
70
- }
71
-
72
55
get hasMode ( ) {
73
56
return this . mode != null ;
74
57
}
79
62
80
63
{ % - comment - % } get the current mode on screen { % - endcomment - % }
81
64
get modeStatus ( ) {
82
- if ( this . isDarkMode || ( ! this . hasMode && this . isSysDarkPrefer ) ) {
83
- return ModeToggle . DARK_MODE ;
65
+ if ( this . hasMode ) {
66
+ return this . mode ;
84
67
} else {
85
- return ModeToggle . LIGHT_MODE ;
68
+ return this . isPreferDark ? ModeToggle . DARK_MODE : ModeToggle . LIGHT_MODE ;
86
69
}
87
70
}
88
71
116
99
117
100
flipMode ( ) {
118
101
if ( this . hasMode ) {
119
- if ( this . isSysDarkPrefer ) {
120
- if ( this . isLightMode ) {
121
- this . clearMode ( ) ;
122
- } else {
123
- this . setLight ( ) ;
124
- }
125
- } else {
126
- if ( this . isDarkMode ) {
127
- this . clearMode ( ) ;
128
- } else {
129
- this . setDark ( ) ;
130
- }
131
- }
102
+ this . clearMode ( ) ;
132
103
} else {
133
- if ( this . isSysDarkPrefer ) {
104
+ if ( this . isPreferDark ) {
134
105
this . setLight ( ) ;
135
106
} else {
136
107
this . setDark ( ) ;
You can’t perform that action at this time.
0 commit comments