Skip to content

Commit

Permalink
Fix and simplify logic for theme switching (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
rpmcfong authored Feb 3, 2022
1 parent c761eab commit 44c6f45
Showing 1 changed file with 9 additions and 22 deletions.
31 changes: 9 additions & 22 deletions src/js/08-theme.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,18 @@
/* global localStorage */
;(function () {
'use strict'
const systemInitiatedDark = window.matchMedia('(prefers-color-scheme: dark)')
if (systemInitiatedDark.matches) {
document.documentElement.setAttribute('data-theme', 'dark')
let theme = localStorage.getItem('theme')
if (theme === null) {
const systemDark = window.matchMedia('(prefers-color-scheme: dark)').matches
theme = systemDark ? 'dark' : 'light'
}
document.addEventListener('DOMContentLoaded', function () {
if (systemInitiatedDark.matches) {
document.documentElement.setAttribute('data-theme', 'dark')
}
})
document.documentElement.setAttribute('data-theme', theme)

const switchButton = document.getElementById('themeSwitch')
switchButton.addEventListener('click', function (e) {
e.preventDefault()
const theme = localStorage.getItem('theme')
if (theme === 'dark') {
document.documentElement.removeAttribute('data-theme')
localStorage.setItem('theme', 'light')
} else if (theme === 'light') {
document.documentElement.setAttribute('data-theme', 'dark')
localStorage.setItem('theme', 'dark')
} else if (systemInitiatedDark.matches) {
document.documentElement.removeAttribute('data-theme')
localStorage.setItem('theme', 'light')
} else {
document.documentElement.setAttribute('data-theme', 'dark')
localStorage.setItem('theme', 'dark')
}
theme = theme === 'dark' ? 'light' : 'dark'
document.documentElement.setAttribute('data-theme', theme)
localStorage.setItem('theme', theme)
})
})()

0 comments on commit 44c6f45

Please sign in to comment.