UNPKG

7.68 kBJavaScriptView Raw
1/**
2 * React Router v6.0.2
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{createContext as e,useRef as t,useState as n,useLayoutEffect as a,createElement as r,useContext as l,useEffect as i,useMemo as s,useCallback as h,Children as c,isValidElement as o,Fragment as u}from"react";import{createMemoryHistory as p,Action as m,parsePath as f}from"history";function g(e,t){if(!e)throw new Error(t)}const d=e(null),v=e(null),y=e({outlet:null,matches:[]});function x({basename:e,children:l,initialEntries:i,initialIndex:s}){let h=t();null==h.current&&(h.current=p({initialEntries:i,initialIndex:s}));let c=h.current,[o,u]=n({action:c.action,location:c.location});return a((()=>c.listen(u)),[c]),r(b,{basename:e,children:l,location:o.location,navigationType:o.action,navigator:c})}function S({to:e,replace:t,state:n}){E()||g(!1);let a=O();return i((()=>{a(e,{replace:t,state:n})})),null}function $(e){return j()}function W(e){g(!1)}function b({basename:e="/",children:t=null,location:n,navigationType:a=m.Pop,navigator:l,static:i=!1}){E()&&g(!1);let h=V(e),c=s((()=>({basename:h,navigator:l,static:i})),[h,l,i]);"string"==typeof n&&(n=f(n));let{pathname:o="/",search:u="",hash:p="",state:y=null,key:x="default"}=n,S=s((()=>{let e=K(o,h);return null==e?null:{pathname:e,search:u,hash:p,state:y,key:x}}),[h,o,u,p,y,x]);return null==S?null:r(d.Provider,{value:c},r(v.Provider,{children:t,value:{location:S,navigationType:a}}))}function B({children:e,location:t}){return J(M(e),t)}function P(e){E()||g(!1);let{basename:t,navigator:n}=l(d),{hash:a,pathname:r,search:i}=A(e),s=r;if("/"!==t){let n=function(e){return""===e||""===e.pathname?"/":"string"==typeof e?f(e).pathname:e.pathname}(e),a=null!=n&&n.endsWith("/");s="/"===r?t+(a?"/":""):Q([t,r])}return n.createHref({pathname:s,search:i,hash:a})}function E(){return null!=l(v)}function w(){return E()||g(!1),l(v).location}function N(){return l(v).navigationType}function I(e){return E()||g(!1),z(e,w().pathname)}function O(){E()||g(!1);let{basename:e,navigator:n}=l(d),{matches:a}=l(y),{pathname:r}=w(),s=JSON.stringify(a.map((e=>e.pathnameBase))),c=t(!1);return i((()=>{c.current=!0})),h(((t,a={})=>{if(!c.current)return;if("number"==typeof t)return void n.go(t);let l=G(t,JSON.parse(s),r);"/"!==e&&(l.pathname=Q([e,l.pathname])),(a.replace?n.replace:n.push)(l,a.state)}),[e,n,s,r])}function j(){return l(y).outlet}function C(){let{matches:e}=l(y),t=e[e.length-1];return t?t.params:{}}function A(e){let{matches:t}=l(y),{pathname:n}=w(),a=JSON.stringify(t.map((e=>e.pathnameBase)));return s((()=>G(e,JSON.parse(a),n)),[e,a,n])}function J(e,t){E()||g(!1);let n,{matches:a}=l(y),r=a[a.length-1],i=r?r.params:{},s=(r&&r.pathname,r?r.pathnameBase:"/"),h=(r&&r.route,w());if(t){let e="string"==typeof t?f(t):t;"/"===s||e.pathname?.startsWith(s)||g(!1),n=e}else n=h;let c=n.pathname||"/",o=T(e,{pathname:"/"===s?c:c.slice(s.length)||"/"});return q(o&&o.map((e=>Object.assign({},e,{params:Object.assign({},i,e.params),pathname:Q([s,e.pathname]),pathnameBase:"/"===e.pathnameBase?s:Q([s,e.pathnameBase])}))),a)}function M(e){let t=[];return c.forEach(e,(e=>{if(!o(e))return;if(e.type===u)return void t.push.apply(t,M(e.props.children));e.type!==W&&g(!1);let n={caseSensitive:e.props.caseSensitive,element:e.props.element,index:e.props.index,path:e.props.path};e.props.children&&(n.children=M(e.props.children)),t.push(n)})),t}function R(e,t={}){return e.replace(/:(\w+)/g,((e,n)=>(null==t[n]&&g(!1),t[n]))).replace(/\/*\*$/,(e=>null==t["*"]?"":t["*"].replace(/^\/*/,"/")))}function T(e,t,n="/"){let a=K(("string"==typeof t?f(t):t).pathname||"/",n);if(null==a)return null;let r=U(e);!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)))))}(r);let l=null;for(let i=0;null==l&&i<r.length;++i)l=k(r[i],e,a);return l}function U(e,t=[],n=[],a=""){return e.forEach(((e,r)=>{let l={relativePath:e.path||"",caseSensitive:!0===e.caseSensitive,childrenIndex:r};l.relativePath.startsWith("/")&&(l.relativePath.startsWith(a)||g(!1),l.relativePath=l.relativePath.slice(a.length));let i=Q([a,l.relativePath]),s=n.concat(l);e.children&&e.children.length>0&&(!0===e.index&&g(!1),U(e.children,t,s,i)),(null!=e.path||e.index)&&t.push({path:i,score:_(i,e.index),routesMeta:s})})),t}const F=/^:\w+$/,L=e=>"*"===e;function _(e,t){let n=e.split("/"),a=n.length;return n.some(L)&&(a+=-2),t&&(a+=2),n.filter((e=>!L(e))).reduce(((e,t)=>e+(F.test(t)?3:""===t?1:10)),a)}function k(e,t,n){let a=t,{routesMeta:r}=e,l={},i="/",s=[];for(let h=0;h<r.length;++h){let e=r[h],t=h===r.length-1,c="/"===i?n:n.slice(i.length)||"/",o=z({path:e.relativePath,caseSensitive:e.caseSensitive,end:t},c);if(!o)return null;Object.assign(l,o.params);let u=a[e.childrenIndex];s.push({params:l,pathname:Q([i,o.pathname]),pathnameBase:Q([i,o.pathnameBase]),route:u}),"/"!==o.pathnameBase&&(i=Q([i,o.pathnameBase])),a=u.children}return s}function H(e){return q(e)}function q(e,t=[]){return null==e?null:e.reduceRight(((n,a,l)=>r(y.Provider,{children:void 0!==a.route.element?a.route.element:r($,null),value:{outlet:n,matches:t.concat(e.slice(0,l+1))}})),null)}function z(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?"\\/*$":"(?:\\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 D(e,t="/"){let{pathname:n,search:a="",hash:r=""}="string"==typeof e?f(e):e,l=n?n.startsWith("/")?n: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("/"):"/"}(n,t):t;return{pathname:l,search:X(a),hash:Y(r)}}function G(e,t,n){let a,r="string"==typeof e?f(e):e,l=""===e||""===r.pathname?"/":r.pathname;if(null==l)a=n;else{let e=t.length-1;if(l.startsWith("..")){let t=l.split("/");for(;".."===t[0];)t.shift(),e-=1;r.pathname=t.join("/")}a=e>=0?t[e]:"/"}let i=D(r,a);return l&&"/"!==l&&l.endsWith("/")&&!i.pathname.endsWith("/")&&(i.pathname+="/"),i}function K(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 Q=e=>e.join("/").replace(/\/\/+/g,"/"),V=e=>e.replace(/\/+$/,"").replace(/^\/*/,"/"),X=e=>e&&"?"!==e?e.startsWith("?")?e:"?"+e:"",Y=e=>e&&"#"!==e?e.startsWith("#")?e:"#"+e:"";export{x as MemoryRouter,S as Navigate,$ as Outlet,W as Route,b as Router,B as Routes,v as UNSAFE_LocationContext,d as UNSAFE_NavigationContext,y as UNSAFE_RouteContext,M as createRoutesFromChildren,R as generatePath,z as matchPath,T as matchRoutes,H as renderMatches,D as resolvePath,P as useHref,E as useInRouterContext,w as useLocation,I as useMatch,O as useNavigate,N as useNavigationType,j as useOutlet,C as useParams,A as useResolvedPath,J as useRoutes};
12//# sourceMappingURL=react-router.production.min.js.map