refactor: move component into a single package (#898)

This commit is contained in:
Himself65
2023-02-08 22:19:11 -06:00
committed by GitHub
parent 0984c37cad
commit cc605251a8
145 changed files with 9609 additions and 450 deletions
@@ -0,0 +1,2 @@
export * from './systemThemeHelper';
export * from './localStorageThemeHelper';
@@ -0,0 +1,13 @@
import { ThemeMode } from '../types';
export class LocalStorageThemeHelper {
name = 'Affine-theme-mode';
get = (): ThemeMode | null => {
return localStorage.getItem(this.name) as ThemeMode | null;
};
set = (mode: ThemeMode) => {
localStorage.setItem(this.name, mode);
};
}
export const localStorageThemeHelper = new LocalStorageThemeHelper();
@@ -0,0 +1,29 @@
import { Theme } from '../types';
export class SystemThemeHelper {
media: MediaQueryList = window.matchMedia('(prefers-color-scheme: light)');
eventList: Array<(e: Event) => void> = [];
eventHandler = (e: Event) => {
this.eventList.forEach(fn => fn(e));
};
constructor() {
this.media.addEventListener('change', this.eventHandler);
}
get = (): Theme => {
if (typeof window === 'undefined') {
return 'light';
}
return this.media.matches ? 'light' : 'dark';
};
onChange = (callback: () => void) => {
this.eventList.push(callback);
};
dispose = () => {
this.eventList = [];
this.media.removeEventListener('change', this.eventHandler);
};
}