-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
33 lines (24 loc) · 941 Bytes
/
script.js
File metadata and controls
33 lines (24 loc) · 941 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
const themeBtn = document.getElementById("themeChanger");
const savedTheme = localStorage.getItem("theme");
if (savedTheme === "dark") {
document.documentElement.classList.add("dark");
}
// Guardar el tema en localstorage
themeBtn.addEventListener("click", (e) => {
e.preventDefault();
document.documentElement.classList.toggle("dark");
const isDark = document.documentElement.classList.contains("dark");
themeBtn.setAttribute("aria-pressed", isDark);
localStorage.setItem("theme", isDark ? "dark" : "light")
});
// Permite hacer click al boton con el enter por cuestiones de accesibilidad
themeBtn.addEventListener("keydown", (e) => {
if (e.key === "Enter" || e.key === " ") {
e.preventDefault();
themeBtn.click();
}
});
if ("serviceWorker" in navigator) {
navigator.serviceWorker.register("service-worker.js")
.then(() => console.log("Service Worker registrado"));
}