UNPKG

fluxel

Version:

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

13 lines (12 loc) 14.5 kB
/** * fluxel-jsx-reactive-router 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 FluxelJSXRouter=(()=>{var z=Object.defineProperty;var ue=Object.getOwnPropertyDescriptor;var de=Object.getOwnPropertyNames;var fe=Object.prototype.hasOwnProperty;var pe=(e,t)=>{for(var r in t)z(e,r,{get:t[r],enumerable:!0})},he=(e,t,r,n)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of de(t))!fe.call(e,s)&&s!==r&&z(e,s,{get:()=>t[s],enumerable:!(n=ue(t,s))||n.enumerable});return e};var me=e=>he(z({},"__esModule",{value:!0}),e);var Ne={};pe(Ne,{Fragment:()=>W,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 X(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 Y{get value(){return this._accessor.get()}set value(t){this._accessor.set(t)}derive(t){let r={get:()=>t(this.value),set:n=>{throw new TypeError("Cannot set value of derived dependency")}};return new Y(r,this.addDependency)}constructor(t,r){X(this,"_accessor",void 0),X(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"],Z=ve;function B(){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=B(),n=r?r.getElementByEid("".concat(r.count++)):document.createElement(e);return te(n,t)}function te(e,t){let r=!!B(),n=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 o=M(t);l=o.children,m=o.variableChildrenLength,i=o.reactiveIndex}else{if(n=Object.assign(Object.create(null),t),"textContent"in n)throw new TypeError("textContent is not allowed");if("children"in n&&n.children!==null&&n.children!==void 0){s=n.children;let o=M(n.children);l=o.children,m=o.variableChildrenLength,i=o.reactiveIndex,delete n.children}}n=n||{},l=l||null;let c=new Map;for(let o in n)if(o.startsWith("on")&&n[o]){let a=o.slice(2).toLowerCase(),f=c.get(a)||[];c.set(a,f);let d=n[o];if(Array.isArray(d))d.forEach(p=>{if(typeof p=="function")f.push(p);else throw new TypeError("Expected function for event handler ".concat(o,", got ").concat(typeof p))});else if(typeof d=="function")f.push(d);else throw new TypeError("Expected function for event handler ".concat(o,", got ").concat(typeof d));delete n[o]}let u=Object.assign(Object.create(null),"style"in n?n.style:null);if(u&&typeof u=="object"&&!Array.isArray(u)){for(let o in u)if(u[o]instanceof w){let a=u[o];a.addDependency(e,()=>{o.startsWith("--")?e.style.setProperty(o,a.value):e.style[o]=a.value}),u[o]=a.value}}delete n.style;let v=new Set,E=o=>{if(!o)return;let a=o.trim();a&&v.add(a)};if(e.classList.forEach(o=>E(o)),n.classList){let o=d=>{if(typeof d=="string")E(d);else if("forEach"in d)d.forEach(p=>E(p));else if(d instanceof w){let p=d;p.addDependency(e,()=>{e.className="",v.clear(),o(p.value),v.forEach(y=>e.classList.add(y))}),o(p.value)}else throw new TypeError("Expected string or iterable for 'classList', got ".concat(typeof n.classList))},a=n.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,p)=>{a.forEach(y=>y instanceof w&&y.addDependency(d,p))}):n.classList;o(f),delete n.classList}"className"in n&&delete n.className;let b=n.dataset||null;if(b&&typeof b=="object"&&!Array.isArray(b)){for(let o in b)if(b[o]instanceof w){let a=b[o];a.addDependency(e,()=>{e.dataset[o]=a.value}),b[o]=a.value}}delete n.dataset;for(let o in n)if(n[o]instanceof w){let a=n[o];a.addDependency(e,()=>{e[o]=a.value}),r||(e[o]=a.value),delete n[o]}if(r||(Object.assign(e,n),e.style&&u&&(Object.assign(e.style,u),Object.keys(u).forEach(o=>{o.startsWith("--")&&e.style.setProperty(o,u[o])})),e.dataset&&b&&Object.assign(e.dataset,b),v.size>0&&v.forEach(o=>e.classList.add(o))),m&&s)s.addDependency(e,()=>{let o=we(e.childNodes),a=M(s).children,f=ee(o),d=ee(a);if(o.length===a.length&&o.every((h,g)=>h.isSameNode(a[g])))return;let p=o.reduce((h,g,O)=>{var R;let T=(R=d.get(g))!==null&&R!==void 0?R:-1;if(T<0)return h;let x=new Set([O]),S=O;for(let q=T+1;q<a.length;q++){var k;let V=(k=f.get(a[q]))!==null&&k!==void 0?k:-1;V<S||(x.add(V),S=V)}return x.size>h.size?x:h},new Set),y=new Set;if(p.size!==o.length){for(let h=o.length-1;h>=0;h--)if(!p.has(h)){let g=o[h];e.removeChild(g),y.add(g)}}e.childNodes.length!==a.length&&(a.forEach((h,g)=>{y.delete(h),e.childNodes[g]?e.childNodes[g]!==h&&e.insertBefore(h,e.childNodes[g]):e.appendChild(h)}),y.forEach(h=>A(h)))});else if(i&&i.length>0&&s)for(let o of i){let a=s instanceof w?s:s[o];a instanceof w&&(a.addDependency(e,()=>{let f=M(a.value).children[0],d=e.childNodes[o];d!==f&&(e.replaceChild(f,d),A(d))}),l[o]=a.value)}return l&&(r?l.forEach((o,a)=>{o instanceof Text&&e.replaceChild(o,e.childNodes[a])}):e.append(...l)),c.forEach((o,a)=>{o.forEach(f=>{e.addEventListener(a,f)})}),e}function M(e){let t=!1,r=[],n=(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)=>n(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)=>n(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:n(e),variableChildrenLength:t,reactiveIndex:r}}C.fragment=function(e,t){let r=M(e),n=r.children,s=r.variableChildrenLength,l=r.reactiveIndex,m=n;if(m.forEach(i=>{i instanceof HTMLElement&&te(i,t)}),s)return e.derive(()=>n);if(l.length>0)for(let i of l)m[i]=e[i].derive(()=>n[i]);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){B()||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 ee(e){let t=new Map;return e.forEach((r,n)=>{t.set(r,n)}),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;Z.forEach(e=>{re[e]=t=>C(e,t)});var P=re;function xe(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function Ee(e){for(var t=1;t<arguments.length;t++){var r=arguments[t]!=null?arguments[t]:{},n=Object.keys(r);typeof Object.getOwnPropertySymbols=="function"&&(n=n.concat(Object.getOwnPropertySymbols(r).filter(function(s){return Object.getOwnPropertyDescriptor(r,s).enumerable}))),n.forEach(function(s){xe(e,s,r[s])})}return e}function be(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter(function(s){return Object.getOwnPropertyDescriptor(e,s).enumerable})),r.push.apply(r,n)}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 F=P,H=["render","use","useWithMemo","listenTarget"];F.reactive=function(e,t){let r=Object.create(null),n=Ce(Ee({},e),{render:(l,m=!1)=>{if(!m&&l&&n.listenTarget.dispatchEvent(new CustomEvent(l,{detail:{newValue:n[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,E=new Map,b=(a,f,d=!1)=>{if(d){let p=JSON.stringify(f);if(E.has(p)){let h=E.get(p);return v.set(p,h),h}let y=a();return v.set(p,y),y}if(u[i]){if(u[i].deps.every((y,h)=>y===f[h]))return u[i++].memoValue;u[i].deps=f;let p=u[i].memoValue=a();return i++,p}else return u.push({deps:f,memoValue:a()}),u[i++].memoValue},o=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 E=v,f}:void 0;if(Array.isArray(l)){let a=l.map(f=>[f,n.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 p=ne(d);a.forEach(y=>y[1].addDependency(f,p))}).derive(o)}else{let a;return r[l]?a=r[l].dep:n[l]instanceof w?a=n[l]:(a=new w({get:()=>n[l],set:f=>{n[l]=f}},function(d,p){if(!r[l].listeners.includes(p)){r[l].listeners.push(p);let y=()=>{r[l].listeners=r[l].listeners.filter(h=>h!==p)};j.has(d)?j.get(d).add(y):j.set(d,new Set([y]))}}),r[l]={dep:a,listeners:[]}),o?a.derive(o):a}},useWithMemo:l=>{let m=null,i=n.use(l,(c,u)=>(m=u,c));return i.value,[i,m]},listenTarget:new EventTarget}),s=new Proxy(n,{set(l,m,i){if(H.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)};F.schedule=function(e){typeof window<"u"&&setTimeout(e,0)};F.mergeDependencies=function(...e){return new w({get:()=>e.map(t=>t.value),set:t=>{throw new TypeError("Invalid operation")}},(t,r)=>{let n=ne(r);e.forEach(s=>s.addDependency(t,n))})};function ne(e){let t=!1;return()=>{t||(t=!0,window.setTimeout(()=>{t=!1,e()},0))}}var L=F;var Le=P.createComponent(e=>{let t=Object.assign({},e),r=t.children;return delete t.children,P.fragment(r,t)}),W=Le;var U=L;U.createStatefulComponent=function(e,t){return(r={})=>U.reactive(typeof t=="function"?t(r):t||{},n=>e(r,n))};var oe=U;var I=new EventTarget;typeof window<"u"&&(window.addEventListener("popstate",e=>{I.dispatchEvent(new CustomEvent("popstate",{detail:e}))}),window.addEventListener("hashchange",e=>{I.dispatchEvent(new CustomEvent("popstate",{detail:e}))}));var J=()=>L.div({dataset:{fluxelRouterFallback:"1"}}),D=[],ae=()=>{};function G(e,t={}){let r=t.basePath||"",n=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,E=>{v=E}),v},m=!1,i=null,c={basePath:r,pathname:"/",route:null,params:{},query:null,get mode(){return n},Provider(u){return L.reactive({children:null},v=>{let E=(o,a)=>{if(m=!0,i){Promise.all(i).then(()=>{(!o||c.route===o)&&(v.children=a),i=null});return}else v.children=a},b=()=>{let o=new URL(location.href),a=n==="history"?o.pathname.replace(r,""):o.hash.replace(/^#/,"").replace(r,""),f=new URLSearchParams(o.search);c.pathname=a;let d=s.find(h=>h[0].test(a));if(d){let h=d[0],g=d[1],O=h.exec(a).groups||{};if(c.route===g&&JSON.stringify(c.params)===JSON.stringify(O)){Object.keys(c.query).filter(x=>!f.has(x)&&!H.includes(x)).forEach(x=>{c.query[x]=void 0}),f.forEach((x,S)=>{c.query[S]=x});return}c.route=g,c.params=O;let R={};f.forEach((x,S)=>{R[S]=x}),c.query=l(R),D.push(c);let T=e[g]();if(D.pop(),T instanceof Promise){var p;T.then(x=>{e[g]=x,c.route===g&&(D.push(c),E(g,x()),D.pop())}),E(g,((p=t.fallback)===null||p===void 0?void 0:p.call(t))||J())}else E(g,T)}else{var y;c.route=null,c.params={};let h={};f.forEach((g,O)=>{h[O]=g}),c.query=l(h),E(null,((y=t.fallback)===null||y===void 0?void 0:y.call(t))||J())}};return I.addEventListener("popstate",()=>b()),b(),L.div({dataset:{fluxelRouter:"1"},classList:(u==null?void 0:u.classList)||[],children:v.use("children",o=>{var a;return o||((a=t.fallback)===null||a===void 0?void 0:a.call(t))||J()})})})},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 K=e=>{let t=(e==null?void 0:e.optional)||!1,r=D[D.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),I.dispatchEvent(new CustomEvent("popstate"))}var _=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 n=r.href||"#",s=r.onclick?Array.isArray(r.onclick)?r.onclick:[r.onclick]:[];return s.push(l=>{l.preventDefault(),N(n,{replace:t||!1})}),r.onclick=s,L.a(r)};var $=oe,se=G,ie=K,le=_;$.createRouter=se;$.useRouter=ie;$.globalNavigate=N;$.Link=le;var Q=L;Q.createStatefulComponent=function(e,t){return(r={})=>Q.reactive(typeof t=="function"?t(r):t||{},n=>e(r,n))};var ce=Q;function Oe(e,t,r){if(t!=null&&t.className){var n,s;(s=(n=t).classList)!==null&&s!==void 0||(n.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);})();