Skip to content

Commit

Permalink
feat: 明暗模式设置为跟随系统时,支持动态切换
Browse files Browse the repository at this point in the history
  • Loading branch information
hooray committed Jan 27, 2024
1 parent e07c59f commit 98792c0
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 23 deletions.
28 changes: 23 additions & 5 deletions src/store/modules/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,28 @@ export const useSettingsStore = defineStore(
'settings',
() => {
const settings = ref(settingsDefault)
watch(() => settings.value.app.colorScheme, (colorScheme) => {

const prefersColorScheme = window.matchMedia('(prefers-color-scheme: dark)')
const currentColorScheme = ref<Exclude<Settings.app['colorScheme'], ''>>()
watch(() => settings.value.app.colorScheme, (val) => {
if (val === '') {
prefersColorScheme.addEventListener('change', updateTheme)
}
else {
prefersColorScheme.removeEventListener('change', updateTheme)
}
}, {
immediate: true,
})
watch(() => settings.value.app.colorScheme, updateTheme, {
immediate: true,
})
function updateTheme() {
let colorScheme = settings.value.app.colorScheme
if (colorScheme === '') {
colorScheme = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light'
colorScheme = prefersColorScheme.matches ? 'dark' : 'light'
}
currentColorScheme.value = colorScheme
switch (colorScheme) {
case 'dark':
document.documentElement.classList.add('dark')
Expand All @@ -18,9 +36,8 @@ export const useSettingsStore = defineStore(
document.documentElement.classList.remove('dark')
break
}
}, {
immediate: true,
})
}

watch(() => settings.value.menu.menuMode, (val) => {
document.body.setAttribute('data-menu-mode', val)
}, {
Expand Down Expand Up @@ -60,6 +77,7 @@ export const useSettingsStore = defineStore(

return {
settings,
currentColorScheme,
os,
title,
previewAllWindows,
Expand Down
19 changes: 9 additions & 10 deletions src/views/components/AppSetting/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,6 @@ const menuStore = useMenuStore()
const isShow = ref(false)
const isDark = computed({
get() {
return settingsStore.settings.app.colorScheme === 'dark'
},
set(value) {
settingsStore.settings.app.colorScheme = value ? 'dark' : 'light'
},
})
watch(() => settingsStore.settings.menu.menuMode, (value) => {
if (value === 'single') {
menuStore.setActived(0)
Expand Down Expand Up @@ -91,7 +82,15 @@ function handleCopy() {
颜色主题风格
</div>
<div class="flex items-center justify-center pb-4">
<HToggle v-model="isDark" on-icon="ri:sun-line" off-icon="ri:moon-line" />
<HTabList
v-model="settingsStore.settings.app.colorScheme"
:options="[
{ icon: 'i-ri:sun-line', label: '明亮', value: 'light' },
{ icon: 'i-ri:moon-line', label: '暗黑', value: 'dark' },
{ icon: 'i-ri:computer-line', label: '系统', value: '' },
]"
class="w-60"
/>
</div>
<div class="divider">
导航栏模式
Expand Down
25 changes: 21 additions & 4 deletions src/views/components/Topbar/ColorScheme/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const settingsStore = useSettingsStore()
function toggleColorScheme(event: MouseEvent) {
const { startViewTransition } = useViewTransition(() => {
settingsStore.setColorScheme(settingsStore.settings.app.colorScheme === 'dark' ? 'light' : 'dark')
settingsStore.currentColorScheme && settingsStore.setColorScheme(settingsStore.currentColorScheme === 'dark' ? 'light' : 'dark')
})
startViewTransition()?.ready.then(() => {
const x = event.clientX
Expand Down Expand Up @@ -37,7 +37,24 @@ function toggleColorScheme(event: MouseEvent) {
</script>

<template>
<span class="flex-center cursor-pointer px-2 py-1" @click="toggleColorScheme">
<SvgIcon :name="settingsStore.settings.app.colorScheme === 'light' ? 'i-ri:sun-line' : 'i-ri:moon-line'" />
</span>
<HDropdown class="flex-center cursor-pointer px-2 py-1">
<SvgIcon
:name="{
'': 'i-ri:computer-line',
'light': 'i-ri:sun-line',
'dark': 'i-ri:moon-line',
}[settingsStore.settings.app.colorScheme]" @click="toggleColorScheme"
/>
<template #dropdown>
<HTabList
v-model="settingsStore.settings.app.colorScheme"
:options="[
{ icon: 'i-ri:sun-line', label: '', value: 'light' },
{ icon: 'i-ri:moon-line', label: '', value: 'dark' },
{ icon: 'i-ri:computer-line', label: '', value: '' },
]"
class="m-3"
/>
</template>
</HDropdown>
</template>
6 changes: 4 additions & 2 deletions src/views/ui-kit/HTabList.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Tab, TabGroup, TabList } from '@headlessui/vue'
const props = defineProps<{
options: {
icon?: string
label: any
value: T
}[]
Expand Down Expand Up @@ -37,11 +38,12 @@ function handleChange(index: number) {
<TabList class="inline-flex select-none items-center justify-center rounded-md bg-stone-1 p-1 ring-1 ring-stone-2 dark:bg-stone-9 dark:ring-stone-8">
<Tab v-for="(option, index) in options" :key="index" v-slot="{ selected }" as="template">
<button
class="w-full inline-flex items-center justify-center truncate border-size-0 rounded-md bg-inherit px-2 py-1.5 text-sm text-dark ring-stone-2 ring-inset dark:text-white focus:outline-none focus:ring-2 dark:ring-stone-8" :class="{
class="w-full inline-flex items-center justify-center gap-1 break-keep border-size-0 rounded-md bg-inherit px-2 py-1.5 text-sm text-dark ring-stone-2 ring-inset dark:text-white focus:outline-none focus:ring-2 dark:ring-stone-8" :class="{
'cursor-default bg-white dark:bg-dark-9': selected,
'cursor-pointer opacity-50 transition hover:(opacity-100)': !selected,
'cursor-pointer opacity-50 hover:(opacity-100)': !selected,
}"
>
<SvgIcon v-if="option.icon" :name="option.icon" class="flex-shrink-0" />
{{ option.label }}
</button>
</Tab>
Expand Down
6 changes: 4 additions & 2 deletions src/views/ui-kit/HTooltip.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
withDefaults(
defineProps<{
text: string
enable: boolean
enable?: boolean
}>(),
{
text: '',
Expand All @@ -20,5 +20,7 @@ withDefaults(
</slot>
</template>
</VTooltip>
<slot v-else />
<div v-else>
<slot />
</div>
</template>

0 comments on commit 98792c0

Please sign in to comment.