diff --git a/src/libs/Button/index.vue b/src/libs/Button/index.vue
index 8b894d1..adc4b15 100644
--- a/src/libs/Button/index.vue
+++ b/src/libs/Button/index.vue
@@ -37,9 +37,10 @@ import { computed } from 'vue';
// size 大小 区分文字按钮和icon按钮
const typeEnum = {
- primary: 'text-white bg-zinc-800 hover:bg-zinc-900 active:bg-zinc-800',
- main: 'text-white bg-main hover:bg-hover-main active:bg-hover-main',
- info: 'text-zinc-800 bg-zinc-200 hover:bg-zinc-300 active:bg-zinc-200',
+ primary:
+ 'text-white bg-zinc-800 dark:bg-zinc-900 hover:bg-zinc-900 dark:hover:bg-zinc-700 active:bg-zinc-800 dark:active:bg-zinc-700',
+ main: 'text-white bg-main dark:bg-zinc-900 hover:bg-hover-main dark:hover:bg-zinc-700 active:bg-hover-main dark:active:bg-zinc-700',
+ info: 'text-zinc-800 dark:text-zinc-300 bg-zinc-200 dark:bg-zinc-700 hover:bg-zinc-300 dark:hover:bg-zinc-600 active:bg-zinc-200 dark:active:bg-zinc-700',
};
const sizeEnum = {
// 文字按钮
@@ -88,7 +89,6 @@ const props = defineProps({
type: String,
default: 'main',
validator(val) {
- console.log('[ val ]', val);
const keys = Object.keys(typeEnum);
const result = keys.includes(val);
if (!result) {
diff --git a/src/libs/Popover/index.vue b/src/libs/Popover/index.vue
index e449e9a..f406d75 100644
--- a/src/libs/Popover/index.vue
+++ b/src/libs/Popover/index.vue
@@ -10,7 +10,7 @@
diff --git a/src/libs/Popup/index.vue b/src/libs/Popup/index.vue
index b6411c9..06fcbf1 100644
--- a/src/libs/Popup/index.vue
+++ b/src/libs/Popup/index.vue
@@ -15,7 +15,7 @@
diff --git a/src/libs/Search/index.vue b/src/libs/Search/index.vue
index abbe412..2c75f10 100644
--- a/src/libs/Search/index.vue
+++ b/src/libs/Search/index.vue
@@ -1,6 +1,6 @@
diff --git a/src/main.js b/src/main.js
index c23669e..b8b7139 100644
--- a/src/main.js
+++ b/src/main.js
@@ -2,13 +2,15 @@ import { createApp } from 'vue';
import App from './App.vue';
import router from './router';
import store from './store';
-
import { useRem } from '@/utils/flexible';
+import useTheme from '@/utils/theme';
+
import libs from './libs';
import './styles/index.scss';
// 导入注册svg
import 'virtual:svg-icons-register';
useRem();
+useTheme();
createApp(App).use(router).use(store).use(libs).mount('#app');
diff --git a/src/store/getters.js b/src/store/getters.js
index 0b62b13..d0fe540 100644
--- a/src/store/getters.js
+++ b/src/store/getters.js
@@ -1,3 +1,4 @@
export default {
categorys: (state) => state.category.categorys,
+ theme: (state) => state.theme.themeType,
};
diff --git a/src/store/index.js b/src/store/index.js
index a34cfc5..6199af4 100644
--- a/src/store/index.js
+++ b/src/store/index.js
@@ -1,12 +1,15 @@
import { createStore } from 'vuex';
import getters from './getters';
import category from './modules/category';
+import theme from './modules/theme';
+
// 强制缓存
import createPersistedstate from 'vuex-persistedstate';
const store = createStore({
modules: {
category,
+ theme,
},
getters,
plugins: [
@@ -14,7 +17,7 @@ const store = createStore({
// 指定保存的 localstorage 中的key
key: 'APP_KEYS',
// 需要保存的模块
- paths: ['category'],
+ paths: ['category', 'theme'],
}),
],
});
diff --git a/src/store/modules/theme.js b/src/store/modules/theme.js
new file mode 100644
index 0000000..212e3b7
--- /dev/null
+++ b/src/store/modules/theme.js
@@ -0,0 +1,12 @@
+import { THEME_LIGHT } from '@/constants';
+export default {
+ namespaced: true,
+ state: () => ({
+ themeType: THEME_LIGHT,
+ }),
+ mutations: {
+ changeThemeType(state, newTheme) {
+ state.themeType = newTheme;
+ },
+ },
+};
diff --git a/src/utils/theme.js b/src/utils/theme.js
new file mode 100644
index 0000000..e4a8311
--- /dev/null
+++ b/src/utils/theme.js
@@ -0,0 +1,47 @@
+import { watch } from 'vue';
+import store from '../store';
+import { THEME_LIGHT, THEME_DARK, THEME_SYSTEM } from '@/constants';
+
+// 监听主题变更
+let matchMedia;
+const watchSystemThemeChange = () => {
+ // 仅需初始化一次即可
+ if (matchMedia) return;
+ matchMedia = window.matchMedia('(prefers-color-scheme: dark)');
+ // 监听主题变更
+ matchMedia.onchange = () => {
+ changeTheme(THEME_SYSTEM);
+ };
+};
+
+const changeTheme = (theme) => {
+ let themeClassName = '';
+ switch (theme) {
+ case THEME_LIGHT:
+ themeClassName = 'light';
+ break;
+ case THEME_DARK:
+ themeClassName = 'dark';
+ break;
+ case THEME_SYSTEM:
+ watchSystemThemeChange();
+ themeClassName = matchMedia.matches ? 'dark' : 'light';
+ break;
+ }
+ const html = document.querySelector('html');
+ html.classList = themeClassName;
+};
+
+// 初始化主题
+export default () => {
+ watch(
+ () => store.getters.theme,
+ (val) => {
+ changeTheme(val);
+ },
+ {
+ // 初始执行一次
+ immediate: true,
+ }
+ );
+};
diff --git a/src/views/layout/components/header/index.vue b/src/views/layout/components/header/index.vue
index 66ccd41..fc5f175 100644
--- a/src/views/layout/components/header/index.vue
+++ b/src/views/layout/components/header/index.vue
@@ -1,6 +1,6 @@
- {{ item.title }}
+ {{
+ item.title
+ }}
diff --git a/src/views/layout/components/header/theme/index.vue b/src/views/layout/components/header/theme/index.vue
index c24232b..606c0eb 100644
--- a/src/views/layout/components/header/theme/index.vue
+++ b/src/views/layout/components/header/theme/index.vue
@@ -2,9 +2,9 @@
@@ -12,14 +12,17 @@
- {{ item.name }}
+ {{
+ item.name
+ }}
@@ -27,6 +30,8 @@
diff --git a/src/views/main/components/menu/index.vue b/src/views/main/components/menu/index.vue
index c8e5d93..8ac4be5 100644
--- a/src/views/main/components/menu/index.vue
+++ b/src/views/main/components/menu/index.vue
@@ -1,11 +1,15 @@