forked from dohinaf/basic-icecream-website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
darkMode.js
23 lines (20 loc) · 788 Bytes
/
darkMode.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
const checkbox = document.getElementById('checkbox')
const modeLabel = document.getElementById('mode-label')
// Check if dark mode is already enabled
if (localStorage.getItem('theme') === 'dark') {
document.documentElement.setAttribute('data-theme', 'dark')
checkbox.checked = true
modeLabel.textContent = 'Dark Mode'
}
// Add event listener for toggle switch
checkbox.addEventListener('change', () => {
if (checkbox.checked) {
document.documentElement.setAttribute('data-theme', 'dark')
localStorage.setItem('theme', 'dark')
modeLabel.textContent = 'Dark Mode'
} else {
document.documentElement.setAttribute('data-theme', 'light')
localStorage.setItem('theme', 'light')
modeLabel.textContent = 'Light Mode'
}
})