34 lines
1.1 KiB
JavaScript
34 lines
1.1 KiB
JavaScript
function initToggle() {
|
|
const toggleSwitch = document.querySelector('.toggle input[type="checkbox"]');
|
|
const currentTheme = localStorage.getItem('theme') ? localStorage.getItem('theme') : null;
|
|
|
|
function switchTheme(e) {
|
|
if (e.target.checked) {
|
|
document.documentElement.setAttribute('data-theme', 'dark');
|
|
localStorage.setItem('theme', 'dark'); //add this
|
|
}
|
|
else {
|
|
document.documentElement.setAttribute('data-theme', 'light');
|
|
localStorage.setItem('theme', 'light'); //add this
|
|
}
|
|
}
|
|
|
|
toggleSwitch.addEventListener('change', switchTheme, false);
|
|
|
|
if (currentTheme) {
|
|
document.documentElement.setAttribute('data-theme', currentTheme);
|
|
|
|
if (currentTheme === 'dark') {
|
|
toggleSwitch.checked = true;
|
|
} else {
|
|
toggleSwitch.checked = false;
|
|
}
|
|
}
|
|
|
|
document.querySelectorAll('.spacer').forEach(function (el) {
|
|
el.style.display = 'none';
|
|
});
|
|
document.querySelectorAll('.toggle').forEach(function (el) {
|
|
el.style.display = 'unset';
|
|
});
|
|
} |