cl-skeleton.uvue 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <template>
  2. <view
  3. class="cl-skeleton"
  4. :class="[
  5. {
  6. 'is-loading': loading,
  7. 'is-dark': isDark
  8. },
  9. `cl-skeleton--${props.type}`,
  10. `${loading ? `${pt.loading?.className}` : ''}`,
  11. pt.className
  12. ]"
  13. ref="skeletonRef"
  14. >
  15. <template v-if="loading">
  16. <cl-icon
  17. name="image-line"
  18. :pt="{
  19. className: '!text-surface-400'
  20. }"
  21. :size="40"
  22. v-if="type == 'image'"
  23. ></cl-icon>
  24. </template>
  25. <template v-else>
  26. <slot></slot>
  27. </template>
  28. </view>
  29. </template>
  30. <script setup lang="ts">
  31. import { computed, onMounted, ref, watch, type PropType } from "vue";
  32. import type { PassThroughProps } from "../../types";
  33. import { AnimationEngine, createAnimation, isDark, parsePt } from "@/cool";
  34. defineOptions({
  35. name: "cl-skeleton"
  36. });
  37. const props = defineProps({
  38. pt: {
  39. type: Object,
  40. default: () => ({})
  41. },
  42. loading: {
  43. type: Boolean,
  44. default: true
  45. },
  46. type: {
  47. type: String as PropType<"text" | "image" | "circle" | "button">,
  48. default: "text"
  49. }
  50. });
  51. type PassThrough = {
  52. className?: string;
  53. loading?: PassThroughProps;
  54. };
  55. const pt = computed(() => parsePt<PassThrough>(props.pt));
  56. // 组件引用
  57. const skeletonRef = ref<UniElement | null>(null);
  58. // 动画实例
  59. let animation: AnimationEngine | null = null;
  60. // 开始动画
  61. function start() {
  62. if (!props.loading) return;
  63. animation = createAnimation(skeletonRef.value, {
  64. duration: 2000,
  65. loop: -1,
  66. alternate: true
  67. })
  68. .opacity("0.3", "1")
  69. .play();
  70. }
  71. // 停止动画
  72. function stop() {
  73. if (animation != null) {
  74. animation!.stop();
  75. animation!.reset();
  76. }
  77. }
  78. onMounted(() => {
  79. watch(
  80. computed(() => props.loading),
  81. (val: boolean) => {
  82. if (val) {
  83. start();
  84. } else {
  85. stop();
  86. }
  87. },
  88. {
  89. immediate: true
  90. }
  91. );
  92. });
  93. </script>
  94. <style lang="scss" scoped>
  95. .cl-skeleton {
  96. &.is-loading {
  97. @apply bg-surface-100 rounded-md;
  98. &.is-dark {
  99. @apply bg-surface-600;
  100. }
  101. &.cl-skeleton--text {
  102. height: 40rpx;
  103. width: 300rpx;
  104. }
  105. &.cl-skeleton--image {
  106. @apply flex flex-row items-center justify-center;
  107. width: 120rpx;
  108. height: 120rpx;
  109. border-radius: 16rpx;
  110. }
  111. &.cl-skeleton--circle {
  112. @apply rounded-full;
  113. width: 120rpx;
  114. height: 120rpx;
  115. }
  116. &.cl-skeleton--button {
  117. @apply rounded-lg;
  118. height: 66rpx;
  119. width: 150rpx;
  120. }
  121. }
  122. }
  123. </style>