-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
37 lines (27 loc) · 1020 Bytes
/
script.js
File metadata and controls
37 lines (27 loc) · 1020 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
28
29
30
31
32
33
34
35
36
37
const toggle = document.getElementById("toggle");
// Function to determine the system's color scheme
const getSystemTheme = () => {
return window.matchMedia("(prefers-color-scheme: dark)").matches
? "dark"
: "light";
};
// Function to set theme in the DOM and Local storage
const setTheme = (theme) => {
localStorage.setItem("theme", theme);
document.documentElement.setAttribute("data-theme", theme);
};
// Function to toggle between light and dark mode
const handleToggle = () => {
const currentTheme = document.documentElement.getAttribute("data-theme");
const theme = currentTheme === "dark" ? "light" : "dark";
setTheme(theme);
};
// Function to initialize the theme when page loads
const initTheme = () => {
// Get the stored theme from Local storage or use the system theme
const storedTheme = localStorage.getItem("theme") || getSystemTheme();
toggle.checked = storedTheme === "light";
setTheme(storedTheme);
};
initTheme();
toggle.addEventListener("change", handleToggle);