react-spring-carousel 1.9.29-beta1 → 1.9.29-beta2
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/index.es.js +1 -1194
- package/dist/index.es.js.map +1 -1
- package/dist/index.umd.js +1 -1204
- package/dist/index.umd.js.map +1 -1
- package/package.json +1 -1
package/dist/index.es.js
CHANGED
|
@@ -1,1195 +1,2 @@
|
|
|
1
|
-
import { jsx } from 'react/jsx-runtime';
|
|
2
|
-
import { useRef, useEffect, createContext, useCallback, useContext, useState } from 'react';
|
|
3
|
-
import { useSpring, animated, config, useTransition } from 'react-spring';
|
|
4
|
-
import { useDrag } from '@use-gesture/react';
|
|
5
|
-
import { Subject } from 'rxjs';
|
|
6
|
-
import screenfull from 'screenfull';
|
|
7
|
-
|
|
8
|
-
function useCustomEventsModule() {
|
|
9
|
-
const eventsObserverRef = useRef(new Subject());
|
|
10
|
-
function useListenToCustomEvent(fn) {
|
|
11
|
-
useEffect(() => {
|
|
12
|
-
const subscribe = eventsObserverRef.current.subscribe(fn);
|
|
13
|
-
return () => subscribe.unsubscribe();
|
|
14
|
-
}, [fn]);
|
|
15
|
-
}
|
|
16
|
-
const emitObservable = data => {
|
|
17
|
-
eventsObserverRef.current.next(data);
|
|
18
|
-
};
|
|
19
|
-
return {
|
|
20
|
-
useListenToCustomEvent,
|
|
21
|
-
emitObservable,
|
|
22
|
-
};
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
function useFullscreenModule({ mainCarouselWrapperRef, emitObservable, handleResize, }) {
|
|
26
|
-
const isFullscreen = useRef(false);
|
|
27
|
-
useEffect(() => {
|
|
28
|
-
function handleFullscreenChange() {
|
|
29
|
-
if (document.fullscreenElement) {
|
|
30
|
-
setIsFullscreen(true);
|
|
31
|
-
emitObservable({
|
|
32
|
-
eventName: 'onFullscreenChange',
|
|
33
|
-
isFullscreen: true,
|
|
34
|
-
});
|
|
35
|
-
handleResize && handleResize();
|
|
36
|
-
}
|
|
37
|
-
if (!document.fullscreenElement) {
|
|
38
|
-
setIsFullscreen(false);
|
|
39
|
-
emitObservable({
|
|
40
|
-
eventName: 'onFullscreenChange',
|
|
41
|
-
isFullscreen: false,
|
|
42
|
-
});
|
|
43
|
-
handleResize && handleResize();
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
if (screenfull.isEnabled) {
|
|
47
|
-
screenfull.on('change', handleFullscreenChange);
|
|
48
|
-
return () => {
|
|
49
|
-
if (screenfull.isEnabled) {
|
|
50
|
-
screenfull.off('change', handleFullscreenChange);
|
|
51
|
-
}
|
|
52
|
-
};
|
|
53
|
-
}
|
|
54
|
-
});
|
|
55
|
-
function setIsFullscreen(_isFullscreen) {
|
|
56
|
-
isFullscreen.current = _isFullscreen;
|
|
57
|
-
}
|
|
58
|
-
function getIsFullscreen() {
|
|
59
|
-
return isFullscreen.current;
|
|
60
|
-
}
|
|
61
|
-
function enterFullscreen(elementRef) {
|
|
62
|
-
if (screenfull.isEnabled) {
|
|
63
|
-
screenfull.request((elementRef || mainCarouselWrapperRef.current));
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
function exitFullscreen() {
|
|
67
|
-
screenfull.isEnabled && screenfull.exit();
|
|
68
|
-
}
|
|
69
|
-
return {
|
|
70
|
-
enterFullscreen,
|
|
71
|
-
exitFullscreen,
|
|
72
|
-
getIsFullscreen,
|
|
73
|
-
};
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
function useMount(callback) {
|
|
77
|
-
const isMounted = useRef(false);
|
|
78
|
-
useEffect(() => {
|
|
79
|
-
if (!isMounted.current) {
|
|
80
|
-
const clean = callback();
|
|
81
|
-
isMounted.current = true;
|
|
82
|
-
return () => {
|
|
83
|
-
clean && clean();
|
|
84
|
-
};
|
|
85
|
-
}
|
|
86
|
-
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
87
|
-
}, []);
|
|
88
|
-
}
|
|
89
|
-
function getIsBrowser() {
|
|
90
|
-
return typeof window !== 'undefined';
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
function useThumbsModule({ items, withThumbs, thumbsSlideAxis = 'x', springConfig, thumbsWrapperRef, prepareThumbsData, }) {
|
|
94
|
-
const internalThumbsWrapperRef = useRef(null);
|
|
95
|
-
const [thumbListStyles, setThumbListStyles] = useSpring(() => ({
|
|
96
|
-
[thumbsSlideAxis]: 0,
|
|
97
|
-
config: springConfig,
|
|
98
|
-
}));
|
|
99
|
-
useMount(() => {
|
|
100
|
-
if (withThumbs) {
|
|
101
|
-
const missingThumbs = items.some(item => !item.renderThumb);
|
|
102
|
-
if (missingThumbs) {
|
|
103
|
-
throw new Error('The renderThumb property is missing in one or more items. You need to add the renderThumb property to every item of the carousel when the prop withThumbs={true} or eventually set withThumbs={false}.');
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
});
|
|
107
|
-
function handleThumbsScroll(activeItem) {
|
|
108
|
-
function getOffsetDirection() {
|
|
109
|
-
return thumbsSlideAxis === 'x' ? 'offsetLeft' : 'offsetTop';
|
|
110
|
-
}
|
|
111
|
-
function getOffsetDimension() {
|
|
112
|
-
return thumbsSlideAxis === 'x' ? 'offsetWidth' : 'offsetHeight';
|
|
113
|
-
}
|
|
114
|
-
function getScrollDirecton() {
|
|
115
|
-
return thumbsSlideAxis === 'x' ? 'scrollLeft' : 'scrollTop';
|
|
116
|
-
}
|
|
117
|
-
function getThumbNode() {
|
|
118
|
-
return internalThumbsWrapperRef.current.querySelector(`#thumb-${items[activeItem].id}`);
|
|
119
|
-
}
|
|
120
|
-
function getThumbOffsetPosition({ thumbNode, offsetDirection, offsetDimension, }) {
|
|
121
|
-
return thumbNode[offsetDirection] + thumbNode[offsetDimension] / 2;
|
|
122
|
-
}
|
|
123
|
-
function getThumbScrollDimension({ thumbWrapper, offsetDimension, }) {
|
|
124
|
-
return thumbWrapper[offsetDimension] / 2;
|
|
125
|
-
}
|
|
126
|
-
function getScrollFromValue({ thumbWrapper, scrollDirection, }) {
|
|
127
|
-
return thumbWrapper[scrollDirection];
|
|
128
|
-
}
|
|
129
|
-
function getScrollToValue({ thumbWrapper, thumbOffsetPosition, thumbScrollDimension, offsetDimension, }) {
|
|
130
|
-
const scrollDimensionProperty = thumbsSlideAxis === 'x' ? 'scrollWidth' : 'scrollHeight';
|
|
131
|
-
if (activeItem === items.length - 1 ||
|
|
132
|
-
thumbOffsetPosition - thumbScrollDimension >
|
|
133
|
-
thumbWrapper[scrollDimensionProperty] - thumbWrapper[offsetDimension]) {
|
|
134
|
-
return thumbWrapper[scrollDimensionProperty] - thumbWrapper[offsetDimension];
|
|
135
|
-
}
|
|
136
|
-
if (activeItem === 0) {
|
|
137
|
-
return 0;
|
|
138
|
-
}
|
|
139
|
-
return thumbOffsetPosition - thumbScrollDimension;
|
|
140
|
-
}
|
|
141
|
-
if (thumbsWrapperRef && thumbsWrapperRef.current) {
|
|
142
|
-
internalThumbsWrapperRef.current = thumbsWrapperRef.current;
|
|
143
|
-
}
|
|
144
|
-
const thumbNode = getThumbNode();
|
|
145
|
-
if (thumbNode) {
|
|
146
|
-
const thumbWrapper = internalThumbsWrapperRef.current;
|
|
147
|
-
const offsetDirection = getOffsetDirection();
|
|
148
|
-
const offsetDimension = getOffsetDimension();
|
|
149
|
-
const scrollDirection = getScrollDirecton();
|
|
150
|
-
const thumbOffsetPosition = getThumbOffsetPosition({
|
|
151
|
-
thumbNode,
|
|
152
|
-
offsetDimension,
|
|
153
|
-
offsetDirection,
|
|
154
|
-
});
|
|
155
|
-
const thumbScrollDimension = getThumbScrollDimension({
|
|
156
|
-
thumbWrapper,
|
|
157
|
-
offsetDimension,
|
|
158
|
-
});
|
|
159
|
-
setThumbListStyles.start({
|
|
160
|
-
from: {
|
|
161
|
-
[thumbsSlideAxis]: getScrollFromValue({
|
|
162
|
-
thumbWrapper,
|
|
163
|
-
scrollDirection,
|
|
164
|
-
}),
|
|
165
|
-
},
|
|
166
|
-
to: {
|
|
167
|
-
[thumbsSlideAxis]: getScrollToValue({
|
|
168
|
-
thumbWrapper,
|
|
169
|
-
thumbOffsetPosition,
|
|
170
|
-
thumbScrollDimension,
|
|
171
|
-
offsetDimension,
|
|
172
|
-
}),
|
|
173
|
-
},
|
|
174
|
-
onChange: val => {
|
|
175
|
-
if (thumbsSlideAxis === 'x') {
|
|
176
|
-
// @ts-ignore
|
|
177
|
-
internalThumbsWrapperRef.current.scrollLeft = val.x;
|
|
178
|
-
}
|
|
179
|
-
else {
|
|
180
|
-
// @ts-ignore
|
|
181
|
-
internalThumbsWrapperRef.current.scrollTop = val.y;
|
|
182
|
-
}
|
|
183
|
-
},
|
|
184
|
-
});
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
function handlePrepareThumbsDate() {
|
|
188
|
-
function getPreparedItems(_items) {
|
|
189
|
-
return _items.map(i => ({
|
|
190
|
-
id: i.id,
|
|
191
|
-
renderThumb: i.renderThumb,
|
|
192
|
-
}));
|
|
193
|
-
}
|
|
194
|
-
if (prepareThumbsData) {
|
|
195
|
-
return prepareThumbsData(getPreparedItems(items));
|
|
196
|
-
}
|
|
197
|
-
return getPreparedItems(items);
|
|
198
|
-
}
|
|
199
|
-
function getScrollDirectionSpringValue() {
|
|
200
|
-
if (thumbsSlideAxis === 'x') {
|
|
201
|
-
return {
|
|
202
|
-
// @ts-ignore
|
|
203
|
-
scrollLeft: thumbListStyles.x,
|
|
204
|
-
};
|
|
205
|
-
}
|
|
206
|
-
return {
|
|
207
|
-
// @ts-ignore
|
|
208
|
-
scrollTop: thumbListStyles.y,
|
|
209
|
-
};
|
|
210
|
-
}
|
|
211
|
-
const thumbsFragment = withThumbs ? (jsx(animated.div, Object.assign({ ref: internalThumbsWrapperRef }, getScrollDirectionSpringValue(), { style: {
|
|
212
|
-
display: 'flex',
|
|
213
|
-
flex: 1,
|
|
214
|
-
position: 'relative',
|
|
215
|
-
flexDirection: thumbsSlideAxis === 'x' ? 'row' : 'column',
|
|
216
|
-
...(thumbsSlideAxis === 'x'
|
|
217
|
-
? { overflowX: 'auto' }
|
|
218
|
-
: {
|
|
219
|
-
overflowY: 'auto',
|
|
220
|
-
maxHeight: '100%',
|
|
221
|
-
}),
|
|
222
|
-
} }, { children: handlePrepareThumbsDate().map(({ id, renderThumb }) => {
|
|
223
|
-
const thumbId = `thumb-${id}`;
|
|
224
|
-
return (jsx("div", Object.assign({ id: thumbId }, { children: renderThumb }), thumbId));
|
|
225
|
-
}) }), void 0)) : null;
|
|
226
|
-
return {
|
|
227
|
-
thumbsFragment,
|
|
228
|
-
handleThumbsScroll,
|
|
229
|
-
};
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
const UseSpringCarouselContext = createContext(undefined);
|
|
233
|
-
function useSpringCarouselContext() {
|
|
234
|
-
const context = useContext(UseSpringCarouselContext);
|
|
235
|
-
if (!context) {
|
|
236
|
-
throw new Error(`useSpringCarouselContext isn't being used within the useSringCarousel context;
|
|
237
|
-
use the context only inside a component that is rendered within the Carousel.`);
|
|
238
|
-
}
|
|
239
|
-
return context;
|
|
240
|
-
}
|
|
241
|
-
function useSpringCarousel({ items, withLoop = false, draggingSlideTreshold = 140, springConfig = config.default, shouldResizeOnWindowResize = true, withThumbs = false, enableThumbsWrapperScroll = true, carouselSlideAxis = 'x', thumbsSlideAxis = 'x', thumbsWrapperRef, prepareThumbsData, itemsPerSlide = 1, initialActiveItem = 0, initialStartingPosition = 'start', disableGestures = false, gutter = 0, startEndGutter = 0, touchAction = 'none', slideAmount, }) {
|
|
242
|
-
function getItems() {
|
|
243
|
-
if (withLoop) {
|
|
244
|
-
return [...items, ...items, ...items];
|
|
245
|
-
}
|
|
246
|
-
return items;
|
|
247
|
-
}
|
|
248
|
-
const slideActionType = useRef('next');
|
|
249
|
-
const internalItems = getItems();
|
|
250
|
-
const activeItem = useRef(initialActiveItem);
|
|
251
|
-
const mainCarouselWrapperRef = useRef(null);
|
|
252
|
-
const carouselTrackWrapperRef = useRef(null);
|
|
253
|
-
const isDragging = useRef(false);
|
|
254
|
-
const isAnimating = useRef(false);
|
|
255
|
-
const windowIsHidden = useRef(false);
|
|
256
|
-
const currentWindowWidth = useRef(0);
|
|
257
|
-
const fluidTotalWrapperScrollValue = useRef(0);
|
|
258
|
-
const slideFluidEndReached = useRef(false);
|
|
259
|
-
const currentSlidedValue = useRef(0);
|
|
260
|
-
const currentStepSlideValue = useRef(0);
|
|
261
|
-
function getCarouselItem() {
|
|
262
|
-
return carouselTrackWrapperRef.current?.querySelector('.use-spring-carousel-item');
|
|
263
|
-
}
|
|
264
|
-
const getFluidWrapperScrollValue = useCallback(() => {
|
|
265
|
-
return Math.round(Number(carouselTrackWrapperRef.current?.[carouselSlideAxis === 'x' ? 'scrollWidth' : 'scrollHeight']) -
|
|
266
|
-
carouselTrackWrapperRef.current.getBoundingClientRect()[carouselSlideAxis === 'x' ? 'width' : 'height']);
|
|
267
|
-
}, [carouselSlideAxis]);
|
|
268
|
-
const [carouselStyles, setCarouselStyles] = useSpring(() => ({
|
|
269
|
-
y: 0,
|
|
270
|
-
x: 0,
|
|
271
|
-
config: springConfig,
|
|
272
|
-
onRest: ({ value }) => {
|
|
273
|
-
currentSlidedValue.current = value[carouselSlideAxis];
|
|
274
|
-
currentStepSlideValue.current = value[carouselSlideAxis];
|
|
275
|
-
},
|
|
276
|
-
}));
|
|
277
|
-
const getSlideValue = useCallback(() => {
|
|
278
|
-
if (!carouselTrackWrapperRef.current) {
|
|
279
|
-
return 0;
|
|
280
|
-
}
|
|
281
|
-
const carouselItem = getCarouselItem();
|
|
282
|
-
if (!carouselItem) {
|
|
283
|
-
throw Error('No carousel items available!');
|
|
284
|
-
}
|
|
285
|
-
const itemVal = carouselItem.getBoundingClientRect()[carouselSlideAxis === 'x' ? 'width' : 'height'] + gutter;
|
|
286
|
-
if (itemsPerSlide === 'fluid' && typeof slideAmount === 'number') {
|
|
287
|
-
if (slideAmount < itemVal) {
|
|
288
|
-
throw new Error('slideAmount must be greater than the width of a single item.');
|
|
289
|
-
}
|
|
290
|
-
return slideAmount;
|
|
291
|
-
}
|
|
292
|
-
return itemVal;
|
|
293
|
-
}, [carouselSlideAxis, gutter, itemsPerSlide, slideAmount]);
|
|
294
|
-
const adjustCarouselWrapperPosition = useCallback((ref) => {
|
|
295
|
-
if (itemsPerSlide !== 'fluid' && typeof itemsPerSlide === 'number') {
|
|
296
|
-
const positionProperty = carouselSlideAxis === 'x' ? 'left' : 'top';
|
|
297
|
-
function getDefaultPositionValue() {
|
|
298
|
-
return getSlideValue() * items.length;
|
|
299
|
-
}
|
|
300
|
-
function setPosition(v) {
|
|
301
|
-
ref.style.top = '0px';
|
|
302
|
-
ref.style.left = '0px';
|
|
303
|
-
ref.style[positionProperty] = `-${v - startEndGutter}px`;
|
|
304
|
-
}
|
|
305
|
-
function setStartPosition() {
|
|
306
|
-
setPosition(getDefaultPositionValue());
|
|
307
|
-
}
|
|
308
|
-
function setCenterPosition() {
|
|
309
|
-
setPosition(getDefaultPositionValue() -
|
|
310
|
-
getSlideValue() * Math.round((itemsPerSlide - 1) / 2));
|
|
311
|
-
}
|
|
312
|
-
function setEndPosition() {
|
|
313
|
-
setPosition(getDefaultPositionValue() -
|
|
314
|
-
getSlideValue() * Math.round(itemsPerSlide - 1));
|
|
315
|
-
}
|
|
316
|
-
if (itemsPerSlide > 1) {
|
|
317
|
-
switch (initialStartingPosition) {
|
|
318
|
-
default:
|
|
319
|
-
case 'start': {
|
|
320
|
-
setStartPosition();
|
|
321
|
-
break;
|
|
322
|
-
}
|
|
323
|
-
case 'center': {
|
|
324
|
-
setCenterPosition();
|
|
325
|
-
break;
|
|
326
|
-
}
|
|
327
|
-
case 'end': {
|
|
328
|
-
setEndPosition();
|
|
329
|
-
break;
|
|
330
|
-
}
|
|
331
|
-
}
|
|
332
|
-
}
|
|
333
|
-
else {
|
|
334
|
-
setStartPosition();
|
|
335
|
-
}
|
|
336
|
-
}
|
|
337
|
-
}, [
|
|
338
|
-
startEndGutter,
|
|
339
|
-
carouselSlideAxis,
|
|
340
|
-
getSlideValue,
|
|
341
|
-
initialStartingPosition,
|
|
342
|
-
items.length,
|
|
343
|
-
itemsPerSlide,
|
|
344
|
-
]);
|
|
345
|
-
const handleResize = useCallback(() => {
|
|
346
|
-
if (window.innerWidth === currentWindowWidth.current) {
|
|
347
|
-
return;
|
|
348
|
-
}
|
|
349
|
-
currentWindowWidth.current = window.innerWidth;
|
|
350
|
-
if (itemsPerSlide === 'fluid') {
|
|
351
|
-
fluidTotalWrapperScrollValue.current = getFluidWrapperScrollValue();
|
|
352
|
-
if (slideFluidEndReached.current) {
|
|
353
|
-
setCarouselStyles.start({
|
|
354
|
-
immediate: true,
|
|
355
|
-
[carouselSlideAxis]: -fluidTotalWrapperScrollValue.current,
|
|
356
|
-
});
|
|
357
|
-
}
|
|
358
|
-
else {
|
|
359
|
-
const lastItem = document.querySelector('.use-spring-carousel-item:last-of-type');
|
|
360
|
-
console.log({
|
|
361
|
-
lastItem: lastItem.offsetLeft,
|
|
362
|
-
});
|
|
363
|
-
}
|
|
364
|
-
}
|
|
365
|
-
else {
|
|
366
|
-
setCarouselStyles.start({
|
|
367
|
-
immediate: true,
|
|
368
|
-
x: 0,
|
|
369
|
-
y: 0,
|
|
370
|
-
});
|
|
371
|
-
setCarouselStyles.start({
|
|
372
|
-
immediate: true,
|
|
373
|
-
[carouselSlideAxis]: -(getSlideValue() * getCurrentActiveItem()),
|
|
374
|
-
});
|
|
375
|
-
}
|
|
376
|
-
if (withLoop) {
|
|
377
|
-
adjustCarouselWrapperPosition(carouselTrackWrapperRef.current);
|
|
378
|
-
}
|
|
379
|
-
}, [
|
|
380
|
-
adjustCarouselWrapperPosition,
|
|
381
|
-
carouselSlideAxis,
|
|
382
|
-
getSlideValue,
|
|
383
|
-
setCarouselStyles,
|
|
384
|
-
withLoop,
|
|
385
|
-
itemsPerSlide,
|
|
386
|
-
getFluidWrapperScrollValue,
|
|
387
|
-
]);
|
|
388
|
-
// Custom modules
|
|
389
|
-
const { useListenToCustomEvent, emitObservable } = useCustomEventsModule();
|
|
390
|
-
const { enterFullscreen, exitFullscreen, getIsFullscreen } = useFullscreenModule({
|
|
391
|
-
mainCarouselWrapperRef,
|
|
392
|
-
emitObservable,
|
|
393
|
-
handleResize,
|
|
394
|
-
});
|
|
395
|
-
const { thumbsFragment: _thumbsFragment, handleThumbsScroll } = useThumbsModule({
|
|
396
|
-
withThumbs,
|
|
397
|
-
items,
|
|
398
|
-
thumbsSlideAxis,
|
|
399
|
-
springConfig,
|
|
400
|
-
thumbsWrapperRef,
|
|
401
|
-
prepareThumbsData,
|
|
402
|
-
});
|
|
403
|
-
function getCurrentSlidedValue() {
|
|
404
|
-
return carouselStyles[carouselSlideAxis].get();
|
|
405
|
-
}
|
|
406
|
-
const bindDrag = useDrag(props => {
|
|
407
|
-
const isDragging = props.dragging;
|
|
408
|
-
const movement = props.movement[carouselSlideAxis === 'x' ? 0 : 1];
|
|
409
|
-
function resetAnimation() {
|
|
410
|
-
if (itemsPerSlide === 'fluid') {
|
|
411
|
-
if (getIsFirstItem()) {
|
|
412
|
-
slideToPrevItem();
|
|
413
|
-
}
|
|
414
|
-
else if (slideFluidEndReached.current) {
|
|
415
|
-
setCarouselStyles.start({
|
|
416
|
-
[carouselSlideAxis]: -fluidTotalWrapperScrollValue.current,
|
|
417
|
-
});
|
|
418
|
-
}
|
|
419
|
-
else {
|
|
420
|
-
setCarouselStyles.start({
|
|
421
|
-
[carouselSlideAxis]: currentStepSlideValue.current,
|
|
422
|
-
});
|
|
423
|
-
}
|
|
424
|
-
}
|
|
425
|
-
else {
|
|
426
|
-
setCarouselStyles.start({
|
|
427
|
-
[carouselSlideAxis]: -(getCurrentActiveItem() * getSlideValue()),
|
|
428
|
-
});
|
|
429
|
-
}
|
|
430
|
-
}
|
|
431
|
-
if (props.first) {
|
|
432
|
-
currentSlidedValue.current = getCurrentSlidedValue();
|
|
433
|
-
}
|
|
434
|
-
if (isDragging) {
|
|
435
|
-
setIsDragging(true);
|
|
436
|
-
emitObservable({
|
|
437
|
-
eventName: 'onDrag',
|
|
438
|
-
...props,
|
|
439
|
-
});
|
|
440
|
-
setCarouselStyles.start({
|
|
441
|
-
[carouselSlideAxis]: currentSlidedValue.current + movement,
|
|
442
|
-
});
|
|
443
|
-
const prevItemTreshold = movement > draggingSlideTreshold;
|
|
444
|
-
const nextItemTreshold = movement < -draggingSlideTreshold;
|
|
445
|
-
if (mainCarouselWrapperRef.current.getBoundingClientRect().width >=
|
|
446
|
-
items.length * getSlideValue()) {
|
|
447
|
-
slideFluidEndReached.current = true;
|
|
448
|
-
}
|
|
449
|
-
if (slideFluidEndReached.current && movement < 0) {
|
|
450
|
-
if (nextItemTreshold) {
|
|
451
|
-
props.cancel();
|
|
452
|
-
setCarouselStyles.start({
|
|
453
|
-
[carouselSlideAxis]: -fluidTotalWrapperScrollValue.current,
|
|
454
|
-
});
|
|
455
|
-
}
|
|
456
|
-
}
|
|
457
|
-
else if (nextItemTreshold) {
|
|
458
|
-
props.cancel();
|
|
459
|
-
if (!withLoop && getIsLastItem()) {
|
|
460
|
-
resetAnimation();
|
|
461
|
-
}
|
|
462
|
-
else {
|
|
463
|
-
slideToNextItem();
|
|
464
|
-
}
|
|
465
|
-
}
|
|
466
|
-
else if (prevItemTreshold) {
|
|
467
|
-
props.cancel();
|
|
468
|
-
if (!withLoop && getIsFirstItem()) {
|
|
469
|
-
resetAnimation();
|
|
470
|
-
}
|
|
471
|
-
else {
|
|
472
|
-
slideToPrevItem();
|
|
473
|
-
}
|
|
474
|
-
}
|
|
475
|
-
}
|
|
476
|
-
if (props.last && !props.pressed) {
|
|
477
|
-
resetAnimation();
|
|
478
|
-
}
|
|
479
|
-
}, {
|
|
480
|
-
enabled: !disableGestures,
|
|
481
|
-
});
|
|
482
|
-
// Perform some check on first mount
|
|
483
|
-
useMount(() => {
|
|
484
|
-
if (itemsPerSlide !== 'fluid' && !Number.isInteger(itemsPerSlide)) {
|
|
485
|
-
throw new Error(`itemsPerSlide should be an integer.`);
|
|
486
|
-
}
|
|
487
|
-
if (itemsPerSlide > items.length) {
|
|
488
|
-
throw new Error(`The itemsPerSlide prop can't be greater than the total length of the items you provide.`);
|
|
489
|
-
}
|
|
490
|
-
if (itemsPerSlide < 1) {
|
|
491
|
-
throw new Error(`The itemsPerSlide prop can't be less than 1.`);
|
|
492
|
-
}
|
|
493
|
-
if (!shouldResizeOnWindowResize) {
|
|
494
|
-
console.warn('You set shouldResizeOnWindowResize={false}; be aware that the carousel could behave in a strange way if you also use the fullscreen functionality or if you change the mobile orientation.');
|
|
495
|
-
}
|
|
496
|
-
if (initialActiveItem < 0) {
|
|
497
|
-
console.warn('The initialActiveItem cannot be less than 0.');
|
|
498
|
-
}
|
|
499
|
-
if (initialActiveItem > items.length) {
|
|
500
|
-
console.warn('The initialActiveItem cannot be greater than the total length of the items you provide.');
|
|
501
|
-
}
|
|
502
|
-
});
|
|
503
|
-
useMount(() => {
|
|
504
|
-
fluidTotalWrapperScrollValue.current = getFluidWrapperScrollValue();
|
|
505
|
-
function handleVisibilityChange() {
|
|
506
|
-
if (document.hidden) {
|
|
507
|
-
windowIsHidden.current = true;
|
|
508
|
-
}
|
|
509
|
-
else {
|
|
510
|
-
windowIsHidden.current = false;
|
|
511
|
-
}
|
|
512
|
-
}
|
|
513
|
-
if (getIsBrowser()) {
|
|
514
|
-
document.addEventListener('visibilitychange', handleVisibilityChange);
|
|
515
|
-
return () => {
|
|
516
|
-
document.removeEventListener('visibilitychange', handleVisibilityChange);
|
|
517
|
-
};
|
|
518
|
-
}
|
|
519
|
-
});
|
|
520
|
-
useMount(() => {
|
|
521
|
-
if (initialActiveItem > 0 && initialActiveItem <= items.length) {
|
|
522
|
-
slideToItem({
|
|
523
|
-
to: initialActiveItem,
|
|
524
|
-
immediate: true,
|
|
525
|
-
});
|
|
526
|
-
setActiveItem(initialActiveItem);
|
|
527
|
-
}
|
|
528
|
-
});
|
|
529
|
-
useEffect(() => {
|
|
530
|
-
if (shouldResizeOnWindowResize) {
|
|
531
|
-
window.addEventListener('resize', handleResize);
|
|
532
|
-
return () => {
|
|
533
|
-
window.removeEventListener('resize', handleResize);
|
|
534
|
-
};
|
|
535
|
-
}
|
|
536
|
-
}, [handleResize, shouldResizeOnWindowResize]);
|
|
537
|
-
useEffect(() => {
|
|
538
|
-
if (carouselTrackWrapperRef.current) {
|
|
539
|
-
if (carouselSlideAxis === 'x') {
|
|
540
|
-
carouselTrackWrapperRef.current.style.top = '0px';
|
|
541
|
-
}
|
|
542
|
-
if (carouselSlideAxis === 'y') {
|
|
543
|
-
carouselTrackWrapperRef.current.style.left = '0px';
|
|
544
|
-
}
|
|
545
|
-
}
|
|
546
|
-
}, [carouselSlideAxis]);
|
|
547
|
-
function setSlideActionType(type) {
|
|
548
|
-
slideActionType.current = type;
|
|
549
|
-
}
|
|
550
|
-
function getSlideActionType() {
|
|
551
|
-
return slideActionType.current;
|
|
552
|
-
}
|
|
553
|
-
function setActiveItem(newItem) {
|
|
554
|
-
activeItem.current = newItem;
|
|
555
|
-
}
|
|
556
|
-
function getCurrentActiveItem() {
|
|
557
|
-
return activeItem.current;
|
|
558
|
-
}
|
|
559
|
-
function getIsAnimating() {
|
|
560
|
-
return isAnimating.current;
|
|
561
|
-
}
|
|
562
|
-
function setIsAnimating(val) {
|
|
563
|
-
isAnimating.current = val;
|
|
564
|
-
}
|
|
565
|
-
function setIsDragging(val) {
|
|
566
|
-
isDragging.current = val;
|
|
567
|
-
}
|
|
568
|
-
function getIsDragging() {
|
|
569
|
-
return isDragging.current;
|
|
570
|
-
}
|
|
571
|
-
function getPrevItem() {
|
|
572
|
-
const currentActiveItem = getCurrentActiveItem();
|
|
573
|
-
if (currentActiveItem === 0) {
|
|
574
|
-
return items.length - 1;
|
|
575
|
-
}
|
|
576
|
-
return currentActiveItem - 1;
|
|
577
|
-
}
|
|
578
|
-
function getNextItem() {
|
|
579
|
-
const currentActiveItem = getCurrentActiveItem();
|
|
580
|
-
if (currentActiveItem === items.length - 1) {
|
|
581
|
-
return 0;
|
|
582
|
-
}
|
|
583
|
-
return currentActiveItem + 1;
|
|
584
|
-
}
|
|
585
|
-
function getIsNextItem(id) {
|
|
586
|
-
const itemIndex = findItemIndex(id);
|
|
587
|
-
const activeItem = getCurrentActiveItem();
|
|
588
|
-
if (withLoop && activeItem === items.length - 1) {
|
|
589
|
-
return itemIndex === 0;
|
|
590
|
-
}
|
|
591
|
-
return itemIndex === activeItem + 1;
|
|
592
|
-
}
|
|
593
|
-
function getIsPrevItem(id) {
|
|
594
|
-
const itemIndex = findItemIndex(id);
|
|
595
|
-
const activeItem = getCurrentActiveItem();
|
|
596
|
-
if (withLoop && activeItem === 0) {
|
|
597
|
-
return itemIndex === items.length - 1;
|
|
598
|
-
}
|
|
599
|
-
return itemIndex === activeItem - 1;
|
|
600
|
-
}
|
|
601
|
-
function findItemIndex(id) {
|
|
602
|
-
return items.findIndex(item => item.id === id);
|
|
603
|
-
}
|
|
604
|
-
function slideToItem({ from, to, customTo, immediate = false, onRest = () => { }, }) {
|
|
605
|
-
if (!immediate) {
|
|
606
|
-
setActiveItem(to);
|
|
607
|
-
setIsAnimating(true);
|
|
608
|
-
emitObservable({
|
|
609
|
-
eventName: 'onSlideStartChange',
|
|
610
|
-
nextItem: to,
|
|
611
|
-
slideActionType: getSlideActionType(),
|
|
612
|
-
});
|
|
613
|
-
}
|
|
614
|
-
function getFromValue() {
|
|
615
|
-
if (from) {
|
|
616
|
-
return {
|
|
617
|
-
from: {
|
|
618
|
-
[carouselSlideAxis]: from,
|
|
619
|
-
},
|
|
620
|
-
};
|
|
621
|
-
}
|
|
622
|
-
return {};
|
|
623
|
-
}
|
|
624
|
-
function getToValue() {
|
|
625
|
-
if (customTo) {
|
|
626
|
-
return {
|
|
627
|
-
[carouselSlideAxis]: customTo,
|
|
628
|
-
};
|
|
629
|
-
}
|
|
630
|
-
return {
|
|
631
|
-
[carouselSlideAxis]: -(getSlideValue() * to),
|
|
632
|
-
};
|
|
633
|
-
}
|
|
634
|
-
setCarouselStyles.start({
|
|
635
|
-
...getFromValue(),
|
|
636
|
-
to: getToValue(),
|
|
637
|
-
immediate,
|
|
638
|
-
onRest: val => {
|
|
639
|
-
if (val.finished) {
|
|
640
|
-
setIsDragging(false);
|
|
641
|
-
setIsAnimating(false);
|
|
642
|
-
onRest();
|
|
643
|
-
if (!immediate) {
|
|
644
|
-
emitObservable({
|
|
645
|
-
eventName: 'onSlideChange',
|
|
646
|
-
currentItem: getCurrentActiveItem(),
|
|
647
|
-
slideActionType: getSlideActionType(),
|
|
648
|
-
});
|
|
649
|
-
}
|
|
650
|
-
}
|
|
651
|
-
},
|
|
652
|
-
});
|
|
653
|
-
if (enableThumbsWrapperScroll && withThumbs && !immediate) {
|
|
654
|
-
handleThumbsScroll(to);
|
|
655
|
-
}
|
|
656
|
-
}
|
|
657
|
-
function getWrapperFromValue(element) {
|
|
658
|
-
if (element.style.transform === 'none') {
|
|
659
|
-
return 0;
|
|
660
|
-
}
|
|
661
|
-
const values = element.style.transform.split(/\w+\(|\);?/);
|
|
662
|
-
return Number(values[1].split(/,\s?/g)[carouselSlideAxis === 'x' ? 0 : 1].replace('px', ''));
|
|
663
|
-
}
|
|
664
|
-
function getIsFirstItem() {
|
|
665
|
-
return getCurrentActiveItem() === 0;
|
|
666
|
-
}
|
|
667
|
-
function getIsLastItem() {
|
|
668
|
-
return getCurrentActiveItem() === items.length - 1;
|
|
669
|
-
}
|
|
670
|
-
function slideToPrevItem() {
|
|
671
|
-
if (itemsPerSlide === 'fluid' && !withLoop) {
|
|
672
|
-
const currentSlideVal = getWrapperFromValue(carouselTrackWrapperRef.current);
|
|
673
|
-
const nextPrevValue = currentSlideVal + getSlideValue() + 100;
|
|
674
|
-
if (getIsFirstItem()) {
|
|
675
|
-
slideToItem({
|
|
676
|
-
to: 0,
|
|
677
|
-
});
|
|
678
|
-
return;
|
|
679
|
-
}
|
|
680
|
-
if (nextPrevValue >= 0) {
|
|
681
|
-
slideToItem({
|
|
682
|
-
to: 0,
|
|
683
|
-
});
|
|
684
|
-
currentStepSlideValue.current = 0;
|
|
685
|
-
}
|
|
686
|
-
else {
|
|
687
|
-
const nextVal = currentStepSlideValue.current + getSlideValue();
|
|
688
|
-
currentStepSlideValue.current = nextVal;
|
|
689
|
-
slideToItem({
|
|
690
|
-
to: getPrevItem(),
|
|
691
|
-
customTo: nextVal,
|
|
692
|
-
});
|
|
693
|
-
}
|
|
694
|
-
if (slideFluidEndReached.current) {
|
|
695
|
-
slideFluidEndReached.current = false;
|
|
696
|
-
}
|
|
697
|
-
}
|
|
698
|
-
else if ((!withLoop && getCurrentActiveItem() === 0) || windowIsHidden.current) {
|
|
699
|
-
return;
|
|
700
|
-
}
|
|
701
|
-
else {
|
|
702
|
-
setSlideActionType('prev');
|
|
703
|
-
if (getIsFirstItem()) {
|
|
704
|
-
slideToItem({
|
|
705
|
-
from: -(Math.abs(getWrapperFromValue(carouselTrackWrapperRef.current)) +
|
|
706
|
-
getSlideValue() * items.length),
|
|
707
|
-
to: items.length - 1,
|
|
708
|
-
});
|
|
709
|
-
}
|
|
710
|
-
else {
|
|
711
|
-
slideToItem({
|
|
712
|
-
to: getPrevItem(),
|
|
713
|
-
});
|
|
714
|
-
}
|
|
715
|
-
}
|
|
716
|
-
}
|
|
717
|
-
function slideToNextItem() {
|
|
718
|
-
if (itemsPerSlide === 'fluid' && !withLoop) {
|
|
719
|
-
const willGoAfterLastFluidItem = Math.abs(getNextItem() * getSlideValue()) + 100 >=
|
|
720
|
-
fluidTotalWrapperScrollValue.current;
|
|
721
|
-
function getDefaultNextValue() {
|
|
722
|
-
return -(getNextItem() * getSlideValue());
|
|
723
|
-
}
|
|
724
|
-
console.log(getDefaultNextValue());
|
|
725
|
-
if (mainCarouselWrapperRef.current.getBoundingClientRect().width >=
|
|
726
|
-
items.length * getSlideValue()) {
|
|
727
|
-
slideFluidEndReached.current = true;
|
|
728
|
-
}
|
|
729
|
-
if (slideFluidEndReached.current) {
|
|
730
|
-
return;
|
|
731
|
-
}
|
|
732
|
-
if (willGoAfterLastFluidItem) {
|
|
733
|
-
const nextValue = -fluidTotalWrapperScrollValue.current;
|
|
734
|
-
slideFluidEndReached.current = true;
|
|
735
|
-
currentStepSlideValue.current = nextValue;
|
|
736
|
-
slideToItem({
|
|
737
|
-
customTo: nextValue,
|
|
738
|
-
to: getNextItem(),
|
|
739
|
-
});
|
|
740
|
-
}
|
|
741
|
-
else {
|
|
742
|
-
const isPure = Math.abs(currentStepSlideValue.current % getSlideValue()) === 0;
|
|
743
|
-
if (!isPure) {
|
|
744
|
-
const nextValue = currentStepSlideValue.current - getSlideValue();
|
|
745
|
-
currentStepSlideValue.current = nextValue;
|
|
746
|
-
slideToItem({
|
|
747
|
-
to: getNextItem(),
|
|
748
|
-
customTo: nextValue,
|
|
749
|
-
});
|
|
750
|
-
}
|
|
751
|
-
else {
|
|
752
|
-
currentStepSlideValue.current = getDefaultNextValue();
|
|
753
|
-
slideToItem({
|
|
754
|
-
to: getNextItem(),
|
|
755
|
-
customTo: getDefaultNextValue(),
|
|
756
|
-
});
|
|
757
|
-
}
|
|
758
|
-
}
|
|
759
|
-
}
|
|
760
|
-
else if ((!withLoop && getCurrentActiveItem() === internalItems.length - 1) ||
|
|
761
|
-
windowIsHidden.current) {
|
|
762
|
-
return;
|
|
763
|
-
}
|
|
764
|
-
else {
|
|
765
|
-
setSlideActionType('next');
|
|
766
|
-
if (getIsLastItem()) {
|
|
767
|
-
slideToItem({
|
|
768
|
-
from: getWrapperFromValue(carouselTrackWrapperRef.current) +
|
|
769
|
-
getSlideValue() * items.length,
|
|
770
|
-
to: 0,
|
|
771
|
-
});
|
|
772
|
-
}
|
|
773
|
-
else {
|
|
774
|
-
slideToItem({
|
|
775
|
-
to: getNextItem(),
|
|
776
|
-
});
|
|
777
|
-
}
|
|
778
|
-
}
|
|
779
|
-
}
|
|
780
|
-
function _slideToItem(item) {
|
|
781
|
-
let itemIndex = 0;
|
|
782
|
-
if (typeof item === 'string') {
|
|
783
|
-
itemIndex = items.findIndex(_item => _item.id === item);
|
|
784
|
-
}
|
|
785
|
-
else {
|
|
786
|
-
itemIndex = item;
|
|
787
|
-
}
|
|
788
|
-
if (itemIndex >= items.length) {
|
|
789
|
-
throw Error(`The item you want to slide to doesn't exist. This could be due to the fact that
|
|
790
|
-
you provide a wrong id or a higher numeric index.`);
|
|
791
|
-
}
|
|
792
|
-
if (itemIndex === getCurrentActiveItem()) {
|
|
793
|
-
return;
|
|
794
|
-
}
|
|
795
|
-
const currentItem = findItemIndex(items[getCurrentActiveItem()].id);
|
|
796
|
-
const newActiveItem = findItemIndex(items[itemIndex].id);
|
|
797
|
-
if (newActiveItem > currentItem) {
|
|
798
|
-
setSlideActionType('next');
|
|
799
|
-
}
|
|
800
|
-
else {
|
|
801
|
-
setSlideActionType('prev');
|
|
802
|
-
}
|
|
803
|
-
slideToItem({
|
|
804
|
-
to: itemIndex,
|
|
805
|
-
});
|
|
806
|
-
}
|
|
807
|
-
const contextProps = {
|
|
808
|
-
useListenToCustomEvent,
|
|
809
|
-
getIsFullscreen,
|
|
810
|
-
enterFullscreen,
|
|
811
|
-
exitFullscreen,
|
|
812
|
-
getIsAnimating,
|
|
813
|
-
getIsDragging,
|
|
814
|
-
getIsNextItem,
|
|
815
|
-
getIsPrevItem,
|
|
816
|
-
slideToPrevItem,
|
|
817
|
-
slideToNextItem,
|
|
818
|
-
slideToItem: _slideToItem,
|
|
819
|
-
getIsActiveItem: id => {
|
|
820
|
-
return findItemIndex(id) === getCurrentActiveItem();
|
|
821
|
-
},
|
|
822
|
-
getCurrentActiveItem: () => ({
|
|
823
|
-
id: items[getCurrentActiveItem()].id,
|
|
824
|
-
index: getCurrentActiveItem(),
|
|
825
|
-
}),
|
|
826
|
-
};
|
|
827
|
-
function getItemStyles() {
|
|
828
|
-
if (typeof itemsPerSlide === 'number') {
|
|
829
|
-
return {
|
|
830
|
-
...(carouselSlideAxis === 'x'
|
|
831
|
-
? { marginRight: `${gutter}px` }
|
|
832
|
-
: { marginBottom: `${gutter}px` }),
|
|
833
|
-
flex: `1 0 calc(100% / ${itemsPerSlide} - ${(gutter * (itemsPerSlide - 1)) / itemsPerSlide}px)`,
|
|
834
|
-
};
|
|
835
|
-
}
|
|
836
|
-
return {
|
|
837
|
-
...(carouselSlideAxis === 'x'
|
|
838
|
-
? { marginRight: `${gutter}px` }
|
|
839
|
-
: { marginBottom: `${gutter}px` }),
|
|
840
|
-
};
|
|
841
|
-
}
|
|
842
|
-
function getAnimatedWrapperStyles() {
|
|
843
|
-
const percentValue = `calc(100% - ${startEndGutter * 2}px)`;
|
|
844
|
-
return {
|
|
845
|
-
width: carouselSlideAxis === 'x' ? percentValue : '100%',
|
|
846
|
-
height: carouselSlideAxis === 'y' ? percentValue : '100%',
|
|
847
|
-
};
|
|
848
|
-
}
|
|
849
|
-
const carouselFragment = (jsx(UseSpringCarouselContext.Provider, Object.assign({ value: contextProps }, { children: jsx("div", Object.assign({ ref: mainCarouselWrapperRef, "data-testid": "use-spring-carousel-wrapper", style: {
|
|
850
|
-
display: 'flex',
|
|
851
|
-
position: 'relative',
|
|
852
|
-
width: '100%',
|
|
853
|
-
height: '100%',
|
|
854
|
-
overflow: 'hidden',
|
|
855
|
-
} }, { children: jsx(animated.div, Object.assign({}, bindDrag(), { "data-testid": "use-spring-carousel-animated-wrapper", style: {
|
|
856
|
-
display: 'flex',
|
|
857
|
-
top: 0,
|
|
858
|
-
left: 0,
|
|
859
|
-
position: 'relative',
|
|
860
|
-
touchAction,
|
|
861
|
-
flexDirection: carouselSlideAxis === 'x' ? 'row' : 'column',
|
|
862
|
-
...getAnimatedWrapperStyles(),
|
|
863
|
-
...carouselStyles,
|
|
864
|
-
}, ref: ref => {
|
|
865
|
-
if (ref) {
|
|
866
|
-
carouselTrackWrapperRef.current = ref;
|
|
867
|
-
if (withLoop) {
|
|
868
|
-
adjustCarouselWrapperPosition(ref);
|
|
869
|
-
}
|
|
870
|
-
}
|
|
871
|
-
} }, { children: internalItems.map(({ id, renderItem }, index) => {
|
|
872
|
-
return (jsx("div", Object.assign({ className: "use-spring-carousel-item", "data-testid": "use-spring-carousel-item-wrapper", style: {
|
|
873
|
-
display: 'flex',
|
|
874
|
-
position: 'relative',
|
|
875
|
-
...getItemStyles(),
|
|
876
|
-
} }, { children: renderItem }), `${id}-${index}`));
|
|
877
|
-
}) }), void 0) }), void 0) }), void 0));
|
|
878
|
-
const thumbsFragment = (jsx(UseSpringCarouselContext.Provider, Object.assign({ value: contextProps }, { children: _thumbsFragment }), void 0));
|
|
879
|
-
return {
|
|
880
|
-
carouselFragment,
|
|
881
|
-
thumbsFragment,
|
|
882
|
-
...contextProps,
|
|
883
|
-
};
|
|
884
|
-
}
|
|
885
|
-
|
|
886
|
-
const UseTransitionCarouselContext = createContext(undefined);
|
|
887
|
-
function useTransitionCarouselContext() {
|
|
888
|
-
const context = useContext(UseTransitionCarouselContext);
|
|
889
|
-
if (!context) {
|
|
890
|
-
throw new Error(`useTransitionCarouselContext isn't being used within the useTransitionCarousel context;
|
|
891
|
-
use the context only inside a component that is rendered within the Carousel.`);
|
|
892
|
-
}
|
|
893
|
-
return context;
|
|
894
|
-
}
|
|
895
|
-
function useTransitionCarousel({ items, withLoop = false, withThumbs = false, springConfig = config.default, thumbsSlideAxis = 'x', enableThumbsWrapperScroll = true, draggingSlideTreshold = 50, prepareThumbsData, toPrevItemSpringProps, toNextItemSpringProps, disableGestures = false, springAnimationProps = {
|
|
896
|
-
initial: {
|
|
897
|
-
opacity: 1,
|
|
898
|
-
position: 'relative',
|
|
899
|
-
},
|
|
900
|
-
from: {
|
|
901
|
-
opacity: 0,
|
|
902
|
-
position: 'absolute',
|
|
903
|
-
},
|
|
904
|
-
enter: {
|
|
905
|
-
opacity: 1,
|
|
906
|
-
position: 'relative',
|
|
907
|
-
},
|
|
908
|
-
leave: {
|
|
909
|
-
opacity: 0,
|
|
910
|
-
position: 'absolute',
|
|
911
|
-
},
|
|
912
|
-
}, }) {
|
|
913
|
-
const slideActionType = useRef('next');
|
|
914
|
-
const mainCarouselWrapperRef = useRef(null);
|
|
915
|
-
const isAnimating = useRef(false);
|
|
916
|
-
const [activeItem, setActiveItem] = useState(0);
|
|
917
|
-
const { emitObservable, useListenToCustomEvent } = useCustomEventsModule();
|
|
918
|
-
const { enterFullscreen, exitFullscreen, getIsFullscreen } = useFullscreenModule({
|
|
919
|
-
emitObservable,
|
|
920
|
-
mainCarouselWrapperRef,
|
|
921
|
-
});
|
|
922
|
-
const { thumbsFragment: _thumbsFragment, handleThumbsScroll } = useThumbsModule({
|
|
923
|
-
items,
|
|
924
|
-
withThumbs,
|
|
925
|
-
thumbsSlideAxis,
|
|
926
|
-
springConfig,
|
|
927
|
-
prepareThumbsData,
|
|
928
|
-
});
|
|
929
|
-
const bindSwipe = useDrag(({ last, movement: [mx] }) => {
|
|
930
|
-
if (getIsAnimating()) {
|
|
931
|
-
return;
|
|
932
|
-
}
|
|
933
|
-
if (last) {
|
|
934
|
-
const prevItemTreshold = mx > draggingSlideTreshold;
|
|
935
|
-
const nextItemTreshold = mx < -draggingSlideTreshold;
|
|
936
|
-
const isFirstItem = activeItem === 0;
|
|
937
|
-
const isLastItem = activeItem === items.length - 1;
|
|
938
|
-
if (nextItemTreshold) {
|
|
939
|
-
if (!withLoop && isLastItem) {
|
|
940
|
-
return;
|
|
941
|
-
}
|
|
942
|
-
slideToNextItem();
|
|
943
|
-
emitObservable({
|
|
944
|
-
eventName: 'onLeftSwipe',
|
|
945
|
-
});
|
|
946
|
-
}
|
|
947
|
-
else if (prevItemTreshold) {
|
|
948
|
-
if (!withLoop && isFirstItem) {
|
|
949
|
-
return;
|
|
950
|
-
}
|
|
951
|
-
slideToPrevItem();
|
|
952
|
-
emitObservable({
|
|
953
|
-
eventName: 'onRightSwipe',
|
|
954
|
-
});
|
|
955
|
-
}
|
|
956
|
-
}
|
|
957
|
-
}, {
|
|
958
|
-
enabled: !disableGestures,
|
|
959
|
-
});
|
|
960
|
-
function getTransitionConfig() {
|
|
961
|
-
const slideActionType = getSlideActionType();
|
|
962
|
-
if (slideActionType === 'prev' && toPrevItemSpringProps) {
|
|
963
|
-
return {
|
|
964
|
-
initial: {
|
|
965
|
-
...springAnimationProps.initial,
|
|
966
|
-
},
|
|
967
|
-
from: {
|
|
968
|
-
...toPrevItemSpringProps.from,
|
|
969
|
-
},
|
|
970
|
-
enter: {
|
|
971
|
-
...toPrevItemSpringProps.enter,
|
|
972
|
-
},
|
|
973
|
-
leave: {
|
|
974
|
-
...toPrevItemSpringProps.leave,
|
|
975
|
-
},
|
|
976
|
-
};
|
|
977
|
-
}
|
|
978
|
-
if (slideActionType === 'next' && toNextItemSpringProps) {
|
|
979
|
-
return {
|
|
980
|
-
initial: {
|
|
981
|
-
...springAnimationProps.initial,
|
|
982
|
-
},
|
|
983
|
-
from: {
|
|
984
|
-
...toNextItemSpringProps.from,
|
|
985
|
-
},
|
|
986
|
-
enter: {
|
|
987
|
-
...toNextItemSpringProps.enter,
|
|
988
|
-
},
|
|
989
|
-
leave: {
|
|
990
|
-
...toNextItemSpringProps.leave,
|
|
991
|
-
},
|
|
992
|
-
};
|
|
993
|
-
}
|
|
994
|
-
return {
|
|
995
|
-
initial: {
|
|
996
|
-
...springAnimationProps.initial,
|
|
997
|
-
},
|
|
998
|
-
from: {
|
|
999
|
-
...springAnimationProps.from,
|
|
1000
|
-
},
|
|
1001
|
-
enter: {
|
|
1002
|
-
...springAnimationProps.enter,
|
|
1003
|
-
},
|
|
1004
|
-
leave: {
|
|
1005
|
-
...springAnimationProps.leave,
|
|
1006
|
-
},
|
|
1007
|
-
};
|
|
1008
|
-
}
|
|
1009
|
-
const transitions = useTransition(activeItem, {
|
|
1010
|
-
config: springConfig,
|
|
1011
|
-
...getTransitionConfig(),
|
|
1012
|
-
onStart: () => setIsAnimating(true),
|
|
1013
|
-
keys: null,
|
|
1014
|
-
onRest: val => {
|
|
1015
|
-
if (val.finished) {
|
|
1016
|
-
setIsAnimating(false);
|
|
1017
|
-
emitObservable({
|
|
1018
|
-
eventName: 'onSlideChange',
|
|
1019
|
-
currentItem: activeItem,
|
|
1020
|
-
slideActionType: getSlideActionType(),
|
|
1021
|
-
});
|
|
1022
|
-
}
|
|
1023
|
-
},
|
|
1024
|
-
});
|
|
1025
|
-
const itemsFragment = transitions((styles, item) => (jsx(animated.div, Object.assign({ style: {
|
|
1026
|
-
...styles,
|
|
1027
|
-
flex: '1 0 100%',
|
|
1028
|
-
width: '100%',
|
|
1029
|
-
height: '100%',
|
|
1030
|
-
} }, { children: items[item].renderItem }), void 0)));
|
|
1031
|
-
function getIsAnimating() {
|
|
1032
|
-
return isAnimating.current;
|
|
1033
|
-
}
|
|
1034
|
-
function setIsAnimating(val) {
|
|
1035
|
-
isAnimating.current = val;
|
|
1036
|
-
}
|
|
1037
|
-
function setSlideActionType(type) {
|
|
1038
|
-
slideActionType.current = type;
|
|
1039
|
-
}
|
|
1040
|
-
function getSlideActionType() {
|
|
1041
|
-
return slideActionType.current;
|
|
1042
|
-
}
|
|
1043
|
-
function slideToItem(item) {
|
|
1044
|
-
let itemIndex = 0;
|
|
1045
|
-
if (typeof item === 'string') {
|
|
1046
|
-
itemIndex = items.findIndex(_item => _item.id === item);
|
|
1047
|
-
}
|
|
1048
|
-
else {
|
|
1049
|
-
itemIndex = item;
|
|
1050
|
-
}
|
|
1051
|
-
if (itemIndex >= items.length) {
|
|
1052
|
-
throw Error(`The item you want to slide to doesn't exist. This could be due to the fact that
|
|
1053
|
-
you provide a wrong id or a higher numeric index.`);
|
|
1054
|
-
}
|
|
1055
|
-
if (itemIndex === activeItem) {
|
|
1056
|
-
return;
|
|
1057
|
-
}
|
|
1058
|
-
const currentItem = findItemIndex(items[activeItem].id);
|
|
1059
|
-
const newActiveItem = findItemIndex(items[itemIndex].id);
|
|
1060
|
-
emitObservable({
|
|
1061
|
-
eventName: 'onSlideStartChange',
|
|
1062
|
-
nextItem: newActiveItem,
|
|
1063
|
-
slideActionType: getSlideActionType(),
|
|
1064
|
-
});
|
|
1065
|
-
if (newActiveItem > currentItem) {
|
|
1066
|
-
setSlideActionType('next');
|
|
1067
|
-
}
|
|
1068
|
-
else {
|
|
1069
|
-
setSlideActionType('prev');
|
|
1070
|
-
}
|
|
1071
|
-
setActiveItem(itemIndex);
|
|
1072
|
-
if (enableThumbsWrapperScroll && withThumbs) {
|
|
1073
|
-
handleThumbsScroll(itemIndex);
|
|
1074
|
-
}
|
|
1075
|
-
}
|
|
1076
|
-
function slideToNextItem() {
|
|
1077
|
-
const isLastItem = activeItem === items.length - 1;
|
|
1078
|
-
if (withLoop) {
|
|
1079
|
-
setSlideActionType('next');
|
|
1080
|
-
if (isLastItem) {
|
|
1081
|
-
emitObservable({
|
|
1082
|
-
eventName: 'onSlideStartChange',
|
|
1083
|
-
nextItem: 0,
|
|
1084
|
-
slideActionType: getSlideActionType(),
|
|
1085
|
-
});
|
|
1086
|
-
setActiveItem(0);
|
|
1087
|
-
}
|
|
1088
|
-
else {
|
|
1089
|
-
emitObservable({
|
|
1090
|
-
eventName: 'onSlideStartChange',
|
|
1091
|
-
nextItem: activeItem + 1,
|
|
1092
|
-
slideActionType: getSlideActionType(),
|
|
1093
|
-
});
|
|
1094
|
-
setActiveItem(activeItem + 1);
|
|
1095
|
-
}
|
|
1096
|
-
}
|
|
1097
|
-
else {
|
|
1098
|
-
if (!isLastItem) {
|
|
1099
|
-
emitObservable({
|
|
1100
|
-
eventName: 'onSlideStartChange',
|
|
1101
|
-
nextItem: activeItem + 1,
|
|
1102
|
-
slideActionType: getSlideActionType(),
|
|
1103
|
-
});
|
|
1104
|
-
setSlideActionType('next');
|
|
1105
|
-
setActiveItem(activeItem + 1);
|
|
1106
|
-
}
|
|
1107
|
-
}
|
|
1108
|
-
}
|
|
1109
|
-
function slideToPrevItem() {
|
|
1110
|
-
const isFirstItem = activeItem === 0;
|
|
1111
|
-
if (withLoop) {
|
|
1112
|
-
setSlideActionType('prev');
|
|
1113
|
-
if (isFirstItem) {
|
|
1114
|
-
emitObservable({
|
|
1115
|
-
eventName: 'onSlideStartChange',
|
|
1116
|
-
nextItem: items.length - 1,
|
|
1117
|
-
slideActionType: getSlideActionType(),
|
|
1118
|
-
});
|
|
1119
|
-
setActiveItem(items.length - 1);
|
|
1120
|
-
}
|
|
1121
|
-
else {
|
|
1122
|
-
emitObservable({
|
|
1123
|
-
eventName: 'onSlideStartChange',
|
|
1124
|
-
nextItem: activeItem - 1,
|
|
1125
|
-
slideActionType: getSlideActionType(),
|
|
1126
|
-
});
|
|
1127
|
-
setActiveItem(activeItem - 1);
|
|
1128
|
-
}
|
|
1129
|
-
}
|
|
1130
|
-
else {
|
|
1131
|
-
if (!isFirstItem) {
|
|
1132
|
-
setSlideActionType('prev');
|
|
1133
|
-
emitObservable({
|
|
1134
|
-
eventName: 'onSlideStartChange',
|
|
1135
|
-
nextItem: activeItem - 1,
|
|
1136
|
-
slideActionType: getSlideActionType(),
|
|
1137
|
-
});
|
|
1138
|
-
setActiveItem(activeItem - 1);
|
|
1139
|
-
}
|
|
1140
|
-
}
|
|
1141
|
-
}
|
|
1142
|
-
function findItemIndex(id) {
|
|
1143
|
-
return items.findIndex(item => item.id === id);
|
|
1144
|
-
}
|
|
1145
|
-
function getIsNextItem(id) {
|
|
1146
|
-
const itemIndex = findItemIndex(id);
|
|
1147
|
-
if (withLoop && activeItem === items.length - 1) {
|
|
1148
|
-
return itemIndex === 0;
|
|
1149
|
-
}
|
|
1150
|
-
return itemIndex === activeItem + 1;
|
|
1151
|
-
}
|
|
1152
|
-
function getIsPrevItem(id) {
|
|
1153
|
-
const itemIndex = findItemIndex(id);
|
|
1154
|
-
if (withLoop && activeItem === 0) {
|
|
1155
|
-
return itemIndex === items.length - 1;
|
|
1156
|
-
}
|
|
1157
|
-
return itemIndex === activeItem - 1;
|
|
1158
|
-
}
|
|
1159
|
-
const contextProps = {
|
|
1160
|
-
activeItem,
|
|
1161
|
-
slideToItem,
|
|
1162
|
-
slideToNextItem,
|
|
1163
|
-
slideToPrevItem,
|
|
1164
|
-
enterFullscreen,
|
|
1165
|
-
exitFullscreen,
|
|
1166
|
-
useListenToCustomEvent,
|
|
1167
|
-
getIsNextItem,
|
|
1168
|
-
getIsPrevItem,
|
|
1169
|
-
getIsAnimating,
|
|
1170
|
-
getIsFullscreen,
|
|
1171
|
-
getIsActiveItem: id => {
|
|
1172
|
-
return findItemIndex(id) === activeItem;
|
|
1173
|
-
},
|
|
1174
|
-
getCurrentActiveItem: () => ({
|
|
1175
|
-
id: items[activeItem].id,
|
|
1176
|
-
index: activeItem,
|
|
1177
|
-
}),
|
|
1178
|
-
};
|
|
1179
|
-
const carouselFragment = (jsx(UseTransitionCarouselContext.Provider, Object.assign({ value: contextProps }, { children: jsx("div", Object.assign({ ref: mainCarouselWrapperRef }, bindSwipe(), { style: {
|
|
1180
|
-
display: 'flex',
|
|
1181
|
-
position: 'relative',
|
|
1182
|
-
width: '100%',
|
|
1183
|
-
height: '100%',
|
|
1184
|
-
overflow: 'hidden',
|
|
1185
|
-
} }, { children: itemsFragment }), void 0) }), void 0));
|
|
1186
|
-
const thumbsFragment = (jsx(UseTransitionCarouselContext.Provider, Object.assign({ value: contextProps }, { children: _thumbsFragment }), void 0));
|
|
1187
|
-
return {
|
|
1188
|
-
carouselFragment,
|
|
1189
|
-
thumbsFragment,
|
|
1190
|
-
...contextProps,
|
|
1191
|
-
};
|
|
1192
|
-
}
|
|
1193
|
-
|
|
1194
|
-
export { useSpringCarousel, useSpringCarouselContext, useTransitionCarousel, useTransitionCarouselContext };
|
|
1
|
+
import{jsx as e}from"react/jsx-runtime";import{useRef as t,useEffect as n,createContext as r,useCallback as i,useContext as o,useState as s}from"react";import{useSpring as u,animated as l,config as c,useTransition as a}from"react-spring";import{useDrag as d}from"@use-gesture/react";import{Subject as m}from"rxjs";import f from"screenfull";function h(){const e=t(new m);return{useListenToCustomEvent:function(t){n((()=>{const n=e.current.subscribe(t);return()=>n.unsubscribe()}),[t])},emitObservable:t=>{e.current.next(t)}}}function g({mainCarouselWrapperRef:e,emitObservable:r,handleResize:i}){const o=t(!1);function s(e){o.current=e}return n((()=>{function e(){document.fullscreenElement&&(s(!0),r({eventName:"onFullscreenChange",isFullscreen:!0}),i&&i()),document.fullscreenElement||(s(!1),r({eventName:"onFullscreenChange",isFullscreen:!1}),i&&i())}if(f.isEnabled)return f.on("change",e),()=>{f.isEnabled&&f.off("change",e)}})),{enterFullscreen:function(t){f.isEnabled&&f.request(t||e.current)},exitFullscreen:function(){f.isEnabled&&f.exit()},getIsFullscreen:function(){return o.current}}}function p(e){const r=t(!1);n((()=>{if(!r.current){const t=e();return r.current=!0,()=>{t&&t()}}}),[])}function b({items:n,withThumbs:r,thumbsSlideAxis:i="x",springConfig:o,thumbsWrapperRef:s,prepareThumbsData:c}){const a=t(null),[d,m]=u((()=>({[i]:0,config:o})));p((()=>{if(r){if(n.some((e=>!e.renderThumb)))throw new Error("The renderThumb property is missing in one or more items. You need to add the renderThumb property to every item of the carousel when the prop withThumbs={true} or eventually set withThumbs={false}.")}}));return{thumbsFragment:r?e(l.div,Object.assign({ref:a},"x"===i?{scrollLeft:d.x}:{scrollTop:d.y},{style:{display:"flex",flex:1,position:"relative",flexDirection:"x"===i?"row":"column",..."x"===i?{overflowX:"auto"}:{overflowY:"auto",maxHeight:"100%"}}},{children:function(){function e(e){return e.map((e=>({id:e.id,renderThumb:e.renderThumb})))}return c?c(e(n)):e(n)}().map((({id:t,renderThumb:n})=>{const r=`thumb-${t}`;return e("div",Object.assign({id:r},{children:n}),r)}))}),void 0):null,handleThumbsScroll:function(e){s&&s.current&&(a.current=s.current);const t=a.current.querySelector(`#thumb-${n[e].id}`);if(t){const r=a.current,o="x"===i?"offsetWidth":"offsetHeight",s="x"===i?"scrollLeft":"scrollTop",u=function({thumbNode:e,offsetDirection:t,offsetDimension:n}){return e[t]+e[n]/2}({thumbNode:t,offsetDimension:o,offsetDirection:"x"===i?"offsetLeft":"offsetTop"}),l=function({thumbWrapper:e,offsetDimension:t}){return e[t]/2}({thumbWrapper:r,offsetDimension:o});m.start({from:{[i]:function({thumbWrapper:e,scrollDirection:t}){return e[t]}({thumbWrapper:r,scrollDirection:s})},to:{[i]:function({thumbWrapper:t,thumbOffsetPosition:r,thumbScrollDimension:o,offsetDimension:s}){const u="x"===i?"scrollWidth":"scrollHeight";return e===n.length-1||r-o>t[u]-t[s]?t[u]-t[s]:0===e?0:r-o}({thumbWrapper:r,thumbOffsetPosition:u,thumbScrollDimension:l,offsetDimension:o})},onChange:e=>{"x"===i?a.current.scrollLeft=e.x:a.current.scrollTop=e.y}})}}}}const v=r(void 0);function x(){const e=o(v);if(!e)throw new Error("useSpringCarouselContext isn't being used within the useSringCarousel context; \n use the context only inside a component that is rendered within the Carousel.");return e}function w({items:r,withLoop:o=!1,draggingSlideTreshold:s=140,springConfig:a=c.default,shouldResizeOnWindowResize:m=!0,withThumbs:f=!1,enableThumbsWrapperScroll:x=!0,carouselSlideAxis:w="x",thumbsSlideAxis:y="x",thumbsWrapperRef:T,prepareThumbsData:I,itemsPerSlide:S=1,initialActiveItem:C=0,initialStartingPosition:A="start",disableGestures:N=!1,gutter:E=0,startEndGutter:F=0,touchAction:R="none",slideAmount:D}){const O=t("next"),W=o?[...r,...r,...r]:r,P=t(C),L=t(null),j=t(null),$=t(!1),z=t(!1),B=t(!1),M=t(0),q=t(0),H=t(!1),k=t(0),G=t(0);const Y=i((()=>Math.round(Number(j.current?.["x"===w?"scrollWidth":"scrollHeight"])-j.current.getBoundingClientRect()["x"===w?"width":"height"])),[w]),[X,J]=u((()=>({y:0,x:0,config:a,onRest:({value:e})=>{k.current=e[w],G.current=e[w]}}))),K=i((()=>{if(!j.current)return 0;const e=j.current?.querySelector(".use-spring-carousel-item");if(!e)throw Error("No carousel items available!");const t=e.getBoundingClientRect()["x"===w?"width":"height"]+E;if("fluid"===S&&"number"==typeof D){if(D<t)throw new Error("slideAmount must be greater than the width of a single item.");return D}return t}),[w,E,S,D]),Q=i((e=>{if("fluid"!==S&&"number"==typeof S){const o="x"===w?"left":"top";function t(){return K()*r.length}function n(t){e.style.top="0px",e.style.left="0px",e.style[o]=`-${t-F}px`}function i(){n(t())}if(S>1)switch(A){default:case"start":i();break;case"center":n(t()-K()*Math.round((S-1)/2));break;case"end":n(t()-K()*Math.round(S-1))}else i()}}),[F,w,K,A,r.length,S]),U=i((()=>{if(window.innerWidth!==M.current){if(M.current=window.innerWidth,"fluid"===S)if(q.current=Y(),H.current)J.start({immediate:!0,[w]:-q.current});else{const e=document.querySelector(".use-spring-carousel-item:last-of-type");console.log({lastItem:e.offsetLeft})}else J.start({immediate:!0,x:0,y:0}),J.start({immediate:!0,[w]:-K()*le()});o&&Q(j.current)}}),[Q,w,K,J,o,S,Y]),{useListenToCustomEvent:V,emitObservable:Z}=h(),{enterFullscreen:_,exitFullscreen:ee,getIsFullscreen:te}=g({mainCarouselWrapperRef:L,emitObservable:Z,handleResize:U}),{thumbsFragment:ne,handleThumbsScroll:re}=b({withThumbs:f,items:r,thumbsSlideAxis:y,springConfig:a,thumbsWrapperRef:T,prepareThumbsData:I});const ie=d((e=>{const t=e.dragging,n=e.movement["x"===w?0:1];function i(){"fluid"===S?pe()?ve():H.current?J.start({[w]:-q.current}):J.start({[w]:G.current}):J.start({[w]:-le()*K()})}if(e.first&&(k.current=X[w].get()),t){ae(!0),Z({eventName:"onDrag",...e}),J.start({[w]:k.current+n});const t=n>s,u=n<-s;L.current.getBoundingClientRect().width>=r.length*K()&&(H.current=!0),H.current&&n<0?u&&(e.cancel(),J.start({[w]:-q.current})):u?(e.cancel(),!o&&be()?i():xe()):t&&(e.cancel(),!o&&pe()?i():ve())}e.last&&!e.pressed&&i()}),{enabled:!N});function oe(e){O.current=e}function se(){return O.current}function ue(e){P.current=e}function le(){return P.current}function ce(e){z.current=e}function ae(e){$.current=e}function de(){const e=le();return 0===e?r.length-1:e-1}function me(){const e=le();return e===r.length-1?0:e+1}function fe(e){return r.findIndex((t=>t.id===e))}function he({from:e,to:t,customTo:n,immediate:r=!1,onRest:i=(()=>{})}){r||(ue(t),ce(!0),Z({eventName:"onSlideStartChange",nextItem:t,slideActionType:se()})),J.start({...e?{from:{[w]:e}}:{},to:n?{[w]:n}:{[w]:-K()*t},immediate:r,onRest:e=>{e.finished&&(ae(!1),ce(!1),i(),r||Z({eventName:"onSlideChange",currentItem:le(),slideActionType:se()}))}}),x&&f&&!r&&re(t)}function ge(e){if("none"===e.style.transform)return 0;const t=e.style.transform.split(/\w+\(|\);?/);return Number(t[1].split(/,\s?/g)["x"===w?0:1].replace("px",""))}function pe(){return 0===le()}function be(){return le()===r.length-1}function ve(){if("fluid"!==S||o){if(!o&&0===le()||B.current)return;oe("prev"),pe()?he({from:-(Math.abs(ge(j.current))+K()*r.length),to:r.length-1}):he({to:de()})}else{const e=ge(j.current)+K()+100;if(pe())return void he({to:0});if(e>=0)he({to:0}),G.current=0;else{const e=G.current+K();G.current=e,he({to:de(),customTo:e})}H.current&&(H.current=!1)}}function xe(){if("fluid"!==S||o){if(!o&&le()===W.length-1||B.current)return;oe("next"),be()?he({from:ge(j.current)+K()*r.length,to:0}):he({to:me()})}else{const e=Math.abs(me()*K())+100>=q.current;if(L.current.getBoundingClientRect().width>=r.length*K()&&(H.current=!0),H.current)return;if(e){const e=-q.current;H.current=!0,G.current=e,he({to:me(),customTo:e})}else{const e=G.current-K();G.current=e,he({to:me(),customTo:e})}}}p((()=>{if("fluid"!==S&&!Number.isInteger(S))throw new Error("itemsPerSlide should be an integer.");if(S>r.length)throw new Error("The itemsPerSlide prop can't be greater than the total length of the items you provide.");if(S<1)throw new Error("The itemsPerSlide prop can't be less than 1.");m||console.warn("You set shouldResizeOnWindowResize={false}; be aware that the carousel could behave in a strange way if you also use the fullscreen functionality or if you change the mobile orientation."),C<0&&console.warn("The initialActiveItem cannot be less than 0."),C>r.length&&console.warn("The initialActiveItem cannot be greater than the total length of the items you provide.")})),p((()=>{function e(){document.hidden?B.current=!0:B.current=!1}if(q.current=Y(),"undefined"!=typeof window)return document.addEventListener("visibilitychange",e),()=>{document.removeEventListener("visibilitychange",e)}})),p((()=>{C>0&&C<=r.length&&(he({to:C,immediate:!0}),ue(C))})),n((()=>{if(m)return window.addEventListener("resize",U),()=>{window.removeEventListener("resize",U)}}),[U,m]),n((()=>{j.current&&("x"===w&&(j.current.style.top="0px"),"y"===w&&(j.current.style.left="0px"))}),[w]);const we={useListenToCustomEvent:V,getIsFullscreen:te,enterFullscreen:_,exitFullscreen:ee,getIsAnimating:function(){return z.current},getIsDragging:function(){return $.current},getIsNextItem:function(e){const t=fe(e),n=le();return o&&n===r.length-1?0===t:t===n+1},getIsPrevItem:function(e){const t=fe(e),n=le();return o&&0===n?t===r.length-1:t===n-1},slideToPrevItem:ve,slideToNextItem:xe,slideToItem:function(e){let t=0;if(t="string"==typeof e?r.findIndex((t=>t.id===e)):e,t>=r.length)throw Error("The item you want to slide to doesn't exist. This could be due to the fact that \n you provide a wrong id or a higher numeric index.");if(t===le())return;const n=fe(r[le()].id);oe(fe(r[t].id)>n?"next":"prev"),he({to:t})},getIsActiveItem:e=>fe(e)===le(),getCurrentActiveItem:()=>({id:r[le()].id,index:le()})};return{carouselFragment:e(v.Provider,Object.assign({value:we},{children:e("div",Object.assign({ref:L,"data-testid":"use-spring-carousel-wrapper",style:{display:"flex",position:"relative",width:"100%",height:"100%",overflow:"hidden"}},{children:e(l.div,Object.assign({},ie(),{"data-testid":"use-spring-carousel-animated-wrapper",style:{display:"flex",top:0,left:0,position:"relative",touchAction:R,flexDirection:"x"===w?"row":"column",...function(){const e=`calc(100% - ${2*F}px)`;return{width:"x"===w?e:"100%",height:"y"===w?e:"100%"}}(),...X},ref:e=>{e&&(j.current=e,o&&Q(e))}},{children:W.map((({id:t,renderItem:n},r)=>e("div",Object.assign({className:"use-spring-carousel-item","data-testid":"use-spring-carousel-item-wrapper",style:{display:"flex",position:"relative",..."number"==typeof S?{..."x"===w?{marginRight:`${E}px`}:{marginBottom:`${E}px`},flex:`1 0 calc(100% / ${S} - ${E*(S-1)/S}px)`}:{..."x"===w?{marginRight:`${E}px`}:{marginBottom:`${E}px`}}}},{children:n}),`${t}-${r}`)))}),void 0)}),void 0)}),void 0),thumbsFragment:e(v.Provider,Object.assign({value:we},{children:ne}),void 0),...we}}const y=r(void 0);function T(){const e=o(y);if(!e)throw new Error("useTransitionCarouselContext isn't being used within the useTransitionCarousel context; \n use the context only inside a component that is rendered within the Carousel.");return e}function I({items:n,withLoop:r=!1,withThumbs:i=!1,springConfig:o=c.default,thumbsSlideAxis:u="x",enableThumbsWrapperScroll:m=!0,draggingSlideTreshold:f=50,prepareThumbsData:p,toPrevItemSpringProps:v,toNextItemSpringProps:x,disableGestures:w=!1,springAnimationProps:T={initial:{opacity:1,position:"relative"},from:{opacity:0,position:"absolute"},enter:{opacity:1,position:"relative"},leave:{opacity:0,position:"absolute"}}}){const I=t("next"),S=t(null),C=t(!1),[A,N]=s(0),{emitObservable:E,useListenToCustomEvent:F}=h(),{enterFullscreen:R,exitFullscreen:D,getIsFullscreen:O}=g({emitObservable:E,mainCarouselWrapperRef:S}),{thumbsFragment:W,handleThumbsScroll:P}=b({items:n,withThumbs:i,thumbsSlideAxis:u,springConfig:o,prepareThumbsData:p}),L=d((({last:e,movement:[t]})=>{if(!$()&&e){const e=t>f,i=t<-f,o=0===A,s=A===n.length-1;if(i){if(!r&&s)return;q(),E({eventName:"onLeftSwipe"})}else if(e){if(!r&&o)return;H(),E({eventName:"onRightSwipe"})}}}),{enabled:!w});const j=a(A,{config:o,...function(){const e=M();return"prev"===e&&v?{initial:{...T.initial},from:{...v.from},enter:{...v.enter},leave:{...v.leave}}:"next"===e&&x?{initial:{...T.initial},from:{...x.from},enter:{...x.enter},leave:{...x.leave}}:{initial:{...T.initial},from:{...T.from},enter:{...T.enter},leave:{...T.leave}}}(),onStart:()=>z(!0),keys:null,onRest:e=>{e.finished&&(z(!1),E({eventName:"onSlideChange",currentItem:A,slideActionType:M()}))}})(((t,r)=>e(l.div,Object.assign({style:{...t,flex:"1 0 100%",width:"100%",height:"100%"}},{children:n[r].renderItem}),void 0)));function $(){return C.current}function z(e){C.current=e}function B(e){I.current=e}function M(){return I.current}function q(){const e=A===n.length-1;r?(B("next"),e?(E({eventName:"onSlideStartChange",nextItem:0,slideActionType:M()}),N(0)):(E({eventName:"onSlideStartChange",nextItem:A+1,slideActionType:M()}),N(A+1))):e||(E({eventName:"onSlideStartChange",nextItem:A+1,slideActionType:M()}),B("next"),N(A+1))}function H(){const e=0===A;r?(B("prev"),e?(E({eventName:"onSlideStartChange",nextItem:n.length-1,slideActionType:M()}),N(n.length-1)):(E({eventName:"onSlideStartChange",nextItem:A-1,slideActionType:M()}),N(A-1))):e||(B("prev"),E({eventName:"onSlideStartChange",nextItem:A-1,slideActionType:M()}),N(A-1))}function k(e){return n.findIndex((t=>t.id===e))}const G={activeItem:A,slideToItem:function(e){let t=0;if(t="string"==typeof e?n.findIndex((t=>t.id===e)):e,t>=n.length)throw Error("The item you want to slide to doesn't exist. This could be due to the fact that \n you provide a wrong id or a higher numeric index.");if(t===A)return;const r=k(n[A].id),o=k(n[t].id);E({eventName:"onSlideStartChange",nextItem:o,slideActionType:M()}),B(o>r?"next":"prev"),N(t),m&&i&&P(t)},slideToNextItem:q,slideToPrevItem:H,enterFullscreen:R,exitFullscreen:D,useListenToCustomEvent:F,getIsNextItem:function(e){const t=k(e);return r&&A===n.length-1?0===t:t===A+1},getIsPrevItem:function(e){const t=k(e);return r&&0===A?t===n.length-1:t===A-1},getIsAnimating:$,getIsFullscreen:O,getIsActiveItem:e=>k(e)===A,getCurrentActiveItem:()=>({id:n[A].id,index:A})};return{carouselFragment:e(y.Provider,Object.assign({value:G},{children:e("div",Object.assign({ref:S},L(),{style:{display:"flex",position:"relative",width:"100%",height:"100%",overflow:"hidden"}},{children:j}),void 0)}),void 0),thumbsFragment:e(y.Provider,Object.assign({value:G},{children:W}),void 0),...G}}export{w as useSpringCarousel,x as useSpringCarouselContext,I as useTransitionCarousel,T as useTransitionCarouselContext};
|
|
1195
2
|
//# sourceMappingURL=index.es.js.map
|