framer-motion
Version:
A simple and powerful JavaScript animation library
2 lines (1 loc) • 10.4 kB
JavaScript
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports,require("react")):"function"==typeof define&&define.amd?define(["exports","react"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).Motion={},t.React)}(this,function(t,e){"use strict";function n(t){const n=e.useRef(null);return null===n.current&&(n.current=t()),n.current}let i=()=>{};"undefined"!=typeof process&&"production"!==process.env?.NODE_ENV&&(i=(t,e,n)=>{if(!t)throw new Error(function(t,e){return e?`${t}. For more information and steps for solving, visit https://motion.dev/troubleshooting/${e}`:t}(e,n))});const s={};const o=t=>t,r=t=>1e3*t,a=t=>t/1e3,l=["setup","read","resolveKeyframes","preUpdate","update","preRender","render","postRender"];function u(t,e){let n=!1,i=!0;const o={delta:0,timestamp:0,isProcessing:!1},r=()=>n=!0,a=l.reduce((t,e)=>(t[e]=function(t){let e=new Set,n=new Set,i=!1,s=!1;const o=new Set;let r={delta:0,timestamp:0,isProcessing:!1};function a(e){o.has(e)&&(n.add(e),t()),e(r)}const l={schedule:(t,s=!1,r=!1)=>{const a=r&&i?e:n;return s&&o.add(t),a.add(t),t},cancel:t=>{n.delete(t),o.delete(t)},process:t=>{if(r=t,i)return void(s=!0);i=!0;const o=e;e=n,n=o,e.forEach(a),e.clear(),i=!1,s&&(s=!1,l.process(t))}};return l}(r),t),{}),{setup:u,read:c,resolveKeyframes:d,preUpdate:m,update:h,preRender:p,render:f,postRender:g}=a,y=()=>{const r=s.useManualTiming,a=r?o.timestamp:performance.now();n=!1,r||(o.delta=i?1e3/60:Math.max(Math.min(a-o.timestamp,40),1)),o.timestamp=a,o.isProcessing=!0,u.process(o),c.process(o),d.process(o),m.process(o),h.process(o),p.process(o),f.process(o),g.process(o),o.isProcessing=!1,n&&e&&(i=!1,t(y))};return{schedule:l.reduce((e,s)=>{const r=a[s];return e[s]=(e,s=!1,a=!1)=>(n||(n=!0,i=!0,o.isProcessing||t(y)),r.schedule(e,s,a)),e},{}),cancel:t=>{for(let e=0;e<l.length;e++)a[l[e]].cancel(t)},state:o,steps:a}}const{state:c}=u("undefined"!=typeof requestAnimationFrame?requestAnimationFrame:o,!0);let d;function m(){d=void 0}const h={now:()=>(void 0===d&&h.set(c.isProcessing||s.useManualTiming?c.timestamp:performance.now()),d),set:t=>{d=t,queueMicrotask(m)}},p=t=>null!==t;function f(t,e){return{kind:t,animation:e,timestamp:h.now(),frameTimestamp:c.timestamp,frameIsProcessing:c.isProcessing}}class g{constructor(){this.isResolved=!1}get finished(){return this._finished||(this._finished=this.isResolved?Promise.resolve():new Promise(t=>{this._resolve=t})),this._finished}updateFinished(){this._finished=this._resolve=void 0,this.isResolved=!1}notifyFinished(){this.isResolved=!0,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 T=t=>t.startsWith("--");const b={};function v(t,e){const n=function(t){let e;return()=>(void 0===e&&(e=t()),e)}(t);return()=>b[e]??n()}const A=v(()=>void 0!==window.ScrollTimeline,"scrollTimeline"),S=v(()=>{try{document.createElement("div").animate({opacity:0},{easing:"linear(0, 1)"})}catch(t){return!1}return!0},"linearEasing"),E=([t,e,n,i])=>`cubic-bezier(${t}, ${e}, ${n}, ${i})`,w={linear:"linear",ease:"ease",easeIn:"ease-in",easeOut:"ease-out",easeInOut:"ease-in-out",circIn:E([0,.65,.55,1]),circOut:E([.55,0,1,.45]),backIn:E([.31,.01,.66,-.59]),backOut:E([.33,1.53,.69,.99])};function k(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)?E(t):Array.isArray(t)?t.map(t=>k(t,e)||w.easeOut):w[t]:void 0}class R extends g{constructor(t){if(super(),this.finishedTime=null,this.isStopped=!1,this.manualStartTime=null,!t)return;const{element:e,name:n,keyframes:s,pseudoElement:o,allowFlatten:r=!1,finalKeyframe:a,onComplete:l}=t;this.isPseudoElement=Boolean(o),this.allowFlatten=r,this.options=t,i("string"!=typeof t.type,'Mini animate() doesn\'t support "type" as a string.',"mini-spring");const u=function({type:t,...e}){return function(t){return"function"==typeof t&&"applyToOptions"in t}(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:r="loop",ease:a="easeOut",times:l}={},u){const c={[e]:n};l&&(c.offset=l);const d=k(a,s);Array.isArray(d)&&(c.easing=d);const m={delay:i,duration:s,easing:Array.isArray(d)?"linear":d,fill:"both",iterations:o+1,direction:"reverse"===r?"alternate":"normal"};return u&&(m.pseudoElement=u),t.animate(c,m)}(e,n,s,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),r=s<0||e&&"loop"!==n&&e%2==1?0:o.length-1;return r&&void 0!==i?i:o[r]}(s,this.options,a,this.speed);this.updateMotionValue&&this.updateMotionValue(t),function(t,e,n){T(e)?t.style.setProperty(e,n):t.style[e]=n}(e,n,t),this.animation.cancel()}l?.(),this.notifyFinished()},function(t,e,n){const i=globalThis.__MOTION_INSPECT__;if(i)try{i({...f("animation-start",t),options:n?{...e,...n}:e})}catch{}}(this,t,u)}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 a(Number(t))}get iterationDuration(){const{delay:t=0}=this.options||{};return this.duration+a(t)}get time(){return a(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&&A()?(this.animation.timeline=t,e&&(this.animation.rangeStart=e),n&&(this.animation.rangeEnd=n),o):i(this)}}class P{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 B(this.animations,"duration")}get iterationDuration(){return B(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 B(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 M extends P{then(t,e){return this.finished.finally(t).then(()=>{})}}const I=new WeakMap,_=(t,e="")=>`${t}:${e}`;function O(t){let e=I.get(t);return e||(e=new Map,I.set(t,e)),e}function F(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 x=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 $(t,e){for(let n=0;n<t.length;n++)"number"==typeof t[n]&&x.has(e)&&(t[n]=t[n]+"px")}function N(t,e){const n=window.getComputedStyle(t);return T(e)?n.getPropertyValue(e):n[e]}function W(t,e,n,s){if(null==t)return[];const o=function(t,e){if(null==t)return[];if(t instanceof EventTarget)return[t];if("string"==typeof t){let n=document;e&&(n=e.current);const i=n.querySelectorAll(t);return i?Array.from(i):[]}return Array.from(t).filter(t=>null!=t)}(t,s),a=o.length;i(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={...F(s,t)};o.duration&&(o.duration=r(o.duration)),o.delay&&(o.delay=r(o.delay));const a=O(i),u=_(t,o.pseudoElement||""),c=a.get(u);c&&c.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]=N(i,s)),y(e),$(e,s),!o&&e.length<2&&e.unshift(N(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 R(i);e.set(n,s),s.finished.finally(()=>e.delete(n)),u.push(s)}return u}t.useAnimate=function(){const t=n(()=>({current:null,animations:[]})),i=n(()=>(t=>function(e,n,i){return new M(W(e,n,i,t))})(t));var s;return s=()=>{t.animations.forEach(t=>t.stop())},e.useEffect(()=>()=>s(),[]),[t,i]}});