UNPKG

@xtreat/solid-iconify

Version:

A reactive and first class native Solid Iconify icon component

3 lines 4.08 kB
import {template,spread,mergeProps}from'solid-js/web';import {splitProps,createResource}from'solid-js';var l=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()}},g=l;var u={ICONIFY_API:"api.iconify.design",REQUEST_OPTIONS:{method:"GET",credentials:"omit",redirect:"follow",cache:"force-cache",headers:{Accept:"application/json"},referrerPolicy:"no-referrer",keepalive:true},CACHE:{strategy:"unlimited"},SANITIZE:false};var n=u;function P(e){n={...n,...e},d();}var c;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 g(i,e)}function d(){let e=n.CACHE;typeof e=="object"&&e.strategy==="no-cache"||(c?c=new Map([...c].map(([t,i])=>[t,w(i)])):c=new Map);}var C=(e,t)=>c?.get(e)?.get(t);function y(e,t,i){return c?.get(e)||c?.set(e,w()),c?.get(e)?.set(t,i)}var O=(e,t)=>c?.get(e)?.delete(t);var E=e=>e.replace(/&lt;/g,"<").replace(/&gt;/g,">").trim(),m;function v(){return m||(m=import('xss').then(({default:e})=>t=>e(t,n.SANITIZE_OPTIONS))),m}async function R(e){let t=await v();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 U({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,o;Array.isArray(r)?o=N(r[i%r.length]):o=N(r);let p=`https://${o}/`,h=new URL(`${e}.json?icons=${t.toString()}`,p);return h.searchParams.sort(),h}var S=e=>new Promise(t=>setTimeout(t,e));function b(e,t=3,i=100){return e().catch(r=>{if(t<=0)throw r;return S(i).then(()=>b(e,t-1,i*2))})}function A(e,t=0){let[i,r]=e.split(":"),o=C(i,r);if(o)return o;let p=U({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 s=await f.json(),a=Object.values(s.icons).pop()?.body;if(!a||a.length===0)throw Error("[solid-iconify] ERROR - Invalid SVG (empty / undefined)");n.SANITIZE&&(a=await R(a));let I={vBox:[s.left??0,s.top??0,s.width??24,s.height??24],body:a};return y(i,r,Promise.resolve(I)),I})).catch(async f=>{let s=n.ICONIFY_API,a=t+1;if(Array.isArray(s)&&a<s.length)return A(e,a);throw O(i,r),f});return y(i,r,h),h}var T=A;var F=template("<svg xmlns=http://www.w3.org/2000/svg>");function K(e){let[t,i]=splitProps(e,["icon"]),[r]=createResource(()=>t.icon,()=>T(t.icon));return (()=>{var o=F();return spread(o,mergeProps({get height(){return r()?.vBox[3]??24},get innerHTML(){return r()?.body},get viewBox(){return r()?.vBox.join(" ")??"0 0 24 24"},get width(){return r()?.vBox[2]??24}},i),true,false),o})()} export{P as configureIconify,K as default};