Skip to content

Commit

Permalink
♻️ Cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
BetaHuhn committed Apr 7, 2021
1 parent f305a03 commit df25c9d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
2 changes: 1 addition & 1 deletion dist/drkmd-js.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 10 additions & 4 deletions src/drkmd.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,15 @@ export default class Darkmode {
const span = document.createElement('span')
span.innerHTML = this.options.label
div.className = 'drkmd-toggle-button'

div.setAttribute('title', 'Toggle dark mode')
div.setAttribute('aria-label', 'Toggle dark mode')
div.setAttribute('aria-checked', 'false')
div.setAttribute('role', 'checkbox')
div.appendChild(span)

div.addEventListener('click', () => {
this.dark === true ? this.toLight() : this.toDark()
this.toggle()
})

document.body.insertBefore(div, document.body.firstChild)
Expand All @@ -89,19 +90,23 @@ export default class Darkmode {

toLight() {
if (this.options.events) window.dispatchEvent(new CustomEvent('theme-change', { detail: { to: 'light' } }))

document.documentElement.setAttribute('data-theme', 'light')
this._setStorageValue(false)
document.body.classList.remove('theme-dark')
document.body.classList.add('theme-light')

this._setStorageValue(false)
this.dark = false
}

toDark() {
if (this.options.events) window.dispatchEvent(new CustomEvent('theme-change', { detail: { to: 'dark' } }))

document.documentElement.setAttribute('data-theme', 'dark')
this._setStorageValue(true)
document.body.classList.add('theme-dark')
document.body.classList.remove('theme-light')

this._setStorageValue(true)
this.dark = true
}

Expand All @@ -119,7 +124,7 @@ export default class Darkmode {
}

_switchThemePrefers() {
this._preferedTheme() === true ? this.swichToDark() : this.swichToLight()
this._preferedTheme() === true ? this.toDark() : this.toLight()
}

_getStorageValue() {
Expand Down Expand Up @@ -150,6 +155,7 @@ export default class Darkmode {
'href',
'data:text/css;charset=UTF-8,' + encodeURIComponent(css)
)

document.head.appendChild(linkElement)
}
}

0 comments on commit df25c9d

Please sign in to comment.