-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
27 lines (23 loc) · 1021 Bytes
/
Copy pathscript.js
File metadata and controls
27 lines (23 loc) · 1021 Bytes
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', () => {
const htmlElement = document.documentElement;
const toggleButton = document.querySelector('[data-theme-toggle]');
const themeKey = 'user-theme';
function setTheme(theme) {
htmlElement.setAttribute('data-theme', theme);
localStorage.setItem(themeKey, theme);
toggleButton.setAttribute('aria-label', theme === 'light' ? 'Change to dark theme' : 'Change to light theme');
}
// Initialize theme on page load
const storedTheme = localStorage.getItem(themeKey);
if (storedTheme) {
setTheme(storedTheme);
} else {
const prefersDark = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
setTheme(prefersDark ? 'dark' : 'light');
}
// Toggle theme on button click
toggleButton.addEventListener('click', () => {
const currentTheme = htmlElement.getAttribute('data-theme');
setTheme(currentTheme === 'light' ? 'dark' : 'light');
});
});