-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
42 lines (38 loc) · 1.31 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
const toggleSwitch = document.querySelector('input[type="checkbox"]');
const nav = document.getElementById('nav');
const toggleIcon = document.getElementById('toggle-icon');
const textBox = document.getElementById('text-box');
function toggleDarkKightMode(isDark) {
nav.style.backgroundColor = isDark
? 'rgb(0 0 0 / 50%)'
: 'rgb(255 255 255 / 50%)';
textBox.style.backgroundColor = isDark
? 'rgb(255 255 255 / 50%)'
: 'rgb(0 0 0 / 50%)';
isDark
? toggleIcon.children[1].classList.replace('fa-sun', 'fa-moon')
: toggleIcon.children[1].classList.replace('fa-moon', 'fa-sun');
}
// Switch Theme Dynamically
function switchTheme(event) {
if (event.target.checked) {
document.documentElement.setAttribute('data-theme', 'dark');
localStorage.setItem('theme', 'dark');
toggleDarkKightMode(true);
} else {
document.documentElement.setAttribute('data-theme', 'light');
localStorage.setItem('theme', 'light');
toggleDarkKightMode(false);
}
}
// Event Listener
toggleSwitch.addEventListener('change', switchTheme);
// check Local Storage for Theme
const currentTheme = localStorage.getItem('theme');
if (currentTheme) {
document.documentElement.setAttribute('data-theme', currentTheme);
if (currentTheme === 'dark') {
toggleSwitch.checked = true;
toggleDarkKightMode(true);
}
}