react-spring-carousel 3.0.0-beta001 → 3.0.0-beta003

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/umd/index.js CHANGED
@@ -1,2 +1,779 @@
1
- !function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react/jsx-runtime"),require("@react-spring/web"),require("react"),require("@use-gesture/react"),require("screenfull")):"function"==typeof define&&define.amd?define(["exports","react/jsx-runtime","@react-spring/web","react","@use-gesture/react","screenfull"],t):t((e="undefined"!=typeof globalThis?globalThis:e||self).ReactSpringCarousel={},e.ReactJSXRuntime,e.web,e.React,e.UseGestureReact,e.Screenfull)}(this,(function(e,t,r,n,i,c){"use strict";const u="RSC::Event";function o({thumbsSlideAxis:e="x",withThumbs:i=!1,prepareThumbsData:c,items:u}){const o=n.useRef(null),[s,l]=r.useSpring((()=>({val:0})));function a(){var t;return Math.round(Number(null===(t=o.current)||void 0===t?void 0:t["x"===e?"scrollWidth":"scrollHeight"])-o.current.getBoundingClientRect()["x"===e?"width":"height"])}return{thumbsFragment:i?t.jsx("div",Object.assign({className:"use-spring-carousel-thumbs-wrapper",ref:o,onWheel:()=>s.val.stop(),style:Object.assign({display:"flex",flex:"1",position:"relative",width:"100%",height:"100%",flexDirection:"x"===e?"row":"column"},"x"===e?{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(u)):e(u)}().map((({id:e,renderThumb:r})=>{const n=`thumb-item-${e}`;return t.jsx("div",Object.assign({id:n,className:"thumb-item"},{children:r}),n)}))})):null,handleScroll:function(t){var r,n;const i=o.current?o.current.querySelector(`#thumb-item-${u[t].id}`):null;if(i&&o.current&&!function(e){const t=e.getBoundingClientRect();return t.top>=0&&t.left>=0&&t.bottom<=(window.innerHeight||document.documentElement.clientHeight)&&t.right<=(window.innerWidth||document.documentElement.clientWidth)}(i)){const t=i.offsetLeft,c=t>a()?a():t;l.start({from:{val:null!==(n=null===(r=o.current)||void 0===r?void 0:r["x"===e?"scrollLeft":"scrollTop"])&&void 0!==n?n:0},to:{val:c},onChange:({value:t})=>{o.current&&(o.current["x"===e?"scrollLeft":"scrollTop"]=Math.abs(t.val))}})}}}}const s=n.createContext(void 0);e.useSpringCarousel=function({items:e,init:s=!0,withThumbs:l,thumbsSlideAxis:a="x",itemsPerSlide:d=1,slideType:f="fixed",gutter:m=0,withLoop:g=!1,startEndGutter:h=0,carouselSlideAxis:v="x",disableGestures:x=!1,draggingSlideTreshold:p,slideWhenThresholdIsReached:b=!1,freeScroll:w,enableFreeScrollDrag:y,initialStartingPosition:E,prepareThumbsData:R,initialActiveItem:T=0}){const j=n.useRef(0),C=n.useRef(null!=p?p:0),I=n.useRef("initial"),S=n.useRef("initial"),O=n.useRef(0),[L,M]=r.useSpring((()=>({val:0,pause:!s,onChange({value:e}){w&&$.current?(te(),"x"===v?$.current.scrollLeft=Math.abs(e.val):$.current.scrollTop=Math.abs(e.val)):W.current&&(W.current.style.transform="x"===v?`translate3d(${e.val}px, 0px,0px)`:`translate3d(0px,${e.val}px,0px)`)}}))),F=n.useRef(T),A=n.useRef(0===T),N=n.useRef(!1),$=n.useRef(null),W=n.useRef(null),q=n.useCallback((()=>g?[...e.map((e=>Object.assign(Object.assign({},e),{id:`prev-repeated-item-${e.id}`}))),...e,...e.map((e=>Object.assign(Object.assign({},e),{id:`next-repeated-item-${e.id}`})))]:[...e]),[e,g])(),{emitEvent:D,useListenToCustomEvent:k}=function(){const e=n.useRef(null);return n.useEffect((()=>{e.current=document.createElement("div")}),[]),{useListenToCustomEvent:function(t){n.useEffect((()=>{function r(e){t(e.detail)}if(e.current)return e.current.addEventListener(u,r,!1),()=>{var t;null===(t=e.current)||void 0===t||t.removeEventListener(u,r,!1)}}),[])},emitEvent:function(t){if(e.current){const r=new CustomEvent(u,{detail:t});e.current.dispatchEvent(r)}}}}(),{thumbsFragment:B,handleScroll:H}=o({withThumbs:!!l,thumbsSlideAxis:a,prepareThumbsData:R,items:e}),{enterFullscreen:z,exitFullscreen:P,getIsFullscreen:G}=function({mainCarouselWrapperRef:e,emitEvent:t,handleResize:r}){const i=n.useRef(!1);function u(e){i.current=e}return n.useEffect((()=>{function e(){document.fullscreenElement&&(u(!0),t({eventName:"onFullscreenChange",isFullscreen:!0}),r&&r()),document.fullscreenElement||(u(!1),t({eventName:"onFullscreenChange",isFullscreen:!1}),r&&r())}if(c.isEnabled)return c.on("change",e),()=>{c.isEnabled&&c.off("change",e)}}),[]),{enterFullscreen:function(t){c.isEnabled&&c.request(t||e.current)},exitFullscreen:function(){c.isEnabled&&c.exit()},getIsFullscreen:function(){return i.current}}}({mainCarouselWrapperRef:$,emitEvent:D,handleResize:()=>K()});function X(){var e;const t=null===(e=$.current)||void 0===e?void 0:e.querySelector(".use-spring-carousel-item");if(!t)throw Error("No carousel items available!");return t.getBoundingClientRect()["x"===v?"width":"height"]+m}function Y({from:t,to:n,nextActiveItem:i,immediate:c=!1,slideMode:u}){S.current=u,"number"==typeof i&&(w||(F.current=i),D({eventName:"onSlideStartChange",slideActionType:I.current,slideMode:S.current,nextItem:{startReached:A.current,endReached:N.current,index:w?-1:F.current,id:w?"":e[F.current].id}})),O.current=n,M.start({immediate:c,from:{val:t},to:{val:n},config:Object.assign(Object.assign({},r.config.default),{velocity:L.val.velocity}),onRest(t){!c&&t.finished&&D({eventName:"onSlideChange",slideActionType:I.current,slideMode:S.current,currentItem:{startReached:A.current,endReached:N.current,index:w?-1:F.current,id:w?"":e[F.current].id}})}}),l&&!c&&H(F.current)}function J(){var t;return g?X()*e.length:Math.round(Number(null===(t=W.current)||void 0===t?void 0:t["x"===v?"scrollWidth":"scrollHeight"])-W.current.getBoundingClientRect()["x"===v?"width":"height"])}function U(){var e;const t=null===(e=W.current)||void 0===e?void 0:e.querySelector(".use-spring-carousel-item");if(!t)throw Error("No carousel items available!");return t.getBoundingClientRect()["x"===v?"width":"height"]+m}function K(){const t="x"===v?"left":"top";function r(e){const r=W.current;r&&(g?(r.style.top="0px",r.style.left="0px",r.style[t]=`-${e-h}px`):(r.style.left="0px",r.style.top="0px"))}const n=Math.abs(Q());if(n<J()&&N.current&&(N.current=!1),n>J()){const e=-J();return N.current=!0,O.current=e,void M.start({immediate:!0,val:e})}if(r("center"===E?U()*e.length-X()*Math.round((d-1)/2):"end"===E?U()*e.length-X()*Math.round(d-1):U()*e.length),!w&&"fixed"===f){const e=-X()*F.current;O.current=e,M.start({immediate:!0,val:e})}}function Q(){return w&&$.current?$.current["x"===v?"scrollLeft":"scrollTop"]:L.val.get()}function V(e,t){if(w&&"next"===e){const e=O.current+X();return e>J()?J():e}if(w&&"prev"===e){const e=O.current-X();return e<0?0:e}return"next"===e?t?-t*X():O.current-X():t?-t*X():O.current+X()}function Z(t="click",r){if(!s||A.current&&!g)return;I.current="prev",N.current=!1;const n=r||F.current-1;if(!g){const e=V("prev",r)+X()/3>0;if(A.current)return;if(e)return A.current=!0,N.current=!1,void Y({slideMode:t,from:Q(),to:0,nextActiveItem:0})}if(g&&A.current)return A.current=!1,N.current=!0,void Y({slideMode:t,from:Q()-X()*e.length,to:-X()*e.length+X(),nextActiveItem:e.length-1});0===n&&(A.current=!0),n!==e.length-1&&-1!==n||(N.current=!0),Y({slideMode:t,from:Q(),to:V("prev",r),nextActiveItem:n})}function _(t="click",r){if(!s||N.current&&!g)return;I.current="next",A.current=!1;const n=r||F.current+1;if(!g){const e=Math.abs(V("next",r))>J()-X()/3;if(N.current)return;if(e)return A.current=!1,N.current=!0,void Y({slideMode:t,from:Q(),to:-J(),nextActiveItem:n})}if(g&&N.current)return N.current=!1,A.current=!0,void Y({slideMode:t,from:Q()+X()*e.length,to:0,nextActiveItem:0});0===n&&(A.current=!0),n===e.length-1&&(N.current=!0),Y({slideMode:t,from:Q(),to:V("next",r),nextActiveItem:n})}n.useEffect((()=>{j.current=window.innerWidth}),[]),n.useEffect((()=>{K()}),[E,d,g,h,m,w,f,s]),n.useLayoutEffect((()=>{W.current&&K()}),[]),n.useEffect((()=>{C.current=p||Math.floor(X()/2/2)}),[p]),n.useEffect((()=>{function e(){window.innerWidth!==j.current&&(j.current=window.innerWidth,K())}return window.addEventListener("resize",e),()=>{window.removeEventListener("resize",e)}}),[]);const ee=i.useDrag((e=>{const t=e.dragging,n=e.offset["x"===v?0:1],i=e.movement["x"===v?0:1],c=e.direction["x"===v?0:1],u=i>C.current,o=i<-C.current;if(t)return I.current=c>0?"prev":"next",D(Object.assign(Object.assign({},e),{eventName:"onDrag",slideActionType:I.current})),w?"prev"===I.current&&n>0?(e.cancel(),void M.start({from:{val:Q()},to:{val:0},config:{velocity:e.velocity,friction:50,tension:1e3}})):void M.start({from:{val:Q()},to:{val:-n},config:{velocity:e.velocity,friction:50,tension:1e3}}):(M.start({val:n,config:{velocity:e.velocity,friction:50,tension:1e3}}),void(b&&o?(_("drag"),e.cancel()):b&&u&&(Z("drag"),e.cancel())));e.last&&!e.canceled&&w&&("prev"===I.current&&Z("drag"),"next"===I.current&&_("drag")),!e.last||e.canceled||w||(o?!g&&N.current?M.start({val:-J(),config:Object.assign(Object.assign({},r.config.default),{velocity:e.velocity})}):_("drag"):u?!g&&A.current?M.start({val:0,config:Object.assign(Object.assign({},r.config.default),{velocity:e.velocity})}):Z("drag"):M.start({val:O.current,config:Object.assign(Object.assign({},r.config.default),{velocity:e.velocity})}))}),{enabled:s&&!x&&!w||!!w&&!!y,axis:v,from:()=>w&&$.current?[-$.current.scrollLeft,-$.current.scrollTop]:[L.val.get(),L.val.get()]});function te(){$.current&&(O.current=$.current["x"===v?"scrollLeft":"scrollTop"],0===$.current["x"===v?"scrollLeft":"scrollTop"]&&(A.current=!0,N.current=!1),$.current["x"===v?"scrollLeft":"scrollTop"]>0&&$.current["x"===v?"scrollLeft":"scrollTop"]<J()&&(A.current=!1,N.current=!1),$.current["x"===v?"scrollLeft":"scrollTop"]===J()&&(A.current=!1,N.current=!0))}function re(t){return e.findIndex((e=>e.id===t))}function ne(t,r){let n=0;if(n="string"==typeof t?e.findIndex((e=>e.id===t)):t,n<0||n>=e.length)throw new Error(r);return n}return{useListenToCustomEvent:k,carouselFragment:t.jsx("div",Object.assign({ref:$},w?{onWheel(){L.val.stop(),te()}}:{},{style:Object.assign({display:"flex",position:"relative",width:"100%",height:"100%"},w?"x"===v?{overflowX:"auto"}:{overflowY:"auto"}:{})},{children:t.jsx("div",Object.assign({ref:W},ee(),{style:Object.assign({position:"relative",display:"flex",flexDirection:"x"===v?"row":"column",touchAction:"none"},function(){const e=`calc(100% - ${2*h}px)`;return{width:"x"===v?e:"100%",height:"y"===v?e:"100%"}}())},{children:q.map(((e,r)=>t.jsx("div",Object.assign({className:"use-spring-carousel-item","data-testid":"use-spring-carousel-item-wrapper",style:Object.assign({display:"flex",position:"relative",flex:"1"},"fixed"!==f||w?Object.assign({marginRight:`${m}px`}):{marginRight:`${m}px`,flex:`1 0 calc(100% / ${d} - ${m*(d-1)/d}px)`})},{children:e.renderItem}),`${e.id}-${r}`)))}))})),enterFullscreen:z,exitFullscreen:P,getIsFullscreen:G,thumbsFragment:B,slideToItem:function(t){if(!s)return;A.current=!1,N.current=!1;const r=ne(t,"The item you want to slide to doesn't exist; please check che item id or the index you're passing to.");if(r===F.current)return;const n=re(e[F.current].id),i=re(e[r].id);i>n?_("click",i):Z("click",i)},getIsNextItem:function(t){const r=re(t),n=F.current;return g&&n===e.length-1?0===r:r===n+1},getIsPrevItem:function(t){const r=re(t),n=F.current;return g&&0===n?r===e.length-1:r===n-1},slideToPrevItem:()=>Z(),slideToNextItem:()=>_(),getIsActiveItem:e=>ne(e,"The item you want to check doesn't exist")===F.current}},e.useSpringCarouselContext=function(){const e=n.useContext(s);if(!e)throw new Error("useSpringCarouselContext must be used within the carousel.");return e}}));
1
+ (function (global, factory) {
2
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('react/jsx-runtime'), require('@react-spring/web'), require('react'), require('@use-gesture/react'), require('screenfull')) :
3
+ typeof define === 'function' && define.amd ? define(['exports', 'react/jsx-runtime', '@react-spring/web', 'react', '@use-gesture/react', 'screenfull'], factory) :
4
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.ReactSpringCarousel = {}, global.ReactJSXRuntime, global.web, global.React, global.UseGestureReact, global.Screenfull));
5
+ })(this, (function (exports, jsxRuntime, web, react, react$1, screenfull) { 'use strict';
6
+
7
+ const eventLabel = "RSC::Event";
8
+ function useEventsModule() {
9
+ const targetEvent = react.useRef(null);
10
+ react.useEffect(() => {
11
+ targetEvent.current = document.createElement("div");
12
+ }, []);
13
+ function useListenToCustomEvent(eventHandler) {
14
+ react.useEffect(() => {
15
+ function handleEvent(event) {
16
+ eventHandler(event.detail);
17
+ }
18
+ if (targetEvent.current) {
19
+ // @ts-ignore
20
+ targetEvent.current.addEventListener(eventLabel, handleEvent, false);
21
+ return () => {
22
+ var _a;
23
+ // @ts-ignore
24
+ (_a = targetEvent.current) === null || _a === void 0 ? void 0 : _a.removeEventListener(eventLabel, handleEvent, false);
25
+ };
26
+ }
27
+ }, []);
28
+ }
29
+ function emitEvent(event) {
30
+ if (targetEvent.current) {
31
+ const newEvent = new CustomEvent(eventLabel, {
32
+ detail: event,
33
+ });
34
+ targetEvent.current.dispatchEvent(newEvent);
35
+ }
36
+ }
37
+ return {
38
+ useListenToCustomEvent,
39
+ emitEvent,
40
+ };
41
+ }
42
+
43
+ function useFullscreenModule({ mainCarouselWrapperRef, emitEvent, handleResize, }) {
44
+ const isFullscreen = react.useRef(false);
45
+ react.useEffect(() => {
46
+ function handleFullscreenChange() {
47
+ if (document.fullscreenElement) {
48
+ setIsFullscreen(true);
49
+ emitEvent({
50
+ eventName: "onFullscreenChange",
51
+ isFullscreen: true,
52
+ });
53
+ handleResize && handleResize();
54
+ }
55
+ if (!document.fullscreenElement) {
56
+ setIsFullscreen(false);
57
+ emitEvent({
58
+ eventName: "onFullscreenChange",
59
+ isFullscreen: false,
60
+ });
61
+ handleResize && handleResize();
62
+ }
63
+ }
64
+ if (screenfull.isEnabled) {
65
+ screenfull.on("change", handleFullscreenChange);
66
+ return () => {
67
+ if (screenfull.isEnabled) {
68
+ screenfull.off("change", handleFullscreenChange);
69
+ }
70
+ };
71
+ }
72
+ }, []);
73
+ function setIsFullscreen(_isFullscreen) {
74
+ isFullscreen.current = _isFullscreen;
75
+ }
76
+ function getIsFullscreen() {
77
+ return isFullscreen.current;
78
+ }
79
+ function enterFullscreen(elementRef) {
80
+ if (screenfull.isEnabled) {
81
+ screenfull.request((elementRef || mainCarouselWrapperRef.current));
82
+ }
83
+ }
84
+ function exitFullscreen() {
85
+ screenfull.isEnabled && screenfull.exit();
86
+ }
87
+ return {
88
+ enterFullscreen,
89
+ exitFullscreen,
90
+ getIsFullscreen,
91
+ };
92
+ }
93
+
94
+ function isInViewport(el) {
95
+ const rect = el.getBoundingClientRect();
96
+ return (rect.top >= 0 &&
97
+ rect.left >= 0 &&
98
+ rect.bottom <=
99
+ (window.innerHeight || document.documentElement.clientHeight) &&
100
+ rect.right <= (window.innerWidth || document.documentElement.clientWidth));
101
+ }
102
+ function useThumbsModule({ thumbsSlideAxis = "x", withThumbs = false, prepareThumbsData, items, }) {
103
+ const wrapperRef = react.useRef(null);
104
+ const [spring, setSpring] = web.useSpring(() => ({
105
+ val: 0,
106
+ }));
107
+ function getTotalScrollValue() {
108
+ var _a;
109
+ return Math.round(Number((_a = wrapperRef.current) === null || _a === void 0 ? void 0 : _a[thumbsSlideAxis === "x" ? "scrollWidth" : "scrollHeight"]) -
110
+ wrapperRef.current.getBoundingClientRect()[thumbsSlideAxis === "x" ? "width" : "height"]);
111
+ }
112
+ function handleScroll(activeItem) {
113
+ var _a, _b;
114
+ function getThumbNode() {
115
+ if (wrapperRef.current) {
116
+ return wrapperRef.current.querySelector(`#thumb-item-${items[activeItem].id}`);
117
+ }
118
+ return null;
119
+ }
120
+ const thumbNode = getThumbNode();
121
+ if (thumbNode && wrapperRef.current) {
122
+ if (!isInViewport(thumbNode)) {
123
+ const offset = thumbNode.offsetLeft;
124
+ const val = offset > getTotalScrollValue() ? getTotalScrollValue() : offset;
125
+ setSpring.start({
126
+ from: {
127
+ val: (_b = (_a = wrapperRef.current) === null || _a === void 0 ? void 0 : _a[thumbsSlideAxis === "x" ? "scrollLeft" : "scrollTop"]) !== null && _b !== void 0 ? _b : 0,
128
+ },
129
+ to: {
130
+ val,
131
+ },
132
+ onChange: ({ value }) => {
133
+ if (wrapperRef.current) {
134
+ wrapperRef.current[thumbsSlideAxis === "x" ? "scrollLeft" : "scrollTop"] = Math.abs(value.val);
135
+ }
136
+ },
137
+ });
138
+ }
139
+ }
140
+ }
141
+ function handlePrepareThumbsData() {
142
+ function getPreparedItems(_items) {
143
+ return _items.map((i) => ({
144
+ id: i.id,
145
+ renderThumb: i.renderThumb,
146
+ }));
147
+ }
148
+ if (prepareThumbsData) {
149
+ return prepareThumbsData(getPreparedItems(items));
150
+ }
151
+ return getPreparedItems(items);
152
+ }
153
+ const thumbsFragment = withThumbs ? (jsxRuntime.jsx("div", Object.assign({ className: "use-spring-carousel-thumbs-wrapper", ref: wrapperRef, onWheel: () => spring.val.stop(), style: Object.assign({ display: "flex", flex: "1", position: "relative", width: "100%", height: "100%", flexDirection: thumbsSlideAxis === "x" ? "row" : "column" }, (thumbsSlideAxis === "x"
154
+ ? { overflowX: "auto" }
155
+ : {
156
+ overflowY: "auto",
157
+ maxHeight: "100%",
158
+ })) }, { children: handlePrepareThumbsData().map(({ id, renderThumb }) => {
159
+ const thumbId = `thumb-item-${id}`;
160
+ return (jsxRuntime.jsx("div", Object.assign({ id: thumbId, className: "thumb-item" }, { children: renderThumb }), thumbId));
161
+ }) }))) : null;
162
+ return {
163
+ thumbsFragment,
164
+ handleScroll,
165
+ };
166
+ }
167
+
168
+ function useSpringCarousel({ items, init = true, withThumbs, thumbsSlideAxis = "x", itemsPerSlide = 1, slideType = "fixed", gutter = 0, withLoop = false, startEndGutter = 0, carouselSlideAxis = "x", disableGestures = false, draggingSlideTreshold: _draggingSlideTreshold, slideWhenThresholdIsReached = false, freeScroll, enableFreeScrollDrag, initialStartingPosition, prepareThumbsData, initialActiveItem = 0, }) {
169
+ const prevWindowWidth = react.useRef(0);
170
+ const draggingSlideTreshold = react.useRef(_draggingSlideTreshold !== null && _draggingSlideTreshold !== void 0 ? _draggingSlideTreshold : 0);
171
+ const slideActionType = react.useRef("initial");
172
+ const slideModeType = react.useRef("initial");
173
+ const prevSlidedValue = react.useRef(0);
174
+ const [spring, setSpring] = web.useSpring(() => ({
175
+ val: 0,
176
+ pause: !init,
177
+ onChange({ value }) {
178
+ if (freeScroll && mainCarouselWrapperRef.current) {
179
+ setStartEndItemReachedOnFreeScroll();
180
+ if (carouselSlideAxis === "x") {
181
+ mainCarouselWrapperRef.current.scrollLeft = Math.abs(value.val);
182
+ }
183
+ else {
184
+ mainCarouselWrapperRef.current.scrollTop = Math.abs(value.val);
185
+ }
186
+ }
187
+ else if (carouselTrackWrapperRef.current) {
188
+ if (carouselSlideAxis === "x") {
189
+ carouselTrackWrapperRef.current.style.transform = `translate3d(${value.val}px, 0px,0px)`;
190
+ }
191
+ else {
192
+ carouselTrackWrapperRef.current.style.transform = `translate3d(0px,${value.val}px,0px)`;
193
+ }
194
+ }
195
+ },
196
+ }));
197
+ const activeItem = react.useRef(initialActiveItem);
198
+ const firstItemReached = react.useRef(initialActiveItem === 0);
199
+ const lastItemReached = react.useRef(false);
200
+ const mainCarouselWrapperRef = react.useRef(null);
201
+ const carouselTrackWrapperRef = react.useRef(null);
202
+ const getItems = react.useCallback(() => {
203
+ if (withLoop) {
204
+ return [
205
+ ...items.map((i) => (Object.assign(Object.assign({}, i), { id: `prev-repeated-item-${i.id}` }))),
206
+ ...items,
207
+ ...items.map((i) => (Object.assign(Object.assign({}, i), { id: `next-repeated-item-${i.id}` }))),
208
+ ];
209
+ }
210
+ return [...items];
211
+ }, [items, withLoop]);
212
+ const internalItems = getItems();
213
+ const { emitEvent, useListenToCustomEvent } = useEventsModule();
214
+ const { thumbsFragment, handleScroll } = useThumbsModule({
215
+ withThumbs: !!withThumbs,
216
+ thumbsSlideAxis,
217
+ prepareThumbsData,
218
+ items: items,
219
+ });
220
+ const { enterFullscreen, exitFullscreen, getIsFullscreen } = useFullscreenModule({
221
+ mainCarouselWrapperRef,
222
+ emitEvent,
223
+ handleResize: () => adjustCarouselWrapperPosition(),
224
+ });
225
+ function getItemStyles() {
226
+ if (slideType === "fixed" && !freeScroll) {
227
+ return {
228
+ marginRight: `${gutter}px`,
229
+ flex: `1 0 calc(100% / ${itemsPerSlide} - ${(gutter * (itemsPerSlide - 1)) / itemsPerSlide}px)`,
230
+ };
231
+ }
232
+ return Object.assign({ marginRight: `${gutter}px` });
233
+ }
234
+ function getSlideValue() {
235
+ var _a;
236
+ const carouselItem = (_a = mainCarouselWrapperRef.current) === null || _a === void 0 ? void 0 : _a.querySelector(".use-spring-carousel-item");
237
+ if (!carouselItem) {
238
+ throw Error("No carousel items available!");
239
+ }
240
+ return (carouselItem.getBoundingClientRect()[carouselSlideAxis === "x" ? "width" : "height"] + gutter);
241
+ }
242
+ function slideToItem({ from, to, nextActiveItem, immediate = false, slideMode, }) {
243
+ slideModeType.current = slideMode;
244
+ if (typeof nextActiveItem === "number") {
245
+ if (!freeScroll) {
246
+ activeItem.current = nextActiveItem;
247
+ }
248
+ emitEvent({
249
+ eventName: "onSlideStartChange",
250
+ slideActionType: slideActionType.current,
251
+ slideMode: slideModeType.current,
252
+ nextItem: {
253
+ startReached: firstItemReached.current,
254
+ endReached: lastItemReached.current,
255
+ index: freeScroll ? -1 : activeItem.current,
256
+ id: freeScroll ? "" : items[activeItem.current].id,
257
+ },
258
+ });
259
+ }
260
+ prevSlidedValue.current = to;
261
+ setSpring.start({
262
+ immediate,
263
+ from: {
264
+ val: from,
265
+ },
266
+ to: {
267
+ val: to,
268
+ },
269
+ config: Object.assign(Object.assign({}, web.config.default), { velocity: spring.val.velocity }),
270
+ onRest(value) {
271
+ if (!immediate && value.finished) {
272
+ emitEvent({
273
+ eventName: "onSlideChange",
274
+ slideActionType: slideActionType.current,
275
+ slideMode: slideModeType.current,
276
+ currentItem: {
277
+ startReached: firstItemReached.current,
278
+ endReached: lastItemReached.current,
279
+ index: freeScroll ? -1 : activeItem.current,
280
+ id: freeScroll ? "" : items[activeItem.current].id,
281
+ },
282
+ });
283
+ }
284
+ },
285
+ });
286
+ if (withThumbs && !immediate) {
287
+ handleScroll(activeItem.current);
288
+ }
289
+ }
290
+ function getTotalScrollValue() {
291
+ var _a;
292
+ if (withLoop) {
293
+ return getSlideValue() * items.length;
294
+ }
295
+ return Math.round(Number((_a = carouselTrackWrapperRef.current) === null || _a === void 0 ? void 0 : _a[carouselSlideAxis === "x" ? "scrollWidth" : "scrollHeight"]) -
296
+ carouselTrackWrapperRef.current.getBoundingClientRect()[carouselSlideAxis === "x" ? "width" : "height"]);
297
+ }
298
+ function getAnimatedWrapperStyles() {
299
+ const percentValue = `calc(100% - ${startEndGutter * 2}px)`;
300
+ return {
301
+ width: carouselSlideAxis === "x" ? percentValue : "100%",
302
+ height: carouselSlideAxis === "y" ? percentValue : "100%",
303
+ };
304
+ }
305
+ function getCarouselItemWidth() {
306
+ var _a;
307
+ const carouselItem = (_a = carouselTrackWrapperRef.current) === null || _a === void 0 ? void 0 : _a.querySelector(".use-spring-carousel-item");
308
+ if (!carouselItem) {
309
+ throw Error("No carousel items available!");
310
+ }
311
+ return (carouselItem.getBoundingClientRect()[carouselSlideAxis === "x" ? "width" : "height"] + gutter);
312
+ }
313
+ function adjustCarouselWrapperPosition() {
314
+ const positionProperty = carouselSlideAxis === "x" ? "left" : "top";
315
+ function setPosition(v) {
316
+ const ref = carouselTrackWrapperRef.current;
317
+ if (!ref)
318
+ return;
319
+ if (withLoop) {
320
+ ref.style.top = "0px";
321
+ ref.style.left = "0px";
322
+ ref.style[positionProperty] = `-${v - startEndGutter}px`;
323
+ }
324
+ else {
325
+ ref.style.left = "0px";
326
+ ref.style.top = "0px";
327
+ }
328
+ }
329
+ const currentFromValue = Math.abs(getFromValue());
330
+ if (currentFromValue < getTotalScrollValue() && lastItemReached.current) {
331
+ lastItemReached.current = false;
332
+ }
333
+ if (currentFromValue > getTotalScrollValue()) {
334
+ const val = -getTotalScrollValue();
335
+ lastItemReached.current = true;
336
+ prevSlidedValue.current = val;
337
+ setSpring.start({
338
+ immediate: true,
339
+ val,
340
+ });
341
+ return;
342
+ }
343
+ if (initialStartingPosition === "center") {
344
+ setPosition(getCarouselItemWidth() * items.length -
345
+ getSlideValue() * Math.round((itemsPerSlide - 1) / 2));
346
+ }
347
+ else if (initialStartingPosition === "end") {
348
+ setPosition(getCarouselItemWidth() * items.length -
349
+ getSlideValue() * Math.round(itemsPerSlide - 1));
350
+ }
351
+ else {
352
+ setPosition(getCarouselItemWidth() * items.length);
353
+ }
354
+ if (!freeScroll && slideType === "fixed") {
355
+ const val = -(getSlideValue() * activeItem.current);
356
+ prevSlidedValue.current = val;
357
+ setSpring.start({
358
+ immediate: true,
359
+ val,
360
+ });
361
+ }
362
+ }
363
+ function getFromValue() {
364
+ if (freeScroll && mainCarouselWrapperRef.current) {
365
+ return mainCarouselWrapperRef.current[carouselSlideAxis === "x" ? "scrollLeft" : "scrollTop"];
366
+ }
367
+ return spring.val.get();
368
+ }
369
+ function getToValue(type, index) {
370
+ if (freeScroll && type === "next") {
371
+ const next = prevSlidedValue.current + getSlideValue();
372
+ if (next > getTotalScrollValue()) {
373
+ return getTotalScrollValue();
374
+ }
375
+ return next;
376
+ }
377
+ if (freeScroll && type === "prev") {
378
+ const next = prevSlidedValue.current - getSlideValue();
379
+ if (next < 0) {
380
+ return 0;
381
+ }
382
+ return next;
383
+ }
384
+ if (type === "next") {
385
+ if (index) {
386
+ return -(index * getSlideValue());
387
+ }
388
+ return prevSlidedValue.current - getSlideValue();
389
+ }
390
+ if (index) {
391
+ return -(index * getSlideValue());
392
+ }
393
+ return prevSlidedValue.current + getSlideValue();
394
+ }
395
+ function slideToPrevItem(type = "click", index) {
396
+ if (!init || (firstItemReached.current && !withLoop))
397
+ return;
398
+ slideActionType.current = "prev";
399
+ lastItemReached.current = false;
400
+ const nextItem = index || activeItem.current - 1;
401
+ if (!withLoop) {
402
+ const nextItemWillExceed = getToValue("prev", index) + getSlideValue() / 3 > 0;
403
+ if (firstItemReached.current)
404
+ return;
405
+ if (nextItemWillExceed) {
406
+ firstItemReached.current = true;
407
+ lastItemReached.current = false;
408
+ slideToItem({
409
+ slideMode: type,
410
+ from: getFromValue(),
411
+ to: 0,
412
+ nextActiveItem: 0,
413
+ });
414
+ return;
415
+ }
416
+ }
417
+ if (withLoop && firstItemReached.current) {
418
+ firstItemReached.current = false;
419
+ lastItemReached.current = true;
420
+ slideToItem({
421
+ slideMode: type,
422
+ from: getFromValue() - getSlideValue() * items.length,
423
+ to: -(getSlideValue() * items.length) + getSlideValue(),
424
+ nextActiveItem: items.length - 1,
425
+ });
426
+ return;
427
+ }
428
+ if (nextItem === 0) {
429
+ firstItemReached.current = true;
430
+ }
431
+ if (nextItem === items.length - 1 || nextItem === -1) {
432
+ lastItemReached.current = true;
433
+ }
434
+ slideToItem({
435
+ slideMode: type,
436
+ from: getFromValue(),
437
+ to: getToValue("prev", index),
438
+ nextActiveItem: nextItem,
439
+ });
440
+ }
441
+ function slideToNextItem(type = "click", index) {
442
+ if (!init || (lastItemReached.current && !withLoop))
443
+ return;
444
+ slideActionType.current = "next";
445
+ firstItemReached.current = false;
446
+ const nextItem = index || activeItem.current + 1;
447
+ if (!withLoop) {
448
+ const nextItemWillExceed = Math.abs(getToValue("next", index)) >
449
+ getTotalScrollValue() - getSlideValue() / 3;
450
+ if (lastItemReached.current)
451
+ return;
452
+ if (nextItemWillExceed) {
453
+ firstItemReached.current = false;
454
+ lastItemReached.current = true;
455
+ slideToItem({
456
+ slideMode: type,
457
+ from: getFromValue(),
458
+ to: -getTotalScrollValue(),
459
+ nextActiveItem: nextItem,
460
+ });
461
+ return;
462
+ }
463
+ }
464
+ if (withLoop && lastItemReached.current) {
465
+ lastItemReached.current = false;
466
+ firstItemReached.current = true;
467
+ slideToItem({
468
+ slideMode: type,
469
+ from: getFromValue() + getSlideValue() * items.length,
470
+ to: 0,
471
+ nextActiveItem: 0,
472
+ });
473
+ return;
474
+ }
475
+ if (nextItem === 0) {
476
+ firstItemReached.current = true;
477
+ }
478
+ if (nextItem === items.length - 1) {
479
+ lastItemReached.current = true;
480
+ }
481
+ slideToItem({
482
+ slideMode: type,
483
+ from: getFromValue(),
484
+ to: getToValue("next", index),
485
+ nextActiveItem: nextItem,
486
+ });
487
+ }
488
+ react.useEffect(() => {
489
+ prevWindowWidth.current = window.innerWidth;
490
+ }, []);
491
+ react.useEffect(() => {
492
+ adjustCarouselWrapperPosition();
493
+ }, [
494
+ initialStartingPosition,
495
+ itemsPerSlide,
496
+ withLoop,
497
+ startEndGutter,
498
+ gutter,
499
+ freeScroll,
500
+ slideType,
501
+ init,
502
+ ]);
503
+ react.useLayoutEffect(() => {
504
+ /**
505
+ * Set initial track position
506
+ */
507
+ if (carouselTrackWrapperRef.current) {
508
+ adjustCarouselWrapperPosition();
509
+ }
510
+ }, []);
511
+ react.useEffect(() => {
512
+ if (_draggingSlideTreshold) {
513
+ draggingSlideTreshold.current = _draggingSlideTreshold;
514
+ }
515
+ else {
516
+ draggingSlideTreshold.current = Math.floor(getSlideValue() / 2 / 2);
517
+ }
518
+ }, [_draggingSlideTreshold]);
519
+ react.useEffect(() => {
520
+ function handleResize() {
521
+ if (window.innerWidth === prevWindowWidth.current)
522
+ return;
523
+ prevWindowWidth.current = window.innerWidth;
524
+ adjustCarouselWrapperPosition();
525
+ }
526
+ window.addEventListener("resize", handleResize);
527
+ return () => {
528
+ window.removeEventListener("resize", handleResize);
529
+ };
530
+ }, []);
531
+ const bindDrag = react$1.useDrag((state) => {
532
+ const isDragging = state.dragging;
533
+ const movement = state.offset[carouselSlideAxis === "x" ? 0 : 1];
534
+ const currentMovement = state.movement[carouselSlideAxis === "x" ? 0 : 1];
535
+ const direction = state.direction[carouselSlideAxis === "x" ? 0 : 1];
536
+ const prevItemTreshold = currentMovement > draggingSlideTreshold.current;
537
+ const nextItemTreshold = currentMovement < -draggingSlideTreshold.current;
538
+ if (isDragging) {
539
+ if (direction > 0) {
540
+ slideActionType.current = "prev";
541
+ }
542
+ else {
543
+ slideActionType.current = "next";
544
+ }
545
+ emitEvent(Object.assign(Object.assign({}, state), { eventName: "onDrag", slideActionType: slideActionType.current }));
546
+ if (freeScroll) {
547
+ if (slideActionType.current === "prev" && movement > 0) {
548
+ state.cancel();
549
+ setSpring.start({
550
+ from: {
551
+ val: getFromValue(),
552
+ },
553
+ to: {
554
+ val: 0,
555
+ },
556
+ config: {
557
+ velocity: state.velocity,
558
+ friction: 50,
559
+ tension: 1000,
560
+ },
561
+ });
562
+ return;
563
+ }
564
+ setSpring.start({
565
+ from: {
566
+ val: getFromValue(),
567
+ },
568
+ to: {
569
+ val: -movement,
570
+ },
571
+ config: {
572
+ velocity: state.velocity,
573
+ friction: 50,
574
+ tension: 1000,
575
+ },
576
+ });
577
+ return;
578
+ }
579
+ setSpring.start({
580
+ val: movement,
581
+ config: {
582
+ velocity: state.velocity,
583
+ friction: 50,
584
+ tension: 1000,
585
+ },
586
+ });
587
+ if (slideWhenThresholdIsReached && nextItemTreshold) {
588
+ slideToNextItem("drag");
589
+ state.cancel();
590
+ }
591
+ else if (slideWhenThresholdIsReached && prevItemTreshold) {
592
+ slideToPrevItem("drag");
593
+ state.cancel();
594
+ }
595
+ return;
596
+ }
597
+ if (state.last && !state.canceled && freeScroll) {
598
+ if (slideActionType.current === "prev") {
599
+ slideToPrevItem("drag");
600
+ }
601
+ if (slideActionType.current === "next") {
602
+ slideToNextItem("drag");
603
+ }
604
+ }
605
+ if (state.last && !state.canceled && !freeScroll) {
606
+ if (nextItemTreshold) {
607
+ if (!withLoop && lastItemReached.current) {
608
+ setSpring.start({
609
+ val: -getTotalScrollValue(),
610
+ config: Object.assign(Object.assign({}, web.config.default), { velocity: state.velocity }),
611
+ });
612
+ }
613
+ else {
614
+ slideToNextItem("drag");
615
+ }
616
+ }
617
+ else if (prevItemTreshold) {
618
+ if (!withLoop && firstItemReached.current) {
619
+ setSpring.start({
620
+ val: 0,
621
+ config: Object.assign(Object.assign({}, web.config.default), { velocity: state.velocity }),
622
+ });
623
+ }
624
+ else {
625
+ slideToPrevItem("drag");
626
+ }
627
+ }
628
+ else {
629
+ setSpring.start({
630
+ val: prevSlidedValue.current,
631
+ config: Object.assign(Object.assign({}, web.config.default), { velocity: state.velocity }),
632
+ });
633
+ }
634
+ }
635
+ }, {
636
+ enabled: (init && !disableGestures && !freeScroll) ||
637
+ (!!freeScroll && !!enableFreeScrollDrag),
638
+ axis: carouselSlideAxis,
639
+ from: () => {
640
+ if (freeScroll && mainCarouselWrapperRef.current) {
641
+ return [
642
+ -mainCarouselWrapperRef.current.scrollLeft,
643
+ -mainCarouselWrapperRef.current.scrollTop,
644
+ ];
645
+ }
646
+ if (carouselSlideAxis === "x") {
647
+ return [spring.val.get(), spring.val.get()];
648
+ }
649
+ return [spring.val.get(), spring.val.get()];
650
+ },
651
+ });
652
+ function getWrapperOverflowStyles() {
653
+ if (freeScroll) {
654
+ if (carouselSlideAxis === "x") {
655
+ return {
656
+ overflowX: "auto",
657
+ };
658
+ }
659
+ return {
660
+ overflowY: "auto",
661
+ };
662
+ }
663
+ return {};
664
+ }
665
+ function setStartEndItemReachedOnFreeScroll() {
666
+ if (mainCarouselWrapperRef.current) {
667
+ prevSlidedValue.current =
668
+ mainCarouselWrapperRef.current[carouselSlideAxis === "x" ? "scrollLeft" : "scrollTop"];
669
+ if (mainCarouselWrapperRef.current[carouselSlideAxis === "x" ? "scrollLeft" : "scrollTop"] === 0) {
670
+ firstItemReached.current = true;
671
+ lastItemReached.current = false;
672
+ }
673
+ if (mainCarouselWrapperRef.current[carouselSlideAxis === "x" ? "scrollLeft" : "scrollTop"] > 0 &&
674
+ mainCarouselWrapperRef.current[carouselSlideAxis === "x" ? "scrollLeft" : "scrollTop"] < getTotalScrollValue()) {
675
+ firstItemReached.current = false;
676
+ lastItemReached.current = false;
677
+ }
678
+ if (mainCarouselWrapperRef.current[carouselSlideAxis === "x" ? "scrollLeft" : "scrollTop"] === getTotalScrollValue()) {
679
+ firstItemReached.current = false;
680
+ lastItemReached.current = true;
681
+ }
682
+ }
683
+ }
684
+ function getScrollHandlers() {
685
+ if (freeScroll) {
686
+ return {
687
+ onWheel() {
688
+ spring.val.stop();
689
+ setStartEndItemReachedOnFreeScroll();
690
+ },
691
+ };
692
+ }
693
+ return {};
694
+ }
695
+ function findItemIndex(id) {
696
+ return items.findIndex((item) => item.id === id);
697
+ }
698
+ function findItemIndexById(id, error) {
699
+ let itemIndex = 0;
700
+ if (typeof id === "string") {
701
+ itemIndex = items.findIndex((_item) => _item.id === id);
702
+ }
703
+ else {
704
+ itemIndex = id;
705
+ }
706
+ if (itemIndex < 0 || itemIndex >= items.length) {
707
+ throw new Error(error);
708
+ }
709
+ return itemIndex;
710
+ }
711
+ function internalSlideToItem(id) {
712
+ if (!init)
713
+ return;
714
+ firstItemReached.current = false;
715
+ lastItemReached.current = false;
716
+ const itemIndex = findItemIndexById(id, "The item you want to slide to doesn't exist; please check che item id or the index you're passing to.");
717
+ if (itemIndex === activeItem.current) {
718
+ return;
719
+ }
720
+ const currentItem = findItemIndex(items[activeItem.current].id);
721
+ const newActiveItem = findItemIndex(items[itemIndex].id);
722
+ if (newActiveItem > currentItem) {
723
+ slideToNextItem("click", newActiveItem);
724
+ }
725
+ else {
726
+ slideToPrevItem("click", newActiveItem);
727
+ }
728
+ }
729
+ function getIsNextItem(id) {
730
+ const itemIndex = findItemIndex(id);
731
+ const _activeItem = activeItem.current;
732
+ if (withLoop && _activeItem === items.length - 1) {
733
+ return itemIndex === 0;
734
+ }
735
+ return itemIndex === _activeItem + 1;
736
+ }
737
+ function getIsPrevItem(id) {
738
+ const itemIndex = findItemIndex(id);
739
+ const _activeItem = activeItem.current;
740
+ if (withLoop && _activeItem === 0) {
741
+ return itemIndex === items.length - 1;
742
+ }
743
+ return itemIndex === _activeItem - 1;
744
+ }
745
+ const carouselFragment = (jsxRuntime.jsx("div", Object.assign({ ref: mainCarouselWrapperRef }, getScrollHandlers(), { style: Object.assign({ display: "flex", position: "relative", width: "100%", height: "100%" }, getWrapperOverflowStyles()) }, { children: jsxRuntime.jsx("div", Object.assign({ ref: carouselTrackWrapperRef }, bindDrag(), { style: Object.assign({ position: "relative", display: "flex", flexDirection: carouselSlideAxis === "x" ? "row" : "column", touchAction: "none" }, getAnimatedWrapperStyles()) }, { children: internalItems.map((item, index) => {
746
+ return (jsxRuntime.jsx("div", Object.assign({ className: "use-spring-carousel-item", "data-testid": "use-spring-carousel-item-wrapper", style: Object.assign({ display: "flex", position: "relative", flex: "1" }, getItemStyles()) }, { children: item.renderItem }), `${item.id}-${index}`));
747
+ }) })) })));
748
+ return {
749
+ useListenToCustomEvent,
750
+ carouselFragment,
751
+ enterFullscreen,
752
+ exitFullscreen,
753
+ getIsFullscreen,
754
+ thumbsFragment,
755
+ slideToItem: internalSlideToItem,
756
+ getIsNextItem,
757
+ getIsPrevItem,
758
+ slideToPrevItem: () => slideToPrevItem(),
759
+ slideToNextItem: () => slideToNextItem(),
760
+ getIsActiveItem: (id) => {
761
+ return (findItemIndexById(id, "The item you want to check doesn't exist") ===
762
+ activeItem.current);
763
+ },
764
+ };
765
+ }
766
+ const Context = react.createContext(undefined);
767
+ function useSpringCarouselContext() {
768
+ const context = react.useContext(Context);
769
+ if (!context) {
770
+ throw new Error("useSpringCarouselContext must be used within the carousel.");
771
+ }
772
+ return context;
773
+ }
774
+
775
+ exports.useSpringCarousel = useSpringCarousel;
776
+ exports.useSpringCarouselContext = useSpringCarouselContext;
777
+
778
+ }));
2
779
  //# sourceMappingURL=index.js.map