-
Notifications
You must be signed in to change notification settings - Fork 36
/
dark-mode.js
27 lines (24 loc) · 1.15 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
document.addEventListener('DOMContentLoaded', function () {
const toggleDarkModeButton = document.getElementById('toggleDarkMode');
const darkModeIcon = document.getElementById('darkModeIcon');
const prefersDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches;
const savedTheme = localStorage.getItem('theme');
// If user already has saved setting
if (savedTheme) {
document.body.classList.toggle('dark-mode', savedTheme === 'dark');
darkModeIcon.textContent = savedTheme === 'dark' ? '☀️' : '🌙';
} else if (prefersDarkMode) {
// Else, use system setting
document.body.classList.add('dark-mode');
darkModeIcon.textContent = '☀️';
} else {
darkModeIcon.textContent = '🌙';
}
toggleDarkModeButton.addEventListener('click', function () {
document.body.classList.toggle('dark-mode');
const theme = document.body.classList.contains('dark-mode') ? 'dark' : 'light';
localStorage.setItem('theme', theme);
// Change icon according to current theme
darkModeIcon.textContent = theme === 'dark' ? '☀️' : '🌙';
});
});