LazyContainer.vue 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <template>
  2. <transition-group
  3. :class="prefixCls"
  4. v-bind="$attrs"
  5. ref="elRef"
  6. :name="transitionName"
  7. :tag="tag"
  8. mode="out-in"
  9. >
  10. <div key="component" v-if="isInit">
  11. <slot :loading="loading"></slot>
  12. </div>
  13. <div key="skeleton" v-else>
  14. <slot name="skeleton" v-if="$slots.skeleton"></slot>
  15. <Skeleton v-else />
  16. </div>
  17. </transition-group>
  18. </template>
  19. <script lang="ts">
  20. import type { PropType } from 'vue';
  21. import { defineComponent, reactive, onMounted, ref, toRef, toRefs } from 'vue';
  22. import { Skeleton } from 'ant-design-vue';
  23. import { useTimeoutFn } from '/@/hooks/core/useTimeout';
  24. import { useIntersectionObserver } from '/@/hooks/event/useIntersectionObserver';
  25. import { propTypes } from '/@/utils/propTypes';
  26. import { useDesign } from '/@/hooks/web/useDesign';
  27. interface State {
  28. isInit: boolean;
  29. loading: boolean;
  30. intersectionObserverInstance: IntersectionObserver | null;
  31. }
  32. export default defineComponent({
  33. name: 'LazyContainer',
  34. components: { Skeleton },
  35. inheritAttrs: false,
  36. props: {
  37. // Waiting time, if the time is specified, whether visible or not, it will be automatically loaded after the specified time
  38. timeout: propTypes.number,
  39. // The viewport where the component is located. If the component is scrolling in the page container, the viewport is the container
  40. viewport: {
  41. type: (typeof window !== 'undefined'
  42. ? window.HTMLElement
  43. : Object) as PropType<HTMLElement>,
  44. default: () => null,
  45. },
  46. // Preload threshold, css unit
  47. threshold: propTypes.string.def('0px'),
  48. // The scroll direction of the viewport, vertical represents the vertical direction, horizontal represents the horizontal direction
  49. direction: propTypes.oneOf(['vertical', 'horizontal']).def('vertical'),
  50. // The label name of the outer container that wraps the component
  51. tag: propTypes.string.def('div'),
  52. maxWaitingTime: propTypes.number.def(80),
  53. // transition name
  54. transitionName: propTypes.string.def('lazy-container'),
  55. },
  56. emits: ['init'],
  57. setup(props, { emit }) {
  58. const elRef = ref<any>(null);
  59. const state = reactive<State>({
  60. isInit: false,
  61. loading: false,
  62. intersectionObserverInstance: null,
  63. });
  64. const { prefixCls } = useDesign('lazy-container');
  65. onMounted(() => {
  66. immediateInit();
  67. initIntersectionObserver();
  68. });
  69. // If there is a set delay time, it will be executed immediately
  70. function immediateInit() {
  71. const { timeout } = props;
  72. timeout &&
  73. useTimeoutFn(() => {
  74. init();
  75. }, timeout);
  76. }
  77. function init() {
  78. state.loading = true;
  79. useTimeoutFn(() => {
  80. if (state.isInit) return;
  81. state.isInit = true;
  82. emit('init');
  83. }, props.maxWaitingTime || 80);
  84. }
  85. function initIntersectionObserver() {
  86. const { timeout, direction, threshold } = props;
  87. if (timeout) return;
  88. // According to the scrolling direction to construct the viewport margin, used to load in advance
  89. let rootMargin = '0px';
  90. switch (direction) {
  91. case 'vertical':
  92. rootMargin = `${threshold} 0px`;
  93. break;
  94. case 'horizontal':
  95. rootMargin = `0px ${threshold}`;
  96. break;
  97. }
  98. try {
  99. const { stop, observer } = useIntersectionObserver({
  100. rootMargin,
  101. target: toRef(elRef.value, '$el'),
  102. onIntersect: (entries: any[]) => {
  103. const isIntersecting = entries[0].isIntersecting || entries[0].intersectionRatio;
  104. if (isIntersecting) {
  105. init();
  106. if (observer) {
  107. stop();
  108. }
  109. }
  110. },
  111. root: toRef(props, 'viewport'),
  112. });
  113. } catch (e) {
  114. init();
  115. }
  116. }
  117. return {
  118. elRef,
  119. prefixCls,
  120. ...toRefs(state),
  121. };
  122. },
  123. });
  124. </script>
  125. <style lang="less">
  126. @prefix-cls: ~'@{namespace}-lazy-container';
  127. .@{prefix-cls} {
  128. width: 100%;
  129. height: 100%;
  130. }
  131. </style>