diff --git a/web/src/app.html b/web/src/app.html index 50f1b31095..d1db02f493 100644 --- a/web/src/app.html +++ b/web/src/app.html @@ -18,11 +18,37 @@ /** * Prevent FOUC on page load. */ - const theme = localStorage.getItem('color-theme') || 'dark'; - if (theme === 'light') { + const colorThemeKeyName = 'color-theme'; + let theme; + const fallbackTheme = { value: 'dark', system: false }; + let item = localStorage.getItem(colorThemeKeyName); + if (item === 'dark' || item === 'light') { + fallbackTheme.value = item; + item = JSON.stringify(fallbackTheme); + localStorage.setItem(colorThemeKeyName, item); + } + + theme = JSON.parse(localStorage.getItem(colorThemeKeyName)); + + if (theme) { + if (theme.system) { + if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) { + theme.value = 'dark'; + } else { + theme.value = 'light'; + } + } + } else { + theme = fallbackTheme; + } + + if (theme.value === 'light') { document.documentElement.classList.remove('dark'); + } else { + document.documentElement.classList.add('dark'); } + diff --git a/web/src/lib/components/onboarding-page/onboarding-theme.svelte b/web/src/lib/components/onboarding-page/onboarding-theme.svelte index b47cce9527..9a801a7be7 100644 --- a/web/src/lib/components/onboarding-page/onboarding-theme.svelte +++ b/web/src/lib/components/onboarding-page/onboarding-theme.svelte @@ -6,19 +6,12 @@ import { createEventDispatcher } from 'svelte'; import { colorTheme } from '$lib/stores/preferences.store'; import { moonPath, moonViewBox, sunPath, sunViewBox } from '$lib/assets/svg-paths'; + import { Theme } from '$lib/constants'; const dispatch = createEventDispatcher<{ done: void; previous: void; }>(); - - const toggleLightTheme = () => { - $colorTheme = 'light'; - }; - - const toggleDarkTheme = () => { - $colorTheme = 'dark'; - }; @@ -31,7 +24,7 @@