Commit e0a6649b authored by lunasorcery's avatar lunasorcery
Browse files

clean up JS

parent 8f7f3c3e
......@@ -4,20 +4,18 @@ const toggleSwitch = document.querySelector('input[type="checkbox"]#checkbox');
const userDefaultTheme = (window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light')
const currentTheme = localStorage?.getItem('theme') ?? userDefaultTheme;
document.documentElement.setAttribute('data-theme', currentTheme);
if (currentTheme === 'dark') {
toggleSwitch.checked = true;
function setTheme(themeStr, shouldSave=true) {
document.documentElement.setAttribute('data-theme', themeStr);
if (shouldSave) {
localStorage?.setItem('theme', themeStr);
}
}
function switchTheme(e) {
if (e.target.checked) {
document.documentElement.setAttribute('data-theme', 'dark');
localStorage?.setItem('theme', 'dark');
}
else {
document.documentElement.setAttribute('data-theme', 'light');
localStorage?.setItem('theme', 'light');
}
function setThemeFromToggle() {
setTheme(toggleSwitch.checked ? 'dark' : 'light');
}
toggleSwitch.addEventListener('change', switchTheme, false);
setTheme(currentTheme);
toggleSwitch.checked = (currentTheme === 'dark');
toggleSwitch.addEventListener('change', setThemeFromToggle, false);
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment