Sha256: 4d003248604097fd7fe657e286850186e935f6dc153b709a7f8d97f628df82cd

Contents?: true

Size: 1.03 KB

Versions: 4

Compression:

Stored size: 1.03 KB

Contents

<script type="text/javascript">
  window.addEventListener('load', themeChange);
  const currentTheme = localStorage.getItem('theme') ? localStorage.getItem('theme') : null;
  if (currentTheme)
    document.documentElement.setAttribute('data-theme', currentTheme);

  function themeChange() {
    let button = document.querySelector('.theme-toggle');

    button.addEventListener('click', function (e) {
      let currentTheme = document.documentElement.getAttribute('data-theme');
      if (currentTheme === 'dark') {
        transition();
        document.documentElement.setAttribute('data-theme', 'light');
        localStorage.setItem('theme', 'light');
      } else {
        transition();
        document.documentElement.setAttribute('data-theme', 'dark');
        localStorage.setItem('theme', 'dark');
      }
    });

    let transition = () => {
      document.documentElement.classList.add('transition');
      window.setTimeout(() => {
        document.documentElement.classList.remove('transition');
      }, 1000);
    }
  }
</script>

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
moonwalk-0.1.3 _includes/toggle_theme_js.html
moonwalk-0.1.2 _includes/toggle_theme_js.html
moonwalk-0.1.1 _includes/toggle_theme_js.html
moonwalk-0.0.4 _includes/toggle_theme_js.html