-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
74 lines (62 loc) · 1.92 KB
/
script.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
const moonIcon = document.getElementById("moon-icon");
const sunIcon = document.getElementById("sun-icon");
const body = document.documentElement;
// Theme variables
const userTheme = localStorage.getItem("theme");
const systemTheme = window.matchMedia("(prefers-color-scheme: dark)").matches;
// Icon toggle
const toggleIcon = () => {
moonIcon.classList.toggle("display-none");
sunIcon.classList.toggle("display-none");
};
// Initial theme check
const themeCheck = () => {
if (userTheme === "dark" || (!userTheme && systemTheme)) {
body.classList.add("dark");
moonIcon.classList.add("display-none");
return;
}
sunIcon.classList.add("icon-transition");
sunIcon.classList.add("display-none");
};
// Manual theme toggle
const toggleTheme = () => {
if (body.classList.contains("dark")) {
body.classList.remove("dark");
localStorage.setItem("theme", "light");
toggleIcon();
return;
}
body.classList.add("dark");
localStorage.setItem("theme", "dark");
toggleIcon();
};
// Event listeners
moonIcon.addEventListener("click", () => {
toggleTheme();
});
sunIcon.addEventListener("click", () => {
toggleTheme();
});
// Initial theme check
themeCheck();
const initApp = () => {
const harmburgerBtn = document.getElementById("harmburger-btn");
const mobileMenu = document.getElementById("mobile-menu");
const toggleMenu = () => {
mobileMenu.classList.toggle("hidden");
mobileMenu.classList.toggle("flex");
};
harmburgerBtn.addEventListener("click", toggleMenu);
mobileMenu.addEventListener("click", toggleMenu);
};
document.addEventListener("DOMContentLoaded", initApp);
// Rgister the service worker
if ("serviceWorker" in navigator) {
window.addEventListener("load", function () {
navigator.serviceWorker
.register("/serviceWorker.js")
.then((res) => console.log("service worker registered"))
.catch((err) => console.log("service worker not registered", err));
});
}