fluxel
Version:
An ultra-lightweight, high-performance library for efficient DOM building and dynamic web UIs
13 lines (12 loc) • 14.7 kB
JavaScript
/**
* fluxel-jsx-reactive-router 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 FluxelJSXRouter=(()=>{var B=Object.defineProperty;var ue=Object.getOwnPropertyDescriptor;var de=Object.getOwnPropertyNames;var fe=Object.prototype.hasOwnProperty;var he=(e,t)=>{for(var r in t)B(e,r,{get:t[r],enumerable:!0})},pe=(e,t,r,o)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of de(t))!fe.call(e,s)&&s!==r&&B(e,s,{get:()=>t[s],enumerable:!(o=ue(t,s))||o.enumerable});return e};var me=e=>pe(B({},"__esModule",{value:!0}),e);var Ne={};he(Ne,{Fragment:()=>U,Link:()=>le,createRouter:()=>se,globalNavigate:()=>N,jsx:()=>Oe,jsxDEV:()=>Oe,jsxs:()=>Oe,useRouter:()=>ie});var j=new WeakMap;function A(e){var t;(t=j.get(e))===null||t===void 0||t.forEach(r=>r()),j.delete(e),e.childNodes.forEach(r=>A(r))}function Y(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}var w=class Z{get value(){return this._accessor.get()}set value(t){this._accessor.set(t)}derive(t){let r={get:()=>t(this.value),set:o=>{throw new TypeError("Cannot set value of derived dependency")}};return new Z(r,this.addDependency)}constructor(t,r){Y(this,"_accessor",void 0),Y(this,"addDependency",void 0),this._accessor=t,this.addDependency=r}};var ve=["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"],ee=ve;function H(){return typeof window<"u"&&"fluxelH"in window?window.fluxelH:null}function C(e,t){if(typeof e!="string")throw new TypeError("Expected string for tagName, got ".concat(typeof e));let r=H(),o=r?r.getElementByEid("".concat(r.count++)):document.createElement(e);return F(o,t)}function F(e,t){let r=!!H(),o=null,s=null,l=null,m=!1,i=null;if(t)if(typeof t=="string"||Array.isArray(t)||t instanceof Node||t instanceof w){s=t;let n=D(t);l=n.children,m=n.variableChildrenLength,i=n.reactiveIndex}else{if(o=Object.assign(Object.create(null),t),"textContent"in o)throw new TypeError("textContent is not allowed");if("children"in o&&o.children!==null&&o.children!==void 0){s=o.children;let n=D(o.children);l=n.children,m=n.variableChildrenLength,i=n.reactiveIndex,delete o.children}}o=o||{},l=l||null;let c=new Map;for(let n in o)if(n.startsWith("on")&&o[n]){let a=n.slice(2).toLowerCase(),f=c.get(a)||[];c.set(a,f);let d=o[n];if(Array.isArray(d))d.forEach(h=>{if(typeof h=="function")f.push(h);else throw new TypeError("Expected function for event handler ".concat(n,", got ").concat(typeof h))});else if(typeof d=="function")f.push(d);else throw new TypeError("Expected function for event handler ".concat(n,", got ").concat(typeof d));delete o[n]}let u=Object.assign(Object.create(null),"style"in o?o.style:null);if(u&&typeof u=="object"&&!Array.isArray(u)){for(let n in u)if(u[n]instanceof w){let a=u[n];a.addDependency(e,()=>{n.startsWith("--")?e.style.setProperty(n,a.value):e.style[n]=a.value}),u[n]=a.value}}delete o.style;let v=new Set,x=n=>{if(!n)return;let a=n.trim();a&&v.add(a)};if(e.classList.forEach(n=>x(n)),o.classList){let n=d=>{if(typeof d=="string")x(d);else if("forEach"in d)d.forEach(h=>x(h));else if(d instanceof w){let h=d;h.addDependency(e,()=>{e.className="",v.clear(),n(h.value),v.forEach(y=>e.classList.add(y))}),n(h.value)}else throw new TypeError("Expected string or iterable for 'classList', got ".concat(typeof o.classList))},a=o.classList,f=Array.isArray(a)&&a.some(d=>d instanceof w)?new w({get:()=>a.map(d=>d instanceof w?d.value:d),set:()=>{throw new TypeError("Cannot set value of classList directly")}},(d,h)=>{a.forEach(y=>y instanceof w&&y.addDependency(d,h))}):o.classList;n(f),delete o.classList}"className"in o&&delete o.className;let b=o.dataset||null;if(b&&typeof b=="object"&&!Array.isArray(b)){for(let n in b)if(b[n]instanceof w){let a=b[n];a.addDependency(e,()=>{e.dataset[n]=a.value}),b[n]=a.value}}delete o.dataset;for(let n in o)if(o[n]instanceof w){let a=o[n];a.addDependency(e,()=>{e[n]=a.value}),r||(e[n]=a.value),delete o[n]}if(r||(Object.assign(e,o),e.style&&u&&(Object.assign(e.style,u),Object.keys(u).forEach(n=>{n.startsWith("--")&&e.style.setProperty(n,u[n])})),e.dataset&&b&&Object.assign(e.dataset,b),v.size>0&&v.forEach(n=>e.classList.add(n))),m&&s&&s instanceof w)s.addDependency(e,()=>{let n=we(e.childNodes),a=D(s).children,f=te(n),d=te(a);if(n.length===a.length&&n.every((p,g)=>p.isSameNode(a[g])))return;let h=n.reduce((p,g,O)=>{var R;let T=(R=d.get(g))!==null&&R!==void 0?R:-1;if(T<0)return p;let E=new Set([O]),S=O;for(let V=T+1;V<a.length;V++){var q;let z=(q=f.get(a[V]))!==null&&q!==void 0?q:-1;z<S||(E.add(z),S=z)}return E.size>p.size?E:p},new Set),y=new Set;if(h.size!==n.length){for(let p=n.length-1;p>=0;p--)if(!h.has(p)){let g=n[p];e.removeChild(g),y.add(g)}}e.childNodes.length!==a.length&&(a.forEach((p,g)=>{y.delete(p),e.childNodes[g]?e.childNodes[g]!==p&&e.insertBefore(p,e.childNodes[g]):e.appendChild(p)}),y.forEach(p=>A(p)))});else if(i&&i.length>0&&s)for(let n of i){let a=s instanceof w?s:s[n];a instanceof w&&(a.addDependency(e,()=>{let f=D(a.value).children[0],d=e.childNodes[n];d!==f&&(e.replaceChild(f,d),A(d))}),l[n]=a.value)}return l&&(r?l.forEach((n,a)=>{n instanceof Text&&(e.childNodes[a]&&e.childNodes[a]instanceof Text?e.replaceChild(n,e.childNodes[a]):e.insertBefore(n,e.childNodes[a]))}):e.append(...l)),c.forEach((n,a)=>{n.forEach(f=>{e.addEventListener(a,f)})}),e}function D(e){let t=!1,r=[],o=(s,l=0,m=-1)=>{if(l>1)throw new TypeError("Children cannot be nested more than one level deep");if(typeof s=="string")return[document.createTextNode(s)];if(Array.isArray(s)){s=s.flat(1/0);let i=s.length;return s.flatMap((u,v)=>o(u,l+(i===1?0:1),v))}else{if(s instanceof Node)return[s];if(s instanceof w){let i=s.value;if(Array.isArray(i))return l===0&&(t=!0),i.flat(1/0).flatMap((c,u)=>o(c,l+1,u));if(i instanceof Node)return l===0&&(t=!0),r.push(m),[i];{let c=document.createTextNode(i);return s.addDependency(c,()=>{c.textContent=String(s.value)}),[c]}}else return[]}};return{children:o(e),variableChildrenLength:t,reactiveIndex:r}}C.fragment=function(e,t){let r=D(e),o=r.children,s=r.variableChildrenLength,l=r.reactiveIndex;if(s&&e instanceof w)return e.derive(m=>{let i=D(m).children;return i.forEach(c=>F(c,t)),i});{let m=o;if(m.forEach(i=>{i instanceof HTMLElement&&F(i,t)}),l.length>0)for(let i of l)m[i]=e[i].derive(c=>F(c,t));return m}};function ye(){let e=document.querySelector("style[data-fluxel]");if(e)return e;let t=document.createElement("style");return t.dataset.fluxel="true",document.head.appendChild(t),t}C.forwardStyle=function(e){H()||C.forwardStyleCache.includes(e)||(C.forwardStyleCache.push(e),typeof window<"u"&&(ye().textContent+=e))};C.forwardStyleCache=[];function ge(){return"".concat(Date.now().toString(36)).concat(Math.random().toString(36).substring(2))}function we(e){if(typeof Array.from=="function")return Array.from(e);let t=[];for(let r=0;r<e.length;r++)t.push(e[r]);return t}function te(e){let t=new Map;return e.forEach((r,o)=>{t.set(r,o)}),t}C.useUniqueString=function(e){let t=ge();return e(t)};C.createComponent=function(e){return(t={})=>e(t)};C.createElement=C;var re=C;ee.forEach(e=>{re[e]=t=>C(e,t)});var P=re;function Ee(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function xe(e){for(var t=1;t<arguments.length;t++){var r=arguments[t]!=null?arguments[t]:{},o=Object.keys(r);typeof Object.getOwnPropertySymbols=="function"&&(o=o.concat(Object.getOwnPropertySymbols(r).filter(function(s){return Object.getOwnPropertyDescriptor(r,s).enumerable}))),o.forEach(function(s){Ee(e,s,r[s])})}return e}function be(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var o=Object.getOwnPropertySymbols(e);t&&(o=o.filter(function(s){return Object.getOwnPropertyDescriptor(e,s).enumerable})),r.push.apply(r,o)}return r}function Ce(e,t){return t=t!=null?t:{},Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):be(Object(t)).forEach(function(r){Object.defineProperty(e,r,Object.getOwnPropertyDescriptor(t,r))}),e}var I=P,W=["render","use","useWithMemo","listenTarget"];I.reactive=function(e,t){let r=Object.create(null),o=Ce(xe({},e),{render:(l,m=!1)=>{if(!m&&l&&o.listenTarget.dispatchEvent(new CustomEvent(l,{detail:{newValue:o[l]}})),l){var i;(i=r[l])===null||i===void 0||i.listeners.forEach(c=>c.call(null))}else for(let c in r)r[c].listeners.forEach(u=>u.call(null))},use:(l,m)=>{let i=-1,c=-1,u=[],v=new Map,x=new Map,b=(a,f,d=!1)=>{if(d){let h=JSON.stringify(f);if(x.has(h)){let p=x.get(h);return v.set(h,p),p}let y=a();return v.set(h,y),y}if(u[i]){if(u[i].deps.every((y,p)=>y===f[p]))return u[i++].memoValue;u[i].deps=f;let h=u[i].memoValue=a();return i++,h}else return u.push({deps:f,memoValue:a()}),u[i++].memoValue},n=m?a=>{i===-1&&c!==-1&&(c=i),i=0,v=new Map;let f=m(a,b);if(c!==-1&&c!==i)throw new TypeError("Memoization count mismatch");return x=v,f}:void 0;if(Array.isArray(l)){let a=l.map(f=>[f,o.use(f)]);return new w({get:()=>Object.fromEntries(a.map(f=>[f[0],f[1].value])),set:f=>{throw new TypeError("Invalid")}},(f,d)=>{let h=ne(d);a.forEach(y=>y[1].addDependency(f,h))}).derive(n)}else{let a;return r[l]?a=r[l].dep:o[l]instanceof w?a=o[l]:(a=new w({get:()=>o[l],set:f=>{o[l]=f}},function(d,h){if(!r[l].listeners.includes(h)){r[l].listeners.push(h);let y=()=>{r[l].listeners=r[l].listeners.filter(p=>p!==h)};j.has(d)?j.get(d).add(y):j.set(d,new Set([y]))}}),r[l]={dep:a,listeners:[]}),n?a.derive(n):a}},useWithMemo:l=>{let m=null,i=o.use(l,(c,u)=>(m=u,c));return i.value,[i,m]},listenTarget:new EventTarget}),s=new Proxy(o,{set(l,m,i){if(W.includes(m))throw new TypeError("Cannot set the specified property: ".concat(String(m)));let c=l[m];return c===i||(l[m]=i,l.listenTarget.dispatchEvent(new CustomEvent(m,{detail:{oldValue:c,newValue:i}})),l.render(m)),!0}});return t(s)};I.schedule=function(e){typeof window<"u"&&setTimeout(e,0)};I.mergeDependencies=function(...e){return new w({get:()=>e.map(t=>t.value),set:t=>{throw new TypeError("Invalid operation")}},(t,r)=>{let o=ne(r);e.forEach(s=>s.addDependency(t,o))})};function ne(e){let t=!1;return()=>{t||(t=!0,window.setTimeout(()=>{t=!1,e()},0))}}var L=I;var Le=P.createComponent(e=>{let t=Object.assign({},e),r=t.children;return delete t.children,P.fragment(r,t)}),U=Le;var J=L;J.createStatefulComponent=function(e,t){return(r={})=>J.reactive(typeof t=="function"?t(r):t||{},o=>e(r,o))};var oe=J;var $=new EventTarget;typeof window<"u"&&(window.addEventListener("popstate",e=>{$.dispatchEvent(new CustomEvent("popstate",{detail:e}))}),window.addEventListener("hashchange",e=>{$.dispatchEvent(new CustomEvent("popstate",{detail:e}))}));var G=()=>L.div({dataset:{fluxelRouterFallback:"1"}}),M=[],ae=()=>{};function K(e,t={}){let r=t.basePath||"",o=t.mode||"history",s=[];Object.keys(e).forEach(u=>{let v=new RegExp("^".concat(u.replace(/\/\*?$/,"").replace(RegExp("(?<cap>:[^\\s/]+)\\?\\/","g"),"($<cap>/)?").replace(RegExp(":(?<name>[^\\s\\?/]+)","g"),"(?<$<name>>[^/]+)")).concat(u.endsWith("/*")?"(/.*)?":"/?","$"));s.push([v,u])});let l=u=>{let v=null;return L.reactive(u,x=>{v=x}),v},m=!1,i=null,c={basePath:r,pathname:"/",route:null,params:{},query:null,get mode(){return o},Provider(u){return L.reactive({children:null},v=>{let x=(n,a)=>{if(m=!0,i){Promise.all(i).then(()=>{(!n||c.route===n)&&(v.children=a),i=null});return}else v.children=a},b=()=>{let n=new URL(location.href),a=o==="history"?n.pathname.replace(r,""):n.hash.replace(/^#/,"").replace(r,""),f=new URLSearchParams(n.search);c.pathname=a;let d=s.find(p=>p[0].test(a));if(d){let p=d[0],g=d[1],O=p.exec(a).groups||{};if(c.route===g&&JSON.stringify(c.params)===JSON.stringify(O)){Object.keys(c.query).filter(E=>!f.has(E)&&!W.includes(E)).forEach(E=>{c.query[E]=void 0}),f.forEach((E,S)=>{c.query[S]=E});return}c.route=g,c.params=O;let R={};f.forEach((E,S)=>{R[S]=E}),c.query=l(R),M.push(c);let T=e[g]();if(M.pop(),T instanceof Promise){var h;T.then(E=>{e[g]=E,c.route===g&&(M.push(c),x(g,E()),M.pop())}),x(g,((h=t.fallback)===null||h===void 0?void 0:h.call(t))||G())}else x(g,T)}else{var y;c.route=null,c.params={};let p={};f.forEach((g,O)=>{p[O]=g}),c.query=l(p),x(null,((y=t.fallback)===null||y===void 0?void 0:y.call(t))||G())}};return $.addEventListener("popstate",()=>b()),b(),L.div({dataset:{fluxelRouter:"1"},classList:(u==null?void 0:u.classList)||[],children:v.use("children",n=>{var a;return n||((a=t.fallback)===null||a===void 0?void 0:a.call(t))||G()})})})},navigate(u,v={}){c.mode==="hash"&&(u="#".concat(c.basePath).concat(u)),N(u,v)},deferUpdate(u){m?(i=i||[],i.push(u().catch(ae))):u().catch(ae)}};return c}var _=e=>{let t=(e==null?void 0:e.optional)||!1,r=M[M.length-1];if(!r&&!t)throw new Error("No router instance found. Make sure the component is rendered within a Router Provider.");return r||null};function N(e,t={}){t.replace||!1?window.history.replaceState({},"",e):window.history.pushState({},"",e),$.dispatchEvent(new CustomEvent("popstate"))}var Q=e=>{if(!e||!e.href)throw new Error("Link component requires a 'href' property.");let t=e.replace||!1,r=Object.assign({},e);delete r.replace;let o=r.href||"#",s=r.onclick?Array.isArray(r.onclick)?r.onclick:[r.onclick]:[];return s.push(l=>{l.preventDefault(),N(o,{replace:t||!1})}),r.onclick=s,L.a(r)};var k=oe,se=K,ie=_,le=Q;k.createRouter=se;k.useRouter=ie;k.globalNavigate=N;k.Link=le;var X=L;X.createStatefulComponent=function(e,t){return(r={})=>X.reactive(typeof t=="function"?t(r):t||{},o=>e(r,o))};var ce=X;function Oe(e,t,r){if(t!=null&&t.className){var o,s;(s=(o=t).classList)!==null&&s!==void 0||(o.classList=[]),t.classList.push(t.className.split(" ").filter(Boolean)),delete t.className}return typeof e=="string"?ce.createElement(e,t):e(t)}return me(Ne);})();