-
Notifications
You must be signed in to change notification settings - Fork 0
/
darkmode.js
61 lines (57 loc) · 2.47 KB
/
darkmode.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
document.addEventListener('DOMContentLoaded', function () {
// Select the dark mode toggle using its ID
const darkModeSwitch = document.querySelector('#darkmode-toggle');
const bodyElement = document.body;
// Function to toggle dark mode
function toggleDarkMode() {
const isDarkMode = bodyElement.classList.contains('darkmode');
if (isDarkMode) {
// Disable dark mode
bodyElement.classList.remove('darkmode');
localStorage.setItem('theme', 'light');
darkModeSwitch.setAttribute('aria-pressed', 'false');
console.log('Dark mode disabled');
} else {
// Enable dark mode
bodyElement.classList.add('darkmode');
localStorage.setItem('theme', 'dark');
darkModeSwitch.setAttribute('aria-pressed', 'true');
console.log('Dark mode enabled');
}
}
// Retrieve the saved theme from localStorage
const savedTheme = localStorage.getItem('theme');
if (savedTheme) {
if (savedTheme === 'dark') {
bodyElement.classList.add('darkmode');
darkModeSwitch.checked = true;
darkModeSwitch.setAttribute('aria-pressed', 'true');
console.log('Dark mode applied from localStorage');
} else {
bodyElement.classList.remove('darkmode');
darkModeSwitch.checked = false;
darkModeSwitch.setAttribute('aria-pressed', 'false');
console.log('Light mode applied from localStorage');
}
} else {
// If no theme is saved, use the system preference
const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
if (prefersDark) {
bodyElement.classList.add('darkmode');
darkModeSwitch.checked = true;
darkModeSwitch.setAttribute('aria-pressed', 'true');
console.log('Dark mode applied from system preference');
} else {
bodyElement.classList.remove('darkmode');
darkModeSwitch.checked = false;
darkModeSwitch.setAttribute('aria-pressed', 'false');
console.log('Light mode applied from system preference');
}
}
// Add event listener to the toggle switch if it exists
if (darkModeSwitch) {
darkModeSwitch.addEventListener('change', toggleDarkMode);
} else {
console.error('Dark mode toggle element not found! Ensure it has the correct ID ("darkmode-toggle").');
}
});