Skip to content

Commit b42e1fd

Browse files
committed
💥♻️ Prepend internal function with _
1 parent ef0ffc3 commit b42e1fd

File tree

3 files changed

+25
-28
lines changed

3 files changed

+25
-28
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: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,6 @@ export const IS_BROWSER = typeof window !== 'undefined'
22

33
export default class Darkmode {
44
constructor(options) {
5-
if (!IS_BROWSER) {
6-
console.warn('Detected environment without a `window` object')
7-
return
8-
}
9-
105
const defaultOptions = {
116
top: 'unset',
127
bottom: '20px',
@@ -29,15 +24,15 @@ export default class Darkmode {
2924
this.dark = false
3025

3126
if (options.autoMatchOsTheme) {
32-
window.matchMedia('(prefers-color-scheme: dark)').addListener((e) => e.matches && this.switchThemePrefers())
33-
window.matchMedia('(prefers-color-scheme: light)').addListener((e) => e.matches && this.switchThemePrefers())
27+
window.matchMedia('(prefers-color-scheme: dark)').addListener((e) => e.matches && this._switchThemePrefers())
28+
window.matchMedia('(prefers-color-scheme: light)').addListener((e) => e.matches && this._switchThemePrefers())
3429
}
3530

36-
const storageValue = this.getStorageValue()
31+
const storageValue = this._getStorageValue()
3732
if (storageValue !== null) {
3833
storageValue === 'true' || storageValue === true ? this.toDark() : this.toLight()
3934
} else if (options.autoMatchOsTheme) {
40-
this.preferedTheme() ? this.toDark() : this.toLight()
35+
this._preferedTheme() ? this.toDark() : this.toLight()
4136
} else {
4237
options.defaultTheme === 'light' ? this.toLight() : this.toDark()
4338
}
@@ -89,13 +84,13 @@ export default class Darkmode {
8984
})
9085

9186
document.body.insertBefore(div, document.body.firstChild)
92-
this.addStyle(css)
87+
this._addStyle(css)
9388
}
9489

9590
toLight() {
9691
if (this.options.events) window.dispatchEvent(new CustomEvent('theme-change', { detail: { to: 'light' } }))
9792
document.documentElement.setAttribute('data-theme', 'light')
98-
this.setStorageValue(false)
93+
this._setStorageValue(false)
9994
document.body.classList.remove('theme-dark')
10095
document.body.classList.add('theme-light')
10196
this.dark = false
@@ -104,21 +99,30 @@ export default class Darkmode {
10499
toDark() {
105100
if (this.options.events) window.dispatchEvent(new CustomEvent('theme-change', { detail: { to: 'dark' } }))
106101
document.documentElement.setAttribute('data-theme', 'dark')
107-
this.setStorageValue(true)
102+
this._setStorageValue(true)
108103
document.body.classList.add('theme-dark')
109104
document.body.classList.remove('theme-light')
110105
this.dark = true
111106
}
112107

113-
preferedTheme() {
108+
toggle() {
109+
this.dark ? this.toLight() : this.toDark()
110+
return this.dark
111+
}
112+
113+
isActivated() {
114+
return this.dark
115+
}
116+
117+
_preferedTheme() {
114118
return window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches
115119
}
116120

117-
switchThemePrefers() {
118-
this.preferedTheme() === true ? this.swichToDark() : this.swichToLight()
121+
_switchThemePrefers() {
122+
this._preferedTheme() === true ? this.swichToDark() : this.swichToLight()
119123
}
120124

121-
getStorageValue() {
125+
_getStorageValue() {
122126
if (this.options.localStorage && window.localStorage !== null) {
123127
return window.localStorage.getItem('darkmode')
124128
} else if (this.options.cookie) {
@@ -129,24 +133,15 @@ export default class Darkmode {
129133
return null
130134
}
131135

132-
setStorageValue(value) {
136+
_setStorageValue(value) {
133137
if (this.options.localStorage && window.localStorage !== null) {
134138
window.localStorage.setItem('darkmode', value)
135139
} else if (this.options.cookie) {
136140
document.cookie = `darkmode=${ value }`
137141
}
138142
}
139143

140-
toggle() {
141-
this.dark ? this.toLight() : this.toDark()
142-
return this.dark
143-
}
144-
145-
isActivated() {
146-
return this.dark
147-
}
148-
149-
addStyle(css) {
144+
_addStyle(css) {
150145
const linkElement = document.createElement('link')
151146

152147
linkElement.setAttribute('rel', 'stylesheet')

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,6 @@ const detect = () => {
2222

2323
if (IS_BROWSER) {
2424
detect()
25+
} else {
26+
console.warn('drkmd.js: Detected environment without a `window` object')
2527
}

0 commit comments

Comments
 (0)