Move dark mode script to site.js

This commit is contained in:
Eryn Wells 2022-09-18 13:41:29 -07:00
parent ea43893d4a
commit c3bf9b61dd
2 changed files with 17 additions and 14 deletions

View file

@ -39,18 +39,5 @@
{{ block "scripts" . }}{{ end }}
<script>
const bodyClasses = document.body.classList;
const systemDarkModeMatch = window.matchMedia('(prefers-color-scheme: dark)');
const localStorageDarkMode = localStorage.getItem('dark');
const setDark = isDark => {
bodyClasses[isDark ? 'add' : 'remove']('dark');
localStorage.setItem('dark', isDark ? 'yes' : 'no');
};
setDark(localStorageDarkMode ? localStorageDarkMode === 'yes' : systemDarkModeMatch.matches);
requestAnimationFrame(() => bodyClasses.remove('not-ready'));
systemDarkModeMatch.addEventListener('change', (event) => setDark(event.matches));
</script>
<script src="{{ `scripts/site.js` | absURL }}"></script>
</html>