UNPKG

fluxel

Version:

An ultra-lightweight, high-performance library for efficient DOM building and dynamic web UIs

13 lines (12 loc) 7.85 kB
/** * fluxel v0.6.3 * * Copyright (c) 2025 mtripg6666tdr * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. * * @see https://github.com/mtripg6666tdr/fluxel * @license MIT */ "use strict";var __Fluxel_esm=(()=>{var M=Object.defineProperty;var q=Object.getOwnPropertyDescriptor;var W=Object.getOwnPropertyNames;var B=Object.prototype.hasOwnProperty;var U=(e,n)=>{for(var s in n)M(e,s,{get:n[s],enumerable:!0})},V=(e,n,s,r)=>{if(n&&typeof n=="object"||typeof n=="function")for(let a of W(n))!B.call(e,a)&&a!==s&&M(e,a,{get:()=>n[a],enumerable:!(r=q(n,a))||r.enumerable});return e};var G=e=>V(M({},"__esModule",{value:!0}),e);var Z={};U(Z,{default:()=>Y});Object.defineProperty(window,"Fluxel",{get:()=>__Fluxel_esm.default,configurable:!0});var R=new WeakMap;function m(e){var n;(n=R.get(e))===null||n===void 0||n.forEach(s=>s()),R.delete(e),e.childNodes.forEach(s=>m(s))}function _(e,n,s){return n in e?Object.defineProperty(e,n,{value:s,enumerable:!0,configurable:!0,writable:!0}):e[n]=s,e}var h=class k{get value(){return this._accessor.get()}set value(n){this._accessor.set(n)}derive(n){let s={get:()=>n(this.value),set:r=>{throw new TypeError("Cannot set value of derived dependency")}};return new k(s,this.addDependency)}constructor(n,s){_(this,"_accessor",void 0),_(this,"addDependency",void 0),this._accessor=n,this.addDependency=s}};var J=["object","a","abbr","address","area","article","aside","audio","b","base","bdi","bdo","blockquote","body","br","button","canvas","caption","cite","code","col","colgroup","data","datalist","dd","del","details","dfn","dialog","div","dl","dt","em","embed","fieldset","figcaption","figure","footer","form","h1","h2","h3","h4","h5","h6","head","header","hgroup","hr","html","i","iframe","img","input","ins","kbd","label","legend","li","link","main","map","mark","menu","meta","meter","nav","noscript","ol","optgroup","option","output","p","picture","pre","progress","q","rp","rt","ruby","s","samp","script","search","section","select","slot","small","source","span","strong","style","sub","summary","sup","table","tbody","td","template","textarea","tfoot","th","thead","time","title","tr","track","u","ul","var","video","wbr"],z=J;function D(){return typeof window<"u"&&"fluxelH"in window?window.fluxelH:null}function y(e,n){if(typeof e!="string")throw new TypeError("Expected string for tagName, got ".concat(typeof e));let s=D(),r=s?s.getElementByEid("".concat(s.count++)):document.createElement(e);return F(r,n)}function F(e,n){let s=!!D(),r=null,a=null,l=null,w=!1,d=null;if(n)if(typeof n=="string"||Array.isArray(n)||n instanceof Node||n instanceof h){a=n;let t=x(n);l=t.children,w=t.variableChildrenLength,d=t.reactiveIndex}else{if(r=Object.assign(Object.create(null),n),"textContent"in r)throw new TypeError("textContent is not allowed");if("children"in r&&r.children!==null&&r.children!==void 0){a=r.children;let t=x(r.children);l=t.children,w=t.variableChildrenLength,d=t.reactiveIndex,delete r.children}}r=r||{},l=l||null;let g=new Map;for(let t in r)if(t.startsWith("on")&&r[t]){let i=t.slice(2).toLowerCase(),p=g.get(i)||[];g.set(i,p);let o=r[t];if(Array.isArray(o))o.forEach(u=>{if(typeof u=="function")p.push(u);else throw new TypeError("Expected function for event handler ".concat(t,", got ").concat(typeof u))});else if(typeof o=="function")p.push(o);else throw new TypeError("Expected function for event handler ".concat(t,", got ").concat(typeof o));delete r[t]}let f=Object.assign(Object.create(null),"style"in r?r.style:null);if(f&&typeof f=="object"&&!Array.isArray(f)){for(let t in f)if(f[t]instanceof h){let i=f[t];i.addDependency(e,()=>{t.startsWith("--")?e.style.setProperty(t,i.value):e.style[t]=i.value}),f[t]=i.value}}delete r.style;let C=new Set,L=t=>{if(!t)return;let i=t.trim();i&&C.add(i)};if(e.classList.forEach(t=>L(t)),r.classList){let t=o=>{if(typeof o=="string")L(o);else if("forEach"in o)o.forEach(u=>L(u));else if(o instanceof h){let u=o;u.addDependency(e,()=>{e.className="",C.clear(),t(u.value),C.forEach(E=>e.classList.add(E))}),t(u.value)}else throw new TypeError("Expected string or iterable for 'classList', got ".concat(typeof r.classList))},i=r.classList,p=Array.isArray(i)&&i.some(o=>o instanceof h)?new h({get:()=>i.map(o=>o instanceof h?o.value:o),set:()=>{throw new TypeError("Cannot set value of classList directly")}},(o,u)=>{i.forEach(E=>E instanceof h&&E.addDependency(o,u))}):r.classList;t(p),delete r.classList}"className"in r&&delete r.className;let b=r.dataset||null;if(b&&typeof b=="object"&&!Array.isArray(b)){for(let t in b)if(b[t]instanceof h){let i=b[t];i.addDependency(e,()=>{e.dataset[t]=i.value}),b[t]=i.value}}delete r.dataset;for(let t in r)if(r[t]instanceof h){let i=r[t];i.addDependency(e,()=>{e[t]=i.value}),s||(e[t]=i.value),delete r[t]}if(s||(Object.assign(e,r),e.style&&f&&(Object.assign(e.style,f),Object.keys(f).forEach(t=>{t.startsWith("--")&&e.style.setProperty(t,f[t])})),e.dataset&&b&&Object.assign(e.dataset,b),C.size>0&&C.forEach(t=>e.classList.add(t))),w&&a)a.addDependency(e,()=>{let t=X(e.childNodes),i=x(a).children,p=H(t),o=H(i);if(t.length===i.length&&t.every((c,v)=>c.isSameNode(i[v])))return;let u=t.reduce((c,v,j)=>{var A;let O=(A=o.get(v))!==null&&A!==void 0?A:-1;if(O<0)return c;let N=new Set([j]),$=j;for(let S=O+1;S<i.length;S++){var T;let I=(T=p.get(i[S]))!==null&&T!==void 0?T:-1;I<$||(N.add(I),$=I)}return N.size>c.size?N:c},new Set),E=new Set;if(u.size!==t.length){for(let c=t.length-1;c>=0;c--)if(!u.has(c)){let v=t[c];e.removeChild(v),E.add(v)}}e.childNodes.length!==i.length&&(i.forEach((c,v)=>{E.delete(c),e.childNodes[v]?e.childNodes[v]!==c&&e.insertBefore(c,e.childNodes[v]):e.appendChild(c)}),E.forEach(c=>m(c)))});else if(d&&d.length>0&&a)for(let t of d){let i=a instanceof h?a:a[t];i instanceof h&&(i.addDependency(e,()=>{let p=x(i.value).children[0],o=e.childNodes[t];o!==p&&(e.replaceChild(p,o),m(o))}),l[t]=i.value)}return l&&(s?l.forEach((t,i)=>{t instanceof Text&&e.replaceChild(t,e.childNodes[i])}):e.append(...l)),g.forEach((t,i)=>{t.forEach(p=>{e.addEventListener(i,p)})}),e}function x(e){let n=!1,s=[],r=(a,l=0,w=-1)=>{if(l>1)throw new TypeError("Children cannot be nested more than one level deep");if(typeof a=="string")return[document.createTextNode(a)];if(Array.isArray(a)){a=a.flat(1/0);let d=a.length;return a.flatMap((f,C)=>r(f,l+(d===1?0:1),C))}else{if(a instanceof Node)return[a];if(a instanceof h){let d=a.value;if(Array.isArray(d))return l===0&&(n=!0),d.flat(1/0).flatMap((g,f)=>r(g,l+1,f));if(d instanceof Node)return l===0&&(n=!0),s.push(w),[d];{let g=document.createTextNode(d);return a.addDependency(g,()=>{g.textContent=String(a.value)}),[g]}}else return[]}};return{children:r(e),variableChildrenLength:n,reactiveIndex:s}}y.fragment=function(e,n){let s=x(e),r=s.children,a=s.variableChildrenLength,l=s.reactiveIndex,w=r;if(w.forEach(d=>{d instanceof HTMLElement&&F(d,n)}),a)return e.derive(()=>r);if(l.length>0)for(let d of l)w[d]=e[d].derive(()=>r[d]);return w};function K(){let e=document.querySelector("style[data-fluxel]");if(e)return e;let n=document.createElement("style");return n.dataset.fluxel="true",document.head.appendChild(n),n}y.forwardStyle=function(e){D()||y.forwardStyleCache.includes(e)||(y.forwardStyleCache.push(e),typeof window<"u"&&(K().textContent+=e))};y.forwardStyleCache=[];function Q(){return"".concat(Date.now().toString(36)).concat(Math.random().toString(36).substring(2))}function X(e){if(typeof Array.from=="function")return Array.from(e);let n=[];for(let s=0;s<e.length;s++)n.push(e[s]);return n}function H(e){let n=new Map;return e.forEach((s,r)=>{n.set(s,r)}),n}y.useUniqueString=function(e){let n=Q();return e(n)};y.createComponent=function(e){return(n={})=>e(n)};y.createElement=y;var P=y;z.forEach(e=>{P[e]=n=>y(e,n)});var Y=P;return G(Z);})();