@crossfox/react-router
Version:
Lightning-fast and highly functional router for React. Effortlessly manage your application's routing with speed and efficiency
2 lines (1 loc) • 3.12 kB
JavaScript
import t,{createContext as e,createElement as r,useContext as n,useState as o,useRef as s}from"react";"function"==typeof SuppressedError&&SuppressedError;const a=Array.isArray,l=t=>"[object Object]"===Object.prototype.toString.call(t),c=/:([a-zA-Z0-9-]+)/g,u=/[^/]+/,i=t=>t,p=(t,e,r,n=!0)=>{var o;const s=l(e.at(-1));((null===(o=t.match(c))||void 0===o?void 0:o.length)||0)>e.length+("function"==typeof e[0]?-1:0)+r.length+(s?-1:0)&&e.push(u),n&&!s&&e.push({})};let f={baseUrl:"/",setSUrl:i,onChange:i,sHistory:{current:[]},sUrl:"",sLayout:{current:"default"}};function h(t,e="",r=[]){let n={};for(const o in t){if("$root"===o)continue;let s=t[o];const c=e+o;if(l(s)){const t=s.$root||[];p(o,t,r,!1),Object.assign(n,h(s,c+"/",t))}else a(s)||(s=[s]),p(o,s,r),s.splice(1,0,...r),n[c]=s}return n}const y=()=>t.createElement("h1",null,"Page not found"),g={error:({children:e})=>t.createElement("div",{className:"page-error"},e),default:({children:e})=>t.createElement(t.Fragment,null,e)},d=e(f),m=(t,e={})=>O(f,t,e),b=(t=-1)=>{const e=f.sHistory.current.at(t);e&&(f.onChange(f.sUrl,e[0]),m(...e))};function O(t,e,r){t.setSUrl(e,r),t.onChange(t.sUrl,e),history.pushState(null,"",t.baseUrl+e);const n=t.sHistory.current||[];n.length>30&&n.shift(),n.push([e,r])}const v=t=>{const e=n(d);if(!e)throw new Error(t+" must be used within a <Router>");return e},E=()=>v("useHistory").sHistory.current,j=()=>(v("useRouterData").sHistory.current.at(-1)||[])[1]||[],U=()=>{const t=v("useNavigate");return(e,r)=>O(t,e,r)},S=()=>v("useLayout").sLayout.current,w=(e,r,n,o={})=>t.createElement(d.Provider,{value:e},t.createElement(r,null,t.createElement(n,Object.assign({},o)))),H=t=>{const{tagName:e="a",data:n={},to:o,children:s}=t,a=function(t,e){var r={};for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&e.indexOf(n)<0&&(r[n]=t[n]);if(null!=t&&"function"==typeof Object.getOwnPropertySymbols){var o=0;for(n=Object.getOwnPropertySymbols(t);o<n.length;o++)e.indexOf(n[o])<0&&Object.prototype.propertyIsEnumerable.call(t,n[o])&&(r[n[o]]=t[n[o]])}return r}(t,["tagName","data","to","children"]);return a.onClick=t=>{t.preventDefault(),m(o,n)},"a"===e&&(a.href=o),r(e,a,s)};function C(t){const e=h(t),r=[];for(let t in e){const n=e[t],o=[];t=t.replace(c,((t,e)=>{return o.push(e),"("+(((r=n.splice(1,1)[0])instanceof RegExp?(r+"").slice(1,-1):r)+")");var r})),n.splice(1,0,...o),r.push([RegExp("^"+t+"$"),...n])}return window.addEventListener("popstate",(()=>{m(location.pathname.slice(1))})),function(t){let{url:e,layout:n="default",layouts:a={},baseUrl:l="/",onChange:c=i}=t;a=Object.assign(Object.assign({},g),a);const[u,p]=o(e||location.pathname.slice(1)),h=s("default"),d={baseUrl:l,sUrl:u,setSUrl:p,sLayout:h,sHistory:s([]),onChange:c};f=d;for(let t of r){const e=t[0];if(t=[...t],~u.search(e)){const r=t.pop(),o=u.match(e)||[],s={};for(let e=1;e<o.length;e++)s[t[e+1]]=o[e];const l=r.layout||n;return l!==n&&(h.current=l),w(d,a[l],t[1],s)}}return w(d,a.error,y)}}export{H as Link,C as default,b as goBack,m as goTo,E as useHistory,S as useLayout,U as useNavigate,j as useRouterData};//# sourceMappingURL=index.esm.js.map