UNPKG

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