refactor: 重构暗黑模式

This commit is contained in:
张传龙 2023-01-30 15:37:10 +08:00
parent c3dc0b4b2c
commit c58605de54
9 changed files with 34 additions and 31 deletions

View File

@ -1,5 +1,11 @@
<template> <template>
<n-config-provider wh-full :locale="zhCN" :date-locale="dateZhCN" :theme-overrides="naiveThemeOverrides"> <n-config-provider
wh-full
:locale="zhCN"
:date-locale="dateZhCN"
:theme="appStore.isDark ? darkTheme : undefined"
:theme-overrides="naiveThemeOverrides"
>
<n-loading-bar-provider> <n-loading-bar-provider>
<n-dialog-provider> <n-dialog-provider>
<n-notification-provider> <n-notification-provider>
@ -15,7 +21,7 @@
<script setup> <script setup>
import { defineComponent, h } from 'vue' import { defineComponent, h } from 'vue'
import { zhCN, dateZhCN, useLoadingBar, useDialog, useMessage, useNotification } from 'naive-ui' import { zhCN, dateZhCN, darkTheme, useLoadingBar, useDialog, useMessage, useNotification } from 'naive-ui'
import { useCssVar } from '@vueuse/core' import { useCssVar } from '@vueuse/core'
import { kebabCase } from 'lodash-es' import { kebabCase } from 'lodash-es'
import { setupMessage, setupDialog } from '@/utils' import { setupMessage, setupDialog } from '@/utils'
@ -50,12 +56,4 @@ const NaiveProviderContent = defineComponent({
return h('div') return h('div')
}, },
}) })
watch(
() => appStore.darkMode,
(v) => {
if (v) document.documentElement.classList.add('dark')
else document.documentElement.classList.remove('dark')
}
)
</script> </script>

View File

@ -1,6 +1,6 @@
<template> <template>
<transition name="fade-slide" mode="out-in" appear> <transition name="fade-slide" mode="out-in" appear>
<section class="cus-scroll-y wh-full p-15 flex-col bg-[#f5f6fb]"> <section class="cus-scroll-y wh-full p-15 flex-col bg-[#f5f6fb] dark:bg-hex-121212">
<slot /> <slot />
<AppFooter v-if="showFooter" mt-15 /> <AppFooter v-if="showFooter" mt-15 />
<n-back-top :bottom="20" /> <n-back-top :bottom="20" />

View File

@ -1,12 +1,18 @@
<script setup> <script setup>
import { useAppStore } from '@/store' import { useAppStore } from '@/store'
import { useDark, useToggle } from '@vueuse/core'
const appStore = useAppStore() const appStore = useAppStore()
const isDark = useDark()
const toggleDark = () => {
appStore.toggleDark()
useToggle(isDark)()
}
</script> </script>
<template> <template>
<n-icon mr-20 cursor-pointer size="18" @click="appStore.toggleDarkMode"> <n-icon mr-20 cursor-pointer size="18" @click="toggleDark">
<icon-mdi-moon-waning-crescent v-if="appStore.darkMode" /> <icon-mdi-moon-waning-crescent v-if="isDark" />
<icon-mdi-white-balance-sunny v-else /> <icon-mdi-white-balance-sunny v-else />
</n-icon> </n-icon>
</template> </template>

View File

@ -1,7 +1,7 @@
<template> <template>
<div flex items-center> <div flex items-center>
<MenuCollapse /> <MenuCollapse />
<BreadCrumb ml-15 /> <BreadCrumb ml-15 hidden sm:block />
</div> </div>
<div ml-auto flex items-center> <div ml-auto flex items-center>
<ThemeMode /> <ThemeMode />

View File

@ -1,5 +1,5 @@
<template> <template>
<ScrollX ref="scrollXRef"> <ScrollX ref="scrollXRef" class="bg-white dark:bg-dark!">
<n-tag <n-tag
v-for="tag in tagsStore.tags" v-for="tag in tagsStore.tags"
ref="tabRefs" ref="tabRefs"

View File

@ -12,13 +12,17 @@
</n-layout-sider> </n-layout-sider>
<article flex-1 flex-col overflow-hidden> <article flex-1 flex-col overflow-hidden>
<header bg-white px-15 border-b bc-eee flex items-center :style="`height: ${header.height}px`"> <header
class="bg-white px-15 border-b bc-eee flex items-center"
dark="bg-dark border-0"
:style="`height: ${header.height}px`"
>
<AppHeader /> <AppHeader />
</header> </header>
<section v-if="tags.visible" border-b bc-eee> <section v-if="tags.visible" border-b bc-eee dark:border-0 hidden sm:block>
<AppTags :style="{ height: `${tags.height}px` }" /> <AppTags :style="{ height: `${tags.height}px` }" />
</section> </section>
<section flex-1 overflow-hidden> <section flex-1 overflow-hidden bg-hex-f5f6fb dark:bg-hex-101014>
<AppMain /> <AppMain />
</section> </section>
</article> </article>

View File

@ -1,5 +1,7 @@
import { defineStore } from 'pinia' import { defineStore } from 'pinia'
import { useDark } from '@vueuse/core'
const isDark = useDark()
export const useAppStore = defineStore('app', { export const useAppStore = defineStore('app', {
state() { state() {
return { return {
@ -7,7 +9,7 @@ export const useAppStore = defineStore('app', {
collapsed: false, collapsed: false,
/** keepAlive路由的key重新赋值可重置keepAlive */ /** keepAlive路由的key重新赋值可重置keepAlive */
aliveKeys: {}, aliveKeys: {},
darkMode: false, isDark,
} }
}, },
actions: { actions: {
@ -32,12 +34,12 @@ export const useAppStore = defineStore('app', {
this.aliveKeys[key] = val this.aliveKeys[key] = val
}, },
/** 设置暗黑模式 */ /** 设置暗黑模式 */
setDarkMode(darkMode) { setDark(isDark) {
this.darkMode = darkMode this.isDark = isDark
}, },
/** 切换/关闭 暗黑模式 */ /** 切换/关闭 暗黑模式 */
toggleDarkMode() { toggleDark() {
this.darkMode = !this.darkMode this.isDark = !this.isDark
}, },
}, },
}) })

View File

@ -7,14 +7,6 @@ body {
html { html {
font-size: 4px; // * 1rem = 4px 方便unocss计算在unocss中 1字体单位 = 0.25rem相当于 1等份 = 1px font-size: 4px; // * 1rem = 4px 方便unocss计算在unocss中 1字体单位 = 0.25rem相当于 1等份 = 1px
&.dark {
filter: invert(1);
img,
video {
filter: invert(1);
}
}
} }
body { body {

View File

@ -40,6 +40,7 @@ export default defineConfig({
error_hover: 'var(--error-color-hover)', error_hover: 'var(--error-color-hover)',
error_pressed: 'var(--error-color-pressed)', error_pressed: 'var(--error-color-pressed)',
error_active: 'var(--error-color-active)', error_active: 'var(--error-color-active)',
dark: '#18181c',
}, },
}, },
}) })