react-spring-carousel 3.0.0-beta090.10 → 3.0.0-beta090.12

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.es2.js DELETED
@@ -1,703 +0,0 @@
1
- import { u as Be, a as He, j as R } from "./useThumbsModule-4e4b7969.js";
2
- import { useSpring as ze, config as _, useIsomorphicLayoutEffect as Xe } from "@react-spring/web";
3
- import { useDrag as Ye } from "@use-gesture/react";
4
- import { useRef as m, useEffect as E, createContext as Je, useCallback as Y, useContext as Ke } from "react";
5
- import Qe from "resize-observer-polyfill";
6
- import N from "screenfull";
7
- function Ue({
8
- mainCarouselWrapperRef: n,
9
- onFullScreenChange: v,
10
- handleResize: W
11
- }) {
12
- const O = m(!1);
13
- E(() => {
14
- function a() {
15
- document.fullscreenElement && (J(!0), v(!0), W && W()), document.fullscreenElement || (J(!1), v(!1), W && W());
16
- }
17
- if (N.isEnabled)
18
- return N.on("change", a), () => {
19
- N.isEnabled && N.off("change", a);
20
- };
21
- }, []);
22
- function J(a) {
23
- O.current = a;
24
- }
25
- function y() {
26
- return O.current;
27
- }
28
- function b(a) {
29
- N.isEnabled && N.request(a || n.current);
30
- }
31
- function u() {
32
- N.isEnabled && N.exit();
33
- }
34
- return {
35
- enterFullscreen: b,
36
- exitFullscreen: u,
37
- getIsFullscreen: y
38
- };
39
- }
40
- function tr({
41
- items: n,
42
- init: v = !0,
43
- withThumbs: W,
44
- thumbsSlideAxis: O = "x",
45
- itemsPerSlide: J = 1,
46
- slideType: y = "fixed",
47
- gutter: b = 0,
48
- withLoop: u = !1,
49
- startEndGutter: a = 0,
50
- carouselSlideAxis: o = "x",
51
- disableGestures: me = !1,
52
- draggingSlideTreshold: ne,
53
- slideWhenThresholdIsReached: ve = !1,
54
- freeScroll: c,
55
- enableFreeScrollDrag: ce,
56
- initialStartingPosition: L,
57
- prepareThumbsData: be,
58
- initialActiveItem: C = 0,
59
- animateWhenActiveItemChange: Te = !0,
60
- getControllerRef: ue,
61
- slideGroupOfItems: j = !1
62
- }) {
63
- const w = J > n.length ? n.length : J, K = m(!1), G = m(ne ?? 0), k = m("initial"), oe = m("initial"), ie = m("initial"), p = m(C), d = m(C === 0), s = m(y === "fixed" && C === n.length - 1), f = m(null), x = m(null), se = m(0), le = m(0), g = m(0), we = m(u), Me = m(y), Ce = m(c), V = m(!1), A = Y(() => u ? [
64
- ...n.map((e) => ({
65
- ...e,
66
- id: `prev-repeated-item-${e.id}`
67
- })),
68
- ...n,
69
- ...n.map((e) => ({
70
- ...e,
71
- id: `next-repeated-item-${e.id}`
72
- }))
73
- ] : [...n], [n, u])(), [P, I] = ze(
74
- () => ({
75
- val: 0,
76
- pause: !v,
77
- onChange: ({ value: e }) => {
78
- c && f.current ? (o === "x" ? f.current.scrollLeft = Math.abs(e.val) : f.current.scrollTop = Math.abs(e.val), te()) : x.current && (o === "x" ? x.current.style.transform = `translate3d(${e.val}px, 0px,0px)` : x.current.style.transform = `translate3d(0px,${e.val}px,0px)`);
79
- }
80
- }),
81
- [c]
82
- ), { emitEvent: S, useListenToCustomEvent: ee } = Be(), { thumbsFragment: ke, handleScroll: Fe } = He({
83
- withThumbs: !!W,
84
- thumbsSlideAxis: O,
85
- prepareThumbsData: be,
86
- items: n,
87
- renderThumbFnProps: {
88
- getIsActiveItem: ge,
89
- getIsPrevItem: de,
90
- useListenToCustomEvent: ee,
91
- getIsNextItem: ae
92
- }
93
- }), { enterFullscreen: pe, exitFullscreen: xe, getIsFullscreen: Ie } = Ue({
94
- mainCarouselWrapperRef: f,
95
- handleResize: () => re(),
96
- onFullScreenChange: (e) => {
97
- S({
98
- eventName: "onFullscreenChange",
99
- isFullscreen: e
100
- });
101
- }
102
- });
103
- function $e(e) {
104
- return y === "fixed" && !c ? {
105
- marginRight: `${e ? 0 : b}px`,
106
- flex: `1 0 calc(100% / ${w} - ${b * (w - 1) / w}px)`
107
- } : {
108
- marginRight: `${e ? 0 : b}px`
109
- };
110
- }
111
- const l = Y(() => {
112
- var r;
113
- const e = j ? f.current : (r = f.current) == null ? void 0 : r.querySelector(".use-spring-carousel-item");
114
- if (!e)
115
- throw Error("No carousel items available!");
116
- let t = e.getBoundingClientRect()[o === "x" ? "width" : "height"];
117
- return j || (t += b), j && (t += b - a * 2), t;
118
- }, [o, b, j, a]);
119
- function D({ from: e, to: t, nextActiveItem: r, immediate: i = !1, slideMode: M }) {
120
- oe.current = M, typeof r == "number" && (c || (p.current = r), S({
121
- eventName: "onSlideStartChange",
122
- slideActionType: k.current,
123
- slideMode: oe.current,
124
- nextItem: {
125
- startReached: d.current,
126
- endReached: s.current,
127
- index: c ? -1 : p.current,
128
- id: c ? "" : n[p.current].id
129
- }
130
- })), g.current = t, I.start({
131
- immediate: i,
132
- from: {
133
- val: e
134
- },
135
- to: {
136
- val: t
137
- },
138
- config: {
139
- ..._.default,
140
- velocity: P.val.velocity
141
- },
142
- onRest(z) {
143
- !i && z.finished && S({
144
- eventName: "onSlideChange",
145
- slideActionType: k.current,
146
- slideMode: oe.current,
147
- currentItem: {
148
- startReached: d.current,
149
- endReached: s.current,
150
- index: c ? -1 : p.current,
151
- id: c ? "" : n[p.current].id
152
- }
153
- });
154
- }
155
- }), W && !i && Fe(p.current);
156
- }
157
- const h = Y(() => {
158
- var e;
159
- return x.current ? u ? l() * n.length : Math.round(
160
- Number(
161
- (e = x.current) == null ? void 0 : e[o === "x" ? "scrollWidth" : "scrollHeight"]
162
- ) - x.current.getBoundingClientRect()[o === "x" ? "width" : "height"] - a * 2
163
- ) : 0;
164
- }, [o, l, n.length, a, u]);
165
- function Ne() {
166
- const e = `calc(100% - ${a * 2}px)`;
167
- return {
168
- width: o === "x" ? e : "100%",
169
- height: o === "y" ? e : "100%"
170
- };
171
- }
172
- const Q = Y(() => {
173
- var t;
174
- const e = (t = x.current) == null ? void 0 : t.querySelector(".use-spring-carousel-item");
175
- if (!e)
176
- throw Error("No carousel items available!");
177
- return e.getBoundingClientRect()[o === "x" ? "width" : "height"] + b;
178
- }, [o, b]), U = Y(
179
- (e) => {
180
- const t = o === "x" ? "left" : "top", r = x.current;
181
- r && (u ? (r.style.top = "0px", r.style.left = "0px", r.style[t] = `-${e - a}px`, d.current = !1, s.current = !1) : (r.style.left = "0px", r.style.top = "0px"));
182
- },
183
- [o, a, u]
184
- ), re = Y(
185
- (e = !1) => {
186
- if (x.current && e && (x.current.style.transform = "translate3d(0px, 0px,0px)", x.current.style.left = "0", x.current.style.top = "0"), y === "fixed" && U(
187
- L === "center" ? Q() * n.length - l() * Math.round((w - 1) / 2) : L === "end" ? Q() * n.length - l() * Math.round(w - 1) : Q() * n.length
188
- ), y === "fluid") {
189
- if (U(Q() * n.length), s.current && h() !== Math.abs(g.current) && !u) {
190
- const t = -h();
191
- g.current = t, I.start({
192
- immediate: !0,
193
- val: g.current
194
- });
195
- return;
196
- }
197
- if (Math.abs(g.current) > 0 && h() !== Math.abs(g.current) && !u && !c && ie.current === "backward") {
198
- const t = se.current - h(), r = g.current + t;
199
- return I.start({
200
- immediate: !0,
201
- val: r
202
- }), () => {
203
- g.current = r;
204
- };
205
- }
206
- console.log("here");
207
- return;
208
- }
209
- if (!c && y === "fixed") {
210
- const t = -(l() * p.current);
211
- if (Math.abs(t) > h() && !u) {
212
- const r = -h();
213
- s.current = !0, g.current = r, I.start({
214
- immediate: !0,
215
- val: g.current
216
- });
217
- } else
218
- g.current = t, I.start({
219
- immediate: !0,
220
- val: t
221
- });
222
- setTimeout(() => {
223
- K.current = !1;
224
- }, 0);
225
- }
226
- },
227
- [
228
- c,
229
- Q,
230
- l,
231
- h,
232
- L,
233
- n.length,
234
- w,
235
- U,
236
- I,
237
- y,
238
- u
239
- ]
240
- );
241
- function F() {
242
- return c && f.current ? f.current[o === "x" ? "scrollLeft" : "scrollTop"] : P.val.get();
243
- }
244
- function Z(e, t) {
245
- if (c && e === "next") {
246
- const r = g.current + l();
247
- return r > h() ? h() : r;
248
- }
249
- if (c && e === "prev") {
250
- const r = g.current - l();
251
- return r < 0 ? 0 : r;
252
- }
253
- return e === "next" ? typeof t == "number" ? -(t * l()) : g.current - l() : typeof t == "number" ? -(t * l()) : g.current + l();
254
- }
255
- function q({ type: e, index: t, immediate: r }) {
256
- if (!v || V.current || d.current && !u)
257
- return;
258
- s.current && (ie.current = "backward"), k.current = "prev", s.current = !1;
259
- const i = typeof t == "number" ? t : p.current - 1;
260
- if (c && te(), !u) {
261
- const M = c ? Z("prev", t) - l() / 3 < 0 : Z("prev", t) + l() / 3 > 0;
262
- if (d.current)
263
- return;
264
- if (M) {
265
- d.current = !0, s.current = !1, D({
266
- slideMode: e,
267
- from: F(),
268
- to: 0,
269
- nextActiveItem: 0,
270
- immediate: r
271
- });
272
- return;
273
- }
274
- }
275
- if (u && (d.current || i < 0)) {
276
- d.current = !1, s.current = !0, D({
277
- slideMode: e,
278
- from: F() - l() * n.length,
279
- to: -(l() * n.length) + l(),
280
- nextActiveItem: n.length - 1,
281
- immediate: r
282
- });
283
- return;
284
- }
285
- i === 0 && (d.current = !0), (i === n.length - 1 || i === -1) && (s.current = !0), D({
286
- slideMode: e,
287
- from: F(),
288
- to: Z("prev", t),
289
- nextActiveItem: i,
290
- immediate: r
291
- });
292
- }
293
- function B({ type: e, index: t, immediate: r }) {
294
- if (!v || V.current || s.current && !u)
295
- return;
296
- d.current && (ie.current = "forward"), k.current = "next", d.current = !1;
297
- const i = t || p.current + 1;
298
- if (c && te(), !u) {
299
- const M = Math.abs(Z("next", t)) > h() - l() / 3;
300
- if (s.current)
301
- return;
302
- if (M) {
303
- d.current = !1, s.current = !0, D({
304
- slideMode: e,
305
- from: F(),
306
- to: c ? h() : -h(),
307
- nextActiveItem: i,
308
- immediate: r
309
- });
310
- return;
311
- }
312
- }
313
- if (u && (s.current || i > n.length - 1)) {
314
- s.current = !1, d.current = !0, D({
315
- slideMode: e,
316
- from: F() + l() * n.length,
317
- to: 0,
318
- nextActiveItem: 0,
319
- immediate: r
320
- });
321
- return;
322
- }
323
- i === 0 && (d.current = !0), i === n.length - 1 && (s.current = !0), D({
324
- slideMode: e,
325
- from: F(),
326
- to: Z("next", t),
327
- nextActiveItem: i,
328
- immediate: r
329
- });
330
- }
331
- function Ee() {
332
- ne ? G.current = ne : G.current = Math.floor(l() / 2 / 2);
333
- }
334
- function ye() {
335
- x.current && (se.current = h(), we.current = u, Me.current = y, Ce.current = c, le.current = window.innerWidth, g.current = 0, fe({ id: 0, immediate: !0, shouldReset: !0 }), Ee(), re());
336
- }
337
- const We = Ye(
338
- (e) => {
339
- const t = e.dragging, r = e.offset[o === "x" ? 0 : 1], i = e.movement[o === "x" ? 0 : 1], M = e.direction[o === "x" ? 0 : 1], z = i > G.current, X = i < -G.current, qe = h(), $ = e.velocity;
340
- if (t) {
341
- if (M > 0 ? k.current = "prev" : k.current = "next", S({
342
- ...e,
343
- eventName: "onDrag",
344
- slideActionType: k.current
345
- }), c) {
346
- if (r > 0) {
347
- I.start({
348
- from: {
349
- val: F()
350
- },
351
- to: {
352
- val: 0
353
- },
354
- config: {
355
- velocity: $,
356
- friction: 50,
357
- tension: 1e3
358
- }
359
- }), e.cancel();
360
- return;
361
- }
362
- I.start({
363
- from: {
364
- val: F()
365
- },
366
- to: {
367
- val: -r
368
- },
369
- config: {
370
- velocity: $,
371
- friction: 50,
372
- tension: 1e3
373
- }
374
- });
375
- return;
376
- }
377
- I.start({
378
- val: r,
379
- config: {
380
- velocity: $,
381
- friction: 50,
382
- tension: 1e3
383
- }
384
- }), ve && X ? (B({ type: "drag" }), e.cancel()) : ve && z && (q({ type: "drag" }), e.cancel()), qe - Math.abs(r) < -(l() * 2) && e.cancel();
385
- return;
386
- }
387
- if (e.last && c && r > 0) {
388
- I.start({
389
- from: {
390
- val: F()
391
- },
392
- to: {
393
- val: 0
394
- },
395
- config: {
396
- velocity: $,
397
- friction: 50,
398
- tension: 1e3
399
- }
400
- });
401
- return;
402
- }
403
- e.last && !e.canceled && c && (k.current === "prev" && q({ type: "drag" }), k.current === "next" && B({ type: "drag" })), e.last && !e.canceled && !c && (X ? !u && s.current ? I.start({
404
- val: -h(),
405
- config: {
406
- ..._.default,
407
- velocity: $
408
- }
409
- }) : B({ type: "drag" }) : z ? !u && d.current ? I.start({
410
- val: 0,
411
- config: {
412
- ..._.default,
413
- velocity: $
414
- }
415
- }) : q({ type: "drag" }) : I.start({
416
- val: g.current,
417
- config: {
418
- ..._.default,
419
- velocity: $
420
- }
421
- })), e.last && e.canceled && I.start({
422
- val: g.current,
423
- config: {
424
- ..._.default,
425
- velocity: $
426
- }
427
- });
428
- },
429
- {
430
- enabled: v && !me && !c || !!c && !!ce,
431
- axis: o,
432
- from: () => c && f.current ? [
433
- -f.current.scrollLeft,
434
- -f.current.scrollTop
435
- ] : o === "x" ? [P.val.get(), P.val.get()] : [P.val.get(), P.val.get()]
436
- }
437
- );
438
- function Pe() {
439
- return c ? o === "x" ? {
440
- overflowX: "auto"
441
- } : {
442
- overflowY: "auto"
443
- } : {};
444
- }
445
- function te() {
446
- f.current && (g.current = f.current[o === "x" ? "scrollLeft" : "scrollTop"], f.current[o === "x" ? "scrollLeft" : "scrollTop"] === 0 && (d.current = !0, s.current = !1), f.current[o === "x" ? "scrollLeft" : "scrollTop"] > 0 && f.current[o === "x" ? "scrollLeft" : "scrollTop"] < h() && (d.current = !1, s.current = !1), f.current[o === "x" ? "scrollLeft" : "scrollTop"] === h() && (d.current = !1, s.current = !0));
447
- }
448
- function Re() {
449
- return c ? {
450
- onWheel() {
451
- P.val.stop(), te();
452
- }
453
- } : {};
454
- }
455
- function H(e, t) {
456
- let r = 0;
457
- if (typeof e == "string" ? r = n.findIndex((i) => i.id === e) : r = e, r < 0 || r >= n.length) {
458
- if (t)
459
- throw new Error(t);
460
- console.error(`The item doesn't exist; check that the id provided - ${e} - is correct.`), r = -1;
461
- }
462
- return r;
463
- }
464
- function fe({ id: e, immediate: t, shouldReset: r, type: i }) {
465
- if (!v || V.current)
466
- return;
467
- d.current = !1, s.current = !1;
468
- const M = H(
469
- e,
470
- "The item you want to slide to doesn't exist; check the provided id."
471
- );
472
- if (M === p.current && !r)
473
- return;
474
- const z = H(n[p.current].id), X = H(n[M].id);
475
- X > z ? B({
476
- type: i || r ? "initial" : "click",
477
- index: X,
478
- immediate: t
479
- }) : q({
480
- type: i || r ? "initial" : "click",
481
- index: X,
482
- immediate: t
483
- });
484
- }
485
- function ae(e) {
486
- const t = H(e, "The item doesn't exist; check the provided id."), r = p.current;
487
- return u && r === n.length - 1 ? t === 0 : t === r + 1;
488
- }
489
- function de(e) {
490
- const t = H(e, "The item doesn't exist; check the provided id."), r = p.current;
491
- return u && r === 0 ? t === n.length - 1 : t === r - 1;
492
- }
493
- function ge(e) {
494
- return H(e, "The item you want to check doesn't exist; check the provided id.") === p.current;
495
- }
496
- function je() {
497
- return me ? "unset" : o === "x" ? "pan-y" : "pan-x";
498
- }
499
- const T = c ? {
500
- useListenToCustomEvent: ee,
501
- enterFullscreen: pe,
502
- exitFullscreen: xe,
503
- getIsFullscreen: Ie,
504
- slideToPrevItem: (e = !0) => {
505
- q({
506
- type: "click",
507
- immediate: !e
508
- });
509
- },
510
- slideToNextItem: (e = !0) => {
511
- B({
512
- type: "click",
513
- immediate: !e
514
- });
515
- }
516
- } : {
517
- useListenToCustomEvent: ee,
518
- enterFullscreen: pe,
519
- exitFullscreen: xe,
520
- getIsFullscreen: Ie,
521
- slideToPrevItem: (e = !0) => {
522
- q({
523
- type: "click",
524
- immediate: !e
525
- });
526
- },
527
- slideToNextItem: (e = !0) => {
528
- B({
529
- type: "click",
530
- immediate: !e
531
- });
532
- },
533
- slideToItem: (e, t = !0) => {
534
- fe({ id: e, immediate: !t });
535
- },
536
- getIsNextItem: ae,
537
- getIsPrevItem: de,
538
- getIsActiveItem: ge
539
- };
540
- Xe(() => {
541
- x.current && v && (K.current = !0, ye());
542
- }, [v]), E(() => {
543
- p.current !== C && (K.current = !0, fe({
544
- id: C,
545
- immediate: !Te
546
- }));
547
- }, [C]), E(() => {
548
- if (v) {
549
- if (C > n.length - 1)
550
- throw new Error(
551
- `initialActiveItem (${C}) is greater than the total quantity available items (${n.length}).`
552
- );
553
- w > n.length && console.warn(
554
- `itemsPerSlide (${w}) is greater than the total quantity available items (${n.length}). Fallback to ${n.length})`
555
- );
556
- }
557
- }, [C, n.length, w, v]), E(() => {
558
- le.current = window.innerWidth;
559
- }, []), E(() => {
560
- if (v) {
561
- if (u && c)
562
- throw new Error("`withLoop` and `freeScroll` can't be used together.");
563
- if (c && y === "fixed")
564
- throw new Error("`freeScroll` can't be used when `scrollType=fixed`.");
565
- if (ce && (y !== "fluid" || !c))
566
- throw new Error(
567
- "`enableFreeScrollDrag` must be used with `slideType=fluid` and `freeScroll=true`"
568
- );
569
- if (j && c)
570
- throw new Error("`slideGroupOfItems` and `freeScroll` can't be used together.");
571
- K.current = !0, ye();
572
- }
573
- }, [
574
- L,
575
- w,
576
- a,
577
- b,
578
- v,
579
- u,
580
- y,
581
- c,
582
- ce,
583
- j
584
- ]), E(() => {
585
- if (!v)
586
- return;
587
- function e() {
588
- document.hidden ? V.current = !0 : V.current = !1;
589
- }
590
- return document.addEventListener("visibilitychange", e), () => {
591
- document.removeEventListener("visibilitychange", e);
592
- };
593
- }, [v]), E(() => {
594
- if (f.current) {
595
- let e;
596
- const t = new Qe(() => {
597
- if (!V.current && !K.current) {
598
- le.current = window.innerWidth;
599
- const r = re();
600
- window.clearTimeout(e), e = setTimeout(() => {
601
- se.current = h(), typeof r == "function" && r();
602
- }, 100);
603
- }
604
- });
605
- return t.observe(f.current), () => {
606
- t.disconnect();
607
- };
608
- }
609
- }, [re, h]), E(() => {
610
- ue && ue({
611
- slideToNextItem: T.slideToNextItem,
612
- slideToPrevItem: T.slideToPrevItem,
613
- slideToItem: T == null ? void 0 : T.slideToItem
614
- });
615
- }, [ue, T.slideToItem, T.slideToNextItem, T.slideToPrevItem]);
616
- const Ve = /* @__PURE__ */ R.jsx(he.Provider, { value: T, children: ke }), De = /* @__PURE__ */ R.jsx(he.Provider, { value: T, children: /* @__PURE__ */ R.jsx(
617
- "div",
618
- {
619
- className: "use-spring-carousel-main-wrapper",
620
- ref: f,
621
- ...Re(),
622
- style: {
623
- display: "flex",
624
- position: "relative",
625
- width: "100%",
626
- height: "100%",
627
- ...Pe()
628
- },
629
- children: /* @__PURE__ */ R.jsxs(
630
- "div",
631
- {
632
- className: "use-spring-carousel-track-wrapper",
633
- ref: x,
634
- ...We(),
635
- style: {
636
- position: "relative",
637
- display: "flex",
638
- flexDirection: o === "x" ? "row" : "column",
639
- touchAction: je(),
640
- ...Ne()
641
- },
642
- children: [
643
- (c || !u) && a ? /* @__PURE__ */ R.jsx(
644
- "div",
645
- {
646
- style: {
647
- flexShrink: 0,
648
- width: a
649
- }
650
- }
651
- ) : null,
652
- A.map((e, t) => /* @__PURE__ */ R.jsx(
653
- "div",
654
- {
655
- className: "use-spring-carousel-item",
656
- "data-testid": "use-spring-carousel-item-wrapper",
657
- style: {
658
- display: "flex",
659
- position: "relative",
660
- flex: "1",
661
- ...$e(
662
- t === A.findIndex(
663
- (r) => r.id === A[A.length - 1].id
664
- )
665
- )
666
- },
667
- children: typeof e.renderItem == "function" ? e.renderItem({
668
- getIsActiveItem: ge,
669
- getIsNextItem: ae,
670
- getIsPrevItem: de,
671
- useListenToCustomEvent: ee
672
- }) : e.renderItem
673
- },
674
- `${e.id}-${t}`
675
- )),
676
- (c || !u) && a ? /* @__PURE__ */ R.jsx(
677
- "div",
678
- {
679
- style: {
680
- flexShrink: 0,
681
- width: a
682
- }
683
- }
684
- ) : null
685
- ]
686
- }
687
- )
688
- }
689
- ) });
690
- return { ...T, carouselFragment: De, thumbsFragment: Ve };
691
- }
692
- const he = Je(void 0);
693
- function nr() {
694
- const n = Ke(he);
695
- if (!n)
696
- throw new Error("useSpringCarouselContext must be used within the carousel.");
697
- return n;
698
- }
699
- export {
700
- tr as useSpringCarousel,
701
- nr as useSpringCarouselContext
702
- };
703
- //# sourceMappingURL=index.es2.js.map