BaseView.ts 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import { computed } from "vue";
  2. import { useTheme } from "../theme/ThemeDefine";
  3. import type { FlexProps } from "./FlexView.vue";
  4. import { configMargin, configPadding } from "../theme/ThemeTools";
  5. export function useBaseViewStyleBuilder(props: FlexProps) {
  6. const themeContext = useTheme();
  7. const commonStyle = computed(() => {
  8. const obj : Record<string, any> = {
  9. display: 'flex',
  10. flexDirection: props.direction,
  11. flexBasis: props.flexBasis,
  12. flexGrow: props.flexGrow,
  13. flexShrink: props.flexShrink,
  14. justifyContent: props.center ? (props.justify || 'center') : props.justify,
  15. alignItems: props.center ? (props.align || 'center') : props.align,
  16. position: props.position,
  17. alignSelf: props.alignSelf,
  18. flexWrap: props.wrap ? 'wrap' : 'nowrap',
  19. backgroundColor: themeContext.resolveThemeColor(props.backgroundColor),
  20. width: themeContext.resolveThemeSize(props.width),
  21. height: themeContext.resolveThemeSize(props.height),
  22. gap: themeContext.resolveThemeSize(props.gap),
  23. borderRadius: themeContext.resolveThemeSize(props.radius),
  24. overflow: props.overflow,
  25. border: props.border && !props.border.includes(' ') ? themeContext.getVar('border.' + props.border, undefined) : props.border,
  26. borderColor: themeContext.resolveThemeColor(props.borderColor),
  27. borderWidth: themeContext.resolveThemeSize(props.borderWidth),
  28. borderStyle: props.borderStyle,
  29. boxShadow: props.shadow ? themeContext.getVar('shadow.' + props.shadow, undefined) : undefined,
  30. zIndex: props.zIndex,
  31. ...(props.innerStyle ? props.innerStyle : {}),
  32. }
  33. //内边距样式
  34. configPadding(obj, themeContext.theme.value, props.padding as any);
  35. //外边距样式
  36. configMargin(obj, themeContext.theme.value, props.margin as any);
  37. if (obj.paddingVertical) {
  38. if (obj.paddingTop === undefined)
  39. obj.paddingTop = obj.paddingVertical;
  40. if (obj.paddingBottom === undefined)
  41. obj.paddingBottom = obj.paddingVertical;
  42. obj.paddingVertical = undefined;
  43. }
  44. if (obj.paddingHorizontal) {
  45. if (obj.paddingLeft === undefined)
  46. obj.paddingLeft = obj.paddingHorizontal;
  47. if (obj.paddingRight === undefined)
  48. obj.paddingRight = obj.paddingHorizontal;
  49. obj.paddingHorizontal = undefined;
  50. }
  51. if (obj.marginVertical) {
  52. obj.marginTop = obj.marginVertical;
  53. obj.marginBottom = obj.marginVertical;
  54. obj.marginVertical = undefined;
  55. }
  56. if (obj.marginHorizontal) {
  57. obj.marginLeft = obj.marginHorizontal;
  58. obj.marginRight = obj.marginHorizontal;
  59. obj.marginHorizontal = undefined;
  60. }
  61. //绝对距样式
  62. if (typeof props.left !== 'undefined')
  63. obj.left = themeContext.resolveThemeSize(props.left);
  64. if (typeof props.right !== 'undefined')
  65. obj.right = themeContext.resolveThemeSize(props.right);
  66. if (typeof props.top !== 'undefined')
  67. obj.top = themeContext.resolveThemeSize(props.top);
  68. if (typeof props.bottom !== 'undefined')
  69. obj.bottom = themeContext.resolveThemeSize(props.bottom);
  70. if (typeof props.flex !== 'undefined')
  71. obj.flex = props.flex;
  72. return obj
  73. });
  74. return {
  75. commonStyle
  76. }
  77. }