diff --git a/src/AppProvider.vue b/src/AppProvider.vue index 20797740..e8a07869 100644 --- a/src/AppProvider.vue +++ b/src/AppProvider.vue @@ -3,7 +3,7 @@ class="h-full" :locale="zhCN" :date-locale="dateZhCN" - :theme="dark" + :theme="naiveTheme" :theme-overrides="theme.themeOverrids" > @@ -22,7 +22,6 @@ diff --git a/src/composables/common/index.ts b/src/composables/common/index.ts index 326c800c..f98f2473 100644 --- a/src/composables/common/index.ts +++ b/src/composables/common/index.ts @@ -2,3 +2,4 @@ export * from './route'; export * from './router'; export * from './system'; export * from './layout'; +export * from './theme'; diff --git a/src/composables/common/theme.ts b/src/composables/common/theme.ts new file mode 100644 index 00000000..46c2cb21 --- /dev/null +++ b/src/composables/common/theme.ts @@ -0,0 +1,29 @@ +import { computed, watch } from 'vue'; +import { darkTheme } from 'naive-ui'; +import { useDark } from '@vueuse/core'; +import { useThemeStore } from '@/store'; + +/** 系统暗黑模式 */ +export function useDarkMode() { + const osDark = useDark(); + const theme = useThemeStore(); + const { handleDarkMode } = useThemeStore(); + + /** naive-ui暗黑主题 */ + const naiveTheme = computed(() => (theme.darkMode ? darkTheme : undefined)); + + // 监听操作系统主题模式 + watch( + osDark, + newValue => { + handleDarkMode(newValue); + }, + { + immediate: true + } + ); + + return { + naiveTheme + }; +}