Skip to content
This repository has been archived by the owner on Aug 3, 2024. It is now read-only.

Commit

Permalink
Fix system theme not respecting preferred dark theme. (#1673)
Browse files Browse the repository at this point in the history
  • Loading branch information
Prospector authored Apr 11, 2024
1 parent 6072765 commit 5b1850e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
4 changes: 3 additions & 1 deletion layouts/default.vue
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,9 @@ function toggleBrowseMenu() {
}
function changeTheme() {
updateTheme(
DARK_THEMES.includes(app.$colorMode.value) ? 'light' : cosmetics.value.preferredDarkTheme,
DARK_THEMES.includes(app.$colorMode.value)
? 'light'
: cosmetics.value.preferredDarkTheme ?? 'dark',
true
)
}
Expand Down
15 changes: 11 additions & 4 deletions pages/settings/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
class="theme-icon"
/>
<MoonIcon
v-else-if="cosmetics.preferredDarkTheme === option"
v-else-if="(cosmetics.preferredDarkTheme ?? 'dark') === option"
v-tooltip="formatMessage(colorTheme.preferredDark)"
class="theme-icon"
/>
Expand Down Expand Up @@ -402,14 +402,21 @@ const themeOptions = computed(() => {
onMounted(() => {
updateSystemTheme()
window.matchMedia('(prefers-color-scheme: light)').addEventListener('change', (event) => {
setSystemTheme(event.matches)
})
})
function updateSystemTheme() {
const colorSchemeQueryList = window.matchMedia('(prefers-color-scheme: light)')
if (colorSchemeQueryList.matches) {
const query = window.matchMedia('(prefers-color-scheme: light)')
setSystemTheme(query.matches)
}
function setSystemTheme(light) {
if (light) {
systemTheme.value = 'light'
} else {
systemTheme.value = cosmetics.value.preferredDarkTheme
systemTheme.value = cosmetics.value.preferredDarkTheme ?? 'dark'
}
}
Expand Down
3 changes: 2 additions & 1 deletion plugins/1.theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export default defineNuxtPlugin(async (nuxtApp) => {
await useAuth()
await useUser()
const themeStore = useTheme()
const cosmetics = useCosmetics()

nuxtApp.hook('app:mounted', () => {
if (process.client && themeStore.value.preference === 'system') {
Expand All @@ -12,7 +13,7 @@ export default defineNuxtPlugin(async (nuxtApp) => {
if (e.matches) {
updateTheme('light')
} else {
updateTheme('dark')
updateTheme(cosmetics.value.preferredDarkTheme ?? 'dark')
}
}
}
Expand Down

0 comments on commit 5b1850e

Please sign in to comment.