refactoring: change to ternary operator
This commit is contained in:
@@ -1,47 +1,44 @@
|
|||||||
(() => {
|
(() => {
|
||||||
// Theme switch
|
// Theme switch
|
||||||
const root = document.body;
|
const body = document.body;
|
||||||
const themeSwitch = document.getElementById("mood");
|
const lamp = document.getElementById("mode");
|
||||||
const themeData = root.getAttribute("data-theme");
|
const data = body.getAttribute("data-theme");
|
||||||
|
|
||||||
if (themeSwitch) {
|
const initTheme = (state) => {
|
||||||
initTheme(localStorage.getItem("theme"));
|
if (state === "dark") {
|
||||||
themeSwitch.addEventListener("click", () =>
|
body.setAttribute("data-theme", "dark");
|
||||||
toggleTheme(localStorage.getItem("theme"))
|
} else if (state === "light") {
|
||||||
);
|
body.removeAttribute("data-theme");
|
||||||
|
|
||||||
function toggleTheme(state) {
|
|
||||||
if (state === "dark") {
|
|
||||||
localStorage.setItem("theme", "light");
|
|
||||||
root.removeAttribute("data-theme");
|
|
||||||
} else if (state === "light") {
|
|
||||||
localStorage.setItem("theme", "dark");
|
|
||||||
document.body.setAttribute("data-theme", "dark");
|
|
||||||
} else {
|
|
||||||
initTheme(state);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function initTheme(state) {
|
|
||||||
if (state === "dark") {
|
|
||||||
document.body.setAttribute("data-theme", "dark");
|
|
||||||
} else if (state === "light") {
|
|
||||||
root.removeAttribute("data-theme");
|
|
||||||
} else {
|
|
||||||
localStorage.setItem("theme", themeData);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Blur content when the menu is open
|
|
||||||
const checkbox = document.getElementById("menu-trigger");
|
|
||||||
const wrapper = document.getElementById("wrapper");
|
|
||||||
|
|
||||||
checkbox.addEventListener("change", function() {
|
|
||||||
if (this.checked) {
|
|
||||||
wrapper.classList.add("trigger-wrapper");
|
|
||||||
} else {
|
} else {
|
||||||
wrapper.classList.remove("trigger-wrapper");
|
localStorage.setItem("theme", data);
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const toggleTheme = (state) => {
|
||||||
|
if (state === "dark") {
|
||||||
|
localStorage.setItem("theme", "light");
|
||||||
|
body.removeAttribute("data-theme");
|
||||||
|
} else if (state === "light") {
|
||||||
|
localStorage.setItem("theme", "dark");
|
||||||
|
body.setAttribute("data-theme", "dark");
|
||||||
|
} else {
|
||||||
|
initTheme(state);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
initTheme(localStorage.getItem("theme"));
|
||||||
|
|
||||||
|
lamp.addEventListener("click", () =>
|
||||||
|
toggleTheme(localStorage.getItem("theme"))
|
||||||
|
);
|
||||||
|
|
||||||
|
// Blur the content when the menu is open
|
||||||
|
const cbox = document.getElementById("menu-trigger");
|
||||||
|
|
||||||
|
cbox.addEventListener("change", function () {
|
||||||
|
const area = document.querySelector(".wrapper");
|
||||||
|
this.checked
|
||||||
|
? area.classList.add("blurry")
|
||||||
|
: area.classList.remove("blurry");
|
||||||
});
|
});
|
||||||
})();
|
})();
|
||||||
|
|||||||
Reference in New Issue
Block a user