framer-motion
Version:
A simple and powerful JavaScript animation library
2 lines (1 loc) • 12.4 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).Motion={})}(this,function(t){"use strict";function e(t,e){const n=t.indexOf(e);n>-1&&t.splice(n,1)}function n(t,e){return e?`${t}. For more information and steps for solving, visit https://motion.dev/troubleshooting/${e}`:t}let i=()=>{},s=()=>{};"undefined"!=typeof process&&"production"!==process.env?.NODE_ENV&&(i=(t,e,i)=>{t||"undefined"==typeof console||console.warn(n(e,i))},s=(t,e,i)=>{if(!t)throw new Error(n(e,i))});const o=t=>t,a=(t,e,n)=>{const i=e-t;return i?(n-t)/i:1},r=t=>1e3*t,l=t=>t/1e3;function u(t,e){return n=t,Array.isArray(n)&&"number"!=typeof n[0]?t[((t,e,n)=>{const i=e-t;return((n-t)%i+i)%i+t})(0,t.length,e)]:t;var n}const h=(t,e,n)=>t+(e-t)*n,c=2e4;function f(t,e=100,n){const i=n({...t,keyframes:[0,e]}),s=Math.min(function(t){let e=0,n=t.next(e);for(;!n.done&&e<c;)e+=50,n=t.next(e);return e>=c?1/0:e}(i),c);return{type:"keyframes",ease:t=>i.next(s*t).value/e,duration:l(s)}}function d(t,e){const n=t[t.length-1];for(let i=1;i<=e;i++){const s=a(0,e,i);t.push(h(n,1,s))}}function m(t){const e=[0];return d(e,t.length-1),e}const p=t=>null!==t;class g{constructor(){this.updateFinished()}get finished(){return this._finished}updateFinished(){this._finished=new Promise(t=>{this.resolve=t})}notifyFinished(){this.resolve()}then(t,e){return this.finished.then(t,e)}}function y(t){for(let e=1;e<t.length;e++)t[e]??(t[e]=t[e-1])}const A=t=>t.startsWith("--");const T={};function b(t,e){const n=function(t){let e;return()=>(void 0===e&&(e=t()),e)}(t);return()=>T[e]??n()}const v=b(()=>void 0!==window.ScrollTimeline,"scrollTimeline"),S=b(()=>{try{document.createElement("div").animate({opacity:0},{easing:"linear(0, 1)"})}catch(t){return!1}return!0},"linearEasing"),k=([t,e,n,i])=>`cubic-bezier(${t}, ${e}, ${n}, ${i})`,E={linear:"linear",ease:"ease",easeIn:"ease-in",easeOut:"ease-out",easeInOut:"ease-in-out",circIn:k([0,.65,.55,1]),circOut:k([.55,0,1,.45]),backIn:k([.31,.01,.66,-.59]),backOut:k([.33,1.53,.69,.99])};function w(t,e){return t?"function"==typeof t?S()?((t,e,n=10)=>{let i="";const s=Math.max(Math.round(e/n),2);for(let e=0;e<s;e++)i+=Math.round(1e4*t(e/(s-1)))/1e4+", ";return`linear(${i.substring(0,i.length-2)})`})(t,e):"ease-out":(t=>Array.isArray(t)&&"number"==typeof t[0])(t)?k(t):Array.isArray(t)?t.map(t=>w(t,e)||E.easeOut):E[t]:void 0}function M(t){return"function"==typeof t&&"applyToOptions"in t}class B extends g{constructor(t){if(super(),this.finishedTime=null,this.isStopped=!1,this.manualStartTime=null,!t)return;const{element:e,name:n,keyframes:i,pseudoElement:o,allowFlatten:a=!1,finalKeyframe:r,onComplete:l}=t;this.isPseudoElement=Boolean(o),this.allowFlatten=a,this.options=t,s("string"!=typeof t.type,'Mini animate() doesn\'t support "type" as a string.',"mini-spring");const u=function({type:t,...e}){return M(t)&&S()?t.applyToOptions(e):(e.duration??(e.duration=300),e.ease??(e.ease="easeOut"),e)}(t);this.animation=function(t,e,n,{delay:i=0,duration:s=300,repeat:o=0,repeatType:a="loop",ease:r="easeOut",times:l}={},u){const h={[e]:n};l&&(h.offset=l);const c=w(r,s);Array.isArray(c)&&(h.easing=c);const f={delay:i,duration:s,easing:Array.isArray(c)?"linear":c,fill:"both",iterations:o+1,direction:"reverse"===a?"alternate":"normal"};return u&&(f.pseudoElement=u),t.animate(h,f)}(e,n,i,u,o),!1===u.autoplay&&this.animation.pause(),this.animation.onfinish=()=>{if(this.finishedTime=this.time,!o){const t=function(t,{repeat:e,repeatType:n="loop"},i,s=1){const o=t.filter(p),a=s<0||e&&"loop"!==n&&e%2==1?0:o.length-1;return a&&void 0!==i?i:o[a]}(i,this.options,r,this.speed);this.updateMotionValue&&this.updateMotionValue(t),function(t,e,n){A(e)?t.style.setProperty(e,n):t.style[e]=n}(e,n,t),this.animation.cancel()}l?.(),this.notifyFinished()}}play(){this.isStopped||(this.manualStartTime=null,this.animation.play(),"finished"===this.state&&this.updateFinished())}pause(){this.animation.pause()}complete(){this.animation.finish?.()}cancel(){try{this.animation.cancel()}catch(t){}}stop(){if(this.isStopped)return;this.isStopped=!0;const{state:t}=this;"idle"!==t&&"finished"!==t&&(this.updateMotionValue?this.updateMotionValue():this.commitStyles(),this.isPseudoElement||this.cancel())}commitStyles(){const t=this.options?.element;!this.isPseudoElement&&t?.isConnected&&this.animation.commitStyles?.()}get duration(){const t=this.animation.effect?.getComputedTiming?.().duration||0;return l(Number(t))}get iterationDuration(){const{delay:t=0}=this.options||{};return this.duration+l(t)}get time(){return l(Number(this.animation.currentTime)||0)}set time(t){const e=null!==this.finishedTime;this.manualStartTime=null,this.finishedTime=null,this.animation.currentTime=r(t),e&&this.animation.pause()}get speed(){return this.animation.playbackRate}set speed(t){t<0&&(this.finishedTime=null),this.animation.playbackRate=t}get state(){return null!==this.finishedTime?"finished":this.animation.playState}get startTime(){return this.manualStartTime??Number(this.animation.startTime)}set startTime(t){this.manualStartTime=this.animation.startTime=t}attachTimeline({timeline:t,rangeStart:e,rangeEnd:n,observe:i}){return this.allowFlatten&&this.animation.effect?.updateTiming({easing:"linear"}),this.animation.onfinish=null,t&&v()?(this.animation.timeline=t,e&&(this.animation.rangeStart=e),n&&(this.animation.rangeEnd=n),o):i(this)}}class x{constructor(t){this.stop=()=>this.runAll("stop"),this.animations=t.filter(Boolean)}get finished(){return Promise.all(this.animations.map(t=>t.finished))}getAll(t){return this.animations[0][t]}setAll(t,e){for(let n=0;n<this.animations.length;n++)this.animations[n][t]=e}attachTimeline(t){const e=this.animations.map(e=>e.attachTimeline(t));return()=>{e.forEach((t,e)=>{t&&t(),this.animations[e].stop()})}}get time(){return this.getAll("time")}set time(t){this.setAll("time",t)}get speed(){return this.getAll("speed")}set speed(t){this.setAll("speed",t)}get state(){return this.getAll("state")}get startTime(){return this.getAll("startTime")}get duration(){return I(this.animations,"duration")}get iterationDuration(){return I(this.animations,"iterationDuration")}runAll(t){this.animations.forEach(e=>e[t]())}play(){this.runAll("play")}pause(){this.runAll("pause")}cancel(){this.runAll("cancel")}complete(){this.runAll("complete")}}function I(t,e){let n=0;for(let i=0;i<t.length;i++){const s=t[i][e];null!==s&&s>n&&(n=s)}return n}class O extends x{then(t,e){return this.finished.finally(t).then(()=>{})}}const F=new WeakMap,R=(t,e="")=>`${t}:${e}`;function W(t){let e=F.get(t);return e||(e=new Map,F.set(t,e)),e}function $(t,e){const n=t?.[e]??t?.default??t;return n!==t?function(t,e){if(t?.inherit&&e){const{inherit:n,...i}=t;return{...e,...i}}return t}(n,t):n}const P=t=>Boolean(t&&t.getVelocity),N=new Set(["borderWidth","borderTopWidth","borderRightWidth","borderBottomWidth","borderLeftWidth","borderRadius","borderTopLeftRadius","borderTopRightRadius","borderBottomRightRadius","borderBottomLeftRadius","width","maxWidth","height","maxHeight","top","right","bottom","left","inset","insetBlock","insetBlockStart","insetBlockEnd","insetInline","insetInlineStart","insetInlineEnd","padding","paddingTop","paddingRight","paddingBottom","paddingLeft","paddingBlock","paddingBlockStart","paddingBlockEnd","paddingInline","paddingInlineStart","paddingInlineEnd","margin","marginTop","marginRight","marginBottom","marginLeft","marginBlock","marginBlockStart","marginBlockEnd","marginInline","marginInlineStart","marginInlineEnd","fontSize","backgroundPositionX","backgroundPositionY"]);function V(t,e){for(let n=0;n<t.length;n++)"number"==typeof t[n]&&N.has(e)&&(t[n]=t[n]+"px")}function L(t,e,n){if(null==t)return[];if(t instanceof EventTarget)return[t];if("string"==typeof t){let e=document;const i=n?.[t]??e.querySelectorAll(t);return i?Array.from(i):[]}return Array.from(t).filter(t=>null!=t)}function D(t,e){const n=window.getComputedStyle(t);return A(e)?n.getPropertyValue(e):n[e]}function q(t,e,n,i){return null==t?[]:"string"==typeof t&&function(t){return"object"==typeof t&&!Array.isArray(t)}(e)?L(t,0,i):t instanceof NodeList?Array.from(t):Array.isArray(t)?t.filter(t=>null!=t):[t]}function C(t,e,n){return t*(e+1)+n*e}function K(t,e,n,i){return"number"==typeof e?e:e.startsWith("-")||e.startsWith("+")?Math.max(0,t+parseFloat(e)):"<"===e?n:e.startsWith("<")?Math.max(0,n+parseFloat(e.slice(1))):i.get(e)??t}function _(t,n,i,s,o,a){!function(t,n,i){for(let s=0;s<t.length;s++){const o=t[s];o.at>n&&o.at<i&&(e(t,o),s--)}}(t,o,a);for(let e=0;e<n.length;e++)t.push({value:n[e],at:h(o,a,s[e]),easing:u(i,e)})}function j(t,e,n=0){const i=e+1+e*n;for(let e=0;e<t.length;e++)t[e]=t[e]/i}function z(t,e){return t.at===e.at?null===t.value?1:null===e.value?-1:0:t.at-e.at}function H(t,e){return!e.has(t)&&e.set(t,{}),e.get(t)}function U(t,e){return e[t]||(e[t]=[]),e[t]}function X(t){return Array.isArray(t)?t:[t]}function Y(t,e){return t&&t[e]?{...t,...t[e]}:{...t}}const G=t=>"number"==typeof t,J=t=>t.every(G);function Q(t,e,n,i){if(null==t)return[];const o=L(t),a=o.length;s(Boolean(a),"No valid elements provided.","no-valid-elements");const l=[];for(let t=0;t<a;t++){const i=o[t],s={...n};"function"==typeof s.delay&&(s.delay=s.delay(t,a));for(const t in e){let n=e[t];Array.isArray(n)||(n=[n]);const o={...$(s,t)};o.duration&&(o.duration=r(o.duration)),o.delay&&(o.delay=r(o.delay));const a=W(i),u=R(t,o.pseudoElement||""),h=a.get(u);h&&h.stop(),l.push({map:a,key:u,unresolvedKeyframes:n,options:{...o,element:i,name:t,allowFlatten:!s.type&&!s.ease}})}}for(let t=0;t<l.length;t++){const{unresolvedKeyframes:e,options:n}=l[t],{element:i,name:s,pseudoElement:o}=n;o||null!==e[0]||(e[0]=D(i,s)),y(e),V(e,s),!o&&e.length<2&&e.unshift(D(i,s)),n.keyframes=e}const u=[];for(let t=0;t<l.length;t++){const{map:e,key:n,options:i}=l[t],s=new B(i);e.set(n,s),s.finished.finally(()=>e.delete(n)),u.push(s)}return u}const Z=(()=>function(t,e,n){return new O(Q(t,e,n))})();t.animate=Z,t.animateSequence=function(t,e){const n=[];return function(t,{defaultTransition:e={},...n}={},s,o){const l=e.duration||.3,h=new Map,c=new Map,p={},g=new Map;let y=0,A=0,T=0;for(let n=0;n<t.length;n++){const s=t[n];if("string"==typeof s){g.set(s,A);continue}if(!Array.isArray(s)){g.set(s.name,K(A,s.at,y,g));continue}let[a,h,b={}]=s;void 0!==b.at&&(A=K(A,b.at,y,g));let v=0;const S=(t,n,s,a=0,h=0)=>{const c=X(t),{delay:p=0,times:g=m(c),type:y=e.type||"keyframes",repeat:b,repeatType:S,repeatDelay:k=0,...E}=n;let{ease:w=e.ease||"easeOut",duration:B}=n;const x="function"==typeof p?p(a,h):p,I=c.length,O=M(y)?y:o?.[y||"keyframes"];if(I<=2&&O){let t=100;if(2===I&&J(c)){const e=c[1]-c[0];t=Math.abs(e)}const n={...e,...E};void 0!==B&&(n.duration=r(B));const i=f(n,t,O);w=i.ease,B=i.duration}B??(B=l);const F=A+x;1===g.length&&0===g[0]&&(g[1]=1);const R=g.length-c.length;if(R>0&&d(g,R),1===c.length&&c.unshift(null),b&&i(b<20,`Sequence segments can't repeat ${b} times — ignoring repeat option. Use a value below 20 or apply repeat at the sequence level instead.`),b&&b<20){const t=B>0?k/B:0;B=C(B,b,k);const e=[...c],n=[...g];w=Array.isArray(w)?[...w]:[w];const i=[...w],s="reverse"===S||"mirror"===S;let o=e,a=i;s&&(o=[...e].reverse(),"reverse"===S&&(a=[...i].reverse().map(t=>{return"function"==typeof t?(e=t,t=>1-e(1-t)):t;var e})));for(let r=0;r<b;r++){const l=s&&r%2==0,h=l?o:e,f=l?a:i,d=(r+1)*(1+t);t>0&&(c.push(c[c.length-1]),g.push(d),w.push("linear")),c.push(...h);for(let t=0;t<h.length;t++)g.push(n[t]+d),w.push(0===t?"linear":u(f,t-1))}j(g,b,t)}const W=F+B;_(s,c,w,g,F,W),v=Math.max(x+B,v),T=Math.max(W,T)};if(P(a))S(h,b,U("default",H(a,c)));else{const t=q(a,h,0,p),e=t.length;for(let n=0;n<e;n++){const i=H(t[n],c);for(const t in h)S(h[t],Y(b,t),U(t,i),n,e)}}y=A,A+=v}return c.forEach((t,i)=>{for(const s in t){const o=t[s];o.sort(z);const r=[],l=[],u=[];for(let t=0;t<o.length;t++){const{at:e,value:n,easing:i}=o[t];r.push(n),l.push(a(0,T,e)),u.push(i||"easeOut")}0!==l[0]&&(l.unshift(0),r.unshift(r[0]),u.unshift("easeInOut")),1!==l[l.length-1]&&(l.push(1),r.push(null)),h.has(i)||h.set(i,{keyframes:{},transition:{}});const c=h.get(i);c.keyframes[s]=r;const{type:f,...d}=e;c.transition[s]={...d,duration:T,ease:u,times:l,...n}}}),h}(t,e).forEach(({keyframes:t,transition:e},i)=>{n.push(...Q(i,t,e))}),new O(n)}});