Vben
authored
4 years ago
1
import { colorIsDark, lighten, darken } from '/@/utils/color';
Vben
authored
4 years ago
2
import { useAppStore } from '/@/store/modules/app';
vben
authored
4 years ago
3
4
5
6
7
8
9
10
11
import { ThemeEnum } from '/@/enums/appEnum';
import { setCssVar } from './util';
const HEADER_BG_COLOR_VAR = '--header-bg-color';
const HEADER_BG_HOVER_COLOR_VAR = '--header-bg-hover-color';
const HEADER_MENU_ACTIVE_BG_COLOR_VAR = '--header-active-menu-bg-color';
const SIDER_DARK_BG_COLOR = '--sider-dark-bg-color';
const SIDER_DARK_DARKEN_BG_COLOR = '--sider-dark-darken-bg-color';
Vben
authored
4 years ago
12
const SIDER_LIGHTEN_BG_COLOR = '--sider-dark-lighten-bg-color';
vben
authored
4 years ago
13
14
15
16
17
/**
* Change the background color of the top header
* @param color
*/
Vben
authored
4 years ago
18
export function updateHeaderBgColor(color?: string) {
Vben
authored
4 years ago
19
const appStore = useAppStore();
Vben
authored
4 years ago
20
21
22
23
24
const darkMode = appStore.getDarkMode === ThemeEnum.DARK;
if (!color) {
if (darkMode) {
color = '#151515';
} else {
Vben
authored
4 years ago
25
color = appStore.getHeaderSetting.bgColor;
Vben
authored
4 years ago
26
27
}
}
vben
authored
4 years ago
28
29
30
31
// bg color
setCssVar(HEADER_BG_COLOR_VAR, color);
// hover color
Vben
authored
4 years ago
32
const hoverColor = lighten(color!, 6);
vben
authored
4 years ago
33
34
35
36
setCssVar(HEADER_BG_HOVER_COLOR_VAR, hoverColor);
setCssVar(HEADER_MENU_ACTIVE_BG_COLOR_VAR, hoverColor);
// Determine the depth of the color value and automatically switch the theme
Vben
authored
4 years ago
37
const isDark = colorIsDark(color!);
vben
authored
4 years ago
38
Vben
authored
4 years ago
39
appStore.setProjectConfig({
vben
authored
4 years ago
40
headerSetting: {
Vben
authored
4 years ago
41
theme: isDark || darkMode ? ThemeEnum.DARK : ThemeEnum.LIGHT,
vben
authored
4 years ago
42
43
44
45
46
47
48
49
},
});
}
/**
* Change the background color of the left menu
* @param color bg color
*/
Vben
authored
4 years ago
50
export function updateSidebarBgColor(color?: string) {
Vben
authored
4 years ago
51
52
const appStore = useAppStore();
Vben
authored
4 years ago
53
54
55
56
57
58
// if (!isHexColor(color)) return;
const darkMode = appStore.getDarkMode === ThemeEnum.DARK;
if (!color) {
if (darkMode) {
color = '#212121';
} else {
Vben
authored
4 years ago
59
color = appStore.getMenuSetting.bgColor;
Vben
authored
4 years ago
60
61
}
}
vben
authored
4 years ago
62
setCssVar(SIDER_DARK_BG_COLOR, color);
Vben
authored
4 years ago
63
64
setCssVar(SIDER_DARK_DARKEN_BG_COLOR, darken(color!, 6));
setCssVar(SIDER_LIGHTEN_BG_COLOR, lighten(color!, 5));
vben
authored
4 years ago
65
66
67
// only #ffffff is light
// Only when the background color is #fff, the theme of the menu will be changed to light
Vben
authored
4 years ago
68
const isLight = ['#fff', '#ffffff'].includes(color!.toLowerCase());
vben
authored
4 years ago
69
Vben
authored
4 years ago
70
appStore.setProjectConfig({
vben
authored
4 years ago
71
menuSetting: {
Vben
authored
4 years ago
72
theme: isLight && !darkMode ? ThemeEnum.LIGHT : ThemeEnum.DARK,
vben
authored
4 years ago
73
74
75
},
});
}