Skip to content

Commit 606112e

Browse files
authored
feat: detect and set preferred theme based on user settings (#634)
* feat: detect and set preferred theme based on user settings * feat: add auto theme settings * fix: clarify dark theme application logic * Lint
1 parent 8ec401c commit 606112e

File tree

4 files changed

+28
-10
lines changed

4 files changed

+28
-10
lines changed

src/App.vue

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ div#wrapper(v-if="loaded")
1515
<script lang="ts">
1616
import { useSettingsStore } from '~/stores/settings';
1717
import { useServerStore } from '~/stores/server';
18+
import { detectPreferredTheme } from '~/util/theme';
1819
// if vite is used, you can import css file as module
1920
//import darkCssUrl from '../static/dark.css?url';
2021
//import darkCssContent from '../static/dark.css?inline';
@@ -39,8 +40,10 @@ export default {
3940
const settingsStore = useSettingsStore();
4041
await settingsStore.ensureLoaded();
4142
const theme = settingsStore.theme;
42-
// Check Application Mode (Light | Dark)
43-
if (theme !== null && theme === 'dark') {
43+
const detectedTheme = theme === 'auto' ? detectPreferredTheme() : theme;
44+
45+
// Apply the dark theme if detected
46+
if (detectedTheme === 'dark') {
4447
const method: 'link' | 'style' = 'link';
4548
4649
if (method === 'link') {
@@ -49,8 +52,8 @@ export default {
4952
const themeLink = document.createElement('link');
5053
themeLink.href = '/dark.css'; // darkCssUrl
5154
themeLink.rel = 'stylesheet';
52-
// Append Dark Theme Element If Selected Mode Is Dark
53-
theme === 'dark' ? document.querySelector('head').appendChild(themeLink) : '';
55+
// Append Dark Theme Element
56+
document.querySelector('head').appendChild(themeLink);
5457
} else {
5558
// Not supported for Webpack due to not supporting ?inline import in a cross-compatible way (afaik)
5659
// Method 2: Create <style> Element

src/stores/settings.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ interface State {
2727
durationDefault: number;
2828
useColorFallback: boolean;
2929
landingpage: string;
30-
theme: 'light' | 'dark';
30+
theme: 'light' | 'dark' | 'auto';
3131

3232
newReleaseCheckData: Record<string, any>;
3333
userSatisfactionPollData: {
@@ -59,7 +59,7 @@ export const useSettingsStore = defineStore('settings', {
5959
useColorFallback: false,
6060
landingpage: '/home',
6161

62-
theme: 'light',
62+
theme: 'auto',
6363

6464
newReleaseCheckData: {
6565
isEnabled: true,

src/util/theme.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
export function detectPreferredTheme(): 'light' | 'dark' {
2+
if (window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches) {
3+
return 'dark';
4+
}
5+
return 'light';
6+
}

src/views/settings/Theme.vue

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ div
55
h5.mt-1.mb-2.mb-sm-0 Theme
66
div
77
b-select.landingpage(v-if="_loaded" size="sm" :value="theme", @change="theme = $event")
8+
option(value="auto") Auto (System)
89
option(value="light") Light
910
option(value="dark") Dark
1011
span(v-else)
@@ -16,6 +17,7 @@ div
1617
<script lang="ts">
1718
import { mapState } from 'pinia';
1819
import { useSettingsStore } from '~/stores/settings';
20+
import { detectPreferredTheme } from '~/util/theme';
1921
2022
export default {
2123
name: 'Theme',
@@ -33,15 +35,22 @@ export default {
3335
theme: value,
3436
});
3537
38+
// Determine the actual theme to apply
39+
const detectedTheme = value === 'auto' ? detectPreferredTheme() : value;
40+
3641
// Apply newly set theme
3742
// Create Dark Theme Element
3843
const themeLink = document.createElement('link');
3944
themeLink.href = '/dark.css';
4045
themeLink.rel = 'stylesheet';
41-
// Append Dark Theme Element If Selected Mode Is Dark
42-
value === 'dark'
43-
? document.querySelector('head').appendChild(themeLink)
44-
: document.querySelector(`link[href="${new URL(themeLink.href).pathname}"]`).remove();
46+
47+
// Remove existing theme link if present
48+
document.querySelector(`link[href="${new URL(themeLink.href).pathname}"]`)?.remove();
49+
50+
// Append Dark Theme Element if dark theme should be applied
51+
if (detectedTheme === 'dark') {
52+
document.querySelector('head').appendChild(themeLink);
53+
}
4554
},
4655
},
4756
},

0 commit comments

Comments
 (0)