Merge remote-tracking branch 'origin/master'

# Conflicts:
#	source/js/dark-light-toggle.js
This commit is contained in:
XPoet 2021-01-24 18:45:53 +08:00
commit df1f9213b6
2 changed files with 65 additions and 33 deletions

View File

@ -136,16 +136,18 @@ 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');
} }

View File

@ -1,58 +1,87 @@
/* 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, prefersColorScheme) { setItemUtil(modeClass) {
document.body.classList.toggle(modeClass); const isDark = document.body.className.indexOf('light-mode') === -1;
const isDark = document.body.className.indexOf(modeClass) === -1; let currentScheme = "";
if (isDark) { if (isDark) {
this.iconDom.className = 'fas fa-moon'; this.enableLightMode();
currentScheme = "light";
} else { } else {
this.iconDom.className = 'fas fa-sun'; this.enableDarkMode();
currentScheme = "dark";
} }
KEEP.styleStatus.isDark = isDark; localStorage.setItem(this.localStorageKey, JSON.stringify(
KEEP.styleStatus.prefersColorScheme = prefersColorScheme; {
KEEP.setStyleStatus(); 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() { initModeStatus() {
const styleStatus = KEEP.getStyleStatus(); this.modeConfig = JSON.parse(localStorage.getItem(this.localStorageKey));
if (styleStatus) { if (this.modeConfig) {
if (styleStatus.prefersColorScheme === 'dark') { if (this.modeConfig.prefersColorScheme === 'dark') {
if (styleStatus.isDark) { if (this.modeConfig.isDark) {
document.body.classList.remove('light-mode'); this.enableDarkMode();
this.iconDom.className = 'fas fa-sun';
} else { } else {
document.body.classList.add('light-mode'); this.enableLightMode();
this.iconDom.className = 'fas fa-moon';
} }
} else { } else {
if (this.modeConfig.isDark) {
if (styleStatus.isDark) { this.enableDarkMode();
document.body.classList.remove('dark-mode');
this.iconDom.className = 'fas fa-moon';
} else { } else {
document.body.classList.add('dark-mode'); this.enableLightMode();
this.iconDom.className = 'fas fa-sun';
} }
} }
} else {
// First time visit with no LocalStorage exists.
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) { if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
this.setItemUtil('light-mode', 'dark'); this.setItemUtil('light-mode');
} else { } 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();
} }
}); });
} }
@ -60,5 +89,6 @@ KEEP.initModeToggle = () => {
KEEP.utils.modeToggle.initModeStatus(); KEEP.utils.modeToggle.initModeStatus();
KEEP.utils.modeToggle.initModeToggleButton(); KEEP.utils.modeToggle.initModeToggleButton();
KEEP.utils.modeToggle.initModeAutoTrigger();
}; };