Blame view

src/logics/theme/updateBackground.ts 2.23 KB
Vben authored
1
import { colorIsDark, lighten, darken } from '/@/utils/color';
Vben authored
2
import { useAppStore } from '/@/store/modules/app';
vben authored
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
12
const SIDER_LIGHTEN_BG_COLOR = '--sider-dark-lighten-bg-color';
vben authored
13
14
15
16
17

/**
 * Change the background color of the top header
 * @param color
 */
Vben authored
18
export function updateHeaderBgColor(color?: string) {
Vben authored
19
  const appStore = useAppStore();
Vben authored
20
21
22
23
24
  const darkMode = appStore.getDarkMode === ThemeEnum.DARK;
  if (!color) {
    if (darkMode) {
      color = '#151515';
    } else {
Vben authored
25
      color = appStore.getHeaderSetting.bgColor;
Vben authored
26
27
    }
  }
vben authored
28
29
30
31
  // bg color
  setCssVar(HEADER_BG_COLOR_VAR, color);

  // hover color
Vben authored
32
  const hoverColor = lighten(color!, 6);
vben authored
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
37
  const isDark = colorIsDark(color!);
vben authored
38
Vben authored
39
  appStore.setProjectConfig({
vben authored
40
    headerSetting: {
Vben authored
41
      theme: isDark || darkMode ? ThemeEnum.DARK : ThemeEnum.LIGHT,
vben authored
42
43
44
45
46
47
48
49
    },
  });
}

/**
 * Change the background color of the left menu
 * @param color  bg color
 */
Vben authored
50
export function updateSidebarBgColor(color?: string) {
Vben authored
51
52
  const appStore = useAppStore();
Vben authored
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
59
      color = appStore.getMenuSetting.bgColor;
Vben authored
60
61
    }
  }
vben authored
62
  setCssVar(SIDER_DARK_BG_COLOR, color);
Vben authored
63
64
  setCssVar(SIDER_DARK_DARKEN_BG_COLOR, darken(color!, 6));
  setCssVar(SIDER_LIGHTEN_BG_COLOR, lighten(color!, 5));
vben authored
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
68
  const isLight = ['#fff', '#ffffff'].includes(color!.toLowerCase());
vben authored
69
Vben authored
70
  appStore.setProjectConfig({
vben authored
71
    menuSetting: {
Vben authored
72
      theme: isLight && !darkMode ? ThemeEnum.LIGHT : ThemeEnum.DARK,
vben authored
73
74
75
    },
  });
}