From b94dc767ff60396b811508bcaa2dd23f1cd07382 Mon Sep 17 00:00:00 2001 From: Danny Liu Date: Sat, 9 Mar 2024 12:17:47 -0800 Subject: [PATCH 01/13] =?UTF-8?q?implement=20system=20wide=20light/dark=20?= =?UTF-8?q?mode=20option=20=F0=9F=94=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../components/chat/Settings/General.svelte | 54 +++++++++++++------ 1 file changed, 37 insertions(+), 17 deletions(-) diff --git a/src/lib/components/chat/Settings/General.svelte b/src/lib/components/chat/Settings/General.svelte index 8825b7902..70670d27e 100644 --- a/src/lib/components/chat/Settings/General.svelte +++ b/src/lib/components/chat/Settings/General.svelte @@ -18,6 +18,41 @@ let showAdvanced = false; + window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', e => { + if (theme === 'system') { + updateSystemTheme(); + } + }); + + function updateSystemTheme() { + const isDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches; + const systemTheme = isDarkMode ? 'dark' : 'light'; + applyTheme(systemTheme); + } + + function applyTheme(theme: string) { + localStorage.theme = theme; + const themeClassList = document.documentElement.classList; + if (theme === 'system') { + updateSystemTheme(); + return; + } + + themes + .filter((e) => e !== theme) + .forEach((e) => { + e.split(' ').forEach((e) => { + document.documentElement.classList.remove(e); + }); + }); + + theme.split(' ').forEach((e) => { + document.documentElement.classList.add(e); + }); + + console.log(theme) + } + const toggleNotification = async () => { const permission = await Notification.requestPermission(); @@ -123,24 +158,9 @@ class="w-fit pr-8 rounded py-2 px-2 text-xs bg-transparent outline-none text-right" bind:value={theme} placeholder="Select a theme" - on:change={(e) => { - localStorage.theme = theme; - - themes - .filter((e) => e !== theme) - .forEach((e) => { - e.split(' ').forEach((e) => { - document.documentElement.classList.remove(e); - }); - }); - - theme.split(' ').forEach((e) => { - document.documentElement.classList.add(e); - }); - - console.log(theme); - }} + on:change="{() => applyTheme(theme)}" > + From 27153ddb037167c3a4b64df4bf4e13f1a7b59a74 Mon Sep 17 00:00:00 2001 From: Danny Liu Date: Sat, 9 Mar 2024 13:12:15 -0800 Subject: [PATCH 02/13] remove unused variable --- src/lib/components/chat/Settings/General.svelte | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/components/chat/Settings/General.svelte b/src/lib/components/chat/Settings/General.svelte index 70670d27e..6c8bca130 100644 --- a/src/lib/components/chat/Settings/General.svelte +++ b/src/lib/components/chat/Settings/General.svelte @@ -32,7 +32,7 @@ function applyTheme(theme: string) { localStorage.theme = theme; - const themeClassList = document.documentElement.classList; + if (theme === 'system') { updateSystemTheme(); return; From 6f3acb347d76068b12c653e035566773e19ea7e7 Mon Sep 17 00:00:00 2001 From: Danny Liu Date: Sat, 16 Mar 2024 23:14:13 -0700 Subject: [PATCH 03/13] update theme handling and persist selection using Svelte store --- .../components/chat/Settings/General.svelte | 72 +++++++++---------- src/lib/stores/index.ts | 24 ++++++- src/routes/+layout.svelte | 4 +- 3 files changed, 59 insertions(+), 41 deletions(-) diff --git a/src/lib/components/chat/Settings/General.svelte b/src/lib/components/chat/Settings/General.svelte index 6c8bca130..8dca7c613 100644 --- a/src/lib/components/chat/Settings/General.svelte +++ b/src/lib/components/chat/Settings/General.svelte @@ -1,10 +1,11 @@
@@ -127,7 +125,7 @@
Theme
- {#if theme === 'dark'} + {#if actualTheme === 'dark'} - {:else if theme === 'light'} + {:else if actualTheme === 'light'} diff --git a/src/lib/stores/index.ts b/src/lib/stores/index.ts index e25cd33ed..7b15cf1d9 100644 --- a/src/lib/stores/index.ts +++ b/src/lib/stores/index.ts @@ -1,5 +1,5 @@ import { APP_NAME } from '$lib/constants'; -import { writable } from 'svelte/store'; +import { writable, derived } from 'svelte/store'; // Backend export const WEBUI_NAME = writable(APP_NAME); @@ -7,7 +7,27 @@ export const config = writable(undefined); export const user = writable(undefined); // Frontend -export const theme = writable('dark'); +const rawThemeSetting = writable('system'); +export const theme = derived(rawThemeSetting, ($rawThemeSetting) => { + if ($rawThemeSetting === 'system') { + return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'; + } + return $rawThemeSetting; +}); + +window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', (e) => { + rawThemeSetting.update((currentTheme) => { + if (currentTheme === 'system') { + return e.matches ? 'dark' : 'light'; + } + return currentTheme; + }); +}); + +export function setTheme(theme){ + rawThemeSetting.set(theme); + localStorage.setItem('theme', theme); +} export const chatId = writable(''); diff --git a/src/routes/+layout.svelte b/src/routes/+layout.svelte index d604f901a..6e19f263a 100644 --- a/src/routes/+layout.svelte +++ b/src/routes/+layout.svelte @@ -1,6 +1,6 @@ %sveltekit.head% From f55dae3027bc31690a380c89ccdea73a8c5a8098 Mon Sep 17 00:00:00 2001 From: Danny Liu Date: Sat, 16 Mar 2024 23:27:08 -0700 Subject: [PATCH 05/13] fix merge conflict --- src/lib/components/chat/Settings/General.svelte | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/lib/components/chat/Settings/General.svelte b/src/lib/components/chat/Settings/General.svelte index 95161f798..6a8bae355 100644 --- a/src/lib/components/chat/Settings/General.svelte +++ b/src/lib/components/chat/Settings/General.svelte @@ -117,9 +117,9 @@ function handleThemeChange(newTheme: string) { selectedTheme = newTheme; - setTheme(newTheme); // Update the store - localStorage.setItem('theme', newTheme); // Persist the theme selection - applyTheme(newTheme); // Apply the selected theme + setTheme(newTheme); + localStorage.setItem('theme', newTheme); + applyTheme(newTheme); } @@ -133,7 +133,7 @@