-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
59 lines (53 loc) · 2.05 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
const toogleSwitch = document.querySelector('input[type="checkbox"]');
const nav = document.getElementById("nav");
const toogleIcon = document.getElementById("toogle-icon");
const image1 = document.getElementById("image1");
const image2 = document.getElementById("image2");
const image3 = document.getElementById("image3");
const textBox = document.getElementById("text-box");
const DARK_THEME = "dark";
const LIGHT_THEME = "light";
const switchTheme = (e) => {
if (e.target.checked) {
document.documentElement.setAttribute("data-theme", DARK_THEME);
localStorage.setItem("theme", DARK_THEME);
toogleDarkLightMode(DARK_THEME);
} else {
document.documentElement.setAttribute("data-theme", LIGHT_THEME);
localStorage.setItem("theme", LIGHT_THEME);
toogleDarkLightMode(LIGHT_THEME);
}
};
function toogleDarkLightMode(mode) {
nav.style.backgroundColor =
mode === LIGHT_THEME ? "rgb(255 255 255 / 50%)" : "rgb(0 0 0 / 50%)";
textBox.style.backgroundColor =
mode === LIGHT_THEME ? "rgb(0 0 0 / 50%)" : "rgb(255 255 255 / 50%)";
toogleIcon.children[0].textContent =
mode === LIGHT_THEME ? "Light Mode" : "Dark Mode";
mode === LIGHT_THEME
? toogleIcon.children[1].classList.replace("fa-sun", "fa-moon")
: toogleIcon.children[1].classList.replace("fa-moon", "fa-sun");
mode === LIGHT_THEME
? changeImgSource(image1, LIGHT_THEME)
: changeImgSource(image1, DARK_THEME);
mode === LIGHT_THEME
? changeImgSource(image2, LIGHT_THEME)
: changeImgSource(image2, DARK_THEME);
mode === LIGHT_THEME
? changeImgSource(image3, LIGHT_THEME)
: changeImgSource(image3, DARK_THEME);
}
const changeImgSource = (element, color) => {
element.src = `img/undraw_proud_coder_${color}.svg`;
};
toogleSwitch.addEventListener("change", switchTheme);
// Check Local Storafe for Theme
const currentTheme = localStorage.getItem("theme");
if (currentTheme) {
document.documentElement.setAttribute("data-theme", currentTheme);
if (currentTheme === DARK_THEME) {
toogleDarkLightMode(DARK_THEME);
toogleSwitch.checked = true;
}
}