UNPKG

@graphery/svg

Version:
1 lines 4.95 kB
let N="gySVGObject",nt="http://www.w3.org/2000/svg",H="svg",j="object",_="function",k="string",st="boolean",Y="undefined",at="symbol",B="path",x="animate",it="none",ot="freeze",v="d",ct="transform",q="rotate",M="translate",ut="offset",U="inherit",Z="finished",O="",R=",",lt=[q,"skewX","skewY"],ft="deg",T="px",J="ms",L=new WeakMap,I=new Set;let P=!1;if(window.matchMedia){let t=window.matchMedia("(prefers-reduced-motion: reduce)");P=t.matches,t.addEventListener("change",()=>{P=t.matches})}let dt=t=>t==="el"||t[0]==="_"||u(t,at),u=(t,e)=>typeof t===e,G=(t,e)=>t instanceof e,gt=()=>N+Math.random().toString(32).substring(2),D=t=>u(t,j)&&t!==null&&t[Symbol.toStringTag]===N,ht=t=>w(document.createElementNS(nt,t)),F=t=>u(t,k)?ht(t):u(t,j)&&t!==null?D(t)?t:w(t):t,mt=t=>({content:"innerHTML",source:"outerHTML",parent:"parentElement",next:"nextElementSibling",previous:"previousElementSibling"})[t]||t,bt=t=>t.replace(/([A-Z])/g,"-$1").toLowerCase();class K{constructor(t){this._el=t,this.gySVG=y}get[Symbol.toStringTag](){return N}get el(){return this._el}add(t){let e=F(t);return e&&this._el.appendChild(e._el),e}addBefore(t){let e=F(t);return e&&this._el.insertBefore(e._el,this._el.firstChild||null),e}attachTo(t){let e=u(t,j)?D(t)?t._el:t:document.querySelector(t);return e.appendChild(this._el),this}id(t){return t?(this._el.setAttribute("id",t),this):this._el.id||(this._el.id=gt())}ref(){return`#${this.id()}`}url(){return`url(${this.ref()})`}animateTo(t,e={duration:200},o=null,i=null){let n=r=>{let s=u(r,j)?Object.assign({},r):{duration:r};return P&&(s.duration=0),s.fill=it,s},b=r=>{r=G(r,Array)?r:[r];let s=window.getComputedStyle(this._el),a=[],l=new Set;for(let m of r){let h=Object.assign({},m);for(let g in h)g in s?g===v?h.d=`${B}("${h.d}")`:g===ct&&(h.transform=c(h.transform)):l.add(g);a.push(h)}return d(l,a),a},f=[],d=(r,s)=>{if(r.size){let a=new KeyframeEffect(null,s).getKeyframes(),l=this.closest(H).getCurrentTime()*1e3;for(let m of r){let h=y(x).attributeName(m).dur(z.duration+J).begin((0|l+(e.delay||0))+J).fill(ot);if(s.length===1)h.to(s[0][m]);else{let g=[],V=[];for(let E in a){let rt=a[E];m in s[E]&&(g.push(rt.computedOffset),V.push(s[E][m]))}g[0]!==0&&(g.unshift(0),V.unshift(this[m]()||U)),g[g.length-1]!==1&&(g.push(1),V.push(this[m]()||U)),h.keyTimes(g.join(";")).values(V.join(";"))}f.push(h),h.attachTo(this)}}},c=r=>{u(r,k)&&(r=JSON.parse("{"+r.replace(/\s*\(\s*/g,":[").replace(/\s*\)\s*/g,"],").split(/\s*,\s*|\s.*/).join(",").replace(/(\w+):/g,'"$1":').replace(/,$/,"")+"}"));let s=O;for(let a in r)if(a===q){let l=p(r[a]);l.length>1&&(s+=`${M}(${l[1]}${T},${l[2]}${T}) `),s+=`${a}(${l[0]}${S(a)}) `,l.length>1&&(s+=`${M}(-${l[1]}${T},-${l[2]}${T}) `)}else s+=`${a}(${p(r[a]).map(l=>l+S(a)).join(R)}) `;return s},p=r=>G(r,Array)?r:String(r).split(/\s+|,/),S=r=>lt.includes(r)?ft:r===M?T:O,tt=r=>typeof r=="string"?r.replace(/(deg)|(px)/g,O).trim():r,et=r=>r.replace(/(path\s*\(\s*["'])|(["']\s*\)\s*$)/g,O).trim().replace(/([a-zA-Z])\s*/g,"$1").replace(/\s+/g,R),z=n(e),C=b(t),$=this._el.animate(C,z);return $.ready.then(()=>u(o,_)&&o.call(this,$)),$.finished.then(()=>{let r=C[C.length-1];for(let s in r){let a=bt(s);/^text-/.test(a)?this._el.style[s]=r[s]:s!==ut&&s in r&&this._el.setAttribute(a,a==="d"?et(r[s]):tt(r[s]))}f.forEach(s=>{s[Z](!0);let a=this._el.querySelectorAll(x),l=this._el.querySelectorAll(`${x}[${Z}]`);a.length===l.length&&a.forEach(m=>m.remove())}),u(i,_)&&i.call(this,$)}),this}}let w=t=>{if(!u(t,j)||t===null)return null;if(L.has(t))return L.get(t);let e=new Proxy(new K(t),{get(o,i){if(dt(i))return o[i];if(!u(o[i],Y))return(...n)=>A(e,i,n)||o[i].call(e,...n);if(i===v&&t.tagName.toLowerCase()===B){let n=O;let b=new Proxy(f=>A(e,i,[f])||f?t.setAttribute(v,f)||e:t.getAttribute(v),{get(f,d){return(...c)=>(n+=pt(e,d,c)||`${d}${c.join(R)}`,t.setAttribute(i,n),b)}});return b}return i=mt(i),u(t[i],_)?(...n)=>{let b=A(e,i,n)||t[i].call(t,...n);return b===void 0?e:Q(b)}:X(t,i,e)}});return L.set(t,e),e},X=(t,e,o,i)=>{let n=e.replace(/_/g,"-"),b=(...f)=>{let d=A(o,i?`${i}.${e}`:e,f);if(d!==void 0)return d;if(f.length===0){let p=t.hasAttribute&&t.hasAttribute(n)?t.getAttribute(n):t[n];return Q(p)}let c=f[0];if(G(t,CSSStyleDeclaration))return t[n]=c,o;if(n in t&&!I.has(n)){if(t[n]===c)return o;let p=t[n];try{t[n]=c}catch(S){I.add(n)}if(u(t[n],j)&&t[n]===c||t[n]!==p)return o;I.add(n)}return c!==0&&!c?t.removeAttribute(n):t.setAttribute(n,u(c,st)?"":String(f)),o};return new Proxy(b,{get(f,d){let c=t[n][d];return u(c,_)?(...p)=>{let S=A(o,`${e}.${d}`,p)||c.call(t[n],...p);return S===void 0?o:S}:X(t[n],d,o,n)},set(f,d,c){return t[n][d]=c,!0}})},Q=t=>G(t,HTMLCollection)||G(t,NodeList)?[...t].map(e=>w(e)):G(t,SVGElement)?w(t):u(t,k)?t===""||Number.isNaN(Number(t))?t:Number(t):t,A=(t,e,o)=>{if(t._call)return t._call(t,e,o)},pt=(t,e,o)=>{if(t._d)return t._d(t,e,o)};function y(t){return A(y,O,[t]),F(u(t,Y)?H:t)}var W=y;y.isWrapped=D;y.extend=t=>(t(y,K),y);export{W as default};