mirror of https://github.com/lxndrblz/anatole.git

Alexander Bilz
12.09.2022 fe27dbac37cf3301ac78c0ce6422eda748649447
assets/js/anatole-theme-switcher.js
@@ -6,6 +6,7 @@
function setTheme(style) {
  document.documentElement.setAttribute('data-theme', style);
  localStorage.setItem('theme', style);
  setCssClass(style);
}
function init() {
@@ -26,12 +27,25 @@
    // load a stored theme
    if (theme === 'light') {
      document.documentElement.setAttribute('data-theme', 'light');
      setCssClass('light');
    } else {
      document.documentElement.setAttribute('data-theme', 'dark');
      setCssClass('dark');
    }
  }
}
function setCssClass(style) {
  var body = document.body;
  if (style === 'light') {
    body.classList.remove('theme--dark');
    body.classList.add('theme--light');
  } else if (style === 'dark') {
    body.classList.remove('theme--light');
    body.classList.add('theme--dark');
  }
}
// switch themes
function switchTheme() {
  const theme = getTheme();
@@ -55,4 +69,6 @@
// Automatic Switching
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', switchTheme, false);
init();
document.addEventListener('DOMContentLoaded', function () {
  init();
});