earthtoday-react-motion
Version:
A spring that solves your animation problems.
2 lines (1 loc) • 17.6 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],t):t((e=e||self).ReactMotion={},e.React)}(this,function(e,r){"use strict";function p(){return(p=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var n,r=arguments[t];for(n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e}).apply(this,arguments)}function E(e,t){return(E=Object.setPrototypeOf?Object.setPrototypeOf.bind():function(e,t){return e.__proto__=t,e})(e,t)}function a(e,t){e.prototype=Object.create(t.prototype),E(e.prototype.constructor=e,t)}function b(e){var t,n={};for(t in e)Object.prototype.hasOwnProperty.call(e,t)&&(n[t]=0);return n}function l(e){var t,n={};for(t in e)Object.prototype.hasOwnProperty.call(e,t)&&(n[t]="number"==typeof e[t]?e[t]:e[t].val);return n}r=r&&Object.prototype.hasOwnProperty.call(r,"default")?r.default:r;var s=[0,0];function w(e,t,n,r,a,o,i){a=n+(-a*(t-r)+-o*n)*e,o=t+a*e;return Math.abs(a)<i&&Math.abs(o-r)<i?(s[0]=r,s[1]=0):(s[0]=o,s[1]=a),s}var R="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:{};function t(e,t){return e(t={exports:{}},t.exports),t.exports}for(var o,i,u,k=t(function(i){!function(){var e,t,n,r,a,o;"undefined"!=typeof performance&&null!==performance&&performance.now?i.exports=function(){return performance.now()}:"undefined"!=typeof process&&null!==process&&process.hrtime?(i.exports=function(){return(e()-a)/1e6},t=process.hrtime,r=(e=function(){var e=t();return 1e9*e[0]+e[1]})(),o=1e9*process.uptime(),a=r-o):n=Date.now?(i.exports=function(){return Date.now()-n},Date.now()):(i.exports=function(){return(new Date).getTime()-n},(new Date).getTime())}.call(R)}),n="undefined"==typeof window?R:window,c=["moz","webkit"],f="AnimationFrame",y=n["request"+f],m=n["cancel"+f]||n["cancelRequest"+f],d=0;!y&&d<c.length;d++)y=n[c[d]+"Request"+f],m=n[c[d]+"Cancel"+f]||n[c[d]+"CancelRequest"+f];y&&m||(i=o=0,u=[],y=function(e){var t,n;return 0===u.length&&(t=k(),n=Math.max(0,1e3/60-(t-o)),o=n+t,setTimeout(function(){for(var e=u.slice(0),t=u.length=0;t<e.length;t++)if(!e[t].cancelled)try{e[t].callback(o)}catch(e){setTimeout(function(){throw e},0)}},Math.round(n))),u.push({handle:++i,callback:e,cancelled:!1}),i},m=function(e){for(var t=0;t<u.length;t++)u[t].handle===e&&(u[t].cancelled=!0)});function h(e){return y.call(n,e)}function j(e,t,n){for(var r in t)if(Object.prototype.hasOwnProperty.call(t,r)){if(0!==n[r])return;var a="number"==typeof t[r]?t[r]:t[r].val;if(e[r]!==a)return}return 1}h.cancel=function(){m.apply(n,arguments)},h.polyfill=function(e){(e=e||n).requestAnimationFrame=y,e.cancelAnimationFrame=m};var v="function"==typeof Symbol&&Symbol.for,N=v?Symbol.for("react.element"):60103,F=v?Symbol.for("react.portal"):60106,S=v?Symbol.for("react.fragment"):60107,g=v?Symbol.for("react.strict_mode"):60108,I=v?Symbol.for("react.profiler"):60114,P=v?Symbol.for("react.provider"):60109,T=v?Symbol.for("react.context"):60110,L=v?Symbol.for("react.async_mode"):60111,O=v?Symbol.for("react.concurrent_mode"):60111,D=v?Symbol.for("react.forward_ref"):60112,M=v?Symbol.for("react.suspense"):60113,U=v?Symbol.for("react.suspense_list"):60120,V=v?Symbol.for("react.memo"):60115,C=v?Symbol.for("react.lazy"):60116,W=v?Symbol.for("react.block"):60121,q=v?Symbol.for("react.fundamental"):60117,z=v?Symbol.for("react.responder"):60118,B=v?Symbol.for("react.scope"):60119;function A(e){if("object"==typeof e&&null!==e){var t=e.$$typeof;switch(t){case N:switch(e=e.type){case L:case O:case S:case I:case g:case M:return e;default:switch(e=e&&e.$$typeof){case T:case D:case C:case V:case P:return e;default:return t}}case F:return t}}}function H(e){return A(e)===O}var K={AsyncMode:L,ConcurrentMode:O,ContextConsumer:T,ContextProvider:P,Element:N,ForwardRef:D,Fragment:S,Lazy:C,Memo:V,Portal:F,Profiler:I,StrictMode:g,Suspense:M,isAsyncMode:function(e){return H(e)||A(e)===L},isConcurrentMode:H,isContextConsumer:function(e){return A(e)===T},isContextProvider:function(e){return A(e)===P},isElement:function(e){return"object"==typeof e&&null!==e&&e.$$typeof===N},isForwardRef:function(e){return A(e)===D},isFragment:function(e){return A(e)===S},isLazy:function(e){return A(e)===C},isMemo:function(e){return A(e)===V},isPortal:function(e){return A(e)===F},isProfiler:function(e){return A(e)===I},isStrictMode:function(e){return A(e)===g},isSuspense:function(e){return A(e)===M},isValidElementType:function(e){return"string"==typeof e||"function"==typeof e||e===S||e===O||e===I||e===g||e===M||e===U||"object"==typeof e&&null!==e&&(e.$$typeof===C||e.$$typeof===V||e.$$typeof===P||e.$$typeof===T||e.$$typeof===D||e.$$typeof===q||e.$$typeof===z||e.$$typeof===B||e.$$typeof===W)},typeOf:A},v=t(function(e,t){});v.AsyncMode,v.ConcurrentMode,v.ContextConsumer,v.ContextProvider,v.Element,v.ForwardRef,v.Fragment,v.Lazy,v.Memo,v.Portal,v.Profiler,v.StrictMode,v.Suspense,v.isAsyncMode,v.isConcurrentMode,v.isContextConsumer,v.isContextProvider,v.isElement,v.isForwardRef,v.isFragment,v.isLazy,v.isMemo,v.isPortal,v.isProfiler,v.isStrictMode,v.isSuspense,v.isValidElementType,v.typeOf,t(function(e){e.exports=K}),Object.getOwnPropertySymbols,Object.prototype.hasOwnProperty,Object.prototype.propertyIsEnumerable;!function(){try{if(Object.assign){var e=new String("abc");if(e[5]="de","5"!==Object.getOwnPropertyNames(e)[0]){for(var t={},n=0;n<10;n++)t["_"+String.fromCharCode(n)]=n;var r,a=Object.getOwnPropertyNames(t).map(function(e){return t[e]});if("0123456789"===a.join(""))return r={},"abcdefghijklmnopqrst".split("").forEach(function(e){r[e]=e}),"abcdefghijklmnopqrst"===Object.keys(Object.assign({},r)).join("")?1:void 0}}}catch(e){}}()||Object.assign,Function.call.bind(Object.prototype.hasOwnProperty);function Y(){}function G(){}G.resetWarningCache=Y;t(function(e){function t(e,t,n,r,a,o){if("SECRET_DO_NOT_PASS_THIS_OR_YOU_WILL_BE_FIRED"!==o)throw(o=new Error("Calling PropTypes validators directly is not supported by the `prop-types` package. Use PropTypes.checkPropTypes() to call them. Read more at http://fb.me/use-check-prop-types")).name="Invariant Violation",o}function n(){return t}e.exports=(e={array:t.isRequired=t,bigint:t,bool:t,func:t,number:t,object:t,string:t,symbol:t,any:t,arrayOf:n,element:t,elementType:t,instanceOf:n,node:t,objectOf:n,oneOf:n,oneOfType:n,shape:n,exact:n,checkPropTypes:G,resetWarningCache:Y}).PropTypes=e});var $=1e3/60,v=function(t){function e(e){var v=t.call(this,e)||this;return v.unmounting=!1,v.wasAnimating=!1,v.animationID=null,v.prevTime=0,v.accumulatedTime=0,v.unreadPropStyle=null,v.clearUnreadPropStyle=function(e){var t,n,r=!1,a=v.state,o=a.currentStyle,i=a.currentVelocity,l=a.lastIdealStyle,s=a.lastIdealVelocity;for(t in e)Object.prototype.hasOwnProperty.call(e,t)&&"number"==typeof(n=e[t])&&(r||(r=!0,o=p({},o),i=p({},i),l=p({},l),s=p({},s)),o[t]=n,i[t]=0,l[t]=n,s[t]=0);r&&v.setState({currentStyle:o,currentVelocity:i,lastIdealStyle:l,lastIdealVelocity:s})},v.startAnimationIfNecessary=function(){v.unmounting||null!=v.animationID||(v.animationID=h(function(e){if(!v.unmounting){var t=v.props.style;if(j(v.state.currentStyle,t,v.state.currentVelocity))v.wasAnimating&&v.props.onRest&&v.props.onRest(),v.animationID=null,v.wasAnimating=!1,v.accumulatedTime=0;else{v.wasAnimating=!0;var e=e||k(),n=e-v.prevTime;if(v.prevTime=e,v.accumulatedTime=v.accumulatedTime+n,v.accumulatedTime>10*$&&(v.accumulatedTime=0),0===v.accumulatedTime)v.animationID=null;else{var r,a=(v.accumulatedTime-Math.floor(v.accumulatedTime/$)*$)/$,o=Math.floor(v.accumulatedTime/$),i={},l={},s={},u={};for(r in t)if(Object.prototype.hasOwnProperty.call(t,r)){var c=t[r];if("number"==typeof c)s[r]=c,u[r]=0,i[r]=c,l[r]=0;else{for(var f=v.state.lastIdealStyle[r],p=v.state.lastIdealVelocity[r],y=0;y<o;y++)var m=w($/1e3,f,p,c.val,c.stiffness,c.damping,c.precision),f=m[0],p=m[1];var d=w($/1e3,f,p,c.val,c.stiffness,c.damping,c.precision),h=d[0],d=d[1];s[r]=f+(h-f)*a,u[r]=p+(d-p)*a,i[r]=f,l[r]=p}}v.animationID=null,v.accumulatedTime-=o*$,v.setState({currentStyle:s,currentVelocity:u,lastIdealStyle:i,lastIdealVelocity:l}),v.unreadPropStyle=null}v.startAnimationIfNecessary()}}}))},v.state=v.defaultState(),v}a(e,t);var n=e.prototype;return n.defaultState=function(){var e=this.props,t=e.defaultStyle,e=e.style,t=t||l(e),e=b(t);return{currentStyle:t,currentVelocity:e,lastIdealStyle:t,lastIdealVelocity:e}},n.componentDidMount=function(){this.prevTime=k(),this.startAnimationIfNecessary()},n.UNSAFE_componentWillReceiveProps=function(e){null!=this.unreadPropStyle&&this.clearUnreadPropStyle(this.unreadPropStyle),this.unreadPropStyle=e.style,null==this.animationID&&(this.prevTime=k(),this.startAnimationIfNecessary())},n.componentWillUnmount=function(){this.unmounting=!0,null!=this.animationID&&(h.cancel(this.animationID),this.animationID=null)},n.render=function(){var e=this.props.children(this.state.currentStyle);return e&&r.Children.only(e)},e}(r.Component),_=1e3/60;var J=function(t){function e(e){var T=t.call(this,e)||this;return T.unmounting=!1,T.animationID=null,T.prevTime=0,T.accumulatedTime=0,T.unreadPropStyles=null,T.clearUnreadPropStyle=function(e){for(var t=T.state,n=t.currentStyles,r=t.currentVelocities,a=t.lastIdealStyles,o=t.lastIdealVelocities,i=!1,l=0;l<e.length;l++){var s,u,c=e[l],f=!1;for(s in c)Object.prototype.hasOwnProperty.call(c,s)&&"number"==typeof(u=c[s])&&(f||(i=f=!0,n[l]=p({},n[l]),r[l]=p({},r[l]),a[l]=p({},a[l]),o[l]=p({},o[l])),n[l][s]=u,r[l][s]=0,a[l][s]=u,o[l][s]=0)}i&&T.setState({currentStyles:n,currentVelocities:r,lastIdealStyles:a,lastIdealVelocities:o})},T.startAnimationIfNecessary=function(){T.unmounting||null!=T.animationID||(T.animationID=h(function(e){if(!T.unmounting){var t=T.props.styles(T.state.lastIdealStyles);if(function(e,t,n){for(var r=0;r<e.length;r++)if(!j(e[r],t[r],n[r]))return;return 1}(T.state.currentStyles,t,T.state.currentVelocities))T.animationID=null,T.accumulatedTime=0;else{var e=e||k(),n=e-T.prevTime;if(T.prevTime=e,T.accumulatedTime=T.accumulatedTime+n,T.accumulatedTime>10*_&&(T.accumulatedTime=0),0===T.accumulatedTime)T.animationID=null;else{for(var r=(T.accumulatedTime-Math.floor(T.accumulatedTime/_)*_)/_,a=Math.floor(T.accumulatedTime/_),o=[],i=[],l=[],s=[],u=0;u<t.length;u++){var c,f=t[u],p={},y={},m={},d={};for(c in f)if(Object.prototype.hasOwnProperty.call(f,c)){var h=f[c];if("number"==typeof h)p[c]=h,y[c]=0,m[c]=h,d[c]=0;else{for(var v=T.state.lastIdealStyles[u][c],S=T.state.lastIdealVelocities[u][c],b=0;b<a;b++)var g=w(_/1e3,v,S,h.val,h.stiffness,h.damping,h.precision),v=g[0],S=g[1];var I=w(_/1e3,v,S,h.val,h.stiffness,h.damping,h.precision),P=I[0],I=I[1];p[c]=v+(P-v)*r,y[c]=S+(I-S)*r,m[c]=v,d[c]=S}}l[u]=p,s[u]=y,o[u]=m,i[u]=d}T.animationID=null,T.accumulatedTime-=a*_,T.setState({currentStyles:l,currentVelocities:s,lastIdealStyles:o,lastIdealVelocities:i}),T.unreadPropStyles=null}T.startAnimationIfNecessary()}}}))},T.state=T.defaultState(),T}a(e,t);var n=e.prototype;return n.defaultState=function(){var e=this.props,t=e.defaultStyles,e=e.styles,t=t||e().map(l),e=t.map(b);return{currentStyles:t,currentVelocities:e,lastIdealStyles:t,lastIdealVelocities:e}},n.componentDidMount=function(){this.prevTime=k(),this.startAnimationIfNecessary()},n.UNSAFE_componentWillReceiveProps=function(e){null!=this.unreadPropStyles&&this.clearUnreadPropStyle(this.unreadPropStyles),this.unreadPropStyles=e.styles(this.state.lastIdealStyles),null==this.animationID&&(this.prevTime=k(),this.startAnimationIfNecessary())},n.componentWillUnmount=function(){this.unmounting=!0,null!=this.animationID&&(h.cancel(this.animationID),this.animationID=null)},n.render=function(){var e=this.props.children(this.state.currentStyles);return e&&r.Children.only(e)},e}(r.Component);var x=1e3/60;function Q(e,t,r){var a=t;return null==a?e.map(function(e,t){return{key:e.key,data:e.data,style:r[t]}}):e.map(function(e,t){for(var n=0;n<a.length;n++)if(a[n].key===e.key)return{key:a[n].key,data:a[n].data,style:r[t]};return{key:e.key,data:e.data,style:r[t]}})}function X(e,r,a,t,n,o,i,l,s){for(var u=function(e,c,t){for(var f={},n=0;n<e.length;n++)f[e[n].key]=n;for(var p={},r=0;r<c.length;r++)p[c[r].key]=r;for(var a=[],o=0;o<c.length;o++)a[o]=c[o];for(var i,l=0;l<e.length;l++)Object.prototype.hasOwnProperty.call(p,e[l].key)||null!=(i=t(l,e[l]))&&a.push(i);return a.sort(function(e,t){var n=p[e.key],r=p[t.key],a=f[e.key],o=f[t.key];if(null!=n&&null!=r)return p[e.key]-p[t.key];if(null!=a&&null!=o)return f[e.key]-f[t.key];if(null!=n){for(var i=0;i<c.length;i++){var l=c[i].key;if(Object.prototype.hasOwnProperty.call(f,l)){if(n<p[l]&&f[l]<o)return-1;if(p[l]<n&&o<f[l])return 1}}return 1}for(var s=0;s<c.length;s++){var u=c[s].key;if(Object.prototype.hasOwnProperty.call(f,u)){if(r<p[u]&&f[u]<a)return 1;if(p[u]<r&&a<f[u])return-1}}return-1})}(t,n,function(e,t){var n=r(t);return null==n||j(o[e],n,i[e])?(a({key:t.key,data:t.data}),null):{key:t.key,data:t.data,style:n}}),c=[],f=[],p=[],y=[],m=0;m<u.length;m++){for(var d,h=u[m],v=null,S=0;S<t.length;S++)if(t[S].key===h.key){v=S;break}null==v?(d=e(h),p[m]=c[m]=d,d=b(h.style),y[m]=f[m]=d):(c[m]=o[v],p[m]=l[v],f[m]=i[v],y[m]=s[v])}return[u,c,f,p,y]}var Z=function(t){function e(e){var O=t.call(this,e)||this;return O.unmounting=!1,O.animationID=null,O.prevTime=0,O.accumulatedTime=0,O.unreadPropStyles=null,O.clearUnreadPropStyle=function(e){for(var t=X(O.props.willEnter,O.props.willLeave,O.props.didLeave,O.state.mergedPropsStyles,e,O.state.currentStyles,O.state.currentVelocities,O.state.lastIdealStyles,O.state.lastIdealVelocities),n=t[0],r=t[1],a=t[2],o=t[3],i=t[4],l=0;l<e.length;l++){var s,u,c=e[l].style,f=!1;for(s in c)Object.prototype.hasOwnProperty.call(c,s)&&"number"==typeof(u=c[s])&&(f||(f=!0,r[l]=p({},r[l]),a[l]=p({},a[l]),o[l]=p({},o[l]),i[l]=p({},i[l]),n[l]={key:n[l].key,data:n[l].data,style:p({},n[l].style)}),r[l][s]=u,a[l][s]=0,o[l][s]=u,i[l][s]=0,n[l].style[s]=u)}O.setState({currentStyles:r,currentVelocities:a,mergedPropsStyles:n,lastIdealStyles:o,lastIdealVelocities:i})},O.startAnimationIfNecessary=function(){O.unmounting||null!=O.animationID||(O.animationID=h(function(e){if(!O.unmounting){var t=O.props.styles,t="function"==typeof t?t(Q(O.state.mergedPropsStyles,O.unreadPropStyles,O.state.lastIdealStyles)):t;if(function(e,t,n,r){if(r.length===t.length){for(var a=0;a<r.length;a++)if(r[a].key!==t[a].key)return;for(var o=0;o<r.length;o++)if(!j(e[o],t[o].style,n[o]))return;return 1}}(O.state.currentStyles,t,O.state.currentVelocities,O.state.mergedPropsStyles))O.animationID=null,O.accumulatedTime=0;else{var e=e||k(),n=e-O.prevTime;if(O.prevTime=e,O.accumulatedTime=O.accumulatedTime+n,O.accumulatedTime>10*x&&(O.accumulatedTime=0),0===O.accumulatedTime)O.animationID=null;else{for(var r=(O.accumulatedTime-Math.floor(O.accumulatedTime/x)*x)/x,a=Math.floor(O.accumulatedTime/x),e=X(O.props.willEnter,O.props.willLeave,O.props.didLeave,O.state.mergedPropsStyles,t,O.state.currentStyles,O.state.currentVelocities,O.state.lastIdealStyles,O.state.lastIdealVelocities),o=e[0],i=e[1],l=e[2],s=e[3],u=e[4],c=0;c<o.length;c++){var f,p=o[c].style,y={},m={},d={},h={};for(f in p)if(Object.prototype.hasOwnProperty.call(p,f)){var v=p[f];if("number"==typeof v)y[f]=v,m[f]=0,d[f]=v,h[f]=0;else{for(var S=s[c][f],b=u[c][f],g=0;g<a;g++)var I=w(x/1e3,S,b,v.val,v.stiffness,v.damping,v.precision),S=I[0],b=I[1];var P=w(x/1e3,S,b,v.val,v.stiffness,v.damping,v.precision),T=P[0],P=P[1];y[f]=S+(T-S)*r,m[f]=b+(P-b)*r,d[f]=S,h[f]=b}}s[c]=d,u[c]=h,i[c]=y,l[c]=m}O.animationID=null,O.accumulatedTime-=a*x,O.setState({currentStyles:i,currentVelocities:l,lastIdealStyles:s,lastIdealVelocities:u,mergedPropsStyles:o}),O.unreadPropStyles=null}O.startAnimationIfNecessary()}}}))},O.state=O.defaultState(),O}a(e,t);var n=e.prototype;return n.defaultState=function(){var e=this.props,t=e.defaultStyles,n=e.styles,r=e.willEnter,a=e.willLeave,e=e.didLeave,o="function"==typeof n?n(t):n,n=null==t?o:t.map(function(e){for(var t=0;t<o.length;t++)if(o[t].key===e.key)return o[t];return e}),i=null==t?o.map(function(e){return l(e.style)}):t.map(function(e){return l(e.style)}),t=null==t?o.map(function(e){return b(e.style)}):t.map(function(e){return b(e.style)}),r=X(r,a,e,n,o,i,t,i,t),a=r[0];return{currentStyles:r[1],currentVelocities:r[2],lastIdealStyles:r[3],lastIdealVelocities:r[4],mergedPropsStyles:a}},n.componentDidMount=function(){this.prevTime=k(),this.startAnimationIfNecessary()},n.UNSAFE_componentWillReceiveProps=function(e){this.unreadPropStyles&&this.clearUnreadPropStyle(this.unreadPropStyles);e=e.styles;this.unreadPropStyles="function"==typeof e?e(Q(this.state.mergedPropsStyles,this.unreadPropStyles,this.state.lastIdealStyles)):e,null==this.animationID&&(this.prevTime=k(),this.startAnimationIfNecessary())},n.componentWillUnmount=function(){this.unmounting=!0,null!=this.animationID&&(h.cancel(this.animationID),this.animationID=null)},n.render=function(){var e=Q(this.state.mergedPropsStyles,this.unreadPropStyles,this.state.currentStyles),e=this.props.children(e);return e&&r.Children.only(e)},e}(r.Component),ee=(Z.defaultProps={willEnter:function(e){return l(e.style)},willLeave:function(){return null},didLeave:function(){}},{noWobble:{stiffness:170,damping:26},gentle:{stiffness:120,damping:14},wobbly:{stiffness:180,damping:12},stiff:{stiffness:210,damping:20}}),te=p({},ee.noWobble,{precision:.01});e.Motion=v,e.StaggeredMotion=J,e.TransitionMotion=Z,e.presets=ee,e.reorderKeys=function(){},e.spring=function(e,t){return p({},te,t,{val:e})},e.stripStyle=l,Object.defineProperty(e,"__esModule",{value:!0})});