perf: optimize dark and light mode toggle

This commit is contained in:
XPoet 2021-01-24 19:54:44 +08:00
parent df1f9213b6
commit 96286031ff
4 changed files with 29 additions and 65 deletions

View File

@ -56,6 +56,7 @@ code-theme(mode) {
code-theme('light'); code-theme('light');
} }
@media (prefers-color-scheme: dark) { @media (prefers-color-scheme: dark) {
:root { :root {
code-theme('dark'); code-theme('dark');

View File

@ -136,22 +136,23 @@ root-color(mode) {
} }
:root {
root-color('light');
}
@media (prefers-color-scheme: dark) { @media (prefers-color-scheme: dark) {
:root { :root {
root-color('dark'); root-color('dark');
} }
} }
@media (prefers-color-scheme: light) {
:root {
root-color('light');
}
}
.light-mode { .light-mode {
root-color('light'); root-color('light');
} }
.dark-mode { .dark-mode {
root-color('dark'); root-color('dark');
} }

View File

@ -1,88 +1,51 @@
/* global KEEP */
KEEP.initModeToggle = () => { KEEP.initModeToggle = () => {
KEEP.utils.modeToggle = { KEEP.utils.modeToggle = {
localStorageKey: 'KEEP',
modeToggleButton_dom: document.querySelector('.tool-dark-light-toggle'), modeToggleButton_dom: document.querySelector('.tool-dark-light-toggle'),
iconDom: document.querySelector('.tool-dark-light-toggle i'), iconDom: document.querySelector('.tool-dark-light-toggle i'),
articleContent: document.querySelector('.main-content'),
setItemUtil(modeClass) {
const isDark = document.body.className.indexOf('light-mode') === -1;
let currentScheme = "";
if (isDark) {
this.enableLightMode();
currentScheme = "light";
} else {
this.enableDarkMode();
currentScheme = "dark";
}
localStorage.setItem(this.localStorageKey, JSON.stringify(
{
prefersColorScheme: currentScheme,
isDark: !isDark
}
));
},
enableLightMode() { enableLightMode() {
document.body.classList.remove('dark-mode'); document.body.classList.remove('dark-mode');
document.body.classList.add('light-mode');
this.articleContent.classList.remove('night-code-theme');
this.iconDom.className = 'fas fa-moon'; this.iconDom.className = 'fas fa-moon';
KEEP.styleStatus.isDark = false;
KEEP.setStyleStatus();
}, },
enableDarkMode() { enableDarkMode() {
document.body.classList.remove('light-mode');
document.body.classList.add('dark-mode'); document.body.classList.add('dark-mode');
this.articleContent.classList.add('night-code-theme');
this.iconDom.className = 'fas fa-sun'; this.iconDom.className = 'fas fa-sun';
KEEP.styleStatus.isDark = true;
KEEP.setStyleStatus();
},
isDarkPrefersColorScheme() {
return window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)');
}, },
initModeStatus() { initModeStatus() {
this.modeConfig = JSON.parse(localStorage.getItem(this.localStorageKey)); const styleStatus = KEEP.getStyleStatus();
if (this.modeConfig) {
if (this.modeConfig.prefersColorScheme === 'dark') { if (styleStatus) {
if (this.modeConfig.isDark) { styleStatus.isDark ? this.enableDarkMode() : this.enableLightMode();
this.enableDarkMode();
} else {
this.enableLightMode();
}
} else {
if (this.modeConfig.isDark) {
this.enableDarkMode();
} else {
this.enableLightMode();
}
}
} else { } else {
// First time visit with no LocalStorage exists. this.isDarkPrefersColorScheme().matches ? this.enableDarkMode() : this.enableLightMode();
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
this.enableDarkMode();
} else {
this.enableLightMode();
}
} }
}, },
initModeToggleButton() { initModeToggleButton() {
this.modeToggleButton_dom.addEventListener('click', () => { this.modeToggleButton_dom.addEventListener('click', () => {
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) { const isDark = document.body.classList.contains('dark-mode');
this.setItemUtil('light-mode'); isDark ? this.enableLightMode() : this.enableDarkMode();
} else {
this.setItemUtil('dark-mode');
}
}); });
}, },
initModeAutoTrigger() { initModeAutoTrigger() {
const darkMode = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)'); const isDarkMode = this.isDarkPrefersColorScheme();
darkMode && darkMode.addEventListener('change', e => { isDarkMode.addEventListener('change', e => {
if (e.matches) { e.matches ? this.enableDarkMode() : this.enableLightMode();
this.enableDarkMode();
} else {
this.enableLightMode();
}
}); });
} }
} }
@ -90,5 +53,4 @@ KEEP.initModeToggle = () => {
KEEP.utils.modeToggle.initModeStatus(); KEEP.utils.modeToggle.initModeStatus();
KEEP.utils.modeToggle.initModeToggleButton(); KEEP.utils.modeToggle.initModeToggleButton();
KEEP.utils.modeToggle.initModeAutoTrigger(); KEEP.utils.modeToggle.initModeAutoTrigger();
}; };

View File

@ -12,9 +12,9 @@ window.addEventListener('DOMContentLoaded', () => {
KEEP.styleStatus = { KEEP.styleStatus = {
isExpandPageWidth: false, isExpandPageWidth: false,
prefersColorScheme: 'light',
isDark: false, isDark: false,
fontSizeLevel: 0 fontSizeLevel: 0,
isOpenPageAside: true
} }
// print theme base info // print theme base info