l-loading.uvue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <template>
  2. <view class="l-loading" :class="classes">
  3. <!-- #ifndef UNI-APP-X && APP -->
  4. <view class="l-loading__ball" v-if="type == 'ball'" :style="[spinnerStyle]"></view>
  5. <view class="l-loading__circular" v-if="type == 'circular'" :style="[spinnerStyle]"></view>
  6. <view class="l-loading__spinner" v-if="type == 'spinner'" :style="[spinnerStyle]">
  7. <view class="l-loading__dot" v-for="item in 12" :key="item" :style="{'--l-loading-dot': item}"></view>
  8. </view>
  9. <!-- #endif -->
  10. <!-- #ifdef UNI-APP-X && APP -->
  11. <view class="l-loading__view" ref="loadingRef" :style="spinnerStyle"></view>
  12. <!-- #endif -->
  13. <text class="l-loading__text" v-if="$slots['default'] != null || text != null" :style="textStyle">
  14. <slot>{{text}}</slot>
  15. </text>
  16. </view>
  17. </template>
  18. <script lang="uts" setup>
  19. /**
  20. * Loading 加载指示器
  21. * @description 用于表示加载中的过渡状态,支持多种动画类型和布局方式
  22. * <br> 插件类型:LLoadingComponentPublicInstance
  23. * @tutorial https://ext.dcloud.net.cn/plugin?name=lime-loading
  24. *
  25. * @property {string} color 加载图标颜色(默认:主题色)
  26. * @property {'circular' | 'spinner' | 'failed'} mode 动画实现的模式.只针对APP
  27. * @value raf 延时
  28. * @value animate 基于元素的annimate方法
  29. * @property {'circular' | 'spinner' | 'failed'} type 加载状态类型
  30. * @value circular 环形旋转动画(默认)
  31. * @value spinner 菊花转动画
  32. * @value failed 加载失败提示
  33. * @property {string} text 提示文字内容
  34. * @property {string} textColor 文字颜色(默认同color)
  35. * @property {string} textSize 文字字号(默认:14px)
  36. * @property {boolean} vertical 是否垂直排列图标和文字
  37. * @property {boolean} animated 是否启用旋转动画(failed类型自动禁用)
  38. * @property {string} size 图标尺寸(默认:'40px')
  39. */
  40. import { LoadingProps } from './type'
  41. // #ifdef APP
  42. // import {useLoading} from './useLoading'
  43. const themeVars = inject('limeConfigProviderThemeVars', computed(()=> ({})))
  44. import {useLoading} from '@/uni_modules/lime-loading'
  45. // #endif
  46. const name = 'l-loading'
  47. const props = withDefaults(defineProps<LoadingProps>(), {
  48. // #ifdef APP
  49. size: '20px',
  50. // #endif
  51. type: 'circular',
  52. mode: 'raf',
  53. animated: true,
  54. vertical: false,
  55. })
  56. const classes = computed<Map<string,any>>(():Map<string,any> => {
  57. const cls = new Map<string,any>()
  58. cls.set(name + '--' + props.type, true)
  59. if (props.vertical) {
  60. cls.set('is-vertical', props.vertical)
  61. } else {
  62. cls.set('is-horizontal', !props.vertical)
  63. }
  64. return cls
  65. })
  66. const spinnerStyle = computed<Map<string,any>>(():Map<string,any> => {
  67. const style = new Map<string,any>()
  68. style.set('width', props.size)
  69. style.set('height', props.size)
  70. // #ifndef APP
  71. style.set('color', props.color)
  72. style.set('--l-play-state', props.animated ? 'running' : 'paused')
  73. // #endif
  74. return style
  75. })
  76. const textStyle = computed<Map<string,any>>(():Map<string,any> => {
  77. const style = new Map<string,any>()
  78. if (props.textColor != null) {
  79. style.set('color', props.textColor!)
  80. }
  81. if (props.textSize != null) {
  82. style.set('font-size', props.textSize!)
  83. }
  84. return style
  85. })
  86. // #ifdef APP
  87. const loadingRef = ref<UniElement|null>(null)
  88. const loading = useLoading(loadingRef)
  89. loading.type = props.type;
  90. loading.mode = props.mode;
  91. if(props.animated){
  92. loading.play()
  93. } else {
  94. loading.pause()
  95. }
  96. watchEffect(()=>{
  97. if(loadingRef.value == null) return
  98. loading.color = props.color ?? `${themeVars.value['loadingColor'] ?? '#3283ff'}`
  99. if(props.animated){
  100. loading.play()
  101. } else {
  102. loading.pause()
  103. }
  104. })
  105. // #endif
  106. </script>
  107. <style lang="scss">
  108. @import './index-u.scss';
  109. </style>