react-spring-carousel 3.0.0-beta078 → 3.0.0-beta079
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.cjs +2 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/cjs/index2.cjs +2 -0
- package/dist/cjs/index2.cjs.map +1 -0
- package/dist/cjs/useEventsModule-0feadbf7.cjs +2 -0
- package/dist/cjs/useEventsModule-0feadbf7.cjs.map +1 -0
- package/dist/cjs/useSpringCarousel.cjs +2 -0
- package/dist/cjs/useSpringCarousel.cjs.map +1 -0
- package/dist/cjs/useThumbsModule-57ac2128.cjs +28 -0
- package/dist/cjs/useThumbsModule-57ac2128.cjs.map +1 -0
- package/dist/cjs/useTransitionCarousel.cjs +2 -0
- package/dist/cjs/useTransitionCarousel.cjs.map +1 -0
- package/dist/esm/index.esm.js +16 -0
- package/dist/esm/index.esm.js.map +1 -0
- package/dist/esm/index.esm2.js +7 -0
- package/dist/esm/index.esm2.js.map +1 -0
- package/dist/esm/useEventsModule-538b90c9.js +34 -0
- package/dist/esm/useEventsModule-538b90c9.js.map +1 -0
- package/dist/esm/useSpringCarousel.esm.js +666 -0
- package/dist/esm/useSpringCarousel.esm.js.map +1 -0
- package/dist/esm/useThumbsModule-f4286033.js +724 -0
- package/dist/esm/useThumbsModule-f4286033.js.map +1 -0
- package/dist/esm/useTransitionCarousel.esm.js +256 -0
- package/dist/esm/useTransitionCarousel.esm.js.map +1 -0
- package/dist/index.d.ts +3 -3
- package/dist/types/index.d.ts +3 -0
- package/dist/types/src/index.d.ts +3 -0
- package/dist/types/src/index.js +4 -0
- package/dist/types/src/index.js.map +1 -0
- package/dist/types/src/mockedItems.d.ts +2 -0
- package/dist/types/src/mockedItems.js +46 -0
- package/dist/types/src/mockedItems.js.map +1 -0
- package/dist/types/src/modules/index.d.ts +1 -0
- package/dist/types/src/modules/index.js +2 -0
- package/dist/types/src/modules/index.js.map +1 -0
- package/dist/types/src/modules/useEventsModule.d.ts +5 -0
- package/dist/types/src/modules/useEventsModule.js +36 -0
- package/dist/types/src/modules/useEventsModule.js.map +1 -0
- package/dist/types/src/modules/useFullscreenModule.d.ts +6 -0
- package/dist/types/src/modules/useFullscreenModule.js +48 -0
- package/dist/types/src/modules/useFullscreenModule.js.map +1 -0
- package/dist/types/src/modules/useThumbsModule.d.ts +6 -0
- package/dist/types/src/modules/useThumbsModule.js +82 -0
- package/dist/types/src/modules/useThumbsModule.js.map +1 -0
- package/dist/types/src/types/common.d.ts +34 -0
- package/dist/types/src/types/common.js +2 -0
- package/dist/types/src/types/common.js.map +1 -0
- package/dist/types/src/types/index.d.ts +3 -0
- package/dist/types/src/types/index.js +4 -0
- package/dist/types/src/types/index.js.map +1 -0
- package/dist/types/src/types/useEventsModule.types.js +2 -0
- package/dist/types/src/types/useEventsModule.types.js.map +1 -0
- package/dist/types/src/types/useFullscreenModule.types.d.ts +6 -0
- package/dist/types/src/types/useFullscreenModule.types.js +2 -0
- package/dist/types/src/types/useFullscreenModule.types.js.map +1 -0
- package/dist/types/src/types/useSpringCarousel.types.js +2 -0
- package/dist/types/src/types/useSpringCarousel.types.js.map +1 -0
- package/dist/types/src/types/useThumbsModule.types.d.ts +8 -0
- package/dist/types/src/types/useThumbsModule.types.js +2 -0
- package/dist/types/src/types/useThumbsModule.types.js.map +1 -0
- package/dist/types/src/types/useTransitionCarousel.types.js +2 -0
- package/dist/types/src/types/useTransitionCarousel.types.js.map +1 -0
- package/dist/types/src/useSpringCarousel.d.ts +18 -0
- package/dist/types/src/useSpringCarousel.js +917 -0
- package/dist/types/src/useSpringCarousel.js.map +1 -0
- package/dist/types/src/useTransitionCarousel.d.ts +5 -0
- package/dist/types/src/useTransitionCarousel.js +285 -0
- package/dist/types/src/useTransitionCarousel.js.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -0
- package/dist/types/useEventsModule.types.d.ts +46 -0
- package/dist/types/useFullscreenModule.types.d.ts +6 -0
- package/dist/types/useSpringCarousel.types.d.ts +102 -0
- package/dist/types/useThumbsModule.types.d.ts +8 -0
- package/dist/types/useTransitionCarousel.types.d.ts +33 -0
- package/package.json +1 -1
- package/dist/index.cjs.js +0 -239
- package/dist/index.cjs.js.map +0 -1
- package/dist/index.esm.js +0 -25598
- package/dist/index.esm.js.map +0 -1
- /package/dist/{common.d.ts → types/common.d.ts} +0 -0
- /package/dist/{useEventsModule.types.d.ts → types/src/types/useEventsModule.types.d.ts} +0 -0
- /package/dist/{useSpringCarousel.types.d.ts → types/src/types/useSpringCarousel.types.d.ts} +0 -0
- /package/dist/{useTransitionCarousel.types.d.ts → types/src/types/useTransitionCarousel.types.d.ts} +0 -0
|
@@ -0,0 +1,917 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { config, useIsomorphicLayoutEffect, useSpring } from '@react-spring/web';
|
|
3
|
+
import { createContext, useCallback, useContext, useEffect, useRef } from 'react';
|
|
4
|
+
import { useEventsModule } from './modules/useEventsModule';
|
|
5
|
+
import { useDrag } from '@use-gesture/react';
|
|
6
|
+
import { useThumbsModule } from './modules/useThumbsModule';
|
|
7
|
+
import ResizeObserver from 'resize-observer-polyfill';
|
|
8
|
+
import { useFullscreenModule } from './modules/useFullscreenModule';
|
|
9
|
+
function useSpringCarousel({ items, init = true, withThumbs, thumbsSlideAxis = 'x', itemsPerSlide: _itemsPerSlide = 1, slideType = 'fixed', gutter = 0, withLoop = false, startEndGutter = 0, carouselSlideAxis = 'x', disableGestures = false, draggingSlideTreshold: _draggingSlideTreshold, slideWhenThresholdIsReached = false, freeScroll, enableFreeScrollDrag, initialStartingPosition, prepareThumbsData, initialActiveItem = 0, animateWhenActiveItemChange = true, getControllerRef, }) {
|
|
10
|
+
const itemsPerSlide = _itemsPerSlide > items.length ? items.length : _itemsPerSlide;
|
|
11
|
+
const resizeByPropChange = useRef(false);
|
|
12
|
+
const draggingSlideTreshold = useRef(_draggingSlideTreshold ?? 0);
|
|
13
|
+
const slideActionType = useRef('initial');
|
|
14
|
+
const slideModeType = useRef('initial');
|
|
15
|
+
/**
|
|
16
|
+
* After the user hits start/end edges of the carousel,
|
|
17
|
+
* we check where the user is going. This is useful
|
|
18
|
+
* to correctly resize the carousel when the carousel is going
|
|
19
|
+
* backward after reaching the last item in fluid slide mode
|
|
20
|
+
*/
|
|
21
|
+
const directionAfterReachingEdges = useRef('initial');
|
|
22
|
+
const activeItem = useRef(initialActiveItem);
|
|
23
|
+
const firstItemReached = useRef(initialActiveItem === 0);
|
|
24
|
+
const lastItemReached = useRef(slideType === 'fixed' && initialActiveItem === items.length - 1);
|
|
25
|
+
const mainCarouselWrapperRef = useRef(null);
|
|
26
|
+
const carouselTrackWrapperRef = useRef(null);
|
|
27
|
+
const isFirstMount = useRef(true);
|
|
28
|
+
const prevTotalScrollValue = useRef(0);
|
|
29
|
+
const prevWindowWidth = useRef(0);
|
|
30
|
+
const prevSlidedValue = useRef(0);
|
|
31
|
+
const prevWithLoop = useRef(withLoop);
|
|
32
|
+
const prevSlideType = useRef(slideType);
|
|
33
|
+
const prevFreeScroll = useRef(freeScroll);
|
|
34
|
+
const windowIsHidden = useRef(false);
|
|
35
|
+
const getItems = useCallback(() => {
|
|
36
|
+
if (withLoop) {
|
|
37
|
+
return [
|
|
38
|
+
...items.map((i) => ({
|
|
39
|
+
...i,
|
|
40
|
+
id: `prev-repeated-item-${i.id}`,
|
|
41
|
+
})),
|
|
42
|
+
...items,
|
|
43
|
+
...items.map((i) => ({
|
|
44
|
+
...i,
|
|
45
|
+
id: `next-repeated-item-${i.id}`,
|
|
46
|
+
})),
|
|
47
|
+
];
|
|
48
|
+
}
|
|
49
|
+
return [...items];
|
|
50
|
+
}, [items, withLoop]);
|
|
51
|
+
const internalItems = getItems();
|
|
52
|
+
const [spring, setSpring] = useSpring(() => ({
|
|
53
|
+
val: 0,
|
|
54
|
+
pause: !init,
|
|
55
|
+
onChange: ({ value }) => {
|
|
56
|
+
if (freeScroll && mainCarouselWrapperRef.current) {
|
|
57
|
+
if (carouselSlideAxis === 'x') {
|
|
58
|
+
mainCarouselWrapperRef.current.scrollLeft = Math.abs(value.val);
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
mainCarouselWrapperRef.current.scrollTop = Math.abs(value.val);
|
|
62
|
+
}
|
|
63
|
+
setStartEndItemReachedOnFreeScroll();
|
|
64
|
+
}
|
|
65
|
+
else if (carouselTrackWrapperRef.current) {
|
|
66
|
+
if (carouselSlideAxis === 'x') {
|
|
67
|
+
carouselTrackWrapperRef.current.style.transform = `translate3d(${value.val}px, 0px,0px)`;
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
carouselTrackWrapperRef.current.style.transform = `translate3d(0px,${value.val}px,0px)`;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
},
|
|
74
|
+
}), [freeScroll]);
|
|
75
|
+
const { emitEvent, useListenToCustomEvent } = useEventsModule();
|
|
76
|
+
const { thumbsFragment, handleScroll } = useThumbsModule({
|
|
77
|
+
withThumbs: !!withThumbs,
|
|
78
|
+
thumbsSlideAxis,
|
|
79
|
+
prepareThumbsData,
|
|
80
|
+
items: items,
|
|
81
|
+
renderThumbFnProps: {
|
|
82
|
+
getIsActiveItem,
|
|
83
|
+
getIsPrevItem,
|
|
84
|
+
useListenToCustomEvent,
|
|
85
|
+
getIsNextItem,
|
|
86
|
+
},
|
|
87
|
+
});
|
|
88
|
+
const { enterFullscreen, exitFullscreen, getIsFullscreen } = useFullscreenModule({
|
|
89
|
+
mainCarouselWrapperRef,
|
|
90
|
+
handleResize: () => adjustCarouselWrapperPosition(),
|
|
91
|
+
onFullScreenChange: (val) => {
|
|
92
|
+
emitEvent({
|
|
93
|
+
eventName: 'onFullscreenChange',
|
|
94
|
+
isFullscreen: val,
|
|
95
|
+
});
|
|
96
|
+
},
|
|
97
|
+
});
|
|
98
|
+
function getItemStyles(isLastItem) {
|
|
99
|
+
if (slideType === 'fixed' && !freeScroll) {
|
|
100
|
+
return {
|
|
101
|
+
marginRight: `${isLastItem ? 0 : gutter}px`,
|
|
102
|
+
flex: `1 0 calc(100% / ${itemsPerSlide} - ${(gutter * (itemsPerSlide - 1)) / itemsPerSlide}px)`,
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
return {
|
|
106
|
+
...{ marginRight: `${isLastItem ? 0 : gutter}px` },
|
|
107
|
+
};
|
|
108
|
+
}
|
|
109
|
+
function getSlideValue() {
|
|
110
|
+
const carouselItem = mainCarouselWrapperRef.current?.querySelector('.use-spring-carousel-item');
|
|
111
|
+
if (!carouselItem) {
|
|
112
|
+
throw Error('No carousel items available!');
|
|
113
|
+
}
|
|
114
|
+
return (carouselItem.getBoundingClientRect()[carouselSlideAxis === 'x' ? 'width' : 'height'] + gutter);
|
|
115
|
+
}
|
|
116
|
+
function slideToItem({ from, to, nextActiveItem, immediate = false, slideMode }) {
|
|
117
|
+
slideModeType.current = slideMode;
|
|
118
|
+
if (typeof nextActiveItem === 'number') {
|
|
119
|
+
if (!freeScroll) {
|
|
120
|
+
activeItem.current = nextActiveItem;
|
|
121
|
+
}
|
|
122
|
+
emitEvent({
|
|
123
|
+
eventName: 'onSlideStartChange',
|
|
124
|
+
slideActionType: slideActionType.current,
|
|
125
|
+
slideMode: slideModeType.current,
|
|
126
|
+
nextItem: {
|
|
127
|
+
startReached: firstItemReached.current,
|
|
128
|
+
endReached: lastItemReached.current,
|
|
129
|
+
index: freeScroll ? -1 : activeItem.current,
|
|
130
|
+
id: freeScroll ? '' : items[activeItem.current].id,
|
|
131
|
+
},
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
prevSlidedValue.current = to;
|
|
135
|
+
setSpring.start({
|
|
136
|
+
immediate,
|
|
137
|
+
from: {
|
|
138
|
+
val: from,
|
|
139
|
+
},
|
|
140
|
+
to: {
|
|
141
|
+
val: to,
|
|
142
|
+
},
|
|
143
|
+
config: {
|
|
144
|
+
...config.default,
|
|
145
|
+
velocity: spring.val.velocity,
|
|
146
|
+
},
|
|
147
|
+
onRest(value) {
|
|
148
|
+
if (!immediate && value.finished) {
|
|
149
|
+
emitEvent({
|
|
150
|
+
eventName: 'onSlideChange',
|
|
151
|
+
slideActionType: slideActionType.current,
|
|
152
|
+
slideMode: slideModeType.current,
|
|
153
|
+
currentItem: {
|
|
154
|
+
startReached: firstItemReached.current,
|
|
155
|
+
endReached: lastItemReached.current,
|
|
156
|
+
index: freeScroll ? -1 : activeItem.current,
|
|
157
|
+
id: freeScroll ? '' : items[activeItem.current].id,
|
|
158
|
+
},
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
},
|
|
162
|
+
});
|
|
163
|
+
if (withThumbs && !immediate) {
|
|
164
|
+
handleScroll(activeItem.current);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
168
|
+
function getTotalScrollValue() {
|
|
169
|
+
if (withLoop) {
|
|
170
|
+
return getSlideValue() * items.length;
|
|
171
|
+
}
|
|
172
|
+
return Math.round(Number(carouselTrackWrapperRef.current?.[carouselSlideAxis === 'x' ? 'scrollWidth' : 'scrollHeight']) -
|
|
173
|
+
carouselTrackWrapperRef.current.getBoundingClientRect()[carouselSlideAxis === 'x' ? 'width' : 'height'] -
|
|
174
|
+
startEndGutter * 2);
|
|
175
|
+
}
|
|
176
|
+
function getAnimatedWrapperStyles() {
|
|
177
|
+
const percentValue = `calc(100% - ${startEndGutter * 2}px)`;
|
|
178
|
+
return {
|
|
179
|
+
width: carouselSlideAxis === 'x' ? percentValue : '100%',
|
|
180
|
+
height: carouselSlideAxis === 'y' ? percentValue : '100%',
|
|
181
|
+
};
|
|
182
|
+
}
|
|
183
|
+
function getCarouselItemWidth() {
|
|
184
|
+
const carouselItem = carouselTrackWrapperRef.current?.querySelector('.use-spring-carousel-item');
|
|
185
|
+
if (!carouselItem) {
|
|
186
|
+
throw Error('No carousel items available!');
|
|
187
|
+
}
|
|
188
|
+
return (carouselItem.getBoundingClientRect()[carouselSlideAxis === 'x' ? 'width' : 'height'] + gutter);
|
|
189
|
+
}
|
|
190
|
+
function setPosition(v) {
|
|
191
|
+
const positionProperty = carouselSlideAxis === 'x' ? 'left' : 'top';
|
|
192
|
+
const ref = carouselTrackWrapperRef.current;
|
|
193
|
+
if (!ref)
|
|
194
|
+
return;
|
|
195
|
+
if (withLoop) {
|
|
196
|
+
ref.style.top = '0px';
|
|
197
|
+
ref.style.left = '0px';
|
|
198
|
+
ref.style[positionProperty] = `-${v - startEndGutter}px`;
|
|
199
|
+
firstItemReached.current = false;
|
|
200
|
+
lastItemReached.current = false;
|
|
201
|
+
}
|
|
202
|
+
else {
|
|
203
|
+
ref.style.left = '0px';
|
|
204
|
+
ref.style.top = '0px';
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
208
|
+
function adjustCarouselWrapperPosition(shouldResetPosition = false) {
|
|
209
|
+
if (carouselTrackWrapperRef.current && shouldResetPosition) {
|
|
210
|
+
carouselTrackWrapperRef.current.style.transform = `translate3d(0px, 0px,0px)`;
|
|
211
|
+
carouselTrackWrapperRef.current.style.left = `0`;
|
|
212
|
+
carouselTrackWrapperRef.current.style.top = `0`;
|
|
213
|
+
}
|
|
214
|
+
if (slideType === 'fixed') {
|
|
215
|
+
if (initialStartingPosition === 'center') {
|
|
216
|
+
setPosition(getCarouselItemWidth() * items.length -
|
|
217
|
+
getSlideValue() * Math.round((itemsPerSlide - 1) / 2));
|
|
218
|
+
}
|
|
219
|
+
else if (initialStartingPosition === 'end') {
|
|
220
|
+
setPosition(getCarouselItemWidth() * items.length - getSlideValue() * Math.round(itemsPerSlide - 1));
|
|
221
|
+
}
|
|
222
|
+
else {
|
|
223
|
+
setPosition(getCarouselItemWidth() * items.length);
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
if (slideType === 'fluid') {
|
|
227
|
+
/**
|
|
228
|
+
* User reached the last item and now is resizing the container that becomes smaller/bigger.
|
|
229
|
+
* Example: on mobile devices the user rotates the device
|
|
230
|
+
*/
|
|
231
|
+
if (lastItemReached.current &&
|
|
232
|
+
getTotalScrollValue() !== Math.abs(prevSlidedValue.current) &&
|
|
233
|
+
!withLoop) {
|
|
234
|
+
const newVal = -getTotalScrollValue();
|
|
235
|
+
prevSlidedValue.current = newVal;
|
|
236
|
+
setSpring.start({
|
|
237
|
+
immediate: true,
|
|
238
|
+
val: prevSlidedValue.current,
|
|
239
|
+
});
|
|
240
|
+
return;
|
|
241
|
+
}
|
|
242
|
+
if (Math.abs(prevSlidedValue.current) > 0 &&
|
|
243
|
+
getTotalScrollValue() !== Math.abs(prevSlidedValue.current) &&
|
|
244
|
+
!withLoop &&
|
|
245
|
+
!freeScroll &&
|
|
246
|
+
directionAfterReachingEdges.current === 'backward') {
|
|
247
|
+
const diff = prevTotalScrollValue.current - getTotalScrollValue();
|
|
248
|
+
const next = prevSlidedValue.current + diff;
|
|
249
|
+
setSpring.start({
|
|
250
|
+
immediate: true,
|
|
251
|
+
val: next,
|
|
252
|
+
});
|
|
253
|
+
return () => {
|
|
254
|
+
prevSlidedValue.current = next;
|
|
255
|
+
};
|
|
256
|
+
}
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
if (!freeScroll && slideType === 'fixed') {
|
|
260
|
+
const nextValue = -(getSlideValue() * activeItem.current);
|
|
261
|
+
/**
|
|
262
|
+
* Here we make sure to always show the latest item as the
|
|
263
|
+
* latest item visible in the carousel viewport.
|
|
264
|
+
*/
|
|
265
|
+
if (Math.abs(nextValue) > getTotalScrollValue() && !withLoop) {
|
|
266
|
+
const val = -getTotalScrollValue();
|
|
267
|
+
lastItemReached.current = true;
|
|
268
|
+
prevSlidedValue.current = val;
|
|
269
|
+
setSpring.start({
|
|
270
|
+
immediate: true,
|
|
271
|
+
val: prevSlidedValue.current,
|
|
272
|
+
});
|
|
273
|
+
}
|
|
274
|
+
else {
|
|
275
|
+
prevSlidedValue.current = nextValue;
|
|
276
|
+
setSpring.start({
|
|
277
|
+
immediate: true,
|
|
278
|
+
val: nextValue,
|
|
279
|
+
});
|
|
280
|
+
}
|
|
281
|
+
setTimeout(() => {
|
|
282
|
+
resizeByPropChange.current = false;
|
|
283
|
+
}, 0);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
function getFromValue() {
|
|
287
|
+
if (freeScroll && mainCarouselWrapperRef.current) {
|
|
288
|
+
return mainCarouselWrapperRef.current[carouselSlideAxis === 'x' ? 'scrollLeft' : 'scrollTop'];
|
|
289
|
+
}
|
|
290
|
+
return spring.val.get();
|
|
291
|
+
}
|
|
292
|
+
function getToValue(type, index) {
|
|
293
|
+
if (freeScroll && type === 'next') {
|
|
294
|
+
const next = prevSlidedValue.current + getSlideValue();
|
|
295
|
+
if (next > getTotalScrollValue()) {
|
|
296
|
+
return getTotalScrollValue();
|
|
297
|
+
}
|
|
298
|
+
return next;
|
|
299
|
+
}
|
|
300
|
+
if (freeScroll && type === 'prev') {
|
|
301
|
+
const next = prevSlidedValue.current - getSlideValue();
|
|
302
|
+
if (next < 0) {
|
|
303
|
+
return 0;
|
|
304
|
+
}
|
|
305
|
+
return next;
|
|
306
|
+
}
|
|
307
|
+
if (type === 'next') {
|
|
308
|
+
if (typeof index === 'number') {
|
|
309
|
+
return -(index * getSlideValue());
|
|
310
|
+
}
|
|
311
|
+
return prevSlidedValue.current - getSlideValue();
|
|
312
|
+
}
|
|
313
|
+
if (typeof index === 'number') {
|
|
314
|
+
return -(index * getSlideValue());
|
|
315
|
+
}
|
|
316
|
+
return prevSlidedValue.current + getSlideValue();
|
|
317
|
+
}
|
|
318
|
+
function slideToPrevItem({ type, index, immediate }) {
|
|
319
|
+
if (!init || windowIsHidden.current || (firstItemReached.current && !withLoop))
|
|
320
|
+
return;
|
|
321
|
+
if (lastItemReached.current) {
|
|
322
|
+
directionAfterReachingEdges.current = 'backward';
|
|
323
|
+
}
|
|
324
|
+
slideActionType.current = 'prev';
|
|
325
|
+
lastItemReached.current = false;
|
|
326
|
+
const nextItem = typeof index === 'number' ? index : activeItem.current - 1;
|
|
327
|
+
if (freeScroll) {
|
|
328
|
+
setStartEndItemReachedOnFreeScroll();
|
|
329
|
+
}
|
|
330
|
+
if (!withLoop) {
|
|
331
|
+
const nextItemWillExceed = freeScroll
|
|
332
|
+
? getToValue('prev', index) - getSlideValue() / 3 < 0
|
|
333
|
+
: getToValue('prev', index) + getSlideValue() / 3 > 0;
|
|
334
|
+
if (firstItemReached.current)
|
|
335
|
+
return;
|
|
336
|
+
if (nextItemWillExceed) {
|
|
337
|
+
firstItemReached.current = true;
|
|
338
|
+
lastItemReached.current = false;
|
|
339
|
+
slideToItem({
|
|
340
|
+
slideMode: type,
|
|
341
|
+
from: getFromValue(),
|
|
342
|
+
to: 0,
|
|
343
|
+
nextActiveItem: 0,
|
|
344
|
+
immediate,
|
|
345
|
+
});
|
|
346
|
+
return;
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
if (withLoop && (firstItemReached.current || nextItem < 0)) {
|
|
350
|
+
firstItemReached.current = false;
|
|
351
|
+
lastItemReached.current = true;
|
|
352
|
+
slideToItem({
|
|
353
|
+
slideMode: type,
|
|
354
|
+
from: getFromValue() - getSlideValue() * items.length,
|
|
355
|
+
to: -(getSlideValue() * items.length) + getSlideValue(),
|
|
356
|
+
nextActiveItem: items.length - 1,
|
|
357
|
+
immediate,
|
|
358
|
+
});
|
|
359
|
+
return;
|
|
360
|
+
}
|
|
361
|
+
if (nextItem === 0) {
|
|
362
|
+
firstItemReached.current = true;
|
|
363
|
+
}
|
|
364
|
+
if (nextItem === items.length - 1 || nextItem === -1) {
|
|
365
|
+
lastItemReached.current = true;
|
|
366
|
+
}
|
|
367
|
+
slideToItem({
|
|
368
|
+
slideMode: type,
|
|
369
|
+
from: getFromValue(),
|
|
370
|
+
to: getToValue('prev', index),
|
|
371
|
+
nextActiveItem: nextItem,
|
|
372
|
+
immediate,
|
|
373
|
+
});
|
|
374
|
+
}
|
|
375
|
+
function slideToNextItem({ type, index, immediate }) {
|
|
376
|
+
if (!init || windowIsHidden.current || (lastItemReached.current && !withLoop))
|
|
377
|
+
return;
|
|
378
|
+
if (firstItemReached.current) {
|
|
379
|
+
directionAfterReachingEdges.current = 'forward';
|
|
380
|
+
}
|
|
381
|
+
slideActionType.current = 'next';
|
|
382
|
+
firstItemReached.current = false;
|
|
383
|
+
const nextItem = index || activeItem.current + 1;
|
|
384
|
+
if (freeScroll) {
|
|
385
|
+
setStartEndItemReachedOnFreeScroll();
|
|
386
|
+
}
|
|
387
|
+
if (!withLoop) {
|
|
388
|
+
const nextItemWillExceed = Math.abs(getToValue('next', index)) > getTotalScrollValue() - getSlideValue() / 3;
|
|
389
|
+
if (lastItemReached.current)
|
|
390
|
+
return;
|
|
391
|
+
if (nextItemWillExceed) {
|
|
392
|
+
firstItemReached.current = false;
|
|
393
|
+
lastItemReached.current = true;
|
|
394
|
+
slideToItem({
|
|
395
|
+
slideMode: type,
|
|
396
|
+
from: getFromValue(),
|
|
397
|
+
to: freeScroll ? getTotalScrollValue() : -getTotalScrollValue(),
|
|
398
|
+
nextActiveItem: nextItem,
|
|
399
|
+
immediate,
|
|
400
|
+
});
|
|
401
|
+
return;
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
if (withLoop && (lastItemReached.current || nextItem > items.length - 1)) {
|
|
405
|
+
lastItemReached.current = false;
|
|
406
|
+
firstItemReached.current = true;
|
|
407
|
+
slideToItem({
|
|
408
|
+
slideMode: type,
|
|
409
|
+
from: getFromValue() + getSlideValue() * items.length,
|
|
410
|
+
to: 0,
|
|
411
|
+
nextActiveItem: 0,
|
|
412
|
+
immediate,
|
|
413
|
+
});
|
|
414
|
+
return;
|
|
415
|
+
}
|
|
416
|
+
if (nextItem === 0) {
|
|
417
|
+
firstItemReached.current = true;
|
|
418
|
+
}
|
|
419
|
+
if (nextItem === items.length - 1) {
|
|
420
|
+
lastItemReached.current = true;
|
|
421
|
+
}
|
|
422
|
+
slideToItem({
|
|
423
|
+
slideMode: type,
|
|
424
|
+
from: getFromValue(),
|
|
425
|
+
to: getToValue('next', index),
|
|
426
|
+
nextActiveItem: nextItem,
|
|
427
|
+
immediate,
|
|
428
|
+
});
|
|
429
|
+
}
|
|
430
|
+
function setDraggingSliderTreshold() {
|
|
431
|
+
if (_draggingSlideTreshold) {
|
|
432
|
+
draggingSlideTreshold.current = _draggingSlideTreshold;
|
|
433
|
+
}
|
|
434
|
+
else {
|
|
435
|
+
draggingSlideTreshold.current = Math.floor(getSlideValue() / 2 / 2);
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
function initializeCarousel() {
|
|
439
|
+
if (!isFirstMount.current && carouselTrackWrapperRef.current) {
|
|
440
|
+
prevTotalScrollValue.current = getTotalScrollValue();
|
|
441
|
+
prevWithLoop.current = withLoop;
|
|
442
|
+
prevSlideType.current = slideType;
|
|
443
|
+
prevFreeScroll.current = freeScroll;
|
|
444
|
+
prevWindowWidth.current = window.innerWidth;
|
|
445
|
+
prevSlidedValue.current = 0;
|
|
446
|
+
internalSlideToItem({ id: 0, immediate: true, shouldReset: true });
|
|
447
|
+
setDraggingSliderTreshold();
|
|
448
|
+
adjustCarouselWrapperPosition();
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
const enableDrag = (init && !disableGestures && !freeScroll) || (!!freeScroll && !!enableFreeScrollDrag);
|
|
452
|
+
const bindDrag = useDrag((state) => {
|
|
453
|
+
const isDragging = state.dragging;
|
|
454
|
+
const movement = state.offset[carouselSlideAxis === 'x' ? 0 : 1];
|
|
455
|
+
const currentMovement = state.movement[carouselSlideAxis === 'x' ? 0 : 1];
|
|
456
|
+
const direction = state.direction[carouselSlideAxis === 'x' ? 0 : 1];
|
|
457
|
+
const prevItemTreshold = currentMovement > draggingSlideTreshold.current;
|
|
458
|
+
const nextItemTreshold = currentMovement < -draggingSlideTreshold.current;
|
|
459
|
+
const tot = getTotalScrollValue();
|
|
460
|
+
if (isDragging) {
|
|
461
|
+
if (direction > 0) {
|
|
462
|
+
slideActionType.current = 'prev';
|
|
463
|
+
}
|
|
464
|
+
else {
|
|
465
|
+
slideActionType.current = 'next';
|
|
466
|
+
}
|
|
467
|
+
emitEvent({
|
|
468
|
+
...state,
|
|
469
|
+
eventName: 'onDrag',
|
|
470
|
+
slideActionType: slideActionType.current,
|
|
471
|
+
});
|
|
472
|
+
if (freeScroll) {
|
|
473
|
+
if (movement > 0) {
|
|
474
|
+
setSpring.start({
|
|
475
|
+
from: {
|
|
476
|
+
val: getFromValue(),
|
|
477
|
+
},
|
|
478
|
+
to: {
|
|
479
|
+
val: 0,
|
|
480
|
+
},
|
|
481
|
+
config: {
|
|
482
|
+
velocity: state.velocity,
|
|
483
|
+
friction: 50,
|
|
484
|
+
tension: 1000,
|
|
485
|
+
},
|
|
486
|
+
});
|
|
487
|
+
state.cancel();
|
|
488
|
+
return;
|
|
489
|
+
}
|
|
490
|
+
setSpring.start({
|
|
491
|
+
from: {
|
|
492
|
+
val: getFromValue(),
|
|
493
|
+
},
|
|
494
|
+
to: {
|
|
495
|
+
val: -movement,
|
|
496
|
+
},
|
|
497
|
+
config: {
|
|
498
|
+
velocity: state.velocity,
|
|
499
|
+
friction: 50,
|
|
500
|
+
tension: 1000,
|
|
501
|
+
},
|
|
502
|
+
});
|
|
503
|
+
return;
|
|
504
|
+
}
|
|
505
|
+
setSpring.start({
|
|
506
|
+
val: movement,
|
|
507
|
+
config: {
|
|
508
|
+
velocity: state.velocity,
|
|
509
|
+
friction: 50,
|
|
510
|
+
tension: 1000,
|
|
511
|
+
},
|
|
512
|
+
});
|
|
513
|
+
if (slideWhenThresholdIsReached && nextItemTreshold) {
|
|
514
|
+
slideToNextItem({ type: 'drag' });
|
|
515
|
+
state.cancel();
|
|
516
|
+
}
|
|
517
|
+
else if (slideWhenThresholdIsReached && prevItemTreshold) {
|
|
518
|
+
slideToPrevItem({ type: 'drag' });
|
|
519
|
+
state.cancel();
|
|
520
|
+
}
|
|
521
|
+
const res = tot - Math.abs(movement);
|
|
522
|
+
if (res < -(getSlideValue() * 2)) {
|
|
523
|
+
state.cancel();
|
|
524
|
+
}
|
|
525
|
+
return;
|
|
526
|
+
}
|
|
527
|
+
if (state.last && freeScroll && movement > 0) {
|
|
528
|
+
setSpring.start({
|
|
529
|
+
from: {
|
|
530
|
+
val: getFromValue(),
|
|
531
|
+
},
|
|
532
|
+
to: {
|
|
533
|
+
val: 0,
|
|
534
|
+
},
|
|
535
|
+
config: {
|
|
536
|
+
velocity: state.velocity,
|
|
537
|
+
friction: 50,
|
|
538
|
+
tension: 1000,
|
|
539
|
+
},
|
|
540
|
+
});
|
|
541
|
+
return;
|
|
542
|
+
}
|
|
543
|
+
if (state.last && !state.canceled && freeScroll) {
|
|
544
|
+
if (slideActionType.current === 'prev') {
|
|
545
|
+
slideToPrevItem({ type: 'drag' });
|
|
546
|
+
}
|
|
547
|
+
if (slideActionType.current === 'next') {
|
|
548
|
+
slideToNextItem({ type: 'drag' });
|
|
549
|
+
}
|
|
550
|
+
}
|
|
551
|
+
if (state.last && !state.canceled && !freeScroll) {
|
|
552
|
+
if (nextItemTreshold) {
|
|
553
|
+
if (!withLoop && lastItemReached.current) {
|
|
554
|
+
setSpring.start({
|
|
555
|
+
val: -getTotalScrollValue(),
|
|
556
|
+
config: {
|
|
557
|
+
...config.default,
|
|
558
|
+
velocity: state.velocity,
|
|
559
|
+
},
|
|
560
|
+
});
|
|
561
|
+
}
|
|
562
|
+
else {
|
|
563
|
+
slideToNextItem({ type: 'drag' });
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
else if (prevItemTreshold) {
|
|
567
|
+
if (!withLoop && firstItemReached.current) {
|
|
568
|
+
setSpring.start({
|
|
569
|
+
val: 0,
|
|
570
|
+
config: {
|
|
571
|
+
...config.default,
|
|
572
|
+
velocity: state.velocity,
|
|
573
|
+
},
|
|
574
|
+
});
|
|
575
|
+
}
|
|
576
|
+
else {
|
|
577
|
+
slideToPrevItem({ type: 'drag' });
|
|
578
|
+
}
|
|
579
|
+
}
|
|
580
|
+
else {
|
|
581
|
+
setSpring.start({
|
|
582
|
+
val: prevSlidedValue.current,
|
|
583
|
+
config: {
|
|
584
|
+
...config.default,
|
|
585
|
+
velocity: state.velocity,
|
|
586
|
+
},
|
|
587
|
+
});
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
if (state.last && state.canceled) {
|
|
591
|
+
setSpring.start({
|
|
592
|
+
val: prevSlidedValue.current,
|
|
593
|
+
config: {
|
|
594
|
+
...config.default,
|
|
595
|
+
velocity: state.velocity,
|
|
596
|
+
},
|
|
597
|
+
});
|
|
598
|
+
}
|
|
599
|
+
}, {
|
|
600
|
+
enabled: enableDrag,
|
|
601
|
+
axis: carouselSlideAxis,
|
|
602
|
+
from: () => {
|
|
603
|
+
if (freeScroll && mainCarouselWrapperRef.current) {
|
|
604
|
+
return [
|
|
605
|
+
-mainCarouselWrapperRef.current.scrollLeft,
|
|
606
|
+
-mainCarouselWrapperRef.current.scrollTop,
|
|
607
|
+
];
|
|
608
|
+
}
|
|
609
|
+
if (carouselSlideAxis === 'x') {
|
|
610
|
+
return [spring.val.get(), spring.val.get()];
|
|
611
|
+
}
|
|
612
|
+
return [spring.val.get(), spring.val.get()];
|
|
613
|
+
},
|
|
614
|
+
});
|
|
615
|
+
function getWrapperOverflowStyles() {
|
|
616
|
+
if (freeScroll) {
|
|
617
|
+
if (carouselSlideAxis === 'x') {
|
|
618
|
+
return {
|
|
619
|
+
overflowX: 'auto',
|
|
620
|
+
};
|
|
621
|
+
}
|
|
622
|
+
return {
|
|
623
|
+
overflowY: 'auto',
|
|
624
|
+
};
|
|
625
|
+
}
|
|
626
|
+
return {};
|
|
627
|
+
}
|
|
628
|
+
function setStartEndItemReachedOnFreeScroll() {
|
|
629
|
+
if (mainCarouselWrapperRef.current) {
|
|
630
|
+
prevSlidedValue.current =
|
|
631
|
+
mainCarouselWrapperRef.current[carouselSlideAxis === 'x' ? 'scrollLeft' : 'scrollTop'];
|
|
632
|
+
if (mainCarouselWrapperRef.current[carouselSlideAxis === 'x' ? 'scrollLeft' : 'scrollTop'] === 0) {
|
|
633
|
+
firstItemReached.current = true;
|
|
634
|
+
lastItemReached.current = false;
|
|
635
|
+
}
|
|
636
|
+
if (mainCarouselWrapperRef.current[carouselSlideAxis === 'x' ? 'scrollLeft' : 'scrollTop'] >
|
|
637
|
+
0 &&
|
|
638
|
+
mainCarouselWrapperRef.current[carouselSlideAxis === 'x' ? 'scrollLeft' : 'scrollTop'] <
|
|
639
|
+
getTotalScrollValue()) {
|
|
640
|
+
firstItemReached.current = false;
|
|
641
|
+
lastItemReached.current = false;
|
|
642
|
+
}
|
|
643
|
+
if (mainCarouselWrapperRef.current[carouselSlideAxis === 'x' ? 'scrollLeft' : 'scrollTop'] ===
|
|
644
|
+
getTotalScrollValue()) {
|
|
645
|
+
firstItemReached.current = false;
|
|
646
|
+
lastItemReached.current = true;
|
|
647
|
+
}
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
function getScrollHandlers() {
|
|
651
|
+
if (freeScroll) {
|
|
652
|
+
return {
|
|
653
|
+
onWheel() {
|
|
654
|
+
spring.val.stop();
|
|
655
|
+
setStartEndItemReachedOnFreeScroll();
|
|
656
|
+
},
|
|
657
|
+
};
|
|
658
|
+
}
|
|
659
|
+
return {};
|
|
660
|
+
}
|
|
661
|
+
function findItemIndex(id, error) {
|
|
662
|
+
let itemIndex = 0;
|
|
663
|
+
if (typeof id === 'string') {
|
|
664
|
+
itemIndex = items.findIndex((item) => item.id === id);
|
|
665
|
+
}
|
|
666
|
+
else {
|
|
667
|
+
itemIndex = id;
|
|
668
|
+
}
|
|
669
|
+
if (itemIndex < 0 || itemIndex >= items.length) {
|
|
670
|
+
if (error) {
|
|
671
|
+
throw new Error(error);
|
|
672
|
+
}
|
|
673
|
+
console.error(`The item doesn't exist; check that the id provided - ${id} - is correct.`);
|
|
674
|
+
itemIndex = -1;
|
|
675
|
+
}
|
|
676
|
+
return itemIndex;
|
|
677
|
+
}
|
|
678
|
+
function internalSlideToItem({ id, immediate, shouldReset, type }) {
|
|
679
|
+
if (!init || windowIsHidden.current)
|
|
680
|
+
return;
|
|
681
|
+
firstItemReached.current = false;
|
|
682
|
+
lastItemReached.current = false;
|
|
683
|
+
const itemIndex = findItemIndex(id, "The item you want to slide to doesn't exist; check the provided id.");
|
|
684
|
+
if (itemIndex === activeItem.current && !shouldReset) {
|
|
685
|
+
return;
|
|
686
|
+
}
|
|
687
|
+
const currentItem = findItemIndex(items[activeItem.current].id);
|
|
688
|
+
const newActiveItem = findItemIndex(items[itemIndex].id);
|
|
689
|
+
if (newActiveItem > currentItem) {
|
|
690
|
+
slideToNextItem({
|
|
691
|
+
type: type || shouldReset ? 'initial' : 'click',
|
|
692
|
+
index: newActiveItem,
|
|
693
|
+
immediate,
|
|
694
|
+
});
|
|
695
|
+
}
|
|
696
|
+
else {
|
|
697
|
+
slideToPrevItem({
|
|
698
|
+
type: type || shouldReset ? 'initial' : 'click',
|
|
699
|
+
index: newActiveItem,
|
|
700
|
+
immediate,
|
|
701
|
+
});
|
|
702
|
+
}
|
|
703
|
+
}
|
|
704
|
+
function getIsNextItem(id) {
|
|
705
|
+
const itemIndex = findItemIndex(id, "The item doesn't exist; check the provided id.");
|
|
706
|
+
const _activeItem = activeItem.current;
|
|
707
|
+
if (withLoop && _activeItem === items.length - 1) {
|
|
708
|
+
return itemIndex === 0;
|
|
709
|
+
}
|
|
710
|
+
return itemIndex === _activeItem + 1;
|
|
711
|
+
}
|
|
712
|
+
function getIsPrevItem(id) {
|
|
713
|
+
const itemIndex = findItemIndex(id, "The item doesn't exist; check the provided id.");
|
|
714
|
+
const _activeItem = activeItem.current;
|
|
715
|
+
if (withLoop && _activeItem === 0) {
|
|
716
|
+
return itemIndex === items.length - 1;
|
|
717
|
+
}
|
|
718
|
+
return itemIndex === _activeItem - 1;
|
|
719
|
+
}
|
|
720
|
+
function getIsActiveItem(id) {
|
|
721
|
+
return (findItemIndex(id, "The item you want to check doesn't exist; check the provided id.") ===
|
|
722
|
+
activeItem.current);
|
|
723
|
+
}
|
|
724
|
+
function getTouchAction() {
|
|
725
|
+
if (disableGestures) {
|
|
726
|
+
return 'unset';
|
|
727
|
+
}
|
|
728
|
+
if (carouselSlideAxis === 'x') {
|
|
729
|
+
return 'pan-y';
|
|
730
|
+
}
|
|
731
|
+
return 'pan-x';
|
|
732
|
+
}
|
|
733
|
+
const res = freeScroll
|
|
734
|
+
? {
|
|
735
|
+
useListenToCustomEvent,
|
|
736
|
+
enterFullscreen,
|
|
737
|
+
exitFullscreen,
|
|
738
|
+
getIsFullscreen,
|
|
739
|
+
slideToPrevItem: (animate = true) => {
|
|
740
|
+
slideToPrevItem({
|
|
741
|
+
type: 'click',
|
|
742
|
+
immediate: !animate,
|
|
743
|
+
});
|
|
744
|
+
},
|
|
745
|
+
slideToNextItem: (animate = true) => {
|
|
746
|
+
slideToNextItem({
|
|
747
|
+
type: 'click',
|
|
748
|
+
immediate: !animate,
|
|
749
|
+
});
|
|
750
|
+
},
|
|
751
|
+
}
|
|
752
|
+
: {
|
|
753
|
+
useListenToCustomEvent,
|
|
754
|
+
enterFullscreen,
|
|
755
|
+
exitFullscreen,
|
|
756
|
+
getIsFullscreen,
|
|
757
|
+
slideToPrevItem: (animate = true) => {
|
|
758
|
+
slideToPrevItem({
|
|
759
|
+
type: 'click',
|
|
760
|
+
immediate: !animate,
|
|
761
|
+
});
|
|
762
|
+
},
|
|
763
|
+
slideToNextItem: (animate = true) => {
|
|
764
|
+
slideToNextItem({
|
|
765
|
+
type: 'click',
|
|
766
|
+
immediate: !animate,
|
|
767
|
+
});
|
|
768
|
+
},
|
|
769
|
+
slideToItem: (id, animate = true) => {
|
|
770
|
+
internalSlideToItem({ id, immediate: !animate });
|
|
771
|
+
},
|
|
772
|
+
getIsNextItem,
|
|
773
|
+
getIsPrevItem,
|
|
774
|
+
getIsActiveItem,
|
|
775
|
+
};
|
|
776
|
+
// uwc-debug-below
|
|
777
|
+
useIsomorphicLayoutEffect(() => {
|
|
778
|
+
/**
|
|
779
|
+
* Set initial track position
|
|
780
|
+
*/
|
|
781
|
+
if (carouselTrackWrapperRef.current && init) {
|
|
782
|
+
resizeByPropChange.current = true;
|
|
783
|
+
initializeCarousel();
|
|
784
|
+
}
|
|
785
|
+
}, [init]);
|
|
786
|
+
useEffect(() => {
|
|
787
|
+
if (activeItem.current !== initialActiveItem) {
|
|
788
|
+
internalSlideToItem({
|
|
789
|
+
id: initialActiveItem,
|
|
790
|
+
immediate: !animateWhenActiveItemChange,
|
|
791
|
+
});
|
|
792
|
+
}
|
|
793
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
794
|
+
}, [initialActiveItem]);
|
|
795
|
+
useEffect(() => {
|
|
796
|
+
if (init) {
|
|
797
|
+
if (initialActiveItem > items.length - 1) {
|
|
798
|
+
throw new Error(`initialActiveItem (${initialActiveItem}) is greater than the total quantity available items (${items.length}).`);
|
|
799
|
+
}
|
|
800
|
+
if (itemsPerSlide > items.length) {
|
|
801
|
+
console.warn(`itemsPerSlide (${itemsPerSlide}) is greater than the total quantity available items (${items.length}). Fallback to ${items.length})`);
|
|
802
|
+
}
|
|
803
|
+
}
|
|
804
|
+
}, [initialActiveItem, items.length, itemsPerSlide, init]);
|
|
805
|
+
useEffect(() => {
|
|
806
|
+
prevWindowWidth.current = window.innerWidth;
|
|
807
|
+
}, []);
|
|
808
|
+
useEffect(() => {
|
|
809
|
+
/**
|
|
810
|
+
* When these props change we reset the carousel
|
|
811
|
+
*/
|
|
812
|
+
if (init) {
|
|
813
|
+
resizeByPropChange.current = true;
|
|
814
|
+
initializeCarousel();
|
|
815
|
+
}
|
|
816
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
817
|
+
}, [initialStartingPosition, itemsPerSlide, startEndGutter, gutter, init, withLoop]);
|
|
818
|
+
useEffect(() => {
|
|
819
|
+
if (!init)
|
|
820
|
+
return;
|
|
821
|
+
function handleVisibilityChange() {
|
|
822
|
+
if (document.hidden) {
|
|
823
|
+
windowIsHidden.current = true;
|
|
824
|
+
}
|
|
825
|
+
else {
|
|
826
|
+
windowIsHidden.current = false;
|
|
827
|
+
}
|
|
828
|
+
}
|
|
829
|
+
document.addEventListener('visibilitychange', handleVisibilityChange);
|
|
830
|
+
return () => {
|
|
831
|
+
document.removeEventListener('visibilitychange', handleVisibilityChange);
|
|
832
|
+
};
|
|
833
|
+
}, [init]);
|
|
834
|
+
useEffect(() => {
|
|
835
|
+
if (mainCarouselWrapperRef.current) {
|
|
836
|
+
let timer;
|
|
837
|
+
const observer = new ResizeObserver(() => {
|
|
838
|
+
if (isFirstMount.current) {
|
|
839
|
+
isFirstMount.current = false;
|
|
840
|
+
return;
|
|
841
|
+
}
|
|
842
|
+
if (windowIsHidden.current)
|
|
843
|
+
return;
|
|
844
|
+
if (!resizeByPropChange.current) {
|
|
845
|
+
prevWindowWidth.current = window.innerWidth;
|
|
846
|
+
const cb = adjustCarouselWrapperPosition();
|
|
847
|
+
window.clearTimeout(timer);
|
|
848
|
+
timer = setTimeout(() => {
|
|
849
|
+
prevTotalScrollValue.current = getTotalScrollValue();
|
|
850
|
+
if (typeof cb === 'function') {
|
|
851
|
+
cb();
|
|
852
|
+
}
|
|
853
|
+
}, 100);
|
|
854
|
+
}
|
|
855
|
+
});
|
|
856
|
+
observer.observe(mainCarouselWrapperRef.current);
|
|
857
|
+
return () => {
|
|
858
|
+
observer.disconnect();
|
|
859
|
+
};
|
|
860
|
+
}
|
|
861
|
+
}, [adjustCarouselWrapperPosition, getTotalScrollValue]);
|
|
862
|
+
useEffect(() => {
|
|
863
|
+
if (getControllerRef) {
|
|
864
|
+
getControllerRef({
|
|
865
|
+
slideToNextItem: res.slideToNextItem,
|
|
866
|
+
slideToPrevItem: res.slideToPrevItem,
|
|
867
|
+
slideToItem: res?.slideToItem,
|
|
868
|
+
});
|
|
869
|
+
}
|
|
870
|
+
}, [getControllerRef, res.slideToItem, res.slideToNextItem, res.slideToPrevItem]);
|
|
871
|
+
const _thumbsFragment = _jsx(Context.Provider, { value: res, children: thumbsFragment });
|
|
872
|
+
const carouselFragment = (_jsx(Context.Provider, { value: res, children: _jsx("div", { className: "use-spring-carousel-main-wrapper", ref: mainCarouselWrapperRef, ...getScrollHandlers(), style: {
|
|
873
|
+
display: 'flex',
|
|
874
|
+
position: 'relative',
|
|
875
|
+
width: '100%',
|
|
876
|
+
height: '100%',
|
|
877
|
+
...getWrapperOverflowStyles(),
|
|
878
|
+
}, children: _jsxs("div", { className: "use-spring-carousel-track-wrapper", ref: carouselTrackWrapperRef, ...bindDrag(), style: {
|
|
879
|
+
position: 'relative',
|
|
880
|
+
display: 'flex',
|
|
881
|
+
flexDirection: carouselSlideAxis === 'x' ? 'row' : 'column',
|
|
882
|
+
touchAction: getTouchAction(),
|
|
883
|
+
...getAnimatedWrapperStyles(),
|
|
884
|
+
}, children: [(freeScroll || !withLoop) && startEndGutter ? (_jsx("div", { style: {
|
|
885
|
+
flexShrink: 0,
|
|
886
|
+
width: startEndGutter,
|
|
887
|
+
} })) : null, internalItems.map((item, index) => {
|
|
888
|
+
return (_jsx("div", { className: "use-spring-carousel-item", "data-testid": "use-spring-carousel-item-wrapper", style: {
|
|
889
|
+
display: 'flex',
|
|
890
|
+
position: 'relative',
|
|
891
|
+
flex: '1',
|
|
892
|
+
...getItemStyles(index ===
|
|
893
|
+
internalItems.findIndex((i) => i.id === internalItems[internalItems.length - 1].id)),
|
|
894
|
+
}, children: typeof item.renderItem === 'function'
|
|
895
|
+
? item.renderItem({
|
|
896
|
+
getIsActiveItem,
|
|
897
|
+
getIsNextItem,
|
|
898
|
+
getIsPrevItem,
|
|
899
|
+
useListenToCustomEvent,
|
|
900
|
+
})
|
|
901
|
+
: item.renderItem }, `${item.id}-${index}`));
|
|
902
|
+
}), (freeScroll || !withLoop) && startEndGutter ? (_jsx("div", { style: {
|
|
903
|
+
flexShrink: 0,
|
|
904
|
+
width: startEndGutter,
|
|
905
|
+
} })) : null] }) }) }));
|
|
906
|
+
return { ...res, carouselFragment, thumbsFragment: _thumbsFragment };
|
|
907
|
+
}
|
|
908
|
+
const Context = createContext(undefined);
|
|
909
|
+
function useSpringCarouselContext() {
|
|
910
|
+
const context = useContext(Context);
|
|
911
|
+
if (!context) {
|
|
912
|
+
throw new Error('useSpringCarouselContext must be used within the carousel.');
|
|
913
|
+
}
|
|
914
|
+
return context;
|
|
915
|
+
}
|
|
916
|
+
export { useSpringCarousel, useSpringCarouselContext };
|
|
917
|
+
//# sourceMappingURL=useSpringCarousel.js.map
|