UNPKG

perlite

Version:

[![perlite logo](/docs/logo.svg)]()

3 lines (2 loc) 5.98 kB
import e from"hyperactiv";import{render as t,nothing as r,directive as n,EventPart as o}from"lit-html";export*from"lit-html";import{repeat as a}from"lit-html/directives/repeat";export*from"lit-html/directives/repeat";export*from"lit-html/directives/cache";export*from"lit-html/directives/until";export*from"lit-html/directives/live";export*from"lit-html/directives/guard";export*from"lit-html/directives/class-map";export*from"lit-html/directives/style-map";export*from"lit-html/directives/if-defined";export*from"lit-html/directives/async-append";export*from"lit-html/directives/async-replace";export*from"lit-html/directives/template-content";export*from"lit-html/directives/unsafe-html";export*from"lit-html/directives/unsafe-svg";const i=e=>t(r,e),s=(...e)=>{},c=(e=s)=>new Promise((e=>setTimeout(e))).then(e),l=(e,t)=>{const r=new Map;return(...n)=>{let o;if("function"==typeof t){const a=t.apply(e,n);!1===a?(o=JSON.stringify(n),r.delete(o)):!0!==a&&(o=a)}if(void 0===o&&(o=JSON.stringify(n)),r.has(o))return r.get(o);const a=e.apply(e,n);return r.set(o,a),a}};function d(e){if("true"===e||"false"===e)return"true"===e;if("null"===e)return null;if("undefined"!==e){if(""!==e&&!isNaN(Number(e)))return Number(e);try{return JSON.parse(e)}catch(e){}return e}}function u(e,t=!1){const r=e.replace(/-([a-z])/g,((e,t)=>t.toUpperCase()));return t?r.replace(/^\w/,(e=>e.toUpperCase())):r}function f(e){return e.replace(/[A-Z]/g,"-$&").toLowerCase()}const p=(e,t,r=(e=>e))=>a(e,r,t),m=n((e=>t=>e(t.element))),h=new WeakMap,v=n(((e,...t)=>{const r=n=>{const o=n.element;if(h.has(n)){const[o,a]=h.get(n);a!==e?(o.destroy(),h.delete(n),r(n)):o.update(...t)}else{const r=e(o,...t);h.set(n,[r,e])}};return r})),b=n((e=>t=>{if(!(t instanceof o))throw new Error('"bind" directive can only be used in event listeners');const r=t.element,n=r instanceof HTMLInputElement,a=r instanceof HTMLSelectElement,i=r instanceof HTMLTextAreaElement,s=r instanceof HTMLButtonElement;if(!(n||a||i||s))throw new Error('"bind" directive can only be applied to input/select/textarea/button elements.');t.setValue((function(...t){let o=r.defaultValue;if(a&&r.options.length>0){const e=r.selectedIndex>=0?r.selectedIndex:0;o=r.options[e].value}else if(n)switch(r.type){case"number":case"range":o=r.valueAsNumber;break;case"checkbox":case"radio":o=!!r.checked;break;case"time":case"date":case"datetime":case"datetime-local":o=r.valueAsDate;break;default:o=r.value}else o=r.value;e.call(this,o,...t)}))})),y=(e,...t)=>function(...r){e.call(this,...r,...t)},E=n((e=>t=>{if(!(t instanceof o))throw new Error('"capture" directive can only be used in event listeners');t.setValue("object"==typeof e?{...e,capture:!0}:{handleEvent:e,capture:!0})})),w=n((e=>t=>{if(!(t instanceof o))throw new Error('"once" directive can only be used in event listeners');t.setValue("object"==typeof e?{...e,once:!0}:{handleEvent:e,once:!0})})),g=n((e=>t=>{if(!(t instanceof o))throw new Error('"passive" directive can only be used in event listeners');t.setValue("object"==typeof e?{...e,passive:!0}:{handleEvent:e,passive:!0})})),x=n((e=>t=>{if(!(t instanceof o))throw new Error('"prevent" directive can only be used in event listeners');const{handleEvent:r,...n}=e;t.setValue({handleEvent:function(t){t.preventDefault(),(r||e).call(this,t)},...n})})),N=n(((e,t=!1)=>r=>{if(!(r instanceof o))throw new Error('"stop" directive can only be used in event listeners');const{handleEvent:n,...a}=e;r.setValue({handleEvent:function(r){t?r.stopImmediatePropagation():r.stopPropagation(),(n||e).call(this,r)},...a})})),V=n((e=>t=>{if(!(t instanceof o))throw new Error('"self" directive can only be used in event listeners');const{handleEvent:r,...n}=e;t.setValue({handleEvent:function(t){t.target===t.currentTarget&&(r||e).call(this,t)},...n})})),{observe:A,computed:L,dispose:M}=e,O=({render:e=(()=>r),state:n={},target:o=document.body,...a},...c)=>{const l="function"==typeof n?n(...c):n;Object.entries(o.dataset).forEach((([e,t])=>{e in l&&(l[e]=d(t))}));const p=A(l,{batch:!0,deep:!0,bind:!0,...a}),m=(e,t,{bubbles:r=!1,cancelable:n=!0}={})=>{o.dispatchEvent(new CustomEvent(e,{detail:t,bubbles:r,cancelable:n}))};let h=!1;const v=()=>{t(e(p,m,...c),o),h||(m("mount",l),h=!0),m("update",l)},b=L((({computeAsync:e})=>h&&!document.contains(o)?g():(m("state",l),Promise.resolve().then((()=>e(v))).catch((e=>m("error",e)))))),y=new Set,E=new Set,w=new MutationObserver((e=>{e.forEach((e=>{if("attributes"!==e.type)return;const t=e.target,r=u(e.attributeName.replace("data-",""));if(!(r in p))return;const n=t.getAttribute(e.attributeName);if(n!==e.oldValue){const e=d(n);p[r]!==e&&(p[r]=e)}}))}));w.observe(o,{attributeFilter:Object.entries(l).reduce(((e,[t,r])=>("function"!=typeof r&&e.push("data-"+f(t)),e)),[]),attributeOldValue:!0,characterData:!1,childList:!1,subtree:!1});const g=(e=s)=>{w.disconnect(),M(b),E.forEach((e=>e())),E.clear(),m("destroy",l),y.forEach((e=>e())),y.clear(),i(o),e(l)};return{on:(e,t,r)=>{o.addEventListener(e,t,r);const n=()=>(o.removeEventListener(e,t,r),y.delete(n));return y.add(n),n},ctx:e=>e(...c),model:l,state:p,effect:(e,t)=>{const r=L(e,t),n=()=>(M(r),E.delete(n));return E.add(n),n},target:o,destroy:g,render:v}},T=({target:e,...t},...r)=>{e.length||(e=[e]);const n=Array.prototype.map.call(e,(e=>O({...t,target:e},...r)));return{...n,effect:(e,t)=>{const r=n.map((r=>r.effect(e(r.state),t)));return()=>r.forEach((e=>e()))},on:(...e)=>{const t=n.map((t=>t.on(...e)));return()=>t.forEach((e=>e()))},destroy:e=>n.forEach((t=>t.destroy(e))),render:()=>n.forEach((e=>e.render())),state:e=>{n.forEach((t=>e(t.state)))},ctx:e=>e(...r),forEach:Array.prototype.forEach.bind(n),target:e}};export{O as $,T as $$,d as attrToVal,b as bind,y as call,u as camelCase,E as capture,L as computed,v as decorator,M as dispose,p as each,f as kebabCase,l as memo,s as noop,A as observe,w as once,g as passive,x as prevent,m as ref,V as self,N as stop,c as tick,i as unrender}; //# sourceMappingURL=index.min.mjs.map