fix mixed background color under dark mode

This commit is contained in:
Miranquil 2021-01-23 22:31:20 +08:00
parent bd7c4f9e1f
commit b8c3456420
No known key found for this signature in database
GPG Key ID: F7AD72C904129DCB
2 changed files with 54 additions and 31 deletions

View File

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

View File

@ -7,61 +7,81 @@ KEEP.initModeToggle = () => {
iconDom: document.querySelector('.tool-dark-light-toggle i'),
articleContent: document.querySelector('.main-content'),
setItemUtil(modeClass, prefersColorScheme) {
document.body.classList.toggle(modeClass);
const isDark = document.body.className.indexOf(modeClass) === -1;
setItemUtil(modeClass) {
const isDark = document.body.className.indexOf('light-mode') === -1;
let currentScheme = "";
if (isDark) {
this.iconDom.className = 'fas fa-moon';
this.articleContent.classList.remove('night-code-theme');
this.enableLightMode();
currentScheme = "light";
} else {
this.iconDom.className = 'fas fa-sun';
this.articleContent.classList.add('night-code-theme');
this.enableDarkMode();
currentScheme = "dark";
}
localStorage.setItem(this.localStorageKey, JSON.stringify(
{
prefersColorScheme: prefersColorScheme,
isDark: isDark
prefersColorScheme: currentScheme,
isDark: !isDark
}
));
},
enableLightMode() {
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';
},
enableDarkMode() {
document.body.classList.remove('light-mode');
document.body.classList.add('dark-mode');
this.articleContent.classList.add('night-code-theme');
this.iconDom.className = 'fas fa-sun';
},
initModeStatus() {
this.modeConfig = JSON.parse(localStorage.getItem(this.localStorageKey));
if (this.modeConfig) {
if (this.modeConfig.prefersColorScheme === 'dark') {
if (this.modeConfig.isDark) {
document.body.classList.remove('light-mode');
this.articleContent.classList.remove('night-code-theme');
this.iconDom.className = 'fas fa-sun';
this.enableDarkMode();
} else {
document.body.classList.add('light-mode');
this.articleContent.classList.add('night-code-theme');
this.iconDom.className = 'fas fa-moon';
this.enableLightMode();
}
} else {
if (this.modeConfig.isDark) {
document.body.classList.remove('dark-mode');
this.articleContent.classList.remove('night-code-theme');
this.iconDom.className = 'fas fa-moon';
this.enableDarkMode();
} else {
document.body.classList.add('dark-mode');
this.articleContent.classList.add('night-code-theme');
this.iconDom.className = 'fas fa-sun';
this.enableLightMode();
}
}
} else {
// First time visit with no LocalStorage exists.
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
this.enableDarkMode();
} else {
this.enableLightMode();
}
}
},
initModeToggleButton() {
this.modeToggleButton_dom.addEventListener('click', () => {
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
this.setItemUtil('light-mode', 'dark');
this.setItemUtil('light-mode');
} else {
this.setItemUtil('dark-mode', 'light');
this.setItemUtil('dark-mode');
}
});
},
initModeAutoTrigger() {
const darkMode = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)');
darkMode && darkMode.addEventListener('change', e => {
if (e.matches) {
this.enableDarkMode();
} else {
this.enableLightMode();
}
});
}
@ -69,5 +89,6 @@ KEEP.initModeToggle = () => {
KEEP.utils.modeToggle.initModeStatus();
KEEP.utils.modeToggle.initModeToggleButton();
KEEP.utils.modeToggle.initModeAutoTrigger();
};