-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththeme-toggle.js
40 lines (37 loc) · 1.71 KB
/
theme-toggle.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
document.addEventListener('DOMContentLoaded', () => {
const themeToggleButton = document.getElementById('theme-toggle');
const themeStylesheet = document.getElementById('theme-stylesheet');
const themeImage = document.getElementById('theme-image');
// Get the saved theme from localStorage or default to dark theme
const savedTheme = localStorage.getItem('theme');
const isDarkMode = savedTheme === 'dark' || !savedTheme;
if (isDarkMode) {
document.body.classList.add('dark-mode');
themeStylesheet.href = 'dark-theme.css';
themeImage.src = 'github-mark-white.png';
themeToggleButton.textContent = 'Light Mode';
localStorage.setItem('theme', 'dark');
} else {
document.body.classList.remove('dark-mode');
themeStylesheet.href = 'light-theme.css';
themeImage.src = 'github-mark.png';
themeToggleButton.textContent = 'Dark Mode';
}
themeToggleButton.addEventListener('click', () => {
if (document.body.classList.contains('dark-mode')) {
// Switch to light mode
document.body.classList.remove('dark-mode');
themeStylesheet.href = 'light-theme.css';
themeImage.src = 'github-mark.png';
themeToggleButton.textContent = 'Dark Mode';
localStorage.setItem('theme', 'light');
} else {
// Switch to dark mode
document.body.classList.add('dark-mode');
themeStylesheet.href = 'dark-theme.css';
themeImage.src = 'github-mark-white.png';
themeToggleButton.textContent = 'Light Mode';
localStorage.setItem('theme', 'dark');
}
});
});