nycki.net/static/light-toggle.js
nycki a60c1fef23
All checks were successful
/ build (push) Successful in 31s
try loading script after dom
2025-03-21 16:55:13 -07:00

46 lines
1.1 KiB
JavaScript

function themeToggle() {
const oldValue = localStorage.getItem('theme');
let newValue;
if (oldValue == 'light') {
newValue = 'dark';
} else if (oldValue == 'dark') {
newValue = 'light';
}
localStorage.setItem('theme', newValue);
themeRefresh();
}
function themeClear() {
localStorage.setItem('theme', '');
themeRefresh();
}
function themeRefresh(newValue) {
if (newValue !== undefined) {
localStorage.setItem('theme', newValue);
}
const theme = localStorage.getItem('theme')
const el = document.getElementsByTagName('html')[0];
if (theme == 'light') {
el.setAttribute('theme', 'light');
} else if (theme == 'dark') {
el.setAttribute('theme', 'dark');
} else {
el.setAttribute('theme', '');
}
}
function main() {
const buttonToggle = document.getElementsByClassName('theme-toggle');
for (const el of buttonToggle) {
el.onclick = themeToggle;
}
const buttonClear = document.getElementsByTagName('theme-clear');
for (const el of buttonClear) {
el.onclick = themeClear;
}
themeRefresh();
}
addEventListener('DOMContentLoaded', main);