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
1f0dcb4f
Unverified
Commit
1f0dcb4f
authored
Mar 18, 2024
by
Timothy Jaeryang Baek
Committed by
GitHub
Mar 18, 2024
Browse files
Merge pull request #1115 from dannyl1u/feat/system-wide-theme
feat: system os light/dark mode option
🔦
parents
1bfcd801
19f69c82
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
66 additions
and
33 deletions
+66
-33
src/app.html
src/app.html
+28
-11
src/lib/components/chat/Settings/General.svelte
src/lib/components/chat/Settings/General.svelte
+37
-21
src/lib/stores/index.ts
src/lib/stores/index.ts
+1
-1
No files found.
src/app.html
View file @
1f0dcb4f
...
@@ -8,18 +8,35 @@
...
@@ -8,18 +8,35 @@
<meta
name=
"robots"
content=
"noindex,nofollow"
/>
<meta
name=
"robots"
content=
"noindex,nofollow"
/>
<script>
<script>
// On page load or when changing themes, best to add inline in `head` to avoid FOUC
// On page load or when changing themes, best to add inline in `head` to avoid FOUC
if
(
(()
=>
{
localStorage
.
theme
===
'
light
'
||
if
(
(
!
(
'
theme
'
in
localStorage
)
&&
window
.
matchMedia
(
'
(prefers-color-scheme: light)
'
).
matches
)
localStorage
.
theme
===
'
light
'
||
)
{
(
!
(
'
theme
'
in
localStorage
)
&&
window
.
matchMedia
(
'
(prefers-color-scheme: light)
'
).
matches
)
document
.
documentElement
.
classList
.
add
(
'
light
'
);
)
{
}
else
if
(
localStorage
.
theme
)
{
document
.
documentElement
.
classList
.
add
(
'
light
'
);
localStorage
.
theme
.
split
(
'
'
).
forEach
((
e
)
=>
{
}
else
if
(
localStorage
.
theme
&&
localStorage
.
theme
!==
'
system
'
)
{
document
.
documentElement
.
classList
.
add
(
e
);
localStorage
.
theme
.
split
(
'
'
).
forEach
((
e
)
=>
{
document
.
documentElement
.
classList
.
add
(
e
);
});
}
else
if
(
localStorage
.
theme
&&
localStorage
.
theme
===
'
system
'
)
{
systemTheme
=
window
.
matchMedia
(
'
(prefers-color-scheme: dark)
'
).
matches
;
document
.
documentElement
.
classList
.
add
(
systemTheme
?
'
dark
'
:
'
light
'
);
}
else
{
document
.
documentElement
.
classList
.
add
(
'
dark
'
);
}
window
.
matchMedia
(
'
(prefers-color-scheme: dark)
'
).
addListener
((
e
)
=>
{
if
(
localStorage
.
theme
===
'
system
'
)
{
if
(
e
.
matches
)
{
document
.
documentElement
.
classList
.
add
(
'
dark
'
);
document
.
documentElement
.
classList
.
remove
(
'
light
'
);
}
else
{
document
.
documentElement
.
classList
.
add
(
'
light
'
);
document
.
documentElement
.
classList
.
remove
(
'
dark
'
);
}
}
});
});
}
else
{
})();
document
.
documentElement
.
classList
.
add
(
'
dark
'
);
}
</script>
</script>
%sveltekit.head%
%sveltekit.head%
...
...
src/lib/components/chat/Settings/General.svelte
View file @
1f0dcb4f
...
@@ -4,7 +4,7 @@
...
@@ -4,7 +4,7 @@
import { getLanguages } from '$lib/i18n';
import { getLanguages } from '$lib/i18n';
const dispatch = createEventDispatcher();
const dispatch = createEventDispatcher();
import { models, user } from '$lib/stores';
import { models, user
, theme
} from '$lib/stores';
const i18n = getContext('i18n');
const i18n = getContext('i18n');
...
@@ -15,7 +15,8 @@
...
@@ -15,7 +15,8 @@
// General
// General
let themes = ['dark', 'light', 'rose-pine dark', 'rose-pine-dawn light'];
let themes = ['dark', 'light', 'rose-pine dark', 'rose-pine-dawn light'];
let theme = 'dark';
let selectedTheme = 'system';
let languages = [];
let languages = [];
let lang = $i18n.language;
let lang = $i18n.language;
let notificationEnabled = false;
let notificationEnabled = false;
...
@@ -68,10 +69,11 @@
...
@@ -68,10 +69,11 @@
};
};
onMount(async () => {
onMount(async () => {
selectedTheme = localStorage.theme ?? 'system';
let settings = JSON.parse(localStorage.getItem('settings') ?? '{}');
let settings = JSON.parse(localStorage.getItem('settings') ?? '{}');
languages = await getLanguages();
languages = await getLanguages();
theme = localStorage.theme ?? 'dark';
notificationEnabled = settings.notificationEnabled ?? false;
notificationEnabled = settings.notificationEnabled ?? false;
system = settings.system ?? '';
system = settings.system ?? '';
...
@@ -87,6 +89,35 @@
...
@@ -87,6 +89,35 @@
options = { ...options, ...settings.options };
options = { ...options, ...settings.options };
options.stop = (settings?.options?.stop ?? []).join(',');
options.stop = (settings?.options?.stop ?? []).join(',');
});
});
const applyTheme = (_theme: string) => {
let themeToApply = _theme;
if (_theme === 'system') {
themeToApply = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
}
themes
.filter((e) => e !== themeToApply)
.forEach((e) => {
e.split(' ').forEach((e) => {
document.documentElement.classList.remove(e);
});
});
themeToApply.split(' ').forEach((e) => {
document.documentElement.classList.add(e);
});
console.log(_theme);
};
const themeChangeHandler = (_theme: string) => {
theme.set(_theme);
localStorage.setItem('theme', _theme);
applyTheme(_theme);
};
</script>
</script>
<div class="flex flex-col h-full justify-between text-sm">
<div class="flex flex-col h-full justify-between text-sm">
...
@@ -99,26 +130,11 @@
...
@@ -99,26 +130,11 @@
<div class="flex items-center relative">
<div class="flex items-center relative">
<select
<select
class=" dark:bg-gray-900 w-fit pr-8 rounded py-2 px-2 text-xs bg-transparent outline-none text-right"
class=" dark:bg-gray-900 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"
placeholder="Select a theme"
on:change={(e) => {
on:change={() => themeChangeHandler(selectedTheme)}
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);
}}
>
>
<option value="system">⚙️ {$i18n.t('System')}</option>
<option value="dark">🌑 {$i18n.t('Dark')}</option>
<option value="dark">🌑 {$i18n.t('Dark')}</option>
<option value="light">☀️ {$i18n.t('Light')}</option>
<option value="light">☀️ {$i18n.t('Light')}</option>
<option value="rose-pine dark">🪻 {$i18n.t('Rosé Pine')}</option>
<option value="rose-pine dark">🪻 {$i18n.t('Rosé Pine')}</option>
...
...
src/lib/stores/index.ts
View file @
1f0dcb4f
...
@@ -7,7 +7,7 @@ export const config = writable(undefined);
...
@@ -7,7 +7,7 @@ export const config = writable(undefined);
export
const
user
=
writable
(
undefined
);
export
const
user
=
writable
(
undefined
);
// Frontend
// Frontend
export
const
theme
=
writable
(
'
dark
'
);
export
const
theme
=
writable
(
'
system
'
);
export
const
chatId
=
writable
(
''
);
export
const
chatId
=
writable
(
''
);
...
...
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