Move dark mode script to site.js
This commit is contained in:
parent
ea43893d4a
commit
c3bf9b61dd
2 changed files with 17 additions and 14 deletions
|
@ -39,18 +39,5 @@
|
||||||
|
|
||||||
{{ block "scripts" . }}{{ end }}
|
{{ block "scripts" . }}{{ end }}
|
||||||
|
|
||||||
<script>
|
<script src="{{ `scripts/site.js` | absURL }}"></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>
|
|
||||||
</html>
|
</html>
|
||||||
|
|
16
static/scripts/site.js
Normal file
16
static/scripts/site.js
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
function setupDarkMode() {
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
|
document.addEventListener("load", setupDarkMode);
|
Loading…
Add table
Add a link
Reference in a new issue