Files
RuneStone/src/lib/theme/ThemeSync.svelte
T

26 lines
1010 B
Svelte

<script lang="ts">
import { onMount } from 'svelte';
import { APPEARANCE_KEY, applyAppearance, parseAppearance } from './appearance';
import { CUSTOM_THEMES_KEY } from './customThemes';
onMount(() => {
const read = () => {
try {
const raw = localStorage.getItem(APPEARANCE_KEY);
const appearance = parseAppearance(raw);
const canonical = JSON.stringify(appearance);
if (raw !== null && raw !== canonical) localStorage.setItem(APPEARANCE_KEY, canonical);
applyAppearance(appearance);
}
catch { /* Keep the current appearance if storage is unavailable. */ }
};
const sync = (event: StorageEvent) => { if (event.key === APPEARANCE_KEY || event.key === CUSTOM_THEMES_KEY || event.key === null) read(); };
read();
window.addEventListener('storage', sync);
window.addEventListener('focus', read);
return () => { window.removeEventListener('storage', sync); window.removeEventListener('focus', read); };
});
</script>