@anmiles/theme-switcher
Version:
Theme switcher for websites
23 lines (18 loc) • 605 B
text/typescript
import { EventEmitter } from '../lib/eventEmitter';
import { isTheme } from '../lib/theme';
import type { Theme } from '../lib/theme';
export class UserProvider extends EventEmitter<{ change: [Theme | undefined] }> {
private readonly storageKey = 'theme';
public get(): Theme | undefined {
const theme = localStorage.getItem(this.storageKey);
return isTheme(theme) ? theme : undefined;
}
public set(theme: Theme | undefined): void {
if (theme) {
localStorage.setItem(this.storageKey, theme);
} else {
localStorage.removeItem(this.storageKey);
}
this.emit('change', theme);
}
}