-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdarkmode.js
28 lines (23 loc) · 855 Bytes
/
darkmode.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
import icon_sun from "./images/icon-sun.svg";
import icon_moon from "./images/icon-moon.svg";
//traer el btn que cambiara a dark o light mode
//agregarle un event listener al btn del tipo click
//cuando el usuario haga click toggle la classlist al body
function switchTheme() {
const themePreference = localStorage.getItem("theme");
const switchBtn = document.querySelector(".switch-theme");
if (themePreference === "darkmode") {
document.body.classList.add("darkmode");
}
switchBtn.addEventListener("click", () => {
document.body.classList.toggle("darkmode");
if (document.body.classList.contains("darkmode")) {
switchBtn.src = icon_sun;
localStorage.setItem("theme", "darkmode");
} else {
switchBtn.src = icon_moon;
localStorage.setItem("theme", "lightmode");
}
});
}
export { switchTheme };