@@ -32,8 +32,8 @@ export default class Darkmode {
32
32
33
33
// Listen for prefers-color-scheme change
34
34
if ( options . autoMatchOsTheme ) {
35
- window . matchMedia ( '(prefers-color-scheme: dark)' ) . addListener ( ( e ) => e . matches && this . _switchThemePrefers ( ) )
36
- window . matchMedia ( '(prefers-color-scheme: light)' ) . addListener ( ( e ) => e . matches && this . _switchThemePrefers ( ) )
35
+ window . matchMedia ( '(prefers-color-scheme: dark)' ) . addListener ( ( e ) => e . matches && this . _handlePreferedThemeChangeEvent ( ) )
36
+ window . matchMedia ( '(prefers-color-scheme: light)' ) . addListener ( ( e ) => e . matches && this . _handlePreferedThemeChangeEvent ( ) )
37
37
}
38
38
39
39
// Determine to which theme should be set, start with default, precendence based on descending order
@@ -176,19 +176,39 @@ export default class Darkmode {
176
176
return this . dark ? 'dark' : 'light'
177
177
}
178
178
179
+
180
+ /**
181
+ * Check if the system theme is dark
182
+ * @private
183
+ * @returns {boolean } isDark - true if system theme is dark
184
+ */
179
185
_preferedThemeIsDark ( ) {
180
186
return window . matchMedia && window . matchMedia ( '(prefers-color-scheme: dark)' ) . matches
181
187
}
182
188
183
- _switchThemePrefers ( ) {
189
+ /**
190
+ * Switch the current theme if the prefered system theme changes
191
+ * @private
192
+ */
193
+ _handlePreferedThemeChangeEvent ( ) {
184
194
const val = this . _preferedThemeIsDark ( )
185
195
this . _changeThemeToDark ( val )
186
196
}
187
197
198
+ /**
199
+ * Change theme to dark if true, else change to light
200
+ * @private
201
+ * @param {boolean } toDark - change theme to dark
202
+ */
188
203
_changeThemeToDark ( toDark ) {
189
204
toDark ? this . toDark ( ) : this . toLight ( )
190
205
}
191
206
207
+ /**
208
+ * Save the current theme choice in either local storage as a cookie
209
+ * @private
210
+ * @param {boolean } value - true for dark, false for light
211
+ */
192
212
_setStorageValue ( value ) {
193
213
if ( this . options . localStorage && window . localStorage !== null ) {
194
214
window . localStorage . setItem ( 'darkmode' , value )
@@ -201,6 +221,11 @@ export default class Darkmode {
201
221
}
202
222
}
203
223
224
+ /**
225
+ * Add the specified styles as a new stylesheet to the document
226
+ * @private
227
+ * @param {sring } css - the css which should be added to the document
228
+ */
204
229
_addStyle ( css ) {
205
230
const linkElement = document . createElement ( 'link' )
206
231
0 commit comments