This commit is contained in:
MeiK
2020-12-14 15:59:49 +08:00
parent 76202bfc51
commit 5dbda5af86
18 changed files with 566 additions and 395 deletions

29
static/js/theme-mode.js Normal file
View File

@@ -0,0 +1,29 @@
function switchTheme() {
const currentStyle = currentTheme();
if (currentStyle == 'light') {
setTheme('dark');
}
else {
setTheme('light');
}
}
function setTheme(style) {
console.log(`set theme ${style}`);
document.querySelectorAll('.isInitialToggle').forEach(elem => {
elem.classList.remove('isInitialToggle');
});
document.documentElement.setAttribute('data-color-mode', style);
localStorage.setItem('data-color-mode', style);
}
function currentTheme() {
const localStyle = localStorage.getItem('data-color-mode');
const systemStyle = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
return localStyle || systemStyle;
}
(() => {
setTheme(currentTheme());
})();