-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathscript.js
28 lines (22 loc) · 876 Bytes
/
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
const body = document.body;
const themeBtn = document.querySelector(".fas");
const getBodyTheme = localStorage.getItem("theme");
const getBtnTheme = localStorage.getItem("btn-theme");
const isLight = () => body.classList.contains("body-light");
const addThemeClass = (bodyClass, btnClass) => {
body.classList.add(bodyClass);
themeBtn.classList.add(btnClass);
};
addThemeClass(getBodyTheme, getBtnTheme);
const setTheme = (bodyClass, btnClass) => {
body.classList.remove(localStorage.getItem("theme"));
themeBtn.classList.remove(localStorage.getItem("btn-theme"));
addThemeClass(bodyClass, btnClass);
localStorage.setItem("theme", bodyClass);
localStorage.setItem("btn-theme", btnClass);
};
const toggleTheme = () =>
isLight()
? setTheme("body-dark", "fa-sun")
: setTheme("body-light", "fa-moon");
themeBtn.addEventListener("click", toggleTheme);