Add internal dark mode switching script

This commit is contained in:
Xevion
2023-02-17 03:03:56 -06:00
parent d4491464c1
commit 50b098d2cf
3 changed files with 31 additions and 0 deletions

15
public/js/darkmode.js Normal file
View File

@@ -0,0 +1,15 @@
const valid = ['system', 'dark', 'light'];
console.log(localStorage.theme);
// Set the default to system, ignore & delete invalid values
if (!('theme' in localStorage) || valid.indexOf(localStorage.theme) === -1)
localStorage.theme = 'system';
if (localStorage.theme === 'system') {
const preferDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
document.documentElement.classList.add(preferDark ? 'dark' : 'light');
} else if (localStorage.theme === 'dark') {
document.documentElement.classList.add('dark')
} else if (localStorage.theme === 'light') {
document.documentElement.classList.remove('dark')
}