ThemeStore adjustments

This commit is contained in:
Dallas Hoffman 2023-10-01 17:55:03 -04:00
parent 329e7ce4d4
commit bbb3bbe5ff
1 changed files with 17 additions and 10 deletions

View File

@ -1,4 +1,4 @@
import { writable, type Readable } from 'svelte/store'; import { writable, type Readable, get } from 'svelte/store';
export type Theme = 'light' | 'dark'; export type Theme = 'light' | 'dark';
@ -8,22 +8,29 @@ export class ThemeStore implements Readable<Theme> {
(window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light') (window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light')
); );
constructor() { readonly themeColor: Record<Theme, string> = {
const themeMetaTag = document.querySelector('meta[name="theme-color"]'); light: '#fbfbfc',
dark: '#18232c',
};
this.theme.subscribe((newTheme) => { constructor() {
document.documentElement.dataset.theme = newTheme; this.apply(get(this.theme));
localStorage.setItem('theme', newTheme);
themeMetaTag?.setAttribute('content', newTheme === 'dark' ? '#18232c' : '#fbfbfc');
});
} }
private apply = (theme: Theme) => {
const themeMetaTag = document.querySelector('meta[name="theme-color"]');
document.documentElement.dataset.theme = theme;
localStorage.setItem('theme', theme);
themeMetaTag?.setAttribute('content', this.themeColor[theme]);
};
subscribe = this.theme.subscribe; subscribe = this.theme.subscribe;
set = this.theme.set;
toggle = () => { toggle = () => {
this.theme.update((currentTheme) => { this.theme.update((currentTheme) => {
return currentTheme === 'dark' ? 'light' : 'dark'; const newTheme = currentTheme === 'dark' ? 'light' : 'dark';
this.apply(newTheme);
return newTheme;
}); });
}; };
} }