UNPKG

7.88 kBJavaScriptView Raw
1/**
2 * React Router v6.3.0
3 *
4 * Copyright (c) Remix Software Inc.
5 *
6 * This source code is licensed under the MIT license found in the
7 * LICENSE.md file in the root directory of this source tree.
8 *
9 * @license MIT
10 */
11import{parsePath as e,createMemoryHistory as t,Action as n}from"history";export{Action as NavigationType,createPath,parsePath}from"history";import{createContext as a,useContext as r,useMemo as l,useRef as i,useEffect as s,useCallback as o,createElement as h,useState as c,useLayoutEffect as u,Children as p,isValidElement as m,Fragment as f}from"react";const g=a(null),d=a(null),v=a({outlet:null,matches:[]});function y(e,t){if(!e)throw new Error(t)}function x(e,t={}){return e.replace(/:(\w+)/g,((e,n)=>(null==t[n]&&y(!1),t[n]))).replace(/\/*\*$/,(e=>null==t["*"]?"":t["*"].replace(/^\/*/,"/")))}function S(t,n,a="/"){let r=O(("string"==typeof n?e(n):n).pathname||"/",a);if(null==r)return null;let l=$(t);!function(e){e.sort(((e,t)=>e.score!==t.score?t.score-e.score:function(e,t){return e.length===t.length&&e.slice(0,-1).every(((e,n)=>e===t[n]))?e[e.length-1]-t[t.length-1]:0}(e.routesMeta.map((e=>e.childrenIndex)),t.routesMeta.map((e=>e.childrenIndex)))))}(l);let i=null;for(let e=0;null==i&&e<l.length;++e)i=B(l[e],r);return i}function $(e,t=[],n=[],a=""){return e.forEach(((e,r)=>{let l={relativePath:e.path||"",caseSensitive:!0===e.caseSensitive,childrenIndex:r,route:e};l.relativePath.startsWith("/")&&(l.relativePath.startsWith(a)||y(!1),l.relativePath=l.relativePath.slice(a.length));let i=j([a,l.relativePath]),s=n.concat(l);e.children&&e.children.length>0&&(!0===e.index&&y(!1),$(e.children,t,s,i)),(null!=e.path||e.index)&&t.push({path:i,score:b(i,e.index),routesMeta:s})})),t}const P=/^:\w+$/,W=e=>"*"===e;function b(e,t){let n=e.split("/"),a=n.length;return n.some(W)&&(a+=-2),t&&(a+=2),n.filter((e=>!W(e))).reduce(((e,t)=>e+(P.test(t)?3:""===t?1:10)),a)}function B(e,t){let{routesMeta:n}=e,a={},r="/",l=[];for(let i=0;i<n.length;++i){let e=n[i],s=i===n.length-1,o="/"===r?t:t.slice(r.length)||"/",h=E({path:e.relativePath,caseSensitive:e.caseSensitive,end:s},o);if(!h)return null;Object.assign(a,h.params);let c=e.route;l.push({params:a,pathname:j([r,h.pathname]),pathnameBase:A(j([r,h.pathnameBase])),route:c}),"/"!==h.pathnameBase&&(r=j([r,h.pathnameBase]))}return l}function E(e,t){"string"==typeof e&&(e={path:e,caseSensitive:!1,end:!0});let[n,a]=function(e,t=!1,n=!0){let a=[],r="^"+e.replace(/\/*\*?$/,"").replace(/^\/*/,"/").replace(/[\\.*+^$?{}|()[\]]/g,"\\$&").replace(/:(\w+)/g,((e,t)=>(a.push(t),"([^\\/]+)")));e.endsWith("*")?(a.push("*"),r+="*"===e||"/*"===e?"(.*)$":"(?:\\/(.+)|\\/*)$"):r+=n?"\\/*$":"(?:(?=[.~-]|%[0-9A-F]{2})|\\b|\\/|$)";return[new RegExp(r,t?void 0:"i"),a]}(e.path,e.caseSensitive,e.end),r=t.match(n);if(!r)return null;let l=r[0],i=l.replace(/(.)\/+$/,"$1"),s=r.slice(1);return{params:a.reduce(((e,t,n)=>{if("*"===t){let e=s[n]||"";i=l.slice(0,l.length-e.length).replace(/(.)\/+$/,"$1")}return e[t]=function(e,t){try{return decodeURIComponent(e)}catch(n){return e}}(s[n]||""),e}),{}),pathname:l,pathnameBase:i,pattern:e}}function N(t,n="/"){let{pathname:a,search:r="",hash:l=""}="string"==typeof t?e(t):t,i=a?a.startsWith("/")?a:function(e,t){let n=t.replace(/\/+$/,"").split("/");return e.split("/").forEach((e=>{".."===e?n.length>1&&n.pop():"."!==e&&n.push(e)})),n.length>1?n.join("/"):"/"}(a,n):n;return{pathname:i,search:C(r),hash:I(l)}}function w(t,n,a){let r,l="string"==typeof t?e(t):t,i=""===t||""===l.pathname?"/":l.pathname;if(null==i)r=a;else{let e=n.length-1;if(i.startsWith("..")){let t=i.split("/");for(;".."===t[0];)t.shift(),e-=1;l.pathname=t.join("/")}r=e>=0?n[e]:"/"}let s=N(l,r);return i&&"/"!==i&&i.endsWith("/")&&!s.pathname.endsWith("/")&&(s.pathname+="/"),s}function O(e,t){if("/"===t)return e;if(!e.toLowerCase().startsWith(t.toLowerCase()))return null;let n=e.charAt(t.length);return n&&"/"!==n?null:e.slice(t.length)||"/"}const j=e=>e.join("/").replace(/\/\/+/g,"/"),A=e=>e.replace(/\/+$/,"").replace(/^\/*/,"/"),C=e=>e&&"?"!==e?e.startsWith("?")?e:"?"+e:"",I=e=>e&&"#"!==e?e.startsWith("#")?e:"#"+e:"";function T(t){F()||y(!1);let{basename:n,navigator:a}=r(g),{hash:l,pathname:i,search:s}=q(t),o=i;if("/"!==n){let a=function(t){return""===t||""===t.pathname?"/":"string"==typeof t?e(t).pathname:t.pathname}(t),r=null!=a&&a.endsWith("/");o="/"===i?n+(r?"/":""):j([n,i])}return a.createHref({pathname:o,search:s,hash:l})}function F(){return null!=r(d)}function J(){return F()||y(!1),r(d).location}function M(){return r(d).navigationType}function R(e){F()||y(!1);let{pathname:t}=J();return l((()=>E(e,t)),[t,e])}function U(){F()||y(!1);let{basename:e,navigator:t}=r(g),{matches:n}=r(v),{pathname:a}=J(),l=JSON.stringify(n.map((e=>e.pathnameBase))),h=i(!1);return s((()=>{h.current=!0})),o(((n,r={})=>{if(!h.current)return;if("number"==typeof n)return void t.go(n);let i=w(n,JSON.parse(l),a);"/"!==e&&(i.pathname=j([e,i.pathname])),(r.replace?t.replace:t.push)(i,r.state)}),[e,t,l,a])}const L=a(null);function _(){return r(L)}function k(e){let t=r(v).outlet;return t?h(L.Provider,{value:e},t):t}function H(){let{matches:e}=r(v),t=e[e.length-1];return t?t.params:{}}function q(e){let{matches:t}=r(v),{pathname:n}=J(),a=JSON.stringify(t.map((e=>e.pathnameBase)));return l((()=>w(e,JSON.parse(a),n)),[e,a,n])}function z(t,n){F()||y(!1);let a,{matches:l}=r(v),i=l[l.length-1],s=i?i.params:{},o=(i&&i.pathname,i?i.pathnameBase:"/"),h=(i&&i.route,J());if(n){let t="string"==typeof n?e(n):n;"/"===o||t.pathname?.startsWith(o)||y(!1),a=t}else a=h;let c=a.pathname||"/",u=S(t,{pathname:"/"===o?c:c.slice(o.length)||"/"});return D(u&&u.map((e=>Object.assign({},e,{params:Object.assign({},s,e.params),pathname:j([o,e.pathname]),pathnameBase:"/"===e.pathnameBase?o:j([o,e.pathnameBase])}))),l)}function D(e,t=[]){return null==e?null:e.reduceRight(((n,a,r)=>h(v.Provider,{children:void 0!==a.route.element?a.route.element:n,value:{outlet:n,matches:t.concat(e.slice(0,r+1))}})),null)}function G({basename:e,children:n,initialEntries:a,initialIndex:r}){let l=i();null==l.current&&(l.current=t({initialEntries:a,initialIndex:r}));let s=l.current,[o,p]=c({action:s.action,location:s.location});return u((()=>s.listen(p)),[s]),h(X,{basename:e,children:n,location:o.location,navigationType:o.action,navigator:s})}function K({to:e,replace:t,state:n}){F()||y(!1);let a=U();return s((()=>{a(e,{replace:t,state:n})})),null}function Q(e){return k(e.context)}function V(e){y(!1)}function X({basename:t="/",children:a=null,location:r,navigationType:i=n.Pop,navigator:s,static:o=!1}){F()&&y(!1);let c=A(t),u=l((()=>({basename:c,navigator:s,static:o})),[c,s,o]);"string"==typeof r&&(r=e(r));let{pathname:p="/",search:m="",hash:f="",state:v=null,key:x="default"}=r,S=l((()=>{let e=O(p,c);return null==e?null:{pathname:e,search:m,hash:f,state:v,key:x}}),[c,p,m,f,v,x]);return null==S?null:h(g.Provider,{value:u},h(d.Provider,{children:a,value:{location:S,navigationType:i}}))}function Y({children:e,location:t}){return z(Z(e),t)}function Z(e){let t=[];return p.forEach(e,(e=>{if(!m(e))return;if(e.type===f)return void t.push.apply(t,Z(e.props.children));e.type!==V&&y(!1);let n={caseSensitive:e.props.caseSensitive,element:e.props.element,index:e.props.index,path:e.props.path};e.props.children&&(n.children=Z(e.props.children)),t.push(n)})),t}function ee(e){return D(e)}export{G as MemoryRouter,K as Navigate,Q as Outlet,V as Route,X as Router,Y as Routes,d as UNSAFE_LocationContext,g as UNSAFE_NavigationContext,v as UNSAFE_RouteContext,Z as createRoutesFromChildren,x as generatePath,E as matchPath,S as matchRoutes,ee as renderMatches,N as resolvePath,T as useHref,F as useInRouterContext,J as useLocation,R as useMatch,U as useNavigate,M as useNavigationType,k as useOutlet,_ as useOutletContext,H as useParams,q as useResolvedPath,z as useRoutes};
12//# sourceMappingURL=react-router.production.min.js.map