flexium
Version:
A lightweight, signals-based UI framework with cross-platform renderers
3 lines • 2.59 kB
JavaScript
import {a,b as b$1,e}from'./chunk-H3V4FX7A.mjs';import {b,c}from'./chunk-Z7JNAX53.mjs';function v(){let t=()=>({pathname:window.location.pathname,search:window.location.search,hash:window.location.hash,query:y(window.location.search)}),r=b(t()),n=e=>{window.history.pushState({},"",e),r.value=t();};return window.addEventListener("popstate",()=>{r.value=t();}),[r,n]}function y(t){let r=new URLSearchParams(t),n={};return r.forEach((e,c)=>{n[c]=e;}),n}function k(t,r){let n=[],e=r.replace(/:([^/]+)/g,(o,s)=>(n.push(s),"([^/]+)")),c=new RegExp(`^${e}$`),a=t.match(c);if(!a)return {matches:false,params:{}};let i={};return a.slice(1).forEach((o,s)=>{i[n[s]]=o;}),{matches:true,params:i}}function w(t){let r=[],n=Array.isArray(t)?t:[t];for(let e of n){if(!e||!e.props)continue;let{path:c,index:a,component:i}=e.props,o=e.children,s={path:c||"",index:!!a,component:i,children:o?w(o):[]};r.push(s);}return r}function P(t,r){for(let n of t){let e=L(n,r,"");if(e)return e}return null}function L(t,r,n){let e=n;t.path&&(e=n.replace(/\/$/,"")+"/"+t.path.replace(/^\//,""));let c=t.children.length===0,a=D(e,!c),i=r.match(a);if(i){let[o,...s]=i,R=E(e,s),m={route:t,params:R,pathname:o};if(c)return o===r?[m]:null;for(let h of t.children){let p=L(h,r,e);if(p)return [m,...p]}if(o===r){let h=t.children.find(p=>p.index);return h?[m,{route:h,params:{},pathname:o}]:[m]}}return null}function D(t,r){let e=t.replace(/:([^/]+)/g,(c,a)=>("([^/]+)"));return e==="/"&&r?new RegExp("^"):new RegExp(`^${e}${r?"(?:/|$)":"$"}`)}function E(t,r){let n={},e=0;return t.replace(/:([^/]+)/g,(c,a)=>(n[a]=r[e++],"")),n}var f=a(null),g=a(0);function M(){let t=b$1(f);if(!t)throw new Error("useRouter must be used within a <Router> component");return t}function U(t){let[r,n]=v(),e$1=w(t.children),c$1=c(()=>{let o=r();return P(e$1,o.pathname)||[]}),a=c(()=>{let o=c$1();return o.length>0?o[o.length-1].params:{}}),i={location:r,params:a,navigate:n,matches:c$1};return ()=>{let o=c$1();if(o.length===0)return null;let s=o[0],R=s.route.component;return e(f.Provider,{value:i},[e(g.Provider,{value:1},[e(R,{params:s.params})])])}}function V(t){return null}function z(){let t=b$1(f),r=b$1(g);return t?()=>{let n=t.matches();if(r>=n.length)return null;let e$1=n[r],c=e$1.route.component;return e(g.Provider,{value:r+1},[e(c,{params:e$1.params})])}:null}function G(t){let r=M(),n=e=>{e.preventDefault(),r.navigate(t.to);};return e("a",{href:t.to,class:t.class,onclick:n},t.children)}
export{v as a,k as b,M as c,U as d,V as e,z as f,G as g};//# sourceMappingURL=chunk-4H6G5CMD.mjs.map
//# sourceMappingURL=chunk-4H6G5CMD.mjs.map