-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscript.js
70 lines (53 loc) · 1.83 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
/*********************
* RESPONSIVE WARNING *
*********************/
const responsiveWarning = document.getElementById("responsive-warning");
// "true" if the site is optimized for responsive design, "false" if not.
const responsiveDesign = false;
// Show mobile warning if the user is on mobile and responsive-design is false.
if (!responsiveDesign && window.innerWidth <= 768) {
responsiveWarning.classList.add("show");
}
/***********************
* MODE TOGGLE BEHAVIOR *
***********************/
// Get elements that change with the mode.
const toggleModeBtn = document.getElementById("toggle-mode-btn");
const portfolioLink = document.getElementById("portfolio-link");
const body = document.body;
// Function to apply mode.
function applyMode(mode) {
body.classList.remove("light-mode", "dark-mode");
body.classList.add(mode);
if (mode === "dark-mode") {
// Set dark mode styles.
toggleModeBtn.style.color = "rgb(245, 245, 245)";
toggleModeBtn.innerHTML = '<i class="ri-sun-line"></i>';
portfolioLink.style.color = "rgb(245, 245, 245)";
responsiveWarning.style.backgroundColor = "rgb(2, 4, 8)";
} else {
// Set light mode styles.
toggleModeBtn.style.color = "rgb(2, 4, 8)";
toggleModeBtn.innerHTML = '<i class="ri-moon-line"></i>';
portfolioLink.style.color = "rgb(2, 4, 8)";
responsiveWarning.style.backgroundColor = "rgb(245, 245, 245)";
}
}
// Check and apply saved mode on page load
let savedMode = localStorage.getItem("mode");
if (savedMode === null) {
savedMode = "light-mode"; // Default mode.
}
applyMode(savedMode);
// Toggle mode and save preference.
toggleModeBtn.addEventListener("click", function () {
let newMode;
if (body.classList.contains("light-mode")) {
newMode = "dark-mode";
} else {
newMode = "light-mode";
}
applyMode(newMode);
// Save choice.
localStorage.setItem("mode", newMode);
});