updateBackground.ts 2.2 KB

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