refactor(projects): 系统暗黑主题模式抽离成composables
This commit is contained in:
parent
4e1a09c779
commit
195e5b9e57
@ -3,7 +3,7 @@
|
|||||||
class="h-full"
|
class="h-full"
|
||||||
:locale="zhCN"
|
:locale="zhCN"
|
||||||
:date-locale="dateZhCN"
|
:date-locale="dateZhCN"
|
||||||
:theme="dark"
|
:theme="naiveTheme"
|
||||||
:theme-overrides="theme.themeOverrids"
|
:theme-overrides="theme.themeOverrids"
|
||||||
>
|
>
|
||||||
<n-element class="h-full">
|
<n-element class="h-full">
|
||||||
@ -22,7 +22,6 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script lang="ts" setup>
|
<script lang="ts" setup>
|
||||||
import { computed, watch } from 'vue';
|
|
||||||
import {
|
import {
|
||||||
NConfigProvider,
|
NConfigProvider,
|
||||||
NElement,
|
NElement,
|
||||||
@ -30,30 +29,14 @@ import {
|
|||||||
NDialogProvider,
|
NDialogProvider,
|
||||||
NNotificationProvider,
|
NNotificationProvider,
|
||||||
NMessageProvider,
|
NMessageProvider,
|
||||||
darkTheme,
|
|
||||||
zhCN,
|
zhCN,
|
||||||
dateZhCN
|
dateZhCN
|
||||||
} from 'naive-ui';
|
} from 'naive-ui';
|
||||||
import { useDark } from '@vueuse/core';
|
|
||||||
import { AppProviderContent } from '@/components';
|
import { AppProviderContent } from '@/components';
|
||||||
import { useThemeStore } from '@/store';
|
import { useThemeStore } from '@/store';
|
||||||
|
import { useDarkMode } from '@/composables';
|
||||||
|
|
||||||
const osDark = useDark();
|
|
||||||
const theme = useThemeStore();
|
const theme = useThemeStore();
|
||||||
const { handleDarkMode } = useThemeStore();
|
const { naiveTheme } = useDarkMode();
|
||||||
|
|
||||||
/** 系统暗黑模式 */
|
|
||||||
const dark = computed(() => (theme.darkMode ? darkTheme : undefined));
|
|
||||||
|
|
||||||
// 操作系统的暗黑模式
|
|
||||||
watch(
|
|
||||||
osDark,
|
|
||||||
newValue => {
|
|
||||||
handleDarkMode(newValue);
|
|
||||||
},
|
|
||||||
{
|
|
||||||
immediate: true
|
|
||||||
}
|
|
||||||
);
|
|
||||||
</script>
|
</script>
|
||||||
<style></style>
|
<style></style>
|
||||||
|
@ -2,3 +2,4 @@ export * from './route';
|
|||||||
export * from './router';
|
export * from './router';
|
||||||
export * from './system';
|
export * from './system';
|
||||||
export * from './layout';
|
export * from './layout';
|
||||||
|
export * from './theme';
|
||||||
|
29
src/composables/common/theme.ts
Normal file
29
src/composables/common/theme.ts
Normal file
@ -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
|
||||||
|
};
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user