fluxel
Version:
An ultra-lightweight, high-performance library for efficient DOM building and dynamic web UIs
13 lines (12 loc) • 8.02 kB
JavaScript
/**
* fluxel v0.6.5
*
* Copyright (c) 2026 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
*/
;var __Fluxel_esm=(()=>{var D=Object.defineProperty;var q=Object.getOwnPropertyDescriptor;var W=Object.getOwnPropertyNames;var B=Object.prototype.hasOwnProperty;var U=(e,n)=>{for(var i in n)D(e,i,{get:n[i],enumerable:!0})},V=(e,n,i,r)=>{if(n&&typeof n=="object"||typeof n=="function")for(let a of W(n))!B.call(e,a)&&a!==i&&D(e,a,{get:()=>n[a],enumerable:!(r=q(n,a))||r.enumerable});return e};var G=e=>V(D({},"__esModule",{value:!0}),e);var Z={};U(Z,{default:()=>Y});Object.defineProperty(window,"Fluxel",{get:()=>__Fluxel_esm.default,configurable:!0});var _=new WeakMap;function L(e){var n;(n=_.get(e))===null||n===void 0||n.forEach(i=>i()),_.delete(e),e.childNodes.forEach(i=>L(i))}function k(e,n,i){return n in e?Object.defineProperty(e,n,{value:i,enumerable:!0,configurable:!0,writable:!0}):e[n]=i,e}var u=class z{get value(){return this._accessor.get()}set value(n){this._accessor.set(n)}derive(n){let i={get:()=>n(this.value),set:r=>{throw new TypeError("Cannot set value of derived dependency")}};return new z(i,this.addDependency)}constructor(n,i){k(this,"_accessor",void 0),k(this,"addDependency",void 0),this._accessor=n,this.addDependency=i}};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"],H=J;function j(){return typeof window<"u"&&"fluxelH"in window?window.fluxelH:null}function v(e,n){if(typeof e!="string")throw new TypeError("Expected string for tagName, got ".concat(typeof e));let i=j(),r=i?i.getElementByEid("".concat(i.count++)):document.createElement(e);return m(r,n)}function m(e,n){let i=!!j(),r=null,a=null,c=null,g=!1,d=null;if(n)if(typeof n=="string"||Array.isArray(n)||n instanceof Node||n instanceof u){a=n;let t=x(n);c=t.children,g=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);c=t.children,g=t.variableChildrenLength,d=t.reactiveIndex,delete r.children}}r=r||{},c=c||null;let h=new Map;for(let t in r)if(t.startsWith("on")&&r[t]){let s=t.slice(2).toLowerCase(),y=h.get(s)||[];h.set(s,y);let o=r[t];if(Array.isArray(o))o.forEach(p=>{if(typeof p=="function")y.push(p);else throw new TypeError("Expected function for event handler ".concat(t,", got ").concat(typeof p))});else if(typeof o=="function")y.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 u){let s=f[t];s.addDependency(e,()=>{t.startsWith("--")?e.style.setProperty(t,s.value):e.style[t]=s.value}),f[t]=s.value}}delete r.style;let C=new Set,N=t=>{if(!t)return;let s=t.trim();s&&C.add(s)};if(e.classList.forEach(t=>N(t)),r.classList){let t=o=>{if(typeof o=="string")N(o);else if("forEach"in o)o.forEach(p=>N(p));else if(o instanceof u){let p=o;p.addDependency(e,()=>{e.className="",C.clear(),t(p.value),C.forEach(E=>e.classList.add(E))}),t(p.value)}else throw new TypeError("Expected string or iterable for 'classList', got ".concat(typeof r.classList))},s=r.classList,y=Array.isArray(s)&&s.some(o=>o instanceof u)?new u({get:()=>s.map(o=>o instanceof u?o.value:o),set:()=>{throw new TypeError("Cannot set value of classList directly")}},(o,p)=>{s.forEach(E=>E instanceof u&&E.addDependency(o,p))}):r.classList;t(y),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 u){let s=b[t];s.addDependency(e,()=>{e.dataset[t]=s.value}),b[t]=s.value}}delete r.dataset;for(let t in r)if(r[t]instanceof u){let s=r[t];s.addDependency(e,()=>{e[t]=s.value}),i||(e[t]=s.value),delete r[t]}if(i||(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))),g&&a&&a instanceof u)a.addDependency(e,()=>{let t=X(e.childNodes),s=x(a).children,y=F(t),o=F(s);if(t.length===s.length&&t.every((l,w)=>l.isSameNode(s[w])))return;let p=t.reduce((l,w,O)=>{var A;let $=(A=o.get(w))!==null&&A!==void 0?A:-1;if($<0)return l;let T=new Set([O]),R=O;for(let I=$+1;I<s.length;I++){var S;let M=(S=y.get(s[I]))!==null&&S!==void 0?S:-1;M<R||(T.add(M),R=M)}return T.size>l.size?T:l},new Set),E=new Set;if(p.size!==t.length){for(let l=t.length-1;l>=0;l--)if(!p.has(l)){let w=t[l];e.removeChild(w),E.add(w)}}e.childNodes.length!==s.length&&(s.forEach((l,w)=>{E.delete(l),e.childNodes[w]?e.childNodes[w]!==l&&e.insertBefore(l,e.childNodes[w]):e.appendChild(l)}),E.forEach(l=>L(l)))});else if(d&&d.length>0&&a)for(let t of d){let s=a instanceof u?a:a[t];s instanceof u&&(s.addDependency(e,()=>{let y=x(s.value).children[0],o=e.childNodes[t];o!==y&&(e.replaceChild(y,o),L(o))}),c[t]=s.value)}return c&&(i?c.forEach((t,s)=>{t instanceof Text&&(e.childNodes[s]&&e.childNodes[s]instanceof Text?e.replaceChild(t,e.childNodes[s]):e.insertBefore(t,e.childNodes[s]))}):e.append(...c)),h.forEach((t,s)=>{t.forEach(y=>{e.addEventListener(s,y)})}),e}function x(e){let n=!1,i=[],r=(a,c=0,g=-1)=>{if(c>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,c+(d===1?0:1),C))}else{if(a instanceof Node)return[a];if(a instanceof u){let d=a.value;if(Array.isArray(d))return c===0&&(n=!0),d.flat(1/0).flatMap((h,f)=>r(h,c+1,f));if(d instanceof Node)return c===0&&(n=!0),i.push(g),[d];{let h=document.createTextNode(d);return a.addDependency(h,()=>{h.textContent=String(a.value)}),[h]}}else return[]}};return{children:r(e),variableChildrenLength:n,reactiveIndex:i}}v.fragment=function(e,n){let i=x(e),r=i.children,a=i.variableChildrenLength,c=i.reactiveIndex;if(a&&e instanceof u)return e.derive(g=>{let d=x(g).children;return d.forEach(h=>m(h,n)),d});{let g=r;if(g.forEach(d=>{d instanceof HTMLElement&&m(d,n)}),c.length>0)for(let d of c)g[d]=e[d].derive(h=>m(h,n));return g}};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}v.forwardStyle=function(e){j()||v.forwardStyleCache.includes(e)||(v.forwardStyleCache.push(e),typeof window<"u"&&(K().textContent+=e))};v.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 i=0;i<e.length;i++)n.push(e[i]);return n}function F(e){let n=new Map;return e.forEach((i,r)=>{n.set(i,r)}),n}v.useUniqueString=function(e){let n=Q();return e(n)};v.createComponent=function(e){return(n={})=>e(n)};v.createElement=v;var P=v;H.forEach(e=>{P[e]=n=>v(e,n)});var Y=P;return G(Z);})();