forked from rcbyr/keen-slider
-
Notifications
You must be signed in to change notification settings - Fork 1
/
react-native.js
1 lines (1 loc) · 11.1 KB
/
react-native.js
1
import{createRef as n,useRef as t,useMemo as i,useEffect as r}from"react";import{PanResponder as e}from"react-native";var a=function(){return a=Object.assign||function(n){for(var t,i=1,r=arguments.length;i<r;i++)for(var e in t=arguments[i])Object.prototype.hasOwnProperty.call(t,e)&&(n[e]=t[e]);return n},a.apply(this,arguments)};function o(n,t,i){if(i||2===arguments.length)for(var r,e=0,a=t.length;e<a;e++)!r&&e in t||(r||(r=Array.prototype.slice.call(t,0,e)),r[e]=t[e]);return n.concat(r||Array.prototype.slice.call(t))}function u(){return Date.now()}function s(n){return function(t){t.nativeEvent&&(t=t.nativeEvent);var i=t.changedTouches||[],r=t.targetTouches||[],e=t.detail&&t.detail.x?t.detail:null;return n({id:e?e.identifier?e.identifier:"i":r[0]?r[0]?r[0].identifier:"e":"d",idChanged:e?e.identifier?e.identifier:"i":i[0]?i[0]?i[0].identifier:"e":"d",raw:t,x:e&&e.x?e.x:r[0]?r[0].screenX:e?e.x:t.pageX,y:e&&e.y?e.y:r[0]?r[0].screenY:e?e.y:t.pageY})}}function c(n,t,i){return Math.min(Math.max(n,t),i)}function d(n){return(n>0?1:0)-(n<0?1:0)||+n}function l(n,t,i,r){var e=n&&n[t];return null==e?i:r&&"function"==typeof e?e():e}function f(n){return Math.round(1e6*n)/1e6}function p(n,t){if(n===t)return!0;var i=typeof n;if(i!==typeof t)return!1;if("object"!==i||null===n||null===t)return"function"===i&&n.toString()===t.toString();if(n.length!==t.length||Object.getOwnPropertyNames(n).length!==Object.getOwnPropertyNames(t).length)return!1;for(var r in n)if(!p(n[r],t[r]))return!1;return!0}function v(n,t){return p(n.current,t)||(n.current=t),n.current}function m(n){var t,i,r,e,a,o;function u(t){o||(o=t),s(!0);var a=t-o;a>r&&(a=r);var l=e[i];if(l[3]<a)return i++,u(t);var f=l[2],p=l[4],v=l[0],m=l[1]*(0,l[5])(0===p?1:(a-f)/p);if(m&&n.track.to(v+m),a<r)return d();o=null,s(!1),c(null),n.emit("animationEnded")}function s(n){t.active=n}function c(n){t.targetIdx=n}function d(){var n;n=u,a=window.requestAnimationFrame(n)}function l(){var t;t=a,window.cancelAnimationFrame(t),s(!1),c(null),o&&n.emit("animationStopped"),o=null}return t={active:!1,start:function(t){if(l(),n.track.details){var a=0,o=n.track.details.position;i=0,r=0,e=t.map((function(n){var t,i=Number(o),e=null!==(t=n.earlyExit)&&void 0!==t?t:n.duration,u=n.easing,s=n.distance*u(e/n.duration)||0;o+=s;var c=r;return r+=e,a+=s,[i,n.distance,c,r,n.duration,u]})),c(n.track.distToIdx(a)),d(),n.emit("animationStarted")}},stop:l,targetIdx:null}}function h(n){var t,i,r,e,a,s,p,v,m,h,g,b,x,y,k=1/0,M=[],T=null,w=0;function C(n){j(w+n)}function P(n){var t=z(w+n).abs;return S(t)?t:null}function z(n){var t=Math.floor(Math.abs(f(n/i))),r=f((n%i+i)%i);r===i&&(r=0);var e=d(n),a=p.indexOf(o([],p,!0).reduce((function(n,t){return Math.abs(t-r)<Math.abs(n-r)?t:n}))),u=a;return e<0&&t++,a===s&&(u=0,t+=e>0?1:-1),{abs:u+t*s*e,origin:a,rel:u}}function E(n,t,i){var r;if(t||!D())return I(n,i);if(!S(n))return null;var e=z(null!=i?i:w),a=e.abs,o=n-e.rel,u=a+o;r=I(u);var c=I(u-s*d(o));return(null!==c&&Math.abs(c)<Math.abs(r)||null===r)&&(r=c),f(r)}function I(n,t){if(null==t&&(t=f(w)),!S(n)||null===n)return null;n=Math.round(n);var r=z(t),e=r.abs,a=r.rel,o=r.origin,u=R(n),c=(t%i+i)%i,d=p[o],l=Math.floor((n-(e-a))/s)*i;return f(d-c-d+p[u]+l+(o===s?i:0))}function S(n){return O(n)===n}function O(n){return c(n,m,h)}function D(){return e.loop}function R(n){return(n%s+s)%s}function j(t){var i;i=t-w,M.push({distance:i,timestamp:u()}),M.length>6&&(M=M.slice(-6)),w=f(t);var r=A().abs;if(r!==T){var e=null!==T;T=r,e&&n.emit("slideChanged")}}function A(o){var u=o?null:function(){if(s){var n=D(),t=n?(w%i+i)%i:w,o=(n?w%i:w)-a[0][2],u=0-(o<0&&n?i-Math.abs(o):o),c=0,l=z(w),f=l.abs,p=l.rel,v=a[p][2],k=a.map((function(t,r){var a=u+c;(a<0-t[0]||a>1)&&(a+=(Math.abs(a)>i-1&&n?i:0)*d(-a));var o=r-p,l=d(o),m=o+f;n&&(-1===l&&a>v&&(m+=s),1===l&&a<v&&(m-=s),null!==g&&m<g&&(a+=i),null!==b&&m>b&&(a-=i));var h=a+t[0]+t[1],x=Math.max(a>=0&&h<=1?1:h<0||a>1?0:a<0?Math.min(1,(t[0]+a)/t[0]):(1-a)/t[0],0);return c+=t[0]+t[1],{abs:m,distance:e.rtl?-1*a+1-t[0]:a,portion:x,size:t[0]}}));return f=O(f),p=R(f),{abs:O(f),length:r,max:y,maxIdx:h,min:x,minIdx:m,position:w,progress:n?t/i:w/r,rel:p,slides:k,slidesLength:i}}}();return t.details=u,n.emit("detailsChanged"),u}return t={absToRel:R,add:C,details:null,distToIdx:P,idxToDist:E,init:function(t){if(function(){if(e=n.options,a=(e.trackConfig||[]).map((function(n){return[l(n,"size",1),l(n,"spacing",0),l(n,"origin",0)]})),s=a.length){i=f(a.reduce((function(n,t){return n+t[0]+t[1]}),0));var t,o=s-1;r=f(i+a[0][2]-a[o][0]-a[o][2]-a[o][1]),p=a.reduce((function(n,i){if(!n)return[0];var r=a[n.length-1],e=n[n.length-1]+(r[0]+r[2])+r[1];return e-=i[2],n[n.length-1]>e&&(e=n[n.length-1]),e=f(e),n.push(e),(!t||t<e)&&(v=n.length-1),t=e,n}),null),0===r&&(v=0),p.push(f(i))}}(),!s)return A(!0);var o;!function(){var t=n.options.range,i=n.options.loop;g=m=i?l(i,"min",-k):0,b=h=i?l(i,"max",k):v;var r=l(t,"min",null),e=l(t,"max",null);null!==r&&(m=r),null!==e&&(h=e),x=m===-k?m:n.track.idxToDist(m||0,!0,0),y=h===k?h:E(h,!0,0),null===e&&(b=h),l(t,"align",!1)&&h!==k&&0===a[R(h)][2]&&(y-=1-a[R(h)][0],h=P(y-w)),x=f(x),y=f(y)}(),o=t,Number(o)===o?C(I(O(t))):A()},to:j,velocity:function(){var n=u(),t=M.reduce((function(t,i){var r=i.distance,e=i.timestamp;return n-e>200||(d(r)!==d(t.distance)&&t.distance&&(t={distance:0,lastTimestamp:0,time:0}),t.time&&(t.distance+=r),t.lastTimestamp&&(t.time+=e-t.lastTimestamp),t.lastTimestamp=e),t}),{distance:0,lastTimestamp:0,time:0});return t.distance/t.time||0}}}function g(n){var t,i,r,e,a,o,u,s;function l(n){return 2*n}function f(n){return c(n,u,s)}function p(n){return 1-Math.pow(1-n,3)}function v(){return r?n.track.velocity():0}function m(){b();var t="free-snap"===n.options.mode,i=n.track,r=v();e=d(r);var u=n.track.details,s=[];if(r||!t){var c=h(r),m=c.dist,g=c.dur;if(g=l(g),m*=e,t){var x=i.idxToDist(i.distToIdx(m),!0);x&&(m=x)}s.push({distance:m,duration:g,easing:p});var y=u.position,k=y+m;if(k<a||k>o){var M=k<a?a-y:o-y,T=0,w=r;if(d(M)===e){var C=Math.min(Math.abs(M)/Math.abs(m),1),P=function(n){return 1-Math.pow(1-n,1/3)}(C)*g;s[0].earlyExit=P,w=r*(1-C)}else s[0].earlyExit=0,T+=M;var z=h(w,100),E=z.dist*e;n.options.rubberband&&(s.push({distance:E,duration:l(z.dur),easing:p}),s.push({distance:-E+T,duration:500,easing:p}))}n.animator.start(s)}else n.moveToIdx(f(u.abs),!0,{duration:500,easing:function(n){return 1+--n*n*n*n*n}})}function h(n,t){void 0===t&&(t=1e3);var i=147e-9+(n=Math.abs(n))/t;return{dist:Math.pow(n,2)/i,dur:n/i}}function g(){var t=n.track.details;t&&(a=t.min,o=t.max,u=t.minIdx,s=t.maxIdx)}function b(){n.animator.stop()}n.on("updated",g),n.on("optionsChanged",g),n.on("created",g),n.on("dragStarted",(function(){r=!1,b(),t=i=n.track.details.abs})),n.on("dragChecked",(function(){r=!0})),n.on("dragEnded",(function(){var r=n.options.mode;"snap"===r&&function(){var r=n.track,e=n.track.details,u=e.position,s=d(v());(u>o||u<a)&&(s=0);var c=t+s;0===e.slides[r.absToRel(c)].portion&&(c-=s),t!==i&&(c=i),d(r.idxToDist(c,!0))!==s&&(c+=s),c=f(c);var l=r.idxToDist(c,!0);n.animator.start([{distance:l,duration:500,easing:function(n){return 1+--n*n*n*n*n}}])}(),"free"!==r&&"free-snap"!==r||m()})),n.on("dragged",(function(){i=n.track.details.abs}))}function b(n){var t,i,r,a,o,u,l,f,p,v,m,h,g=2;function b(t){a&&u===t.idChanged&&(a=!1,n.emit("dragEnded"))}function x(t){var i=n.options.vertical,r=i?t.y:t.x,e=i?t.x:t.y,a=void 0!==p&&void 0!==v&&Math.abs(v-e)<=Math.abs(p-r);return p=r,v=e,a}function y(t){return n.options.vertical?t.y:t.x}function k(){var t;!function(){r=n.size;var t=n.track.details;t&&(m=t.min,h=t.max)}(),t=n.options.dragSpeed||1,o="function"==typeof t?t:function(n){return n*t},i=n.options.rtl?-1:1}n.on("updated",k),n.on("layoutChanged",k),n.on("created",k);var M=e.create({onPanResponderMove:s((function(e){if(a&&u===e.idChanged){var s=y(e);if(l){if(!x(e))return b(e);n.emit("dragChecked"),l=!1}var p=function(i){if(m===-1/0&&h===1/0)return i;var e=n.track.details,a=e.length,o=e.position,u=c(i,m-o,h-o);if(0===a)return 0;if(!n.options.rubberband)return u;if(o<=h&&o>=m)return i;if(o<m&&t>0||o>h&&t<0)return i;var s=(o<m?o-m:o-h)/a,d=r*a,l=Math.abs(s*d),f=Math.max(0,1-l/r*g);return f*f*i}(o(f-s)/n.size*i);t=d(p),n.track.add(p),f=s,n.emit("dragged")}})),onPanResponderRelease:s(b),onPanResponderTerminate:s(b),onStartShouldSetPanResponder:s((function(t){if(!a&&n.track.details&&n.track.details.length&&n.options.drag)return a=!0,l=!0,u=t.idChanged,x(t),f=y(t),n.emit("dragStarted"),!0}))});Object.assign(n.containerProps,M.panHandlers)}function x(t){return function(i){function r(n){i.animator.stop();var t=i.track.details;i.track.init(null!=n?n:t?t.abs:0)}function e(){var t=i.options.slides;if("function"==typeof t)return i.options.trackConfig=t(i.size);for(var r="number"==typeof t?t:l(t,"number",0,!0),e=[],a=l(t,"perView",1,!0),o=l(t,"spacing",0,!0)/i.size||0,u=o/a,s=l(t,"origin","auto"),c=0,d=0;d<r;d++){var f=1/a-o+u,p="center"===s?.5-f/2:"auto"===s?0:s;e.push({origin:p,size:f,spacing:o}),c+=f}if(c+=o*(r-1),"auto"===s&&!i.options.loop&&1!==a){var v=0;e.map((function(n){var t=c-v;return v+=n.size+o,t>=1||(n.origin=1-t-(c>1?0:1-c)),n}))}i.options.trackConfig=e,function(t){var r,e=i.slidesProps.length;if(e!==t){var a=t-e;a>0?(r=i.slidesProps).push.apply(r,Array(a).fill(null).map((function(){return{ref:n()}}))):i.slidesProps.splice(a)}}(i.options.trackConfig.length)}i.containerProps={onLayout:function(n){var t=i.options.vertical?n.nativeEvent.layout.height:n.nativeEvent.layout.width;t!==i.size&&(i.size=t,e(),r(),i.emit("layoutChanged"))}},i.slidesProps=[],i.update=function(n,o){n&&(i.options=a(a({},t),n)),e(),r(o),i.emit("updated")},i.prev=function(){i.moveToIdx(i.track.details.abs-1,!0)},i.next=function(){i.moveToIdx(i.track.details.abs+1,!0)},i.options=a(a({},t),i.options),e(),function(n){function t(){n.track.details&&n.track.details.slides.forEach((function(t,i){var r=n.options.vertical?"100%":"".concat(100*t.size,"%"),e=n.options.vertical?"".concat(100*t.size,"%"):"100%",a=n.size?t.distance*n.size:100*t.distance+"%",o=n.options.vertical?0:a,u=n.options.vertical?a:0,s="absolute";n.slidesProps[i].style={height:e,left:o,position:s,top:u,width:r};var c=n.slidesProps[i].ref.current;c&&c.setNativeProps({style:{height:e,left:o,position:s,top:u,width:r}})}))}n.on("detailsChanged",t),n.on("created",t),n.on("updated",t)}(i),b(i)}}"function"==typeof SuppressedError&&SuppressedError;var y=function(n,t){try{return function(n,t){var i,r={};return i={emit:function(n){r[n]&&r[n].forEach((function(n){n(i)}));var t=i.options&&i.options[n];t&&t(i)},moveToIdx:function(n,t,r){var e=i.track.idxToDist(n,t);if(e){var a=i.options.defaultAnimation;i.animator.start([{distance:e,duration:l(r||a,"duration",500),easing:l(r||a,"easing",(function(n){return 1+--n*n*n*n*n}))}])}},on:function(n,t,i){void 0===i&&(i=!1),r[n]||(r[n]=[]);var e=r[n].indexOf(t);e>-1?i&&delete r[n][e]:i||r[n].push(t)},options:n},function(){if(i.track=h(i),i.animator=m(i),t)for(var n=0,r=t;n<r.length;n++)(0,r[n])(i);i.track.init(i.options.initial||0),i.emit("created")}(),i}(n,o([x({drag:!0,mode:"snap",rubberband:!0}),g],t||[],!0))}catch(n){console.error(n)}};function k(n,e){var a=t(!1),o=t(n),u=i((function(){return y(n,e)}),[]);return r((function(){a.current?u&&u.update(o.current):a.current=!0}),[v(o,n)]),u}export{y as default,k as useKeenSliderNative};