File size: 944 Bytes
377fc96 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | // Light or dark. The choice persists in this browser; without one the system setting
// decides. The root element carries data-theme so the stylesheet redefines the tokens.
import { writable } from 'svelte/store';
export type Theme = 'dark' | 'light';
const KEY = 'lifekit.theme';
function initial(): Theme {
try {
const saved = localStorage.getItem(KEY);
if (saved === 'dark' || saved === 'light') return saved;
} catch {
/* storage unavailable */
}
return typeof matchMedia === 'function' && matchMedia('(prefers-color-scheme: light)').matches ? 'light' : 'dark';
}
export const theme = writable<Theme>(initial());
theme.subscribe((t) => {
if (typeof document !== 'undefined') document.documentElement.setAttribute('data-theme', t);
try {
localStorage.setItem(KEY, t);
} catch {
/* storage unavailable */
}
});
export const toggleTheme = () => theme.update((t) => (t === 'dark' ? 'light' : 'dark'));
|