cl-col.uvue 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <template>
  2. <view
  3. class="cl-col"
  4. :class="[
  5. `cl-col-${span}`,
  6. `cl-col-offset-${offset}`,
  7. `cl-col-push-${push}`,
  8. `cl-col-pull-${pull}`,
  9. pt.className
  10. ]"
  11. :style="{
  12. paddingLeft: padding,
  13. paddingRight: padding
  14. }"
  15. >
  16. <slot></slot>
  17. </view>
  18. </template>
  19. <script setup lang="ts">
  20. import { computed } from "vue";
  21. import { parsePt, parseRpx, useParent } from "@/cool";
  22. defineOptions({
  23. name: "cl-col"
  24. });
  25. // 组件属性定义
  26. const props = defineProps({
  27. // 透传样式配置
  28. pt: {
  29. type: Object,
  30. default: () => ({})
  31. },
  32. // 栅格占据的列数
  33. span: {
  34. type: Number,
  35. default: 24
  36. },
  37. // 栅格左侧的间隔格数
  38. offset: {
  39. type: Number,
  40. default: 0
  41. },
  42. // 栅格向右移动格数
  43. push: {
  44. type: Number,
  45. default: 0
  46. },
  47. // 栅格向左移动格数
  48. pull: {
  49. type: Number,
  50. default: 0
  51. }
  52. });
  53. // 获取父组件实例
  54. const parent = useParent<ClRowComponentPublicInstance>("cl-row");
  55. // 透传类型定义
  56. type PassThrough = {
  57. // 自定义类名
  58. className?: string;
  59. };
  60. // 解析透传配置
  61. const pt = computed(() => parsePt<PassThrough>(props.pt));
  62. // 计算列的padding,用于实现栅格间隔
  63. const padding = computed(() => (parent == null ? "0" : parseRpx(parent.gutter / 2)));
  64. </script>
  65. <style lang="scss" scoped>
  66. @use "sass:math";
  67. .cl-col {
  68. @apply w-full;
  69. overflow: visible;
  70. }
  71. @for $i from 1 through 24 {
  72. .cl-col-push-#{$i},
  73. .cl-col-pull-#{$i} {
  74. position: relative;
  75. }
  76. }
  77. @for $i from 1 through 24 {
  78. $w: math.div(100%, 24);
  79. .cl-col-#{$i} {
  80. width: $w * $i;
  81. }
  82. .cl-col-offset-#{$i} {
  83. margin-left: $w * $i;
  84. }
  85. .cl-col-pull-#{$i} {
  86. right: $w * $i;
  87. }
  88. .cl-col-push-#{$i} {
  89. left: $w * $i;
  90. }
  91. }
  92. </style>