use-design-tokens.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import { computed, ref, watch } from 'vue';
  2. import { preferences } from '@vben/preferences';
  3. export function useDesignTokens() {
  4. const rootStyles = getComputedStyle(document.documentElement);
  5. const colorPrimary = ref('');
  6. const colorError = ref('');
  7. const colorSuccess = ref('');
  8. const colorWarning = ref('');
  9. const colorInfo = ref('');
  10. const colorBgBase = ref('');
  11. const colorTextBase = ref('');
  12. const colorBgContainer = ref('');
  13. const colorBgElevated = ref('');
  14. const colorBgLayout = ref('');
  15. const colorBgMask = ref('');
  16. const colorBorder = ref('');
  17. const borderRadius = ref<any>('');
  18. const getCssVariableValue = (variable: string, isColor: boolean = true) => {
  19. const value = rootStyles.getPropertyValue(variable);
  20. return isColor ? `hsl(${value})` : value;
  21. };
  22. watch(
  23. () => preferences.theme,
  24. () => {
  25. colorInfo.value = colorPrimary.value = getCssVariableValue('--primary');
  26. colorError.value = getCssVariableValue('--destructive');
  27. colorWarning.value = getCssVariableValue('--warning');
  28. colorSuccess.value = getCssVariableValue('--success');
  29. colorBgBase.value = getCssVariableValue('--background');
  30. colorBgLayout.value = getCssVariableValue('--background-deep');
  31. colorBgMask.value = getCssVariableValue('--overlay');
  32. colorBorder.value = getCssVariableValue('--border');
  33. colorTextBase.value = getCssVariableValue('--foreground');
  34. colorBgElevated.value = getCssVariableValue('--popover');
  35. colorBgContainer.value = getCssVariableValue('--card');
  36. borderRadius.value = getCssVariableValue('--radius', false);
  37. },
  38. { immediate: true },
  39. );
  40. const antDesignTokens = computed(() => {
  41. return {
  42. borderRadius: borderRadius.value,
  43. colorBgBase: colorBgBase.value,
  44. colorBgContainer: colorBgContainer.value,
  45. colorBgElevated: colorBgElevated.value,
  46. colorBgLayout: colorBgLayout.value,
  47. colorBgMask: colorBgMask.value,
  48. colorBorder: colorBorder.value,
  49. colorError: colorError.value,
  50. colorInfo: colorInfo.value,
  51. colorPrimary: colorPrimary.value,
  52. colorSuccess: colorSuccess.value,
  53. colorTextBase: colorTextBase.value,
  54. colorWarning: colorWarning.value,
  55. };
  56. });
  57. return {
  58. antDesignTokens,
  59. borderRadius,
  60. colorBgBase,
  61. colorBgContainer,
  62. colorBgElevated,
  63. colorBorder,
  64. colorError,
  65. colorInfo,
  66. colorPrimary,
  67. colorSuccess,
  68. colorTextBase,
  69. colorWarning,
  70. };
  71. }