Skip to content

Instantly share code, notes, and snippets.

@bundle-js
Created March 22, 2023 05:41
Show Gist options
  • Save bundle-js/cb8f5c4d450cc28cc270eb614f23eace to your computer and use it in GitHub Desktop.
Save bundle-js/cb8f5c4d450cc28cc270eb614f23eace to your computer and use it in GitHub Desktop.
var Je=e=>[].concat(...e),F=(e,t)=>{let i=Object.keys(e),a,s,r={};for(let n=0,l=i.length;n<l;n++)a=i[n],s=e[a],r[a]=t(s,a,e);return r},K=e=>""+e,de=e=>Array.isArray(e)||typeof e=="string"?(typeof e=="string"&&(e=e.split(",")),e):[e],E=e=>Array.isArray(e)||typeof e=="string"?!!e.length:e!=null&&e!=null&&!Number.isNaN(e),Te=e=>(e=e.replace(/([A-Z])/g,t=>`-${t.toLowerCase()}`),e.charAt(0)=="-"?e.substr(1):e),qe=e=>e.includes("--")?e:`${e}`.replace(/-([a-z])/g,(t,i)=>i.toUpperCase()),q=(e,t)=>{let i=[...e],a={...t};for(;i.length;){let{[i.pop()]:s,...r}=a;a=r}return a},et=(e,t)=>{let i=[...e],a={};for(let s of i)E(t[s])&&(a[s]=t[s]);return a},ye=(...e)=>{let t=0;e=e.map(s=>{let r=de(s),n=r.length;return n>t&&(t=n),r});let i=[],a=e.length;for(let s=0;s<t;s++){i[s]=[];for(let r=0;r<a;r++){let n=e[r][s];E(n)&&(i[s][r]=n)}}return i},he=e=>typeof e=="string"?e.includes("%")?parseFloat(e)/100:e=="from"?0:e=="to"?1:parseFloat(e):e,tt=e=>{let t=new Set,i=Object.keys(e),a=i.length;for(let s=0;s<a;s++){let r=""+i[s],n=e[r],l=r.split(","),o=l.length;for(let u=0;u<o;u++){let c=he(l[u]);t.add({...n,offset:c})}}return[...t].sort((s,r)=>s.offset-r.offset)},Tt={},be=(e="")=>t=>typeof t=="string"?t:`${t}${e}`,ae=be(),pe=be("px"),ce=be("deg"),re=e=>t=>E(t)?de(t).map(i=>{if(typeof i!="number"&&typeof i!="string")return i;let a=Number(i),s=Number.isNaN(a)?typeof i=="string"?i.trim():i:a;return e(s)}):[],D=(e,t)=>de(e).map(re(t)),C=re(ae),N=re(pe),w=re(ce),ze=e=>{let t=Object.keys(e),i,a,s={};for(let r=0,n=t.length;r<n;r++)i=qe(t[r]),a=e[t[r]],s[i]=a;return s},Fe={translate:e=>D(e,pe),translate3d:e=>D(e,pe),translateX:e=>N(e),translateY:e=>N(e),translateZ:e=>N(e),rotate:e=>D(e,ce),rotate3d:e=>D(e,ae),rotateX:e=>w(e),rotateY:e=>w(e),rotateZ:e=>w(e),scale:e=>D(e,ae),scale3d:e=>D(e,ae),scaleX:e=>C(e),scaleY:e=>C(e),scaleZ:e=>C(e),skew:e=>D(e,ce),skewX:e=>w(e),skewY:e=>w(e),perspective:e=>N(e)},me=Object.keys(Fe),je=(e,t)=>{let i="",a=e.length;for(let s=0;s<a;s++){let r=e[s],n=t[s];E(n)&&(i+=`${r}(${Array.isArray(n)?n.join(", "):n}) `)}return i.trim()},Le=["margin","padding","size","width","height","left","right","top","bottom","radius","gap","basis","inset","outline-offset","perspective","thickness","position","distance","spacing"].map(qe).join("|"),it=e=>{let t=ze(e),i=q(me,t),a=Object.keys(t).filter(n=>me.includes(n)),s=a.map(n=>Fe[n](t[n])),r=ye(...s).filter(E).map(n=>je(a,n));return i=F(i,(n,l)=>{let o;if(!/color/i.test(l)){let u=/rotate/i.test(l),c=new RegExp(Le,"i").test(l);if(u||c)return u?o=w:c&&(o=N),o(n).map(m=>{let h=m.trim().split(" ");return o(h).join(" ")})}return[].concat(n).map(K)}),Object.assign({},E(r)?{transform:r}:null,i)},at=e=>e.map(t=>{let{translate:i,translate3d:a,translateX:s,translateY:r,translateZ:n,rotate:l,rotate3d:o,rotateX:u,rotateY:c,rotateZ:m,scale:h,scale3d:g,scaleX:p,scaleY:f,scaleZ:y,skew:b,skewX:k,skewY:j,perspective:v,easing:P,iterations:T,offset:I,...M}=ze(t);return i=N(i),a=N(a),s=N(s)[0],r=N(r)[0],n=N(n)[0],l=w(l),o=C(o),u=w(u)[0],c=w(c)[0],m=w(m)[0],h=C(h),g=C(g),p=C(p)[0],f=C(f)[0],y=C(y)[0],b=w(b),k=w(k)[0],j=w(j)[0],v=N(v)[0],[M,i,a,s,r,n,l,o,u,c,m,h,g,p,f,y,b,k,j,v]}).map(([t,...i])=>{let a=je(me,i);return t=F(t,(s,r)=>{let n;if(!/color/i.test(r)){let l=/rotate/i.test(r),o=new RegExp(Le,"i").test(r);if(l||o){l?n=w:o&&(n=N);let u=K(s).trim().split(" ");return n(u).join(" ")}}return K(s)}),Object.assign({},E(a)?{transform:a}:null,t)}),st=4,rt=.001,nt=1e-7,ot=10,U=11,_=1/(U-1),lt=typeof Float32Array=="function",Ue=(e,t)=>1-3*t+3*e,Ke=(e,t)=>3*t-6*e,Re=e=>3*e,se=(e,t,i)=>((Ue(t,i)*e+Ke(t,i))*e+Re(t))*e,$e=(e,t,i)=>3*Ue(t,i)*e*e+2*Ke(t,i)*e+Re(t),ut=(e,t,i,a,s)=>{let r,n,l=0;do n=t+(i-t)/2,r=se(n,a,s)-e,r>0?i=n:t=n;while(Math.abs(r)>nt&&++l<ot);return n},ht=(e,t,i,a)=>{for(var s=0;s<st;++s){let r=$e(t,i,a);if(r===0)return t;t-=(se(t,i,a)-e)/r}return t},pt=(e,t,i,a)=>{if(!(0<=e&&e<=1&&0<=i&&i<=1))throw new Error("bezier x values must be in [0, 1] range");if(e===t&&i===a)return l=>l;for(var s=lt?new Float32Array(U):new Array(U),r=0;r<U;++r)s[r]=se(r*_,e,i);let n=l=>{let o=0,u=1,c=U-1;for(;u!==c&&s[u]<=l;++u)o+=_;--u;let m=(l-s[u])/(s[u+1]-s[u]),h=o+m*_,g=$e(h,e,i);return g>=rt?ht(l,h,e,i):g===0?h:ut(l,o,o+_,e,i)};return l=>l===0||l===1?l:se(n(l),t,a)},Ge=pt,Pe={aliceblue:[240,248,255],antiquewhite:[250,235,215],aqua:[0,255,255],aquamarine:[127,255,212],azure:[240,255,255],beige:[245,245,220],bisque:[255,228,196],black:[0,0,0],blanchedalmond:[255,235,205],blue:[0,0,255],blueviolet:[138,43,226],brown:[165,42,42],burlywood:[222,184,135],cadetblue:[95,158,160],chartreuse:[127,255,0],chocolate:[210,105,30],coral:[255,127,80],cornflowerblue:[100,149,237],cornsilk:[255,248,220],crimson:[220,20,60],cyan:[0,255,255],darkblue:[0,0,139],darkcyan:[0,139,139],darkgoldenrod:[184,134,11],darkgray:[169,169,169],darkgreen:[0,100,0],darkgrey:[169,169,169],darkkhaki:[189,183,107],darkmagenta:[139,0,139],darkolivegreen:[85,107,47],darkorange:[255,140,0],darkorchid:[153,50,204],darkred:[139,0,0],darksalmon:[233,150,122],darkseagreen:[143,188,143],darkslateblue:[72,61,139],darkslategray:[47,79,79],darkslategrey:[47,79,79],darkturquoise:[0,206,209],darkviolet:[148,0,211],deeppink:[255,20,147],deepskyblue:[0,191,255],dimgray:[105,105,105],dimgrey:[105,105,105],dodgerblue:[30,144,255],firebrick:[178,34,34],floralwhite:[255,250,240],forestgreen:[34,139,34],fuchsia:[255,0,255],gainsboro:[220,220,220],ghostwhite:[248,248,255],gold:[255,215,0],goldenrod:[218,165,32],gray:[128,128,128],green:[0,128,0],greenyellow:[173,255,47],grey:[128,128,128],honeydew:[240,255,240],hotpink:[255,105,180],indianred:[205,92,92],indigo:[75,0,130],ivory:[255,255,240],khaki:[240,230,140],lavender:[230,230,250],lavenderblush:[255,240,245],lawngreen:[124,252,0],lemonchiffon:[255,250,205],lightblue:[173,216,230],lightcoral:[240,128,128],lightcyan:[224,255,255],lightgoldenrodyellow:[250,250,210],lightgray:[211,211,211],lightgreen:[144,238,144],lightgrey:[211,211,211],lightpink:[255,182,193],lightsalmon:[255,160,122],lightseagreen:[32,178,170],lightskyblue:[135,206,250],lightslategray:[119,136,153],lightslategrey:[119,136,153],lightsteelblue:[176,196,222],lightyellow:[255,255,224],lime:[0,255,0],limegreen:[50,205,50],linen:[250,240,230],magenta:[255,0,255],maroon:[128,0,0],mediumaquamarine:[102,205,170],mediumblue:[0,0,205],mediumorchid:[186,85,211],mediumpurple:[147,112,219],mediumseagreen:[60,179,113],mediumslateblue:[123,104,238],mediumspringgreen:[0,250,154],mediumturquoise:[72,209,204],mediumvioletred:[199,21,133],midnightblue:[25,25,112],mintcream:[245,255,250],mistyrose:[255,228,225],moccasin:[255,228,181],navajowhite:[255,222,173],navy:[0,0,128],oldlace:[253,245,230],olive:[128,128,0],olivedrab:[107,142,35],orange:[255,165,0],orangered:[255,69,0],orchid:[218,112,214],palegoldenrod:[238,232,170],palegreen:[152,251,152],paleturquoise:[175,238,238],palevioletred:[219,112,147],papayawhip:[255,239,213],peachpuff:[255,218,185],peru:[205,133,63],pink:[255,192,203],plum:[221,160,221],powderblue:[176,224,230],purple:[128,0,128],rebeccapurple:[102,51,153],red:[255,0,0],rosybrown:[188,143,143],royalblue:[65,105,225],saddlebrown:[139,69,19],salmon:[250,128,114],sandybrown:[244,164,96],seagreen:[46,139,87],seashell:[255,245,238],sienna:[160,82,45],silver:[192,192,192],skyblue:[135,206,235],slateblue:[106,90,205],slategray:[112,128,144],slategrey:[112,128,144],snow:[255,250,250],springgreen:[0,255,127],steelblue:[70,130,180],tan:[210,180,140],teal:[0,128,128],thistle:[216,191,216],tomato:[255,99,71],turquoise:[64,224,208],violet:[238,130,238],wheat:[245,222,179],white:[255,255,255],whitesmoke:[245,245,245],yellow:[255,255,0],yellowgreen:[154,205,50]},ct={name:"hsl",min:[0,0,0],max:[360,100,100],channel:["hue","saturation","lightness"],alias:["HSL"],rgb:function(e){var t=e[0]/360,i=e[1]/100,a=e[2]/100,s,r,n,l,o;if(i===0)return o=a*255,[o,o,o];a<.5?r=a*(1+i):r=a+i-a*i,s=2*a-r,l=[0,0,0];for(var u=0;u<3;u++)n=t+1/3*-(u-1),n<0?n++:n>1&&n--,6*n<1?o=s+(r-s)*6*n:2*n<1?o=r:3*n<2?o=s+(r-s)*(2/3-n)*6:o=s,l[u]=o*255;return l}},Me={red:0,orange:60,yellow:120,green:180,blue:240,purple:300},mt=e=>{var t,i=[],a=1,s;if(typeof e=="string")if(Pe[e])i=Pe[e].slice(),s="rgb";else if(e==="transparent")a=0,s="rgb",i=[0,0,0];else if(/^#[A-Fa-f0-9]+$/.test(e)){var r=e.slice(1),n=r.length,l=n<=4;a=1,l?(i=[parseInt(r[0]+r[0],16),parseInt(r[1]+r[1],16),parseInt(r[2]+r[2],16)],n===4&&(a=parseInt(r[3]+r[3],16)/255)):(i=[parseInt(r[0]+r[1],16),parseInt(r[2]+r[3],16),parseInt(r[4]+r[5],16)],n===8&&(a=parseInt(r[6]+r[7],16)/255)),i[0]||(i[0]=0),i[1]||(i[1]=0),i[2]||(i[2]=0),s="rgb"}else if(t=/^((?:rgb|hs[lvb]|hwb|cmyk?|xy[zy]|gray|lab|lchu?v?|[ly]uv|lms)a?)\s*\(([^\)]*)\)/.exec(e)){var o=t[1],u=o==="rgb",r=o.replace(/a$/,"");s=r;var n=r==="cmyk"?4:r==="gray"?1:3;i=t[2].trim().split(/\s*[,\/]\s*|\s+/).map(function(h,g){if(/%$/.test(h))return g===n?parseFloat(h)/100:r==="rgb"?parseFloat(h)*255/100:parseFloat(h);if(r[g]==="h"){if(/deg$/.test(h))return parseFloat(h);if(Me[h]!==void 0)return Me[h]}return parseFloat(h)}),o===r&&i.push(1),a=u||i[n]===void 0?1:i[n],i=i.slice(0,n)}else e.length>10&&/[0-9](?:\s|\/)/.test(e)&&(i=e.match(/([0-9]+)/g).map(function(c){return parseFloat(c)}),s=e.match(/([a-z])/ig).join("").toLowerCase());else Number.isNaN(e)?Array.isArray(e)||e.length?(i=[e[0],e[1],e[2]],s="rgb",a=e.length===4?e[3]:1):e instanceof Object&&(e.r!=null||e.red!=null||e.R!=null?(s="rgb",i=[e.r||e.red||e.R||0,e.g||e.green||e.G||0,e.b||e.blue||e.B||0]):(s="hsl",i=[e.h||e.hue||e.H||0,e.s||e.saturation||e.S||0,e.l||e.lightness||e.L||e.b||e.brightness]),a=e.a||e.alpha||e.opacity||1,e.opacity!=null&&(a/=100)):(s="rgb",i=[e>>>16,(e&65280)>>>8,e&255]);return{space:s,values:i,alpha:a}},gt=e=>{Array.isArray(e)&&e.raw&&(e=String.raw(...arguments));var t,i,a,s=mt(e);return s.space?(t=Array(3),t[0]=Math.min(Math.max(s.values[0],0),255),t[1]=Math.min(Math.max(s.values[1],0),255),t[2]=Math.min(Math.max(s.values[2],0),255),s.space[0]==="h"&&(t=ct.rgb(t)),t.push(Math.min(Math.max(s.alpha,0),1)),t):[]},_e=gt,x=(e,t,i)=>Math.min(Math.max(e,t),i),V=e=>Math.pow(e,2),Y=e=>Math.pow(e,3),X=e=>Math.pow(e,4),Z=e=>Math.pow(e,5),H=e=>Math.pow(e,6),W=e=>1-Math.cos(e*Math.PI/2),B=e=>1-Math.sqrt(1-e*e),Q=e=>e*e*(3*e-2),J=e=>{let t,i=4;for(;e<((t=Math.pow(2,--i))-1)/11;);return 1/Math.pow(4,3-i)-7.5625*Math.pow((t*3-2)/22-e,2)},ee=(e,t=[])=>{let[i=1,a=.5]=t,s=x(i,1,10),r=x(a,.1,2);return e===0||e===1?e:-s*Math.pow(2,10*(e-1))*Math.sin((e-1-r/(Math.PI*2)*Math.asin(1/s))*(Math.PI*2)/r)},L=(e,t=[],i)=>{let[a=1,s=100,r=10,n=0]=t;a=x(a,.1,1e3),s=x(s,.1,1e3),r=x(r,.1,1e3),n=x(n,.1,1e3);let l=Math.sqrt(s/a),o=r/(2*Math.sqrt(s*a)),u=o<1?l*Math.sqrt(1-o*o):0,c=1,m=o<1?(o*l+-n)/u:-n+l,h=i?i*e/1e3:e;return o<1?h=Math.exp(-h*o*l)*(c*Math.cos(u*h)+m*Math.sin(u*h)):h=(c+m*h)*Math.exp(-h*l),e===0||e===1?e:1-h},oe=new Map,ft=1e4,dt=(e="spring")=>{if(oe.has(e))return oe.get(e);let t=typeof e=="function"?e:we(e),i=typeof e=="function"?[]:Ve(e),a=1/6,s=0,r=0,n=0;for(;++n<ft;)if(s+=a,t(s,i,null)===1){if(r++,r>=16)break}else r=0;let l=s*a*1e3;return oe.set(e,l),l},le=(e,t=[])=>{let[i=10,a]=t;return(a=="start"?Math.ceil:Math.floor)(x(e,0,1)*i)/i},Ce=(e,t=[])=>{let[i,a,s,r]=t;return Ge(i,a,s,r)(e)},te=Ge(.42,0,1,1),S=e=>(t,i=[],a)=>1-e(1-t,i,a),A=e=>(t,i=[],a)=>t<.5?e(t*2,i,a)/2:1-e(t*-2+2,i,a)/2,O=e=>(t,i=[],a)=>t<.5?(1-e(1-t*2,i,a))/2:(e(t*2-1,i,a)+1)/2,z={steps:le,"step-start":e=>le(e,[1,"start"]),"step-end":e=>le(e,[1,"end"]),linear:e=>e,"cubic-bezier":Ce,ease:e=>Ce(e,[.25,.1,.25,1]),in:te,out:S(te),"in-out":A(te),"out-in":O(te),"in-quad":V,"out-quad":S(V),"in-out-quad":A(V),"out-in-quad":O(V),"in-cubic":Y,"out-cubic":S(Y),"in-out-cubic":A(Y),"out-in-cubic":O(Y),"in-quart":X,"out-quart":S(X),"in-out-quart":A(X),"out-in-quart":O(X),"in-quint":Z,"out-quint":S(Z),"in-out-quint":A(Z),"out-in-quint":O(Z),"in-expo":H,"out-expo":S(H),"in-out-expo":A(H),"out-in-expo":O(H),"in-sine":W,"out-sine":S(W),"in-out-sine":A(W),"out-in-sine":O(W),"in-circ":B,"out-circ":S(B),"in-out-circ":A(B),"out-in-circ":O(B),"in-back":Q,"out-back":S(Q),"in-out-back":A(Q),"out-in-back":O(Q),"in-bounce":J,"out-bounce":S(J),"in-out-bounce":A(J),"out-in-bounce":O(J),"in-elastic":ee,"out-elastic":S(ee),"in-out-elastic":A(ee),"out-in-elastic":O(ee),spring:L,"spring-in":L,"spring-out":S(L),"spring-in-out":A(L),"spring-out-in":O(L)},ne=Object.keys(z),qt=(e,t)=>{Object.assign(z,{[e]:t}),ne=Object.keys(z)},zt=(...e)=>{Object.assign(z,...e),ne=Object.keys(z)},ke=e=>Te(e).replace(/^ease-/,"").replace(/(\(|\s).+/,"").toLowerCase().trim(),we=e=>{let t=ke(e);return ne.includes(t)?z[t]:null},Ve=e=>{let t=/(\(|\s)([^)]+)\)?/.exec(e);return t?t[2].split(",").map(i=>{let a=parseFloat(i);return Number.isNaN(a)?i.trim():a}):[]},yt=(e,t,i)=>t+(i-t)*e,Ye=(e,t)=>Math.round(e*10**t)/10**t,Ee=(e,t,i=3)=>{let a=t.length-1,s=x(Math.floor(e*a),0,a-1),r=t[s],n=t[s+1],l=(e-s/a)*a;return Ye(yt(l,r,n),i)},Xe=e=>{let t=parseFloat(e);return typeof t=="number"&&!Number.isNaN(t)},bt=(e,t)=>{e=x(e,0,1);let i=t.length-1,a=Math.round(e*i);return t[a]},kt=(e,t,i=3)=>{let a="";return Xe(t[0])&&(a=K(t[0]).replace(/^\d+/,"")),Ee(e,t.map(parseFloat),i)+a},wt=(e,t,i=3)=>ye(...t.map(_e)).map((a,s)=>{let r=Ee(e,a);return s<3?Math.round(r):Ye(r,i)}),ge=e=>K(e).trim(),Et=e=>ge(e).replace(/(\d|\)|\w)\s/g,t=>t[0]+"__").split("__").map(ge).filter(E),ve=(e,t,i=3)=>{if(t.every(a=>typeof a=="number"))return Ee(e,t,i);if(t.every(a=>E(_e(a??null))))return`rgba(${wt(e,t,i)})`;if(t.some(a=>typeof a=="string"))return t.some(a=>/(\d|\)|\w)\s/.test(ge(a)))?ye(...t.map(Et)).map(a=>ve(e,a,i)).join(" "):t.every(a=>Xe(a))?kt(e,t,i):bt(e,t)},Ze=(e={})=>{let t=typeof e=="string"||typeof e=="function",{easing:i="spring(1, 100, 10, 0)",numPoints:a=100,decimal:s=3,duration:r}=t?{easing:e}:e;return{easing:i,numPoints:a,decimal:s,duration:r}},ue=new Map,vt=({easing:e,numPoints:t,duration:i}={})=>{let a=[],s=`${e}${t}`;if(ue.has(s))return ue.get(s);let r=typeof e=="function"?e:we(e),n=typeof e=="function"?[]:Ve(e);for(let l=0;l<t;l++)a[l]=r(l/(t-1),n,i);return ue.set(s,a),a},Se=(e={})=>{if(typeof e.easing=="string"){let t=ke(e.easing);/(spring|spring-in)$/i.test(t)&&(e.duration=dt(e.easing))}},He=(e,t={})=>{let i=Ze(t);return Se(i),vt(i).map(a=>ve(a,e,i.decimal))},Ft=(e,t={})=>{let i=Ze(t),{duration:a}=i;return Se(i),[He(e,i),E(a)?a:i.duration]},St=(e={})=>{let{easing:t,numPoints:i,decimal:a,duration:s,...r}=e,n={easing:t,numPoints:i,decimal:a,duration:s};Se(n);let l=F(r,u=>He(u,n)),o={};return E(s)?o={duration:s}:E(n.duration)&&(o={duration:n.duration}),Object.assign({},l,o)},Ae=class{constructor(e){this.map=new Map(e)}getMap(){return this.map}get(e){return this.map.get(e)}keys(){return Array.from(this.map.keys())}values(){return Array.from(this.map.values())}set(e,t){return this.map.set(e,t),this}add(e){let t=this.size;return this.set(t,e),this}get size(){return this.map.size}get length(){return this.map.size}last(e=1){let t=this.keys()[this.size-e];return this.get(t)}delete(e){return this.map.delete(e)}remove(e){return this.map.delete(e),this}clear(){return this.map.clear(),this}has(e){return this.map.has(e)}entries(){return this.map.entries()}forEach(e,t){return this.map.forEach(e,t),this}[Symbol.iterator](){return this.entries()}},At=(e,t,...i)=>{e.forEach(a=>{a[t](...i)})},xe=({callback:e=()=>{},scope:t=null,name:i="event"})=>({callback:e,scope:t,name:i}),ie=class extends Ae{constructor(e="event"){super(),this.name=e}},Ot=class extends Ae{constructor(){super()}getEvent(e){let t=this.get(e);return t instanceof ie?t:(this.set(e,new ie(e)),this.get(e))}newListener(e,t,i){let a=this.getEvent(e);return a.add(xe({name:e,callback:t,scope:i})),a}on(e,t,i){if(typeof e>"u"||e==null)return this;typeof e=="string"&&(e=e.trim().split(/\s/g));let a,s,r=typeof e=="object"&&!Array.isArray(e),n=r?t:i;return r||(s=t),Object.keys(e).forEach(l=>{a=r?l:e[l],r&&(s=e[l]),this.newListener(a,s,n)},this),this}removeListener(e,t,i){let a=this.get(e);if(a instanceof ie&&t){let s=xe({name:e,callback:t,scope:i});a.forEach((r,n)=>{if(r.callback===s.callback&&r.scope===s.scope)return a.remove(n)})}return a}off(e,t,i){if(typeof e>"u"||e==null)return this;typeof e=="string"&&(e=e.trim().split(/\s/g));let a,s,r=typeof e=="object"&&!Array.isArray(e),n=r?t:i;return r||(s=t),Object.keys(e).forEach(l=>{a=r?l:e[l],r&&(s=e[l]),typeof s=="function"?this.removeListener(a,s,n):this.remove(a)},this),this}once(e,t,i){if(typeof e>"u"||e==null)return this;typeof e=="string"&&(e=e.trim().split(/\s/g));let a=typeof e=="object"&&!Array.isArray(e);return Object.keys(e).forEach(s=>{let r=a?s:e[s],n=a?e[s]:t,l=a?t:i,o=(...u)=>{n.apply(l,u),this.removeListener(r,o,l)};this.newListener(r,o,l)},this),this}emit(e,...t){return typeof e>"u"||e==null?this:(typeof e=="string"&&(e=e.trim().split(/\s/g)),e.forEach(i=>{let a=this.get(i);a instanceof ie&&a.forEach(s=>{let{callback:r,scope:n}=s;r.apply(n,t)})},this),this)}clear(){return At(this,"clear"),super.clear(),this}},Nt=e=>typeof e=="string"?Array.from(document.querySelectorAll(e)):[e],R=e=>Array.isArray(e)?Je(e.map(R)):typeof e=="string"||e instanceof Node?Nt(e):e instanceof NodeList||e instanceof HTMLCollection?Array.from(e):[],Pt=(e,t,i)=>typeof e=="function"?e.apply(i,t):e,Ie=(e,t,i)=>F(e,a=>Pt(a,t,i)),We={in:"ease-in",out:"ease-out","in-out":"ease-in-out","in-sine":"cubic-bezier(0.47, 0, 0.745, 0.715)","out-sine":"cubic-bezier(0.39, 0.575, 0.565, 1)","in-out-sine":"cubic-bezier(0.445, 0.05, 0.55, 0.95)","in-quad":"cubic-bezier(0.55, 0.085, 0.68, 0.53)","out-quad":"cubic-bezier(0.25, 0.46, 0.45, 0.94)","in-out-quad":"cubic-bezier(0.455, 0.03, 0.515, 0.955)","in-cubic":"cubic-bezier(0.55, 0.055, 0.675, 0.19)","out-cubic":"cubic-bezier(0.215, 0.61, 0.355, 1)","in-out-cubic":"cubic-bezier(0.645, 0.045, 0.355, 1)","in-quart":"cubic-bezier(0.895, 0.03, 0.685, 0.22)","out-quart":"cubic-bezier(0.165, 0.84, 0.44, 1)","in-out-quart":"cubic-bezier(0.77, 0, 0.175, 1)","in-quint":"cubic-bezier(0.755, 0.05, 0.855, 0.06)","out-quint":"cubic-bezier(0.23, 1, 0.32, 1)","in-out-quint":"cubic-bezier(0.86, 0, 0.07, 1)","in-expo":"cubic-bezier(0.95, 0.05, 0.795, 0.035)","out-expo":"cubic-bezier(0.19, 1, 0.22, 1)","in-out-expo":"cubic-bezier(1, 0, 0, 1)","in-circ":"cubic-bezier(0.6, 0.04, 0.98, 0.335)","out-circ":"cubic-bezier(0.075, 0.82, 0.165, 1)","in-out-circ":"cubic-bezier(0.785, 0.135, 0.15, 0.86)","in-back":"cubic-bezier(0.6, -0.28, 0.735, 0.045)","out-back":"cubic-bezier(0.175, 0.885, 0.32, 1.275)","in-out-back":"cubic-bezier(0.68, -0.55, 0.265, 1.55)"},Be=Object.keys(We),fe=(e="ease")=>{let t=Te(e).replace(/^ease-/,"");return Be.includes(t)?We[t]:e},De={keyframes:[],offset:[],loop:1,delay:0,speed:1,endDelay:0,easing:"ease",timelineOffset:0,autoplay:!0,duration:1e3,fillMode:"none",direction:"normal",padEndDelay:!1,timeline:document.timeline,extend:{}},Oe=e=>{let{options:t,...i}=e,a=t instanceof $?t.options:Array.isArray(t)?t?.[0]?.options:t;return Object.assign({},a,i)},Qe=["easing","loop","endDelay","duration","speed","delay","timelineOffset","direction","extend","fillMode","offset"],Mt=["keyframes","padEndDelay","onfinish","oncancel","autoplay","target","targets","timeline"],Ne=[...Qe,...Mt],d=class{constructor(e){this.options={},this.properties={},this.totalDuration=-1/0,this.minDelay=1/0,this.maxSpeed=1/0,this.emitter=new Ot,this.targets=new Ae,this.targetIndexes=new WeakMap,this.keyframeEffects=new WeakMap,this.computedOptions=new WeakMap,this.animations=new WeakMap,this.computedKeyframes=new WeakMap,this.loop=this.loop.bind(this),this.onVisibilityChange=this.onVisibilityChange.bind(this),this.on("error",console.error),this.updateOptions(e),this.mainAnimation&&(this.visibilityPlayState=this.getPlayState(),d.pauseOnPageHidden&&document.addEventListener("visibilitychange",this.onVisibilityChange,!1)),this.newPromise()}static requestFrame(){d.cancelFrame(),d.RUNNING.forEach(e=>{e.emitter.getEvent("update").length<=0?e.stopLoop():e.loop()}),d.RUNNING.size>0?d.animationFrame=window.requestAnimationFrame(d.requestFrame):d.cancelFrame()}static cancelFrame(){window.cancelAnimationFrame(d.animationFrame),d.animationFrame=null}loop(){this.emit("update",this.getProgress(),this)}stopLoop(){d.RUNNING.delete(this)}onVisibilityChange(){document.hidden?(this.visibilityPlayState=this.getPlayState(),this.is("running")&&(this.loop(),this.pause())):this.visibilityPlayState=="running"&&this.is("paused")&&this.play()}newPromise(){return this.promise=new Promise((e,t)=>{this.emitter?.once?.("finish",()=>e([this])),this.emitter?.once?.("error",i=>t(i))}),this.promise}then(e,t){return e=e?.bind(this),t=t?.bind(this),this.promise?.then?.(e,t),this}catch(e){return e=e?.bind(this),this.promise?.catch?.(e),this}finally(e){return e=e?.bind(this),this.promise?.finally?.(e),this}allAnimations(e){return this.targets.forEach(t=>{let i=this.keyframeEffects.get(t),a=this.animations.get(i);return e(a,t)}),this}all(e){return this.mainAnimation&&e(this.mainAnimation,this.mainElement),this.allAnimations(e),this}beginEvent(){this.getProgress()==0&&this.emit("begin",this)}play(){let e=this.getPlayState();return this.beginEvent(),this.all(t=>t.play()),this.emit("play",e,this),this.is(e)||this.emit("playstate-change",e,this),this.loop(),d.RUNNING.add(this),d.requestFrame(),this}pause(){let e=this.getPlayState();return this.all(t=>t.pause()),this.emit("pause",e,this),this.is(e)||this.emit("playstate-change",e,this),this.stopLoop(),this}reverse(){return this.all(e=>e.reverse()),this}reset(){return this.setProgress(0),this.options.autoplay?this.play():this.pause(),this}cancel(){return this.all(e=>e.cancel()),this}finish(){return this.all(e=>e.finish()),this}stop(){this.cancel(),this.stopLoop(),document.removeEventListener("visibilitychange",this.onVisibilityChange,!1),this.targets.forEach(e=>this.removeTarget(e)),this.emit("stop"),this.emitter.clear(),this.mainkeyframeEffect=null,this.mainAnimation=null,this.mainElement?.remove?.(),this.mainElement=null,this.promise=null,this.computedOptions=null,this.animations=null,this.keyframeEffects=null,this.emitter=null,this.targets=null,this.options=null,this.properties=null}getAnimation(e){let t=this.keyframeEffects.get(e);return this.animations.get(t)}getTiming(e){let t=this.computedOptions.get(e)??{},i=this.keyframeEffects.get(e).getTiming?.()??{};return{...t,...i}}getCurrentTime(){return this.mainAnimation.currentTime}getProgress(){return this.getCurrentTime()/this.totalDuration*100}getSpeed(){return this.mainAnimation.playbackRate}getPlayState(){return this.mainAnimation.playState}is(e){return this.getPlayState()==e}setCurrentTime(e){return this.all(t=>t.currentTime=e),this.emit("update",this.getProgress()),this}setProgress(e){let t=e/100*this.totalDuration;return this.setCurrentTime(t),this}setSpeed(e=1){return this.maxSpeed=e,this.all(t=>{t.updatePlaybackRate?t.updatePlaybackRate(e):t.playbackRate=e}),this}createArrayOfComputedOptions(e,t){let i=[];return this.targets.forEach((a,s)=>{let r=this.computedOptions.get(a)??{},n=b=>{let k=b;return b=="loop"&&(k="iterations"),b=="fillMode"&&(k="fill"),e[b]??r[k]??this.options[b]??De[b]},l=Object.assign({easing:n("easing"),iterations:n("loop"),direction:n("direction"),endDelay:n("endDelay"),duration:n("duration"),speed:n("speed"),delay:n("delay"),timelineOffset:n("timelineOffset"),keyframes:n("keyframes")},n("extend")??{}),o=Ie(l,[s,t,a],this);typeof o.easing=="string"&&(o.easing=fe(o.easing)),o.iterations===!0&&(o.iterations=1/0),o.fill=n("fillMode");let{timelineOffset:u,speed:c,endDelay:m,delay:h,duration:g,iterations:p,...f}=o;p=Number(p),g=Number(g),m=Number(m),c=Number(c),h=Number(h)+Number(u);let y=h+g*p+m;this.totalDuration<y&&(this.totalDuration=y),i[s]={...f,speed:c,tempDurations:y,endDelay:m,delay:h,duration:g,iterations:p},this.minDelay>h&&(this.minDelay=h),this.maxSpeed>c&&(this.maxSpeed=c)}),i}createAnimations(e,t){let{arrOfComputedOptions:i,padEndDelay:a,oldCSSProperties:s,onfinish:r,oncancel:n,timeline:l}=e;this.targets.forEach((o,u)=>{let{speed:c,keyframes:m,tempDurations:h,...g}=i[u];a&&g.endDelay==0&&Math.abs(g.iterations)!=Math.abs(1/0)&&(g.endDelay=this.totalDuration-h);let p,f,y=m;typeof y=="object"&&(y=tt(y));let b=this.computedKeyframes.get(o)??{},k=Object.assign({},s,b),j=F(k,(T,I)=>this.properties[I]??T);if(f=E(y)?y:j,Array.isArray(f))p=f.map(T=>{let{easing:I,offset:M,...G}=q(["speed","loop"],T);return Object.assign({},G,typeof I=="string"?{easing:fe(I)}:null,typeof M=="string"||typeof M=="number"?{offset:he(M)}:null)}),p=at(p);else{let T=q(["keyframes"],f),{offset:I,...M}=Ie(T,[u,t,o],this);M=it(M);let G=I;p=Object.assign({},M,E(G)?{offset:G.map(he)}:null)}let v,P;this.keyframeEffects.has(o)?(P=this.keyframeEffects.get(o),v=this.animations.get(P),P?.setKeyframes?.(p),P?.updateTiming?.(g)):(P=new KeyframeEffect(o,p,g),v=new Animation(P,l),this.keyframeEffects.set(o,P),this.animations.set(P,v)),v.playbackRate=c,v.onfinish=()=>{typeof r=="function"&&r.call(this,o,u,t,v)},v.oncancel=()=>{typeof n=="function"&&n.call(this,o,u,t,v)},this.computedOptions.set(o,g),this.computedKeyframes.set(o,p)})}updateOptions(e={}){try{let t=Oe(e);this.options=Object.assign({},De,this.options,t);let{padEndDelay:i,autoplay:a,target:s,targets:r,timeline:n,onfinish:l,oncancel:o,...u}=q(Qe,this.options);this.properties=q(Ne,t);let c=this.targets.values(),m=[...new Set([...c,...R(r),...R(s)])];this.targets.clear(),m.forEach((p,f)=>{this.targets.set(f,p),this.targetIndexes.set(p,f)});let h=this.targets.size,g=this.createArrayOfComputedOptions(t,h);if(this.createAnimations({arrOfComputedOptions:g,padEndDelay:i,oldCSSProperties:u,onfinish:l,oncancel:o,timeline:n},h),h<=0&&(this.maxSpeed==1/0&&(this.maxSpeed=Number(this.options.speed)),this.minDelay==1/0&&(this.minDelay=Number(this.options.delay)+Number(this.options.timelineOffset)),this.totalDuration==-1/0&&(this.totalDuration=Number(this.options.duration))),this.mainAnimation?(this.mainkeyframeEffect?.updateTiming?.({duration:this.totalDuration}),(!this.mainkeyframeEffect.setKeyframes||!this.mainkeyframeEffect.updateTiming)&&console.warn("@okikio/animate - `KeyframeEffect.setKeyframes` and/or `KeyframeEffect.updateTiming` are not supported in this browser.")):(this.mainkeyframeEffect=new KeyframeEffect(this.mainElement,null,{duration:this.totalDuration}),this.mainAnimation=new Animation(this.mainkeyframeEffect,n)),this.mainAnimation.playbackRate=this.maxSpeed,this.mainAnimation.onfinish=()=>{if(this.mainAnimation){let p=this.getPlayState();this.is(p)||this.emit("playstate-change",p,this),this.loop(),this.stopLoop()}this.emit("finish",this)},this.mainAnimation.oncancel=()=>{if(this.mainAnimation){let p=this.getPlayState();this.is(p)||this.emit("playstate-change",p,this),this.loop(),this.stopLoop()}this.emit("cancel",this)},a){let p=window.setTimeout(()=>{this.emit("begin",this),p=window.clearTimeout(p)},0);this.play()}else this.pause()}catch(t){this.emit("error",t)}}add(e){let t=this.getProgress(),i=this.is("running"),a=this.is("paused");this.updateOptions({target:e}),this.setProgress(t),i?this.play():a&&this.pause()}removeTarget(e){let t=this.keyframeEffects.get(e);this.animations.delete(t),t=null,this.computedKeyframes.delete(e),this.computedOptions.delete(e),this.keyframeEffects.delete(e);let i=this.targetIndexes.get(e);this.targets.delete(i),this.targetIndexes.delete(e)}remove(e){this.removeTarget(e);let t=new Set([].concat(this.targets.values()));this.options.target=[...t],this.options.targets=[],t.clear(),t=null;let i=this.getProgress(),a=this.is("running"),s=this.is("paused");this.updateOptions(),a?this.play():s&&this.pause(),this.setProgress(i)}on(e,t,i){return this.emitter?.on?.(e,t,i??this),this.emitter.getEvent("update").length>0&&(d.RUNNING.add(this),d.animationFrame==null&&d.requestFrame()),this}off(e,t,i){return this.emitter?.off?.(e,t,i??this),this}emit(e,...t){return this.emitter?.emit?.(e,...t),this}toJSON(){return this.options}get[Symbol.toStringTag](){return"Animate"}},$=d;$.pauseOnPageHidden=!0,$.RUNNING=new Set;var jt=(e={})=>new $(e),Ct=0,xt=()=>{let e=document.createElement("div");return e.id=`empty-animate-el-${Ct++}`,e.style.setProperty("display","none"),document.body.appendChild(e),e},It=class extends ${constructor(e={}){super(e)}stop(){this.targets.forEach(e=>e?.remove?.()),super.stop()}},Dt=(e={})=>{let t=xt(),{target:i,easing:a,decimal:s,numPoints:r,...n}=Oe(e),l=et(Ne,n),o=a;if(typeof a=="string"){let c=ke(a);Be.includes(c)||["linear","steps","step-start","step-end"].includes(c)?o=fe(a):ne.includes(c)?o=we(a):o=a}let u=typeof o=="function"?St({opacity:[0,1],easing:a,decimal:s,numPoints:r}):{opacity:[0,1],easing:a};return new It({target:t,...u,...l})},Lt=(e={},t="attribute")=>{let i=Oe(e),a=q([...Ne,"opacity","decimal","numPoints"],i),s=Dt(i),{target:r,targets:n}=i,l=[...new Set([...R(n),...R(r)])];try{let o=l.length,u=s.targets.get(0),c=getComputedStyle(u);l.forEach((m,h)=>{let g=/style/i.test(t)?getComputedStyle(m):null;F(a,(p,f)=>{let y=/style/i.test(t)?g?.getPropertyValue(f):m.getAttribute(f);s.on("update",()=>{let b=Number(c.getPropertyValue("opacity")),k;typeof p=="function"?k=p(b,h,o,m):k=ve(b,Array.isArray(p)?p:[y,p],e.decimal),/style/i.test(t)?m.style.setProperty(f,""+k):m.setAttribute(f,""+k)})})})}catch(o){s?.stopLoop(),console.error(o)}return s};export{Ne as ALL_TIMING_KEYS,$ as Animate,St as ApplyCustomEasing,Q as Back,Ce as Bezier,J as Bounce,D as CSSArrValue,Le as CSSPXDataType,re as CSSValue,B as Circ,ke as ComplexEasingSyntax,Et as ComplexStrtoArr,Y as Cubic,He as CustomEasing,Ze as CustomEasingOptions,De as DefaultAnimationOptions,It as DestroyableAnimate,We as EASINGS,Tt as EFFECTS,A as EaseInOut,S as EaseOut,O as EaseOutIn,oe as EasingDurationCache,ne as EasingFunctionKeys,z as EasingFunctions,Be as EasingKeys,vt as EasingPts,ee as Elastic,H as Expo,Qe as FUNCTION_SUPPORTED_TIMING_KEYS,fe as GetEase,we as GetEasingFunction,ft as INTINITE_LOOP_LIMIT,tt as KeyframeParse,Mt as NOT_FUNCTION_SUPPORTED_TIMING_KEYS,ze as ParseCSSProperties,at as ParseTransformableCSSKeyframes,it as ParseTransformableCSSProperties,V as Quad,X as Quart,Z as Quint,W as Sine,L as Spring,Ft as SpringEasing,le as Steps,me as TransformFunctionNames,Fe as TransformFunctions,ue as TweenCache,Ct as UIDCount,ce as UnitDEG,w as UnitDEGCSSValue,ae as UnitLess,C as UnitLessCSSValue,pe as UnitPX,N as UnitPXCSSValue,be as addCSSUnit,jt as animate,qe as camelCase,Pt as computeOption,Te as convertToDash,xt as createEmptyEl,je as createTransformProperty,te as easein,Je as flatten,dt as getEasingDuration,Nt as getElements,R as getTargets,wt as interpolateColor,ve as interpolateComplex,Ee as interpolateNumber,kt as interpolateString,bt as interpolateUsingIndex,Xe as isNumberLike,E as isValid,x as limit,Ie as mapAnimationOptions,F as mapObject,q as omit,Ve as parseEasingParameters,he as parseOffset,Oe as parseOptions,et as pick,qt as registerEasingFunction,zt as registerEasingFunctions,yt as scale,de as toArr,Ye as toFixed,K as toStr,ye as transpose,ge as trim,Dt as tween,Lt as tweenAttr};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment