@xtreat/solid-iconify
Version:
A reactive and first class native Solid Iconify icon component
3 lines (2 loc) • 3.87 kB
JSX
import{createResource as L,splitProps as $}from"solid-js";var y=class{cache;strategy;capacity;constructor(t,i){if(typeof t=="number")this.strategy="static",this.capacity=Math.abs(t);else switch(this.strategy=t.strategy,t.strategy){case"unlimited":this.capacity=Number.POSITIVE_INFINITY-1;break;case"grow":this.capacity=Math.max(1,t.initial??1);break;case"static":this.capacity=Math.abs(t.limit);break}if(!Number.isInteger(this.capacity)&&this.capacity!==Number.POSITIVE_INFINITY||this.capacity<=0)throw new TypeError("invalid capacity");if(this.cache=new Map(i),this.strategy==="static")for(;this.cache.size>this.capacity;)this.cache.delete(this.cache.keys().next().value)}get size(){return this.cache.size}get(t){let i=this.cache.get(t);if(i!==void 0)return this.cache.delete(t),this.cache.set(t,i),i}set(t,i){return this.cache.has(t)?this.cache.delete(t):this.cache.size>=this.capacity&&(this.strategy==="grow"?this.capacity*=2:this.strategy!=="unlimited"&&this.cache.delete(this.cache.keys().next().value)),this.cache.set(t,i),this}delete(t){let i=this.cache.get(t);return i!==void 0&&this.cache.delete(t),i}entries(){return this.cache.entries()}[Symbol.iterator](){return this.entries()}},u=y;var m={ICONIFY_API:"api.iconify.design",REQUEST_OPTIONS:{method:"GET",credentials:"omit",redirect:"follow",cache:"force-cache",headers:{Accept:"application/json"},referrerPolicy:"no-referrer",keepalive:!0},CACHE:{strategy:"unlimited"},SANITIZE:!1};var n=m;function P(e){n={...n,...e},d()}var o;function w(e){let t=n.CACHE,i;if(typeof t=="number")i={strategy:"grow",initial:t};else{if(t.strategy==="no-cache")throw new Error("[solid-iconify] ERROR - Attempting to cache with 'no-cache' strategy");i=t}return new u(i,e)}function d(){let e=n.CACHE;typeof e=="object"&&e.strategy==="no-cache"||(o?o=new Map([...o].map(([t,i])=>[t,w(i)])):o=new Map)}var C=(e,t)=>o?.get(e)?.get(t);function l(e,t,i){return o?.get(e)||o?.set(e,w()),o?.get(e)?.set(t,i)}var O=(e,t)=>o?.get(e)?.delete(t);var E=e=>e.replace(/</g,"<").replace(/>/g,">").trim(),I;function U(){return I||(I=import("xss").then(({default:e})=>t=>e(t,n.SANITIZE_OPTIONS))),I}async function R(e){let t=await U();return E(t(e))}function N(e){let t=e instanceof URL?e.href:e.trim();t=/^[a-zA-Z][\w+.-]*:/.test(t)?t.replace(/^([a-zA-Z][\w+.-]*:)(?!\/\/)/,"$1//"):`https://${t}`;let i;try{i=new URL(t)}catch{i=new URL(`https://${t.replace(/^([a-zA-Z][\w+.-]*:)?\/?\/?/,"")}`)}let r=i.pathname.replace(/^\/|\/$/g,"");return i.host.toLowerCase()+(r?`/${r}`:"")}function S({collection:e,icons:t,api:i}){if(!e||!t||t.length<=0)throw Error(`[solid-iconify] Bad specifier: ${e}, ${t}`);let r=n.ICONIFY_API,a;Array.isArray(r)?a=N(r[i%r.length]):a=N(r);let p=`https://${a}/`,h=new URL(`${e}.json?icons=${t.toString()}`,p);return h.searchParams.sort(),h}var v=e=>new Promise(t=>setTimeout(t,e));function b(e,t=3,i=100){return e().catch(r=>{if(t<=0)throw r;return v(i).then(()=>b(e,t-1,i*2))})}function x(e,t=0){let[i,r]=e.split(":"),a=C(i,r);if(a)return a;let p=S({collection:i,icons:[r],api:t}),h=b(()=>fetch(p,n.REQUEST_OPTIONS).then(async f=>{if(!f.ok)throw Error(`[solid-iconify] ERROR - Iconify API bad response ${f.status}
${f}`);let c=await f.json(),s=Object.values(c.icons).pop()?.body;if(!s||s.length===0)throw Error("[solid-iconify] ERROR - Invalid SVG (empty / undefined)");n.SANITIZE&&(s=await R(s));let g={vBox:[c.left??0,c.top??0,c.width??24,c.height??24],body:s};return l(i,r,Promise.resolve(g)),g})).catch(async f=>{let c=n.ICONIFY_API,s=t+1;if(Array.isArray(c)&&s<c.length)return x(e,s);throw O(i,r),f});return l(i,r,h),h}var A=x;function F(e){let[t,i]=$(e,["icon"]),[r]=L(()=>t.icon,()=>A(t.icon));return<svg height={r()?.vBox[3]??24}innerHTML={r()?.body}viewBox={r()?.vBox.join(" ")??"0 0 24 24"}width={r()?.vBox[2]??24}xmlns="http://www.w3.org/2000/svg"{...i}/>}export{P as configureIconify,F as default};