-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdark-mode.js
34 lines (31 loc) · 1.14 KB
/
dark-mode.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
const themeToggles = document.querySelectorAll('.toggle-checkbox');
const themeColorMeta = document.getElementById('theme-color-meta');
themeToggles.forEach(function(themeToggle) {
const moonIcon = themeToggle.parentElement.querySelector('.dark-mode-moon');
const sunIcon = themeToggle.parentElement.querySelector('.dark-mode-sun');
themeToggle.addEventListener('change', function() {
document.body.classList.toggle('theme-dark', this.checked);
themeColorMeta.setAttribute('content', this.checked ? '#111111' : '#c9c9c9');
if (this.checked) {
moonIcon.style.opacity = 1;
sunIcon.style.opacity = 0;
} else {
moonIcon.style.opacity = 0;
sunIcon.style.opacity = 1;
}
});
const toggleButton = themeToggle.parentElement;
if (toggleButton) {
toggleButton.addEventListener('keydown', (event) => {
if (event.code === 'Space' || event.code === 'Enter') {
themeToggle.click();
themeToggle.focus();
}
});
toggleButton.addEventListener('click', function() {
console.log("Toggle button clicked");
themeToggle.click();
themeToggle.focus();
});
}
});