web: proper theming

This commit is contained in:
wukko
2024-06-29 20:09:17 +06:00
parent 3b31efdfbb
commit 078530ba1a
3 changed files with 68 additions and 31 deletions

View File

@@ -0,0 +1,34 @@
import { readable, derived } from 'svelte/store';
import settings from '$lib/settings';
import { themeOptions } from '$lib/types/settings';
let set: (_: typeof themeOptions[number]) => void;
const browserPreference = () =>
window.matchMedia('(prefers-color-scheme: light)')
.matches ? 'light' : 'dark';
const browserPreferenceReadable = readable(
browserPreference(),
_set => { set = _set }
)
const matchMedia = window.matchMedia('(prefers-color-scheme: dark)');
if (matchMedia.addEventListener) {
matchMedia.addEventListener('change', () => set(browserPreference()));
}
export default derived(
[settings, browserPreferenceReadable],
([$settings, $browserPref]) => {
if ($settings.appearance.theme !== 'auto') {
return $settings.appearance.theme;
}
return $browserPref;
},
browserPreference()
);