forked from yuvrajsinghgmx/ShopSmart
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DarkModeToggle
34 lines (30 loc) · 890 Bytes
/
DarkModeToggle
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
<label>
<input type="checkbox" id="dark-mode-toggle">
Dark Mode
</label>
/* Light Theme */
body {
background-color: white;
color: black;
}
/* Dark Theme */
body.dark-mode {
background-color: black;
color: white;
}
const toggleSwitch = document.getElementById('dark-mode-toggle');
// Function to toggle dark mode
const toggleDarkMode = () => {
document.body.classList.toggle('dark-mode', toggleSwitch.checked);
localStorage.setItem('dark-mode', toggleSwitch.checked);
};
// Event listener for toggle switch
toggleSwitch.addEventListener('change', toggleDarkMode);
// Load user preference on page load
window.addEventListener('load', () => {
const darkModePreference = localStorage.getItem('dark-mode') === 'true';
toggleSwitch.checked = darkModePreference;
if (darkModePreference) {
document.body.classList.add('dark-mode');
}
});