mirror of
https://github.com/wah-su/wah-su.github.io.git
synced 2025-04-28 17:09:40 +05:00
feat: add theme switcher
This commit is contained in:
parent
78efd2500a
commit
40a31ce007
14 changed files with 349 additions and 176 deletions
36
public/js/theme.js
Normal file
36
public/js/theme.js
Normal file
|
@ -0,0 +1,36 @@
|
|||
const bodyElement = document.getElementById("body");
|
||||
const logo = document.getElementById("logo");
|
||||
const themeToggle = document.getElementById("theme-toggle");
|
||||
|
||||
const setTheme = (theme) => {
|
||||
bodyElement.classList.remove(theme == "light" ? "dark" : "light");
|
||||
bodyElement.classList.add(theme == "light" ? "light" : "dark");
|
||||
theme == "light"
|
||||
? (themeToggle.checked = false)
|
||||
: (themeToggle.checked = true);
|
||||
logo.src = `/public/images/logo-${theme}.svg`;
|
||||
localStorage.setItem("theme", theme);
|
||||
};
|
||||
|
||||
function updateTheme() {
|
||||
let theme = localStorage.getItem("theme");
|
||||
console.log("updatingTheme", theme);
|
||||
if (theme) {
|
||||
setTheme(theme);
|
||||
} else {
|
||||
setTheme("light");
|
||||
}
|
||||
}
|
||||
|
||||
themeToggle.addEventListener("click", () => {
|
||||
let theme = localStorage.getItem("theme");
|
||||
if (theme == "light") {
|
||||
setTheme("dark");
|
||||
renderCards();
|
||||
} else {
|
||||
setTheme("light");
|
||||
renderCards();
|
||||
}
|
||||
});
|
||||
|
||||
updateTheme();
|
Loading…
Add table
Add a link
Reference in a new issue