From 7f0d841130154a7399a879aff13600cd789d7076 Mon Sep 17 00:00:00 2001 From: Hooray Hu <304327508@qq.com> Date: Sat, 27 Jan 2024 10:39:51 +0800 Subject: [PATCH] =?UTF-8?q?=E6=98=8E=E6=9A=97=E6=A8=A1=E5=BC=8F=E8=AE=BE?= =?UTF-8?q?=E7=BD=AE=E4=B8=BA=E8=B7=9F=E9=9A=8F=E7=B3=BB=E7=BB=9F=E6=97=B6?= =?UTF-8?q?=EF=BC=8C=E6=94=AF=E6=8C=81=E5=8A=A8=E6=80=81=E5=88=87=E6=8D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/layouts/components/AppSetting/index.vue | 19 ++++++------- .../Topbar/Toolbar/ColorScheme/index.vue | 25 ++++++++++++++--- src/layouts/ui-kit/HTabList.vue | 6 ++-- src/layouts/ui-kit/HTooltip.vue | 6 ++-- src/store/modules/settings.ts | 28 +++++++++++++++---- 5 files changed, 61 insertions(+), 23 deletions(-) diff --git a/src/layouts/components/AppSetting/index.vue b/src/layouts/components/AppSetting/index.vue index e15cd811d..e8b189858 100755 --- a/src/layouts/components/AppSetting/index.vue +++ b/src/layouts/components/AppSetting/index.vue @@ -17,15 +17,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) @@ -97,7 +88,15 @@ function handleCopy() { 颜色主题风格
- +
导航栏模式 diff --git a/src/layouts/components/Topbar/Toolbar/ColorScheme/index.vue b/src/layouts/components/Topbar/Toolbar/ColorScheme/index.vue index 1e1b0691b..569492cf6 100644 --- a/src/layouts/components/Topbar/Toolbar/ColorScheme/index.vue +++ b/src/layouts/components/Topbar/Toolbar/ColorScheme/index.vue @@ -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 @@ -37,7 +37,24 @@ function toggleColorScheme(event: MouseEvent) { diff --git a/src/layouts/ui-kit/HTabList.vue b/src/layouts/ui-kit/HTabList.vue index 2163b0bcf..630a5ce4a 100644 --- a/src/layouts/ui-kit/HTabList.vue +++ b/src/layouts/ui-kit/HTabList.vue @@ -3,6 +3,7 @@ import { Tab, TabGroup, TabList } from '@headlessui/vue' const props = defineProps<{ options: { + icon?: string label: any value: T }[] @@ -37,11 +38,12 @@ function handleChange(index: number) { diff --git a/src/layouts/ui-kit/HTooltip.vue b/src/layouts/ui-kit/HTooltip.vue index c3546fdb2..08fdae928 100644 --- a/src/layouts/ui-kit/HTooltip.vue +++ b/src/layouts/ui-kit/HTooltip.vue @@ -2,7 +2,7 @@ withDefaults( defineProps<{ text: string - enable: boolean + enable?: boolean }>(), { text: '', @@ -20,5 +20,7 @@ withDefaults( - +
+ +
diff --git a/src/store/modules/settings.ts b/src/store/modules/settings.ts index 2adfb759a..e0e73f559 100755 --- a/src/store/modules/settings.ts +++ b/src/store/modules/settings.ts @@ -8,10 +8,28 @@ 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>() + 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 'light': document.documentElement.classList.remove('dark') @@ -20,9 +38,8 @@ const useSettingsStore = defineStore( document.documentElement.classList.add('dark') break } - }, { - immediate: true, - }) + } + watch(() => settings.value.menu.menuMode, (val) => { document.body.setAttribute('data-menu-mode', val) }, { @@ -107,6 +124,7 @@ const useSettingsStore = defineStore( return { settings, + currentColorScheme, os, title, setTitle,