15
15
*/
16
16
17
17
// @license © 2019 Google LLC. Licensed under the Apache License, Version 2.0.
18
- const win = window ;
19
18
const doc = document ;
20
- const store = win . localStorage ;
19
+ const store = localStorage ;
21
20
const PREFERS_COLOR_SCHEME = 'prefers-color-scheme' ;
22
21
const MEDIA = 'media' ;
23
22
const LIGHT = 'light' ;
@@ -107,7 +106,7 @@ export class DarkModeToggle extends HTMLElement {
107
106
108
107
doc . addEventListener ( PERMANENT_COLOR_SCHEME , ( event ) => {
109
108
this . permanent = event . detail . permanent ;
110
- this . permanentCheckbox . checked = this . permanent ;
109
+ this . _permanentCheckbox . checked = this . permanent ;
111
110
} ) ;
112
111
113
112
this . _initializeDOM ( ) ;
@@ -122,26 +121,26 @@ export class DarkModeToggle extends HTMLElement {
122
121
// We need to support `media="(prefers-color-scheme: dark)"` (with space)
123
122
// and `media="(prefers-color-scheme:dark)"` (without space)
124
123
this . _darkCSS = doc . querySelectorAll ( `${ LINK_REL_STYLESHEET } [${ MEDIA } *=${ PREFERS_COLOR_SCHEME } ][${ MEDIA } *="${ DARK } "]` ) ;
125
- this . _lightCSS = document . querySelectorAll ( `${ LINK_REL_STYLESHEET } [${ MEDIA } *=${ PREFERS_COLOR_SCHEME } ][${ MEDIA } *="${ LIGHT } "],${ LINK_REL_STYLESHEET } [${ MEDIA } *=${ PREFERS_COLOR_SCHEME } ][${ MEDIA } *="${ NO_PREFERENCE } "]` ) ;
124
+ this . _lightCSS = doc . querySelectorAll ( `${ LINK_REL_STYLESHEET } [${ MEDIA } *=${ PREFERS_COLOR_SCHEME } ][${ MEDIA } *="${ LIGHT } "],${ LINK_REL_STYLESHEET } [${ MEDIA } *=${ PREFERS_COLOR_SCHEME } ][${ MEDIA } *="${ NO_PREFERENCE } "]` ) ;
126
125
127
126
// Get DOM references.
128
- this . lightRadio = shadowRoot . querySelector ( '#lightRadio' ) ;
129
- this . lightLabel = shadowRoot . querySelector ( '#lightLabel' ) ;
130
- this . darkRadio = shadowRoot . querySelector ( '#darkRadio' ) ;
131
- this . darkLabel = shadowRoot . querySelector ( '#darkLabel' ) ;
132
- this . darkCheckbox = shadowRoot . querySelector ( '#darkCheckbox' ) ;
133
- this . checkboxLabel = shadowRoot . querySelector ( '#checkboxLabel' ) ;
134
- this . legendLabel = shadowRoot . querySelector ( 'legend' ) ;
135
- this . permanentAside = shadowRoot . querySelector ( 'aside' ) ;
136
- this . permanentCheckbox = shadowRoot . querySelector ( '#permanentCheckbox' ) ;
137
- this . permanentLabel = shadowRoot . querySelector ( '#permanentLabel' ) ;
127
+ this . _lightRadio = shadowRoot . querySelector ( '#lightRadio' ) ;
128
+ this . _lightLabel = shadowRoot . querySelector ( '#lightLabel' ) ;
129
+ this . _darkRadio = shadowRoot . querySelector ( '#darkRadio' ) ;
130
+ this . _darkLabel = shadowRoot . querySelector ( '#darkLabel' ) ;
131
+ this . _darkCheckbox = shadowRoot . querySelector ( '#darkCheckbox' ) ;
132
+ this . _checkboxLabel = shadowRoot . querySelector ( '#checkboxLabel' ) ;
133
+ this . _legendLabel = shadowRoot . querySelector ( 'legend' ) ;
134
+ this . _permanentAside = shadowRoot . querySelector ( 'aside' ) ;
135
+ this . _permanentCheckbox = shadowRoot . querySelector ( '#permanentCheckbox' ) ;
136
+ this . _permanentLabel = shadowRoot . querySelector ( '#permanentLabel' ) ;
138
137
139
138
// Does the browser support native `prefers-color-scheme`?
140
139
const hasNativePrefersColorScheme =
141
- win . matchMedia ( MQ_DARK ) . media !== NOT_ALL ;
140
+ matchMedia ( MQ_DARK ) . media !== NOT_ALL ;
142
141
// Listen to `prefers-color-scheme` changes, unless `permanent` is true.
143
142
if ( hasNativePrefersColorScheme ) {
144
- win . matchMedia ( MQ_DARK ) . addListener ( ( { matches} ) => {
143
+ matchMedia ( MQ_DARK ) . addListener ( ( { matches} ) => {
145
144
if ( ! this . permanent ) {
146
145
this . mode = matches ? DARK : LIGHT ;
147
146
this . _dispatchEvent ( COLOR_SCHEME_CHANGE , { colorScheme : this . mode } ) ;
@@ -154,13 +153,13 @@ export class DarkModeToggle extends HTMLElement {
154
153
const rememberedValue = store . getItem ( NAME ) ;
155
154
if ( rememberedValue && [ DARK , LIGHT ] . includes ( rememberedValue ) ) {
156
155
this . mode = rememberedValue ;
157
- this . permanentCheckbox . checked = true ;
156
+ this . _permanentCheckbox . checked = true ;
158
157
this . permanent = true ;
159
158
} else if ( hasNativePrefersColorScheme ) {
160
- if ( ( win . matchMedia ( MQ_LIGHT [ 0 ] ) . matches ) ||
161
- ( win . matchMedia ( MQ_LIGHT [ 1 ] ) . matches ) ) {
159
+ if ( ( matchMedia ( MQ_LIGHT [ 0 ] ) . matches ) ||
160
+ ( matchMedia ( MQ_LIGHT [ 1 ] ) . matches ) ) {
162
161
this . mode = LIGHT ;
163
- } else if ( win . matchMedia ( MQ_DARK ) . matches ) {
162
+ } else if ( matchMedia ( MQ_DARK ) . matches ) {
164
163
this . mode = DARK ;
165
164
}
166
165
}
@@ -186,22 +185,22 @@ export class DarkModeToggle extends HTMLElement {
186
185
this . _updateCheckbox ( ) ;
187
186
188
187
// Synchronize the behavior of the radio and the checkbox.
189
- [ this . lightRadio , this . darkRadio ] . forEach ( ( input ) => {
188
+ [ this . _lightRadio , this . _darkRadio ] . forEach ( ( input ) => {
190
189
input . addEventListener ( 'change' , ( ) => {
191
- this . mode = this . lightRadio . checked ? LIGHT : DARK ;
190
+ this . mode = this . _lightRadio . checked ? LIGHT : DARK ;
192
191
this . _updateCheckbox ( ) ;
193
192
this . _dispatchEvent ( COLOR_SCHEME_CHANGE , { colorScheme : this . mode } ) ;
194
193
} ) ;
195
194
} ) ;
196
- this . darkCheckbox . addEventListener ( 'change' , ( ) => {
197
- this . mode = this . darkCheckbox . checked ? DARK : LIGHT ;
195
+ this . _darkCheckbox . addEventListener ( 'change' , ( ) => {
196
+ this . mode = this . _darkCheckbox . checked ? DARK : LIGHT ;
198
197
this . _updateRadios ( ) ;
199
198
this . _dispatchEvent ( COLOR_SCHEME_CHANGE , { colorScheme : this . mode } ) ;
200
199
} ) ;
201
200
202
201
// Make remembering the last mode optional
203
- this . permanentCheckbox . addEventListener ( 'change' , ( ) => {
204
- this . permanent = this . permanentCheckbox . checked ;
202
+ this . _permanentCheckbox . addEventListener ( 'change' , ( ) => {
203
+ this . permanent = this . _permanentCheckbox . checked ;
205
204
this . _dispatchEvent ( PERMANENT_COLOR_SCHEME , {
206
205
permanent : this . permanent ,
207
206
} ) ;
@@ -222,7 +221,7 @@ export class DarkModeToggle extends HTMLElement {
222
221
}
223
222
// Only show the dialog programmatically on devices not capable of hover
224
223
// and only if there is a label
225
- if ( win . matchMedia ( '(hover:none)' ) . matches && this . remember ) {
224
+ if ( matchMedia ( '(hover:none)' ) . matches && this . remember ) {
226
225
this . _showPermanentAside ( ) ;
227
226
}
228
227
if ( this . permanent ) {
@@ -242,20 +241,20 @@ export class DarkModeToggle extends HTMLElement {
242
241
} else {
243
242
store . removeItem ( NAME ) ;
244
243
}
245
- this . permanentCheckbox . checked = this . permanent ;
244
+ this . _permanentCheckbox . checked = this . permanent ;
246
245
} else if ( name === LEGEND ) {
247
- this . legendLabel . textContent = newValue ;
246
+ this . _legendLabel . textContent = newValue ;
248
247
} else if ( name === REMEMBER ) {
249
- this . permanentLabel . textContent = newValue ;
248
+ this . _permanentLabel . textContent = newValue ;
250
249
} else if ( name === LIGHT ) {
251
- this . lightLabel . textContent = newValue ;
250
+ this . _lightLabel . textContent = newValue ;
252
251
if ( this . mode === LIGHT ) {
253
- this . checkboxLabel . textContent = newValue ;
252
+ this . _checkboxLabel . textContent = newValue ;
254
253
}
255
254
} else if ( name === DARK ) {
256
- this . darkLabel . textContent = newValue ;
255
+ this . _darkLabel . textContent = newValue ;
257
256
if ( this . mode === DARK ) {
258
- this . checkboxLabel . textContent = newValue ;
257
+ this . _checkboxLabel . textContent = newValue ;
259
258
}
260
259
}
261
260
}
@@ -272,33 +271,33 @@ export class DarkModeToggle extends HTMLElement {
272
271
// Hide or show the light-related affordances dependent on the appearance,
273
272
// which can be "switch" or "toggle".
274
273
const appearAsToggle = this . appearance === TOGGLE ;
275
- this . lightRadio . hidden = appearAsToggle ;
276
- this . lightLabel . hidden = appearAsToggle ;
277
- this . darkRadio . hidden = appearAsToggle ;
278
- this . darkLabel . hidden = appearAsToggle ;
279
- this . darkCheckbox . hidden = ! appearAsToggle ;
280
- this . checkboxLabel . hidden = ! appearAsToggle ;
274
+ this . _lightRadio . hidden = appearAsToggle ;
275
+ this . _lightLabel . hidden = appearAsToggle ;
276
+ this . _darkRadio . hidden = appearAsToggle ;
277
+ this . _darkLabel . hidden = appearAsToggle ;
278
+ this . _darkCheckbox . hidden = ! appearAsToggle ;
279
+ this . _checkboxLabel . hidden = ! appearAsToggle ;
281
280
}
282
281
283
282
_updateRadios ( ) {
284
283
if ( this . mode === LIGHT ) {
285
- this . lightRadio . checked = true ;
284
+ this . _lightRadio . checked = true ;
286
285
} else {
287
- this . darkRadio . checked = true ;
286
+ this . _darkRadio . checked = true ;
288
287
}
289
288
}
290
289
291
290
_updateCheckbox ( ) {
292
291
if ( this . mode === LIGHT ) {
293
- this . checkboxLabel . style . setProperty ( `--${ NAME } -checkbox-icon` ,
292
+ this . _checkboxLabel . style . setProperty ( `--${ NAME } -checkbox-icon` ,
294
293
`var(--${ NAME } -light-icon,url("${ DEFAULT_URL } moon.png"))` ) ;
295
- this . checkboxLabel . textContent = this . light ;
296
- this . darkCheckbox . checked = false ;
294
+ this . _checkboxLabel . textContent = this . light ;
295
+ this . _darkCheckbox . checked = false ;
297
296
} else {
298
- this . checkboxLabel . style . setProperty ( `--${ NAME } -checkbox-icon` ,
297
+ this . _checkboxLabel . style . setProperty ( `--${ NAME } -checkbox-icon` ,
299
298
`var(--${ NAME } -dark-icon,url("${ DEFAULT_URL } sun.png"))` ) ;
300
- this . checkboxLabel . textContent = this . dark ;
301
- this . darkCheckbox . checked = true ;
299
+ this . _checkboxLabel . textContent = this . dark ;
300
+ this . _darkCheckbox . checked = true ;
302
301
}
303
302
}
304
303
@@ -325,11 +324,11 @@ export class DarkModeToggle extends HTMLElement {
325
324
}
326
325
327
326
_showPermanentAside ( ) {
328
- this . permanentAside . style . visibility = 'visible' ;
329
- win . setTimeout ( ( ) => {
330
- this . permanentAside . style . visibility = 'hidden' ;
327
+ this . _permanentAside . style . visibility = 'visible' ;
328
+ setTimeout ( ( ) => {
329
+ this . _permanentAside . style . visibility = 'hidden' ;
331
330
} , 3000 ) ;
332
331
}
333
332
}
334
333
335
- win . customElements . define ( NAME , DarkModeToggle ) ;
334
+ customElements . define ( NAME , DarkModeToggle ) ;
0 commit comments