From 84e60ea15542cb819e4e06c3ce882f2571fa046a Mon Sep 17 00:00:00 2001 From: martin <74269598+martabal@users.noreply.github.com> Date: Sun, 7 Jan 2024 01:15:25 +0100 Subject: [PATCH] feat(web): auto switch theme (#6176) * move from app.html to user-page-layout.svelte * fix: use layout.svelte * simplify * fix: map style don't change * fix: auto switch theme map * use constants * simplify * rename * rename settign * fix: remove * pr feedback * fix: tests * fix: migration * fix: migration * pr feedback * simplify * simplify * pr feedback * fix: merge * chore: set insetad of toggle on click --------- Co-authored-by: Jason Rasmussen --- web/src/app.html | 30 ++++++++++++- .../onboarding-page/onboarding-theme.svelte | 13 ++---- .../shared-components/map/map.svelte | 3 +- .../shared-components/theme-button.svelte | 36 ++++++---------- .../appearance-settings.svelte | 24 +++++++++++ .../user-settings-list.svelte | 5 +++ web/src/lib/constants.ts | 6 +++ web/src/lib/stores/preferences.store.ts | 37 ++++++++++++++-- web/src/routes/+layout.svelte | 42 ++++++++++++++----- 9 files changed, 144 insertions(+), 52 deletions(-) create mode 100644 web/src/lib/components/user-settings-page/appearance-settings.svelte 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 @@