react-native-reanimated-carousel
Version:
Simple carousel component.fully implemented using Reanimated 2.Infinitely scrolling, very smooth.
2 lines • 16.8 kB
JavaScript
Object.defineProperty(exports,"__esModule",{value:true});exports.useCarouselController=useCarouselController;var _react=_interopRequireWildcard(require("react"));var _reactNativeReanimated=require("react-native-reanimated");var _constants=require("../constants");var _store=require("../store");var _computedWithAutoFillData=require("../utils/computed-with-auto-fill-data");var _dealWithAnimation=require("../utils/deal-with-animation");var _handleroffsetDirection=require("../utils/handleroffset-direction");var _log=require("../utils/log");function _getRequireWildcardCache(nodeInterop){if(typeof WeakMap!=="function")return null;var cacheBabelInterop=new WeakMap();var cacheNodeInterop=new WeakMap();return(_getRequireWildcardCache=function _getRequireWildcardCache(nodeInterop){return nodeInterop?cacheNodeInterop:cacheBabelInterop;})(nodeInterop);}function _interopRequireWildcard(obj,nodeInterop){if(!nodeInterop&&obj&&obj.__esModule){return obj;}if(obj===null||typeof obj!=="object"&&typeof obj!=="function"){return{default:obj};}var cache=_getRequireWildcardCache(nodeInterop);if(cache&&cache.has(obj)){return cache.get(obj);}var newObj={};var hasPropertyDescriptor=Object.defineProperty&&Object.getOwnPropertyDescriptor;for(var key in obj){if(key!=="default"&&Object.prototype.hasOwnProperty.call(obj,key)){var desc=hasPropertyDescriptor?Object.getOwnPropertyDescriptor(obj,key):null;if(desc&&(desc.get||desc.set)){Object.defineProperty(newObj,key,desc);}else{newObj[key]=obj[key];}}}newObj.default=obj;if(cache){cache.set(obj,newObj);}return newObj;}var _worklet_3425041857917_init_data={code:"function anonymous(){const{handlerOffset,round,size,dataInfo,convertToSharedIndex,loop,autoFillData}=this.__closure;const handlerOffsetValue=handlerOffset.value;const toInt=round(handlerOffsetValue/size)%dataInfo.length;const isPositive=handlerOffsetValue<=0;const i=isPositive?Math.abs(toInt):Math.abs(toInt>0?dataInfo.length-toInt:0);const newSharedIndexValue=convertToSharedIndex({loop:loop,rawDataLength:dataInfo.originalLength,autoFillData:autoFillData,index:i});return{i:i,newSharedIndexValue:newSharedIndexValue};}",location:"/home/runner/work/react-native-reanimated-carousel/react-native-reanimated-carousel/src/hooks/useCarouselController.tsx",sourceMap:"{\"version\":3,\"mappings\":\"AA4FI,QAAAA,UAAA,EAAM,gGACJ,KAAMC,mBAAkB,CAAGC,aAAa,CAACC,KAAzC,CACA,KAAMC,MAAK,CAAGC,KAAK,CAACJ,kBAAkB,CAAGK,IAAtB,CAALD,CAAmCE,QAAQ,CAACC,MAA1D,CACA,KAAMC,WAAU,CAAGR,kBAAkB,EAAI,CAAzC,CACA,KAAMS,EAAC,CAAGD,UAAU,CAAGE,IAAI,CAACC,GAALD,CAASP,KAATO,CAAH,CAAqBA,IAAI,CAACC,GAALD,CAASP,KAAK,CAAG,CAARA,CAAYG,QAAQ,CAACC,MAATD,CAAkBH,KAA9BA,CAAsC,CAA/CO,CAAzC,CAEA,KAAME,oBAAmB,CAAGC,oBAAoB,CAAC,CAC/CC,IAAI,CAAJA,IAD+C,CAE/CC,aAAa,CAAET,QAAQ,CAACU,cAFuB,CAG/CC,YAAY,CAAEA,YAHiC,CAI/CC,KAAK,CAAET,CAJwC,CAAD,CAAhD,CAOA,MAAO,CACLA,CAAC,CAADA,CADK,CAELG,uCAFK,CAAP,CAbF\",\"names\":[\"anonymous\",\"handlerOffsetValue\",\"handlerOffset\",\"value\",\"toInt\",\"round\",\"size\",\"dataInfo\",\"length\",\"isPositive\",\"i\",\"Math\",\"abs\",\"newSharedIndexValue\",\"convertToSharedIndex\",\"loop\",\"rawDataLength\",\"originalLength\",\"autoFillData\",\"index\"],\"sources\":[\"/home/runner/work/react-native-reanimated-carousel/react-native-reanimated-carousel/src/hooks/useCarouselController.tsx\"]}",version:"3.10.1"};var _worklet_13024854302038_init_data={code:"function anonymous({i:i,newSharedIndexValue:newSharedIndexValue}){const{index,runOnJS,setSharedIndex}=this.__closure;index.value=i;runOnJS(setSharedIndex)(newSharedIndexValue);}",location:"/home/runner/work/react-native-reanimated-carousel/react-native-reanimated-carousel/src/hooks/useCarouselController.tsx",sourceMap:"{\"version\":3,\"mappings\":\"AA8GI,QAACA,UAAD,CAAC,CAAEC,CAAC,CAADA,CAAF,CAAKC,uCAAL,CAAD,CAAgC,oDAC9BC,KAAK,CAACC,KAAND,CAAcF,CAAdE,CACAE,OAAO,CAACC,cAAD,CAAPD,CAAwBH,mBAAxBG,EAFF\",\"names\":[\"anonymous\",\"i\",\"newSharedIndexValue\",\"index\",\"value\",\"runOnJS\",\"setSharedIndex\"],\"sources\":[\"/home/runner/work/react-native-reanimated-carousel/react-native-reanimated-carousel/src/hooks/useCarouselController.tsx\"]}",version:"3.10.1"};var _worklet_1474026268410_init_data={code:"function anonymous(toValue,onFinished){const{runOnJS,onScrollEnd,duration,Easing,dealWithAnimation,withAnimation}=this.__closure;var _withAnimation;const callback=function(isFinished){\"worklet\";if(isFinished){runOnJS(onScrollEnd)();onFinished&&runOnJS(onFinished)();}};const defaultWithAnimation={type:\"timing\",config:{duration:duration,easing:Easing.easeOutQuart}};return dealWithAnimation((_withAnimation=withAnimation)!==null&&_withAnimation!==void 0?_withAnimation:defaultWithAnimation)(toValue,callback);}",location:"/home/runner/work/react-native-reanimated-carousel/react-native-reanimated-carousel/src/hooks/useCarouselController.tsx",sourceMap:"{\"version\":3,\"mappings\":\"AA6II,QAACA,UAAD,CAACA,OAAD,CAAkBC,UAAlB,CAA8C,8FAAAC,eAAA,qCAE5C,yBAEEC,OAAIC,aAAJ,GACED,UAAQE,SAARF,YAAQE,EAARF,CACAF,CACF,EACD,KANDK,qBAAA,gBAQAC,MAAMD,EACJE,QAAM,CAAAC,QADFH,CAEJC,MAAQ,CAAAG,MAAA,CAAAC,YAFJL,CARN,CAMC,CAIS,MAAAM,kBAAA,EAAAV,cAAA,CAAAW,aAAA,UAAAX,cAAA,UAAAA,cAAA,CAAAI,oBAAA,EAAAN,OAAA,CAAAc,QAAA,EAFwC\",\"names\":[\"toValue\",\"onFinished\",\"_withAnimation\",\"runOnJS\",\"isFinished\",\"onScrollEnd\",\"defaultWithAnimation\",\"config\",\"type\",\"duration\",\"Easing\",\"easeOutQuart\",\"dealWithAnimation\",\"withAnimation\",\"callback\"],\"sources\":[\"/home/runner/work/react-native-reanimated-carousel/react-native-reanimated-carousel/src/hooks/useCarouselController.tsx\"]}",version:"3.10.1"};var _worklet_14889674324583_init_data={code:"function anonymous(isFinished){const{runOnJS,onScrollEnd,onFinished}=this.__closure;if(isFinished){runOnJS(onScrollEnd)();onFinished&&runOnJS(onFinished)();}}",location:"/home/runner/work/react-native-reanimated-carousel/react-native-reanimated-carousel/src/hooks/useCarouselController.tsx",sourceMap:"{\"version\":3,\"mappings\":\"AA+IuB,QAACA,UAAD,CAACA,UAAD,CAAyB,sDAExC,GAAIA,UAAJ,CAAgB,CACdC,OAAO,CAACC,WAAD,CAAPD,GACAE,UAAU,EAAIF,OAAO,CAACE,UAAD,CAAPF,EAAdE,CACF,CALe\",\"names\":[\"isFinished\",\"runOnJS\",\"onScrollEnd\",\"onFinished\"],\"sources\":[\"/home/runner/work/react-native-reanimated-carousel/react-native-reanimated-carousel/src/hooks/useCarouselController.tsx\"]}",version:"3.10.1"};var _worklet_9387606738433_init_data={code:"function anonymous(opts={}){const{canSliding,loop,index,dataInfo,size,overscrollEnabled,containerSize,onScrollStart,currentFixedPage,handlerOffset,scrollWithTiming}=this.__closure;var _onScrollStart;const{count=1,animated=true,onFinished:onFinished}=opts;if(!canSliding())return;if(!loop&&index.value>=dataInfo.length-1)return;const visibleContentWidth=(dataInfo.length-index.value)*size;if(!overscrollEnabled&&!(visibleContentWidth>containerSize.value.width)){return;}(_onScrollStart=onScrollStart)===null||_onScrollStart===void 0||_onScrollStart();const nextPage=currentFixedPage()+count;index.value=nextPage;if(animated){handlerOffset.value=scrollWithTiming(-nextPage*size,onFinished);}else{handlerOffset.value=-nextPage*size;onFinished===null||onFinished===void 0||onFinished();}}",location:"/home/runner/work/react-native-reanimated-carousel/react-native-reanimated-carousel/src/hooks/useCarouselController.tsx",sourceMap:"{\"version\":3,\"mappings\":\"AAkKI,QAACA,UAAD,CAAiCA,IAAM,GAAvC,CAAuC,4JAAAC,eAAA,OAErCC,KAAM,GAAEA,QAAQ,CAAV,KAAaC,UAAQ,CAAGC,YAAMA,KAA9B,GAA6CJ,WAAnD,EAAM,CAAN,OACA,GAAI,CAACK,IAAD,EAACA,KAAL,CAAmBC,KAAdD,EAAcE,QAAA,CAAAC,MAAA,EAAnB,CAAmB,oEAkCnB,IAAAC,iBAAA,EAAyB,EAAGC,mBAAmBC,cAAeC,MAAfD,CAA/CE,KAAyB,CAAzB,SACA,CACE,CAAAZ,cAAA,CAAAa,aAAA,UAAAb,cAAA,WAAAA,cAAA,GACF,KAAAc,SAAA,CAAAC,gBAAA,GAAAd,KAAA,sBAEAY,aAEAG,aAAc,CAAAX,KAAd,CAAiBU,gBAAqBd,WAAtCU,IAAsCV,CAAtCE,UAAsCF,CAAtC,CACAS,CAHAG,IAGMR,oCAENF,UAAA,GAAc,IAAd,EAAcA,UAAA,SAAd,EAAcA,UAAA,EAAd,CACEa,CACD\",\"names\":[\"opts\",\"_onScrollStart\",\"count\",\"animated\",\"onFinished\",\"canSliding\",\"value\",\"dataInfo\",\"length\",\"overscrollEnabled\",\"visibleContentWidth\",\"index\",\"size\",\"width\",\"onScrollStart\",\"nextPage\",\"currentFixedPage\",\"handlerOffset\"],\"sources\":[\"/home/runner/work/react-native-reanimated-carousel/react-native-reanimated-carousel/src/hooks/useCarouselController.tsx\"]}",version:"3.10.1"};function useCarouselController(options){var ref=options.ref,size=options.size,loop=options.loop,dataLength=options.dataLength,handlerOffset=options.handlerOffset,withAnimation=options.withAnimation,_options$defaultIndex=options.defaultIndex,defaultIndex=_options$defaultIndex===void 0?0:_options$defaultIndex,duration=options.duration,autoFillData=options.autoFillData,fixedDirection=options.fixedDirection;var globalState=(0,_store.useGlobalState)();var overscrollEnabled=globalState.props.overscrollEnabled,containerSize=globalState.layout.containerSize;var dataInfo=_react.default.useMemo(function(){return{length:dataLength,disable:!dataLength,originalLength:dataLength};},[dataLength]);var index=(0,_reactNativeReanimated.useSharedValue)(defaultIndex);var sharedIndex=(0,_react.useRef)(defaultIndex);var sharedPreIndex=(0,_react.useRef)(defaultIndex);var currentFixedPage=_react.default.useCallback(function(){if(loop)return-Math.round(handlerOffset.value/size);var fixed=handlerOffset.value/size%dataInfo.length;return Math.round(handlerOffset.value<=0?Math.abs(fixed):Math.abs(fixed>0?dataInfo.length-fixed:0));},[handlerOffset,dataInfo,size,loop]);function setSharedIndex(newSharedIndex){sharedIndex.current=newSharedIndex;}(0,_reactNativeReanimated.useAnimatedReaction)(function(){var _e=[new global.Error(),-8,-27];var anonymous=function anonymous(){var handlerOffsetValue=handlerOffset.value;var toInt=(0,_log.round)(handlerOffsetValue/size)%dataInfo.length;var isPositive=handlerOffsetValue<=0;var i=isPositive?Math.abs(toInt):Math.abs(toInt>0?dataInfo.length-toInt:0);var newSharedIndexValue=(0,_computedWithAutoFillData.convertToSharedIndex)({loop:loop,rawDataLength:dataInfo.originalLength,autoFillData:autoFillData,index:i});return{i:i,newSharedIndexValue:newSharedIndexValue};};anonymous.__closure={handlerOffset:handlerOffset,round:_log.round,size:size,dataInfo:dataInfo,convertToSharedIndex:_computedWithAutoFillData.convertToSharedIndex,loop:loop,autoFillData:autoFillData};anonymous.__workletHash=3425041857917;anonymous.__initData=_worklet_3425041857917_init_data;anonymous.__stackDetails=_e;return anonymous;}(),function(){var _e=[new global.Error(),-4,-27];var anonymous=function anonymous(_ref){var i=_ref.i,newSharedIndexValue=_ref.newSharedIndexValue;index.value=i;(0,_reactNativeReanimated.runOnJS)(setSharedIndex)(newSharedIndexValue);};anonymous.__closure={index:index,runOnJS:_reactNativeReanimated.runOnJS,setSharedIndex:setSharedIndex};anonymous.__workletHash=13024854302038;anonymous.__initData=_worklet_13024854302038_init_data;anonymous.__stackDetails=_e;return anonymous;}(),[sharedPreIndex,sharedIndex,size,dataInfo,index,loop,autoFillData,handlerOffset]);var getCurrentIndex=_react.default.useCallback(function(){var realIndex=(0,_computedWithAutoFillData.computedRealIndexWithAutoFillData)({index:index.value,dataLength:dataInfo.originalLength,loop:loop,autoFillData:autoFillData});return realIndex;},[index,autoFillData,dataInfo,loop]);var canSliding=_react.default.useCallback(function(){return!dataInfo.disable;},[dataInfo]);var onScrollEnd=_react.default.useCallback(function(){options.onScrollEnd==null?void 0:options.onScrollEnd();},[options]);var onScrollStart=_react.default.useCallback(function(){options.onScrollStart==null?void 0:options.onScrollStart();},[options]);var scrollWithTiming=_react.default.useCallback(function(){var _e=[new global.Error(),-7,-27];var anonymous=function anonymous(toValue,onFinished){var callback=function(){var _e=[new global.Error(),-4,-27];var anonymous=function anonymous(isFinished){if(isFinished){(0,_reactNativeReanimated.runOnJS)(onScrollEnd)();onFinished&&(0,_reactNativeReanimated.runOnJS)(onFinished)();}};anonymous.__closure={runOnJS:_reactNativeReanimated.runOnJS,onScrollEnd:onScrollEnd,onFinished:onFinished};anonymous.__workletHash=14889674324583;anonymous.__initData=_worklet_14889674324583_init_data;anonymous.__stackDetails=_e;return anonymous;}();var defaultWithAnimation={type:"timing",config:{duration:duration,easing:_constants.Easing.easeOutQuart}};return(0,_dealWithAnimation.dealWithAnimation)(withAnimation!=null?withAnimation:defaultWithAnimation)(toValue,callback);};anonymous.__closure={runOnJS:_reactNativeReanimated.runOnJS,onScrollEnd:onScrollEnd,duration:duration,Easing:_constants.Easing,dealWithAnimation:_dealWithAnimation.dealWithAnimation,withAnimation:withAnimation};anonymous.__workletHash=1474026268410;anonymous.__initData=_worklet_1474026268410_init_data;anonymous.__stackDetails=_e;return anonymous;}(),[duration,withAnimation,onScrollEnd]);var next=_react.default.useCallback(function(){var _e=[new global.Error(),-12,-27];var anonymous=function anonymous(){var opts=arguments.length>0&&arguments[0]!==undefined?arguments[0]:{};var _opts$count=opts.count,count=_opts$count===void 0?1:_opts$count,_opts$animated=opts.animated,animated=_opts$animated===void 0?true:_opts$animated,onFinished=opts.onFinished;if(!canSliding())return;if(!loop&&index.value>=dataInfo.length-1)return;var visibleContentWidth=(dataInfo.length-index.value)*size;if(!overscrollEnabled&&!(visibleContentWidth>containerSize.value.width)){return;}onScrollStart==null?void 0:onScrollStart();var nextPage=currentFixedPage()+count;index.value=nextPage;if(animated){handlerOffset.value=scrollWithTiming(-nextPage*size,onFinished);}else{handlerOffset.value=-nextPage*size;onFinished==null?void 0:onFinished();}};anonymous.__closure={canSliding:canSliding,loop:loop,index:index,dataInfo:dataInfo,size:size,overscrollEnabled:overscrollEnabled,containerSize:containerSize,onScrollStart:onScrollStart,currentFixedPage:currentFixedPage,handlerOffset:handlerOffset,scrollWithTiming:scrollWithTiming};anonymous.__workletHash=9387606738433;anonymous.__initData=_worklet_9387606738433_init_data;anonymous.__stackDetails=_e;return anonymous;}(),[canSliding,loop,index,dataInfo,onScrollStart,handlerOffset,size,scrollWithTiming,currentFixedPage]);var prev=_react.default.useCallback(function(){var opts=arguments.length>0&&arguments[0]!==undefined?arguments[0]:{};var _opts$count2=opts.count,count=_opts$count2===void 0?1:_opts$count2,_opts$animated2=opts.animated,animated=_opts$animated2===void 0?true:_opts$animated2,onFinished=opts.onFinished;if(!canSliding())return;if(!loop&&index.value<=0)return;onScrollStart==null?void 0:onScrollStart();var prevPage=currentFixedPage()-count;index.value=prevPage;if(animated){handlerOffset.value=scrollWithTiming(-prevPage*size,onFinished);}else{handlerOffset.value=-prevPage*size;onFinished==null?void 0:onFinished();}},[canSliding,loop,index,onScrollStart,handlerOffset,size,scrollWithTiming,currentFixedPage]);var to=_react.default.useCallback(function(opts){var i=opts.i,_opts$animated3=opts.animated,animated=_opts$animated3===void 0?false:_opts$animated3,onFinished=opts.onFinished;if(i===index.value)return;if(!canSliding())return;onScrollStart==null?void 0:onScrollStart();var direction=(0,_handleroffsetDirection.handlerOffsetDirection)(handlerOffset,fixedDirection);var offset=i*size*direction;var totalSize=dataInfo.length*size;var isCloseToNextLoop=false;if(loop){isCloseToNextLoop=Math.abs(handlerOffset.value%totalSize)/totalSize>=0.5;}var finalOffset=(Math.floor(Math.abs(handlerOffset.value/totalSize))+(isCloseToNextLoop?1:0))*totalSize*direction+offset;if(animated){index.value=i;handlerOffset.value=scrollWithTiming(finalOffset,onFinished);}else{handlerOffset.value=finalOffset;index.value=i;onFinished==null?void 0:onFinished();}},[size,loop,index,fixedDirection,handlerOffset,dataInfo.length,canSliding,onScrollStart,scrollWithTiming]);var scrollTo=_react.default.useCallback(function(){var opts=arguments.length>0&&arguments[0]!==undefined?arguments[0]:{};var i=opts.index,count=opts.count,_opts$animated4=opts.animated,animated=_opts$animated4===void 0?false:_opts$animated4,onFinished=opts.onFinished;if(typeof i==="number"&&i>-1){to({i:i,animated:animated,onFinished:onFinished});return;}if(!count)return;var n=Math.round(count);if(n<0)prev({count:Math.abs(n),animated:animated,onFinished:onFinished});else next({count:n,animated:animated,onFinished:onFinished});},[prev,next,to]);_react.default.useImperativeHandle(ref,function(){return{next:next,prev:prev,getCurrentIndex:getCurrentIndex,scrollTo:scrollTo};},[getCurrentIndex,next,prev,scrollTo]);return{next:next,prev:prev,scrollTo:scrollTo,getCurrentIndex:getCurrentIndex,getSharedIndex:function getSharedIndex(){return sharedIndex.current;},index:index};}
//# sourceMappingURL=useCarouselController.js.map