2020-12-23 19:03:40 +01:00
|
|
|
type colorScheme = 'light' | 'dark' | 'auto';
|
|
|
|
|
|
|
|
class StackColorScheme {
|
|
|
|
private localStorageKey = 'StackColorScheme';
|
|
|
|
private currentScheme: colorScheme;
|
|
|
|
private systemPreferScheme: colorScheme;
|
|
|
|
|
|
|
|
constructor(toggleEl: HTMLElement) {
|
|
|
|
this.bindMatchMedia();
|
|
|
|
this.currentScheme = this.getSavedScheme();
|
2024-10-06 19:32:13 +02:00
|
|
|
if (window.matchMedia('(prefers-color-scheme: dark)').matches === true)
|
|
|
|
this.systemPreferScheme = 'dark'
|
|
|
|
else
|
|
|
|
this.systemPreferScheme = 'light';
|
2020-12-23 19:03:40 +01:00
|
|
|
|
2021-06-20 15:49:35 +02:00
|
|
|
this.dispatchEvent(document.documentElement.dataset.scheme as colorScheme);
|
2021-01-04 10:52:14 +01:00
|
|
|
|
2020-12-23 19:03:40 +01:00
|
|
|
if (toggleEl)
|
|
|
|
this.bindClick(toggleEl);
|
|
|
|
|
|
|
|
if (document.body.style.transition == '')
|
|
|
|
document.body.style.setProperty('transition', 'background-color .3s ease');
|
|
|
|
}
|
|
|
|
|
|
|
|
private saveScheme() {
|
|
|
|
localStorage.setItem(this.localStorageKey, this.currentScheme);
|
|
|
|
}
|
|
|
|
|
2021-07-06 12:25:27 +02:00
|
|
|
private bindClick(toggleEl: HTMLElement) {
|
2020-12-23 19:03:40 +01:00
|
|
|
toggleEl.addEventListener('click', (e) => {
|
|
|
|
if (this.isDark()) {
|
|
|
|
/// Disable dark mode
|
|
|
|
this.currentScheme = 'light';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.currentScheme = 'dark';
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setBodyClass();
|
|
|
|
|
|
|
|
if (this.currentScheme == this.systemPreferScheme) {
|
|
|
|
/// Set to auto
|
|
|
|
this.currentScheme = 'auto';
|
|
|
|
}
|
|
|
|
|
|
|
|
this.saveScheme();
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
private isDark() {
|
|
|
|
return (this.currentScheme == 'dark' || this.currentScheme == 'auto' && this.systemPreferScheme == 'dark');
|
|
|
|
}
|
|
|
|
|
|
|
|
private dispatchEvent(colorScheme: colorScheme) {
|
|
|
|
const event = new CustomEvent('onColorSchemeChange', {
|
|
|
|
detail: colorScheme
|
|
|
|
});
|
|
|
|
window.dispatchEvent(event);
|
|
|
|
}
|
|
|
|
|
|
|
|
private setBodyClass() {
|
|
|
|
if (this.isDark()) {
|
2021-06-20 15:49:35 +02:00
|
|
|
document.documentElement.dataset.scheme = 'dark';
|
2020-12-23 19:03:40 +01:00
|
|
|
}
|
|
|
|
else {
|
2021-06-20 15:49:35 +02:00
|
|
|
document.documentElement.dataset.scheme = 'light';
|
2020-12-23 19:03:40 +01:00
|
|
|
}
|
|
|
|
|
2021-06-20 15:49:35 +02:00
|
|
|
this.dispatchEvent(document.documentElement.dataset.scheme as colorScheme);
|
2020-12-23 19:03:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private getSavedScheme(): colorScheme {
|
|
|
|
const savedScheme = localStorage.getItem(this.localStorageKey);
|
|
|
|
|
|
|
|
if (savedScheme == 'light' || savedScheme == 'dark' || savedScheme == 'auto') return savedScheme;
|
|
|
|
else return 'auto';
|
|
|
|
}
|
|
|
|
|
|
|
|
private bindMatchMedia() {
|
|
|
|
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', (e) => {
|
|
|
|
if (e.matches) {
|
|
|
|
this.systemPreferScheme = 'dark';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.systemPreferScheme = 'light';
|
|
|
|
}
|
|
|
|
this.setBodyClass();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-20 15:49:35 +02:00
|
|
|
export default StackColorScheme;
|