update-css-variables.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import type { Preferences } from './types';
  2. import {
  3. updateCSSVariables as executeUpdateCSSVariables,
  4. generatorColorVariables,
  5. } from '@vben-core/shared';
  6. import { BUILT_IN_THEME_PRESETS } from './constants';
  7. /**
  8. * 更新主题的 CSS 变量以及其他 CSS 变量
  9. * @param preferences - 当前偏好设置对象,它的主题值将被用来设置文档的主题。
  10. */
  11. function updateCSSVariables(preferences: Preferences) {
  12. // 当修改到颜色变量时,更新 css 变量
  13. const root = document.documentElement;
  14. if (!root) {
  15. return;
  16. }
  17. const theme = preferences?.theme ?? {};
  18. const { builtinType, colorPrimary, mode, radius } = theme;
  19. // html 设置 dark 类
  20. if (Reflect.has(theme, 'mode')) {
  21. const dark = isDarkTheme(mode);
  22. root.classList.toggle('dark', dark);
  23. }
  24. // html 设置 data-theme=[builtinType]
  25. if (Reflect.has(theme, 'builtinType')) {
  26. const rootTheme = root.dataset.theme;
  27. if (rootTheme !== builtinType) {
  28. root.dataset.theme = builtinType;
  29. }
  30. }
  31. // 获取当前的内置主题
  32. const currentBuiltType = [...BUILT_IN_THEME_PRESETS].find(
  33. (item) => item.type === builtinType,
  34. );
  35. let builtinTypeColorPrimary: string | undefined = '';
  36. if (currentBuiltType) {
  37. const isDark = isDarkTheme(preferences.theme.mode);
  38. // 设置不同主题的主要颜色
  39. const color = isDark
  40. ? currentBuiltType.darkPrimaryColor || currentBuiltType.primaryColor
  41. : currentBuiltType.primaryColor;
  42. builtinTypeColorPrimary = color || currentBuiltType.color;
  43. }
  44. // 如果内置主题颜色和自定义颜色都不存在,则不更新主题颜色
  45. if (
  46. builtinTypeColorPrimary ||
  47. Reflect.has(theme, 'colorPrimary') ||
  48. Reflect.has(theme, 'colorDestructive') ||
  49. Reflect.has(theme, 'colorSuccess') ||
  50. Reflect.has(theme, 'colorWarning')
  51. ) {
  52. preferences.theme.colorPrimary = builtinTypeColorPrimary || colorPrimary;
  53. updateMainColorVariables(preferences);
  54. }
  55. // 更新圆角
  56. if (Reflect.has(theme, 'radius')) {
  57. document.documentElement.style.setProperty('--radius', `${radius}rem`);
  58. }
  59. }
  60. /**
  61. * 更新主要的 CSS 变量
  62. * @param preference - 当前偏好设置对象,它的颜色值将被转换成 HSL 格式并设置为 CSS 变量。
  63. */
  64. function updateMainColorVariables(preference: Preferences) {
  65. if (!preference.theme) {
  66. return;
  67. }
  68. const { colorDestructive, colorPrimary, colorSuccess, colorWarning } =
  69. preference.theme;
  70. const colorVariables = generatorColorVariables([
  71. { color: colorPrimary, name: 'primary' },
  72. { alias: 'warning', color: colorWarning, name: 'yellow' },
  73. { alias: 'success', color: colorSuccess, name: 'green' },
  74. { alias: 'destructive', color: colorDestructive, name: 'red' },
  75. ]);
  76. if (colorPrimary) {
  77. document.documentElement.style.setProperty(
  78. '--primary',
  79. colorVariables['--primary-500'],
  80. );
  81. }
  82. if (colorVariables['--green-500']) {
  83. colorVariables['--success'] = colorVariables['--green-500'];
  84. }
  85. if (colorVariables['--yellow-500']) {
  86. colorVariables['--warning'] = colorVariables['--yellow-500'];
  87. }
  88. if (colorVariables['--red-500']) {
  89. colorVariables['--destructive'] = colorVariables['--red-500'];
  90. }
  91. executeUpdateCSSVariables(colorVariables);
  92. }
  93. function isDarkTheme(theme: string) {
  94. let dark = theme === 'dark';
  95. if (theme === 'auto') {
  96. dark = window.matchMedia('(prefers-color-scheme: dark)').matches;
  97. }
  98. return dark;
  99. }
  100. export { isDarkTheme, updateCSSVariables };