Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
chenpangpang
open-webui
Commits
6f3acb34
Commit
6f3acb34
authored
Mar 16, 2024
by
Danny Liu
Browse files
update theme handling and persist selection using Svelte store
parent
27153ddb
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
59 additions
and
41 deletions
+59
-41
src/lib/components/chat/Settings/General.svelte
src/lib/components/chat/Settings/General.svelte
+35
-37
src/lib/stores/index.ts
src/lib/stores/index.ts
+22
-2
src/routes/+layout.svelte
src/routes/+layout.svelte
+2
-2
No files found.
src/lib/components/chat/Settings/General.svelte
View file @
6f3acb34
<script lang="ts">
import { toast } from 'svelte-sonner';
import { createEventDispatcher, onMount } from 'svelte';
import { theme, setTheme } from '../../../stores/index';
const dispatch = createEventDispatcher();
import { models, user } from '$lib/stores';
...
...
@@ -12,41 +13,29 @@
// General
let themes = ['dark', 'light', 'rose-pine dark', 'rose-pine-dawn light'];
let theme = 'dark';
let selectedTheme = 'system';
let actualTheme: string;
$: actualTheme = $theme;
let notificationEnabled = false;
let system = '';
let showAdvanced = false;
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', e => {
function applyTheme(theme: string) { // only apply visually
let themeToApply = theme;
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;
if (theme === 'system') {
updateSystemTheme();
return;
themeToApply = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
}
themes
.filter((e) => e !== theme)
.filter((e) => e !== theme
ToApply
)
.forEach((e) => {
e.split(' ').forEach((e) => {
document.documentElement.classList.remove(e);
});
});
theme
.split(' ').forEach((e) => {
themeToApply
.split(' ').forEach((e) => {
document.documentElement.classList.add(e);
});
...
...
@@ -98,9 +87,11 @@
};
onMount(async () => {
selectedTheme = localStorage.getItem('theme') ?? 'system';
applyTheme(selectedTheme);
let settings = JSON.parse(localStorage.getItem('settings') ?? '{}');
theme = localStorage.theme ?? 'dark';
notificationEnabled = settings.notificationEnabled ?? false;
system = settings.system ?? '';
...
...
@@ -116,6 +107,13 @@
options = { ...options, ...settings.options };
options.stop = (settings?.options?.stop ?? []).join(',');
});
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
}
</script>
<div class="flex flex-col h-full justify-between text-sm">
...
...
@@ -127,7 +125,7 @@
<div class=" self-center text-xs font-medium">Theme</div>
<div class="flex items-center relative">
<div class=" absolute right-16">
{#if
t
heme === 'dark'}
{#if
actualT
heme === 'dark'}
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 20 20"
...
...
@@ -140,7 +138,7 @@
clip-rule="evenodd"
/>
</svg>
{:else if
t
heme === 'light'}
{:else if
actualT
heme === 'light'}
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 20 20"
...
...
@@ -156,9 +154,9 @@
<select
class="w-fit pr-8 rounded py-2 px-2 text-xs bg-transparent outline-none text-right"
bind:value={
t
heme}
bind:value={
selectedT
heme}
placeholder="Select a theme"
on:change="{() =>
applyTheme(t
heme)}"
on:change="{() =>
handleThemeChange(selectedT
heme)}"
>
<option value="system">System</option>
<option value="dark">Dark</option>
...
...
src/lib/stores/index.ts
View file @
6f3acb34
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
(
''
);
...
...
src/routes/+layout.svelte
View file @
6f3acb34
<script>
import { onMount, tick } from 'svelte';
import { config, user,
t
heme, WEBUI_NAME } from '$lib/stores';
import { config, user,
setT
heme, WEBUI_NAME } from '$lib/stores';
import { goto } from '$app/navigation';
import { Toaster, toast } from 'svelte-sonner';
...
...
@@ -15,7 +15,7 @@
let loaded = false;
onMount(async () => {
theme.set
(localStorage.theme)
;
setTheme
(localStorage.theme)
// Check Backend Status
const backendConfig = await getBackendConfig();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment