framer-motion
Version:
A simple and powerful JavaScript animation library
2 lines (1 loc) • 5.81 kB
JavaScript
function t(t){let e;return()=>(void 0===e&&(e=t()),e)}const e=t=>t,n=t=>1e3*t,i=t=>t/1e3,a=t((()=>void 0!==window.ScrollTimeline));class r{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,e){const n=this.animations.map((n=>a()&&n.attachTimeline?n.attachTimeline(t):"function"==typeof e?e(n):void 0));return()=>{n.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 startTime(){return this.getAll("startTime")}get duration(){let t=0;for(let e=0;e<this.animations.length;e++)t=Math.max(t,this.animations[e].duration);return t}runAll(t){this.animations.forEach((e=>e[t]()))}flatten(){this.runAll("flatten")}play(){this.runAll("play")}pause(){this.runAll("pause")}cancel(){this.runAll("cancel")}complete(){this.runAll("complete")}}class o extends r{then(t,e){return this.finished.finally(t).then((()=>{}))}}const s=t=>t.startsWith("--"),l=(t,e,n)=>{s(e)?t.style.setProperty(e,n):t.style[e]=n},u=(t,e)=>s(e)?t.style.getPropertyValue(e):t.style[e],m=t=>null!==t;const h=t((()=>{try{document.createElement("div").animate({opacity:[1]})}catch(t){return!1}return!0})),c=new Set(["borderWidth","borderTopWidth","borderRightWidth","borderBottomWidth","borderLeftWidth","borderRadius","radius","borderTopLeftRadius","borderTopRightRadius","borderBottomRightRadius","borderBottomLeftRadius","width","maxWidth","height","maxHeight","top","right","bottom","left","padding","paddingTop","paddingRight","paddingBottom","paddingLeft","margin","marginTop","marginRight","marginBottom","marginLeft","backgroundPositionX","backgroundPositionY"]);const d={};function p(e,n){const i=t(e);return()=>d[n]??i()}const f=p((()=>{try{document.createElement("div").animate({opacity:0},{easing:"linear(0, 1)"})}catch(t){return!1}return!0}),"linearEasing"),y=([t,e,n,i])=>`cubic-bezier(${t}, ${e}, ${n}, ${i})`,g={linear:"linear",ease:"ease",easeIn:"ease-in",easeOut:"ease-out",easeInOut:"ease-in-out",circIn:y([0,.65,.55,1]),circOut:y([.55,0,1,.45]),backIn:y([.31,.01,.66,-.59]),backOut:y([.33,1.53,.69,.99])};function A(t,e){return t?"function"==typeof t&&f()?((t,e,n=10)=>{let i="";const a=Math.max(Math.round(e/n),2);for(let e=0;e<a;e++)i+=t(e/(a-1))+", ";return`linear(${i.substring(0,i.length-2)})`})(t,e):(t=>Array.isArray(t)&&"number"==typeof t[0])(t)?y(t):Array.isArray(t)?t.map((t=>A(t,e)||g.easeOut)):g[t]:void 0}const b=new WeakMap;class T{constructor(t){if("animation"in t)return void(this.animation=t.animation);const{element:e,name:n,keyframes:i,pseudoElement:a,allowFlatten:r=!1}=t;let{transition:o}=t;this.isPseudoElement=Boolean(a),this.allowFlatten=r;const s=function(t){const e=b.get(t)||new Map;return b.set(t,e),e}(e),d=((t,e)=>`${t}:${e}`)(n,a||""),p=s.get(d);p&&p.stop();const f=function(t,e,n,i){Array.isArray(n)||(n=[n]);for(let a=0;a<n.length;a++)null===n[a]&&(n[a]=0!==a||i?n[a-1]:u(t,e)),"number"==typeof n[a]&&c.has(e)&&(n[a]=n[a]+"px");return!i&&!h()&&n.length<2&&n.unshift(u(t,e)),n}(e,n,i,a);o.type,o=function({type:t,...e}){return function(t){return"function"==typeof t&&"applyToOptions"in t}(t)?t.applyToOptions(e):(e.duration??(e.duration=300),e.ease??(e.ease="easeOut"),e)}(o),this.animation=function(t,e,n,{delay:i=0,duration:a=300,repeat:r=0,repeatType:o="loop",ease:s="easeInOut",times:l}={},u){const m={[e]:n};l&&(m.offset=l);const h=A(s,a);return Array.isArray(h)&&(m.easing=h),t.animate(m,{delay:i,duration:a,easing:Array.isArray(h)?"linear":h,fill:"both",iterations:r+1,direction:"reverse"===o?"alternate":"normal",pseudoElement:u})}(e,n,f,o,a),!1===o.autoplay&&this.animation.pause(),this.removeAnimation=()=>s.delete(d),this.animation.onfinish=()=>{a||(l(e,n,function(t,{repeat:e,repeatType:n="loop"},i){const a=t.filter(m),r=e&&"loop"!==n&&e%2==1?0:a.length-1;return r&&void 0!==i?i:a[r]}(f,o)),this.cancel())},s.set(d,this)}play(){this.animation.play()}pause(){this.animation.pause()}complete(){this.animation.finish()}cancel(){try{this.animation.cancel()}catch(t){}this.removeAnimation()}stop(){const{state:t}=this;"idle"!==t&&"finished"!==t&&(this.commitStyles(),this.cancel())}commitStyles(){this.isPseudoElement||this.animation.commitStyles?.()}get duration(){const t=this.animation.effect?.getComputedTiming().duration||0;return i(Number(t))}get time(){return i(Number(this.animation.currentTime)||0)}set time(t){this.animation.currentTime=n(t)}get speed(){return this.animation.playbackRate}set speed(t){this.animation.playbackRate=t}get state(){return this.animation.playState}get startTime(){return Number(this.animation.startTime)}get finished(){return this.animation.finished}flatten(){this.allowFlatten&&this.animation.effect?.updateTiming({easing:"linear"})}attachTimeline(t){return this.animation.timeline=t,this.animation.onfinish=null,e}then(t,e){return this.finished.then(t).catch(e)}}function v(t,e,i,a){const r=function(t,e,n){if(t instanceof EventTarget)return[t];if("string"==typeof t){let i=document;e&&(i=e.current);const a=n?.[t]??i.querySelectorAll(t);return a?Array.from(a):[]}return Array.from(t)}(t,a),o=r.length,s=[];for(let t=0;t<o;t++){const a=r[t],m={...i};"function"==typeof m.delay&&(m.delay=m.delay(t,o));for(const t in e){const i=e[t],r={...(l=m,u=t,l?.[u]??l?.default??l)};r.duration&&(r.duration=n(r.duration)),r.delay&&(r.delay=n(r.delay)),s.push(new T({element:a,name:t,keyframes:i,transition:r,allowFlatten:!m.type&&!m.ease}))}}var l,u;return s}const w=t=>function(e,n,i){return new o(v(e,n,i,t))},R=w();export{R as animateMini,w as createScopedWaapiAnimate};