use-carousel-item.mjs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import { inject, getCurrentInstance, ref, reactive, onBeforeUnmount, unref } from 'vue';
  2. import { carouselContextKey, CAROUSEL_ITEM_NAME } from './constants.mjs';
  3. import { debugWarn } from '../../../utils/error.mjs';
  4. import { isUndefined } from '../../../utils/types.mjs';
  5. const useCarouselItem = (props) => {
  6. const carouselContext = inject(carouselContextKey);
  7. const instance = getCurrentInstance();
  8. if (!carouselContext) {
  9. debugWarn(
  10. CAROUSEL_ITEM_NAME,
  11. "usage: <el-carousel></el-carousel-item></el-carousel>"
  12. );
  13. }
  14. if (!instance) {
  15. debugWarn(
  16. CAROUSEL_ITEM_NAME,
  17. "compositional hook can only be invoked inside setups"
  18. );
  19. }
  20. const carouselItemRef = ref();
  21. const hover = ref(false);
  22. const translate = ref(0);
  23. const scale = ref(1);
  24. const active = ref(false);
  25. const ready = ref(false);
  26. const inStage = ref(false);
  27. const animating = ref(false);
  28. const { isCardType, isVertical, cardScale } = carouselContext;
  29. function processIndex(index, activeIndex, length) {
  30. const lastItemIndex = length - 1;
  31. const prevItemIndex = activeIndex - 1;
  32. const nextItemIndex = activeIndex + 1;
  33. const halfItemIndex = length / 2;
  34. if (activeIndex === 0 && index === lastItemIndex) {
  35. return -1;
  36. } else if (activeIndex === lastItemIndex && index === 0) {
  37. return length;
  38. } else if (index < prevItemIndex && activeIndex - index >= halfItemIndex) {
  39. return length + 1;
  40. } else if (index > nextItemIndex && index - activeIndex >= halfItemIndex) {
  41. return -2;
  42. }
  43. return index;
  44. }
  45. function calcCardTranslate(index, activeIndex) {
  46. var _a, _b;
  47. const parentWidth = unref(isVertical) ? ((_a = carouselContext.root.value) == null ? void 0 : _a.offsetHeight) || 0 : ((_b = carouselContext.root.value) == null ? void 0 : _b.offsetWidth) || 0;
  48. if (inStage.value) {
  49. return parentWidth * ((2 - cardScale) * (index - activeIndex) + 1) / 4;
  50. } else if (index < activeIndex) {
  51. return -(1 + cardScale) * parentWidth / 4;
  52. } else {
  53. return (3 + cardScale) * parentWidth / 4;
  54. }
  55. }
  56. function calcTranslate(index, activeIndex, isVertical2) {
  57. const rootEl = carouselContext.root.value;
  58. if (!rootEl)
  59. return 0;
  60. const distance = (isVertical2 ? rootEl.offsetHeight : rootEl.offsetWidth) || 0;
  61. return distance * (index - activeIndex);
  62. }
  63. const translateItem = (index, activeIndex, oldIndex) => {
  64. var _a;
  65. const _isCardType = unref(isCardType);
  66. const carouselItemLength = (_a = carouselContext.items.value.length) != null ? _a : Number.NaN;
  67. const isActive = index === activeIndex;
  68. if (!_isCardType && !isUndefined(oldIndex)) {
  69. animating.value = isActive || index === oldIndex;
  70. }
  71. if (!isActive && carouselItemLength > 2 && carouselContext.loop) {
  72. index = processIndex(index, activeIndex, carouselItemLength);
  73. }
  74. const _isVertical = unref(isVertical);
  75. active.value = isActive;
  76. if (_isCardType) {
  77. inStage.value = Math.round(Math.abs(index - activeIndex)) <= 1;
  78. translate.value = calcCardTranslate(index, activeIndex);
  79. scale.value = unref(active) ? 1 : cardScale;
  80. } else {
  81. translate.value = calcTranslate(index, activeIndex, _isVertical);
  82. }
  83. ready.value = true;
  84. if (isActive && carouselItemRef.value) {
  85. carouselContext.setContainerHeight(carouselItemRef.value.offsetHeight);
  86. }
  87. };
  88. function handleItemClick() {
  89. if (carouselContext && unref(isCardType)) {
  90. const index = carouselContext.items.value.findIndex(
  91. ({ uid }) => uid === instance.uid
  92. );
  93. carouselContext.setActiveItem(index);
  94. }
  95. }
  96. const carouselItemContext = {
  97. props,
  98. states: reactive({
  99. hover,
  100. translate,
  101. scale,
  102. active,
  103. ready,
  104. inStage,
  105. animating
  106. }),
  107. uid: instance.uid,
  108. getVnode: () => instance.vnode,
  109. translateItem
  110. };
  111. carouselContext.addItem(carouselItemContext);
  112. onBeforeUnmount(() => {
  113. carouselContext.removeItem(carouselItemContext);
  114. });
  115. return {
  116. carouselItemRef,
  117. active,
  118. animating,
  119. hover,
  120. inStage,
  121. isVertical,
  122. translate,
  123. isCardType,
  124. scale,
  125. ready,
  126. handleItemClick
  127. };
  128. };
  129. export { useCarouselItem };
  130. //# sourceMappingURL=use-carousel-item.mjs.map