hugo-theme-minima/assets/js/theme.js
2021-08-29 23:55:45 +08:00

17 lines
No EOL
647 B
JavaScript

const light = '{{ index .Site.Params.switch 1 }}',
dark = '{{ index .Site.Params.switch 0 }}';
const LIGHT = 'light', DARK = 'dark';
const themeSwitcher = document.getElementById('theme-switcher');
themeSwitcher.innerHTML = localStorage.theme === LIGHT ? light : dark;
themeSwitcher.addEventListener('click', function () {
const currentTheme = localStorage.theme,
newTheme = currentTheme === LIGHT ? DARK : LIGHT,
{ classList } = document.documentElement,
text = newTheme === LIGHT ? light : dark;
classList.remove(currentTheme);
classList.add(newTheme);
localStorage.theme = newTheme;
themeSwitcher.innerHTML = text;
});