-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththeme-toggle.js
More file actions
19 lines (16 loc) · 813 Bytes
/
Copy paththeme-toggle.js
File metadata and controls
19 lines (16 loc) · 813 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// Theme Toggle Functionality
document.addEventListener('DOMContentLoaded', () => {
const themeToggle = document.querySelector('.theme-toggle');
const prefersDarkScheme = window.matchMedia('(prefers-color-scheme: dark)');
// Check for saved theme preference or use system preference
const currentTheme = localStorage.getItem('theme') ||
(prefersDarkScheme.matches ? 'dark' : 'light');
// Set initial theme
document.body.className = `${currentTheme}-theme`;
// Toggle theme
themeToggle.addEventListener('click', () => {
const isDark = document.body.classList.contains('dark-theme');
document.body.className = isDark ? 'light-theme' : 'dark-theme';
localStorage.setItem('theme', isDark ? 'light' : 'dark');
});
});