Blame view

src/logics/theme/index.ts 2.67 KB
1
2
import { isHexColor, colorIsDark, lighten, darken } from '/@/utils/color';
import { appStore } from '/@/store/modules/app';
vben authored
3
import { ThemeEnum } from '/@/enums/appEnum';
4
5
6
7
8
9
10
11
12
13

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';
const SIDER_LIGHTEN_1_BG_COLOR = '--sider-dark-lighten-1-bg-color';
const SIDER_LIGHTEN_2_BG_COLOR = '--sider-dark-lighten-2-bg-color';
14
15
16
17
export function setCssVar(prop: string, val: any, dom = document.documentElement) {
  dom.style.setProperty(prop, val);
}
18
19
20
function toggleClass(flag: boolean, clsName: string, target?: HTMLElement) {
  const targetEl = target || document.body;
  let { className } = targetEl;
陈文彬 authored
21
  className = className.replace(clsName, '');
22
  targetEl.className = flag ? `${className} ${clsName} ` : className;
陈文彬 authored
23
}
24
vben authored
25
26
/**
 * Change the status of the project's color weakness mode
vben authored
27
 * @param colorWeak
vben authored
28
 */
陈文彬 authored
29
export const updateColorWeak = (colorWeak: boolean) => {
30
  toggleClass(colorWeak, 'color-weak', document.documentElement);
陈文彬 authored
31
32
};
vben authored
33
34
35
36
/**
 * Change project gray mode status
 * @param gray
 */
陈文彬 authored
37
export const updateGrayMode = (gray: boolean) => {
38
  toggleClass(gray, 'gray-mode', document.documentElement);
陈文彬 authored
39
};
40
vben authored
41
42
43
44
/**
 * Change the background color of the top header
 * @param color
 */
45
46
47
export function updateHeaderBgColor(color: string) {
  if (!isHexColor(color)) return;
  // bg color
vben authored
48
49
  setCssVar(HEADER_BG_COLOR_VAR, color);
50
51
  // hover color
  const hoverColor = lighten(color, 6);
vben authored
52
53
  setCssVar(HEADER_BG_HOVER_COLOR_VAR, hoverColor);
  setCssVar(HEADER_MENU_ACTIVE_BG_COLOR_VAR, hoverColor);
54
vben authored
55
  // Determine the depth of the color value and automatically switch the theme
56
57
58
59
  const isDark = colorIsDark(color);

  appStore.commitProjectConfigState({
    headerSetting: {
vben authored
60
      theme: isDark ? ThemeEnum.DARK : ThemeEnum.LIGHT,
61
62
63
64
    },
  });
}
vben authored
65
66
67
68
/**
 * Change the background color of the left menu
 * @param color  bg color
 */
69
70
71
export function updateSidebarBgColor(color: string) {
  if (!isHexColor(color)) return;
vben authored
72
73
74
75
  setCssVar(SIDER_DARK_BG_COLOR, color);
  setCssVar(SIDER_DARK_DARKEN_BG_COLOR, darken(color, 6));
  setCssVar(SIDER_LIGHTEN_1_BG_COLOR, lighten(color, 4));
  setCssVar(SIDER_LIGHTEN_2_BG_COLOR, lighten(color, 8));
76
77

  // only #ffffff is light
vben authored
78
  // Only when the background color is #fff, the theme of the menu will be changed to light
79
80
81
82
  const isLight = ['#fff', '#ffffff'].includes(color.toLowerCase());

  appStore.commitProjectConfigState({
    menuSetting: {
vben authored
83
      theme: isLight ? ThemeEnum.LIGHT : ThemeEnum.DARK,
84
85
86
    },
  });
}