UNPKG

@stephahart/react-native-pager

Version:
3 lines (2 loc) 9.03 kB
"use strict";function e(e){return e&&"object"==typeof e&&"default"in e?e.default:e}var t,n=require("react"),r=e(n),i=require("react-native"),a=e(require("react-native-reanimated")),o=require("react-native-gesture-handler");function u(){return(u=Object.assign||function(e){for(var t=1;t<arguments.length;t++){var n=arguments[t];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(e[r]=n[r])}return e}).apply(this,arguments)}function c(e,t){if(null==e)return{};var n,r,i={},a=Object.keys(e);for(r=0;r<a.length;r++)t.indexOf(n=a[r])>=0||(i[n]=e[n]);return i}(t=exports.Extrapolate||(exports.Extrapolate={})).EXTEND="extend",t.CLAMP="clamp",t.IDENTITY="identity";var l=a.event,s=a.defined,d=a.block,f=a.Value,v=a.divide,p=a.cond,m=a.eq,x=a.add,h=a.stopClock,w=a.Clock,y=a.set,g=a.clockRunning,I=a.multiply,E=a.sub,b=a.call,V=a.max,C=a.min,S=a.modulo,O=a.not,P=a.greaterThan,j=a.abs,k=a.ceil,A=a.interpolate,D=a.concat,T=a.neq,q=a.and,N=a.proc,z=a.startClock,R=a.spring,L=a.greaterOrEq,X=r.memo(function(e){var t,n,o=e.children,l=e.index,s=e.dimension,d=e.targetTransform,f=e.targetDimension,v=e.pageInterpolation,x=e.animatedIndex,h=e.animatedNumberOfScreens,w=J(p(m(h,1),0,p(q(m(l,0),P(x,E(h,1))),[I(h,s)],I(l,s)))),y=J(((n={})[f]=s,n.transform=[(t={},t[d]=w,t)],n)),g=J(p(m(h,1),0,p(m(l,0),p(L(x,E(h,1)),E(h,x),E(l,x)),E(l,x)))),b=J(B(g,h,v)),V=b.zIndex,C=c(b,["zIndex"]);return V||(V=0),r.createElement(a.View,{style:u({},i.StyleSheet.absoluteFillObject,{},y,{zIndex:V})},r.createElement(a.View,{style:[i.StyleSheet.absoluteFillObject,C]},o))});function Y(e,t){void 0===t&&(t=0);var r=J("number"==typeof t?new f(void 0!==e?e:t):t);return n.useEffect(function(){void 0!==e&&r.setValue(e)},[e,t]),r}var F=n.createContext({animatedValue:new f(0),animatedIndex:new f(0),nextIndex:new f(0),activeIndex:0,setActiveIndex:function(e){return null},content:[],setContent:function(e){return null},setState:function(e){return null}});function G(){var e=n.useContext(F);if(void 0===e)throw new Error("usePager() must be used within a <PagerProvider />");return e}var H=r.createContext(void 0);function M(){var e=n.useContext(H);if(void 0===e)throw new Error("useIndex() must be used within an <IndexProvider />");return e}function W(){return G()[2]}function _(e,t){var n=W();return J(p(m(t,1),0,p(m(e,0),p(L(n,E(t,1)),E(t,n),E(e,n)),E(e,n))))}function B(e,t,n){return n?Object.keys(n).reduce(function(r,i){var a=n[i];if(Array.isArray(a)){var o=a.map(function(n){return B(e,t,n)});return r[i]=o,r}if("object"==typeof a){var u,l=c(a,["unit"]);return u=a.unit?D(A(e,l),a.unit):A(e,a),r[i]=u,r}if("function"==typeof a){var s=a(e);return r[i]=s,r}return r},{}):{}}function J(e){return r.useRef(e).current}var K={stiffness:1e3,damping:500,mass:3,overshootClamping:!1,restDisplacementThreshold:.01,restSpeedThreshold:.01};function Q(e,t,n,r,i,a){var o={finished:new f(0),velocity:new f(0),position:t,time:new f(0)},c=u({},K,{},i,{toValue:new f(0)}),l=N(function(e,t,n){return d([p(q(m(t,0),L(e,E(n,1))),n,t)])}),s=new f(0),v=new f(-1),x=function(){return p(T(v,c.toValue),[y(v,n),null==a?void 0:a()])};return d([y(s,l(t,n,r)),p(g(e),0,[y(o.finished,0),y(o.time,0),y(o.velocity,0),x(),y(c.toValue,s),z(e)]),R(e,o,c),p(o.finished,[h(e),y(n,S(n,r)),y(o.position,n),x()]),o.position])}var U=function(e,t){return(e%t+t)%t},Z=a.interpolate,$=a.concat;function ee(e,t){return t?Object.keys(t).reduce(function(n,r){var i=t[r];if(Array.isArray(i)){var a=i.map(function(t){return ee(e,t)});return n[r]=a,n}if("object"==typeof i){var o,u=c(i,["unit"]);return o=i.unit?$(Z(e,u),i.unit):Z(e,i),n[r]=o,n}if("function"==typeof i){var l=i(e);return n[r]=l,n}return n},{}):{}}function te(e){return n.useRef(e).current}var ne=a.Value,re=a.divide,ie=a.multiply,ae=a.add,oe={height:50,width:"100%",flexDirection:"row"};function ue(e){var t=e.children,n=e.pageInterpolation,i=e.style,o=te(ee(_(e.index),n));return r.createElement(a.View,{style:[i||{flex:1},o]},t)}var ce={height:2,backgroundColor:"aquamarine"};exports.Pager=function(e){var t,u,c=e.onChange,A=e.children,D=e.springConfig,T=e.panProps,z=void 0===T?{}:T,R=e.pageSize,L=void 0===R?1:R,G=e.threshold,H=void 0===G?.1:G,M=e.minIndex,W=e.maxIndex,_=e.adjacentChildOffset,B=void 0===_?5:_,K=e.style,Z=e.containerStyle,$=e.type,ee=void 0===$?"horizontal":$,te=e.pageInterpolation,ne=e.clamp,re=void 0===ne?{}:ne,ie=e.clampDrag,ae=void 0===ie?{}:ie,oe=e.loop,ue=void 0===oe||oe,ce=n.useContext(F),le=ce.animatedValue,se=ce.animatedIndex,de=ce.nextIndex,fe=ce.activeIndex,ve=ce.setActiveIndex,pe=ce.content,me=pe?pe.length:0,xe=J(new f(me));xe.setValue(me);var he=J(fe);J(new f(ue?1:0)).setValue(ue?1:0);var we=J(new f(0)),ye=J(new f(0)),ge=J(new f(0)),Ie=n.useRef(!1),Ee=n.useRef(-1),be=J(l([{nativeEvent:{translationX:we,translationY:ye}}],{useNativeDriver:!0})),Ve=J(l([{nativeEvent:{state:ge}}],{useNativeDriver:!0})),Ce=-1;K&&K.width&&"number"==typeof K.width&&(Ce=K.width);var Se=-1;K&&K.height&&"number"==typeof K.height&&(Se=K.height);var Oe=n.useState(Ce),Pe=Oe[0],je=Oe[1],ke=n.useState(Se),Ae=ke[0],De=ke[1],Te=J(new f(0)),qe="vertical"===ee?"height":"width",Ne="vertical"===ee?"translateY":"translateX",ze="vertical"===ee?ye:we,Re="vertical"===ee?Ae:Pe,Le=I(Te,xe),Xe="vertical"===ee?1:2,Ye=Y(H),Fe=Y(ae.prev,1e9),Ge=Y(ae.next,1e9),He=void 0===W?new f:new f(W),Me=void 0===M?new f:new f(M),We=J(new f(0)),_e=J(new f(0)),Be=J(new f(he)),Je=J(p(q(m(Be,0),P(le,E(xe,1))),E(xe,le),E(Be,le))),Ke=J(j(Je)),Qe=J(P(Ke,Ye)),Ue=J(N(function(e){return C(V(e,Me),He)})),Ze=J(new f(0)),$e=J(C(V(v(ze,Te),I(Ge,-1)),Fe)),et=J(new w),tt=J(new f(he)),nt=J(function(){return b([S(de,xe)],function(e){Ee.current=e[0],Ie.current||(Ie.current=!0,i.InteractionManager.runAfterInteractions(function(){Ie.current=!1,ve(Ee.current),null==c||c(Ee.current)}))})}),rt=J(d([p(m(ge,o.State.ACTIVE),[p(g(et),h(et)),p(_e,0,[y(We,le),y(_e,1)]),y(le,E(We,$e))],[p(_e,[y(_e,0),p(Qe,[y(Ze,k(Ke)),y(de,p(P(Je,0),[p(s(He),[Ue(E(Be,Ze))],[p(m(Be,0),E(xe,1),E(Be,Ze))])],[p(s(He),[Ue(x(Be,Ze))],[x(Be,Ze)])]))])]),y(Be,S(de,xe)),y(se,S(de,xe)),y(le,Q(et,le,de,xe,D,nt))]),p(O(m(tt,de)),[y(tt,de)]),y(le,S(le,xe)),le])),it=Y(re.prev,x(xe,1)),at=Y(re.next,x(xe,1)),ot=J(I(E(rt,it),Te)),ut=J(I(x(rt,at),Te)),ct=Y(L),lt=J(p(m(xe,1),0,I(rt,Te,ct,-1)));return r.createElement(a.View,{style:Z||(K&&K.height?{height:K.height}:void 0)||{flex:1}},r.createElement(a.Code,{key:Re,exec:p(q(m(Te,Pe),m(Te,Ae)),[],[p(m(Xe,1),y(Te,Ae),y(Te,Pe))])}),r.createElement(o.PanGestureHandler,Object.assign({},z,{onGestureEvent:be,onHandlerStateChange:Ve}),r.createElement(a.View,{style:{flex:1}},r.createElement(a.View,{style:K||{flex:1}},r.createElement(a.View,{style:{flex:1},onLayout:function(e){var t=e.nativeEvent.layout;t.width!==Pe&&je(t.width),t.height!==Ae&&De(t.height)}},r.createElement(a.View,{style:(u={flex:1},u[qe]=Le,u.transform=[(t={},t[Ne]=lt,t)],u)},function(){if(-1===Pe)return null;var e=2*B+1>=me?0:U(fe-B,me),t=2*B+1>=me?me:U(fe+B+1,me);return(e>=t?[].concat(A.slice(e,me),A.slice(0,t)):A.slice(e,t)).map(function(t,n){var i=U(n+e,me);return r.createElement(X,{key:i+"-"+t.props.content.content_id,index:i,animatedIndex:rt,minimum:ot,maximum:ut,dimension:Te,targetTransform:Ne,targetDimension:qe,pageInterpolation:te,animatedNumberOfScreens:xe,loop:ue},t)})}()))))))},exports.PagerContext=F,exports.PagerProvider=function(e){var t=e.children,i=e.initialIndex,a=void 0===i?0:i,o=n.useState({activeIndex:a,content:null}),c=o[0],l=o[1],s=J(new f(a)),d=J(new f(a)),v=J(new f(a)),p=function(e){return l(function(t){return u({},t,{activeIndex:e})})},m=function(e){return l(function(t){return u({},t,{content:e})})};return r.createElement(F.Provider,{value:{animatedValue:s,animatedIndex:d,nextIndex:v,activeIndex:c.activeIndex,content:c.content,setActiveIndex:p,setContent:m,setState:l}},"function"==typeof t?t({animatedValue:s,animatedIndex:d,nextIndex:v,activeIndex:c.activeIndex,setActiveIndex:p,content:c.content,setContent:m,setState:l}):t)},exports.Pagination=function(e){var t=e.children,i=e.pageInterpolation;return r.createElement(a.View,{style:u({},oe,{},e.style)},n.Children.map(t,function(e,t){return r.createElement(ue,{index:t,pageInterpolation:i,style:e.props.style},e)}))},exports.Progress=function(e){var t=e.numberOfScreens,n=e.style,i=W(),o=te(new ne(0)),c=te(re(o,t,re(1,ae(i,1))));return r.createElement(a.View,{onLayout:function(e){o.setValue(e.nativeEvent.layout.width)}},r.createElement(a.View,{style:u({width:c,height:2,backgroundColor:"rebeccapurple"},ce,{},n)}))},exports.Slider=function(e){var t=e.numberOfScreens,n=e.style,i=W(),o=te(new ne(0)),c=re(o,t),l=te(ie(i,c));return r.createElement(a.View,{onLayout:function(e){o.setValue(e.nativeEvent.layout.width)}},r.createElement(a.View,{style:u({width:c,transform:[{translateX:l}]},ce,{},n)}))},exports.interpolateWithConfig=ee,exports.useAnimatedIndex=W,exports.useIndex=M,exports.useInterpolation=function(e,t,n){return J(B(_(void 0!==n?n:M(),t),t,e))},exports.useOffset=_,exports.usePager=G; //# sourceMappingURL=react-native-pager.cjs.production.min.js.map