Skip to content

Commit 8bed3dd

Browse files
committed
💡 Add more jsdoc comments
1 parent 4ebce82 commit 8bed3dd

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

dist/drkmd-js.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/drkmd.js

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ export default class Darkmode {
3232

3333
// Listen for prefers-color-scheme change
3434
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())
3737
}
3838

3939
// Determine to which theme should be set, start with default, precendence based on descending order
@@ -176,19 +176,39 @@ export default class Darkmode {
176176
return this.dark ? 'dark' : 'light'
177177
}
178178

179+
180+
/**
181+
* Check if the system theme is dark
182+
* @private
183+
* @returns {boolean} isDark - true if system theme is dark
184+
*/
179185
_preferedThemeIsDark() {
180186
return window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches
181187
}
182188

183-
_switchThemePrefers() {
189+
/**
190+
* Switch the current theme if the prefered system theme changes
191+
* @private
192+
*/
193+
_handlePreferedThemeChangeEvent() {
184194
const val = this._preferedThemeIsDark()
185195
this._changeThemeToDark(val)
186196
}
187197

198+
/**
199+
* Change theme to dark if true, else change to light
200+
* @private
201+
* @param {boolean} toDark - change theme to dark
202+
*/
188203
_changeThemeToDark(toDark) {
189204
toDark ? this.toDark() : this.toLight()
190205
}
191206

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+
*/
192212
_setStorageValue(value) {
193213
if (this.options.localStorage && window.localStorage !== null) {
194214
window.localStorage.setItem('darkmode', value)
@@ -201,6 +221,11 @@ export default class Darkmode {
201221
}
202222
}
203223

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+
*/
204229
_addStyle(css) {
205230
const linkElement = document.createElement('link')
206231

0 commit comments

Comments
 (0)