use-carousel-item.mjs 4.1 KB

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