UNPKG

13.5 kBJavaScriptView Raw
1/**
2 * React Router v6.11.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*as e from"react";import{UNSAFE_invariant as t,joinPaths as r,matchPath as n,UNSAFE_getPathContributingMatches as a,resolveTo as o,parsePath as i,matchRoutes as l,Action as u,isRouteErrorResponse as s,createMemoryHistory as c,stripBasename as d,AbortedDeferredError as p,createRouter as m}from"@remix-run/router";export{AbortedDeferredError,Action as NavigationType,createPath,defer,generatePath,isRouteErrorResponse,json,matchPath,matchRoutes,parsePath,redirect,resolvePath}from"@remix-run/router";const h=e.createContext(null),f=e.createContext(null),v=e.createContext(null),E=e.createContext(null),g=e.createContext(null),x=e.createContext({outlet:null,matches:[],isDataRoute:!1}),C=e.createContext(null);function y(n,{relative:a}={}){R()||t(!1);let{basename:o,navigator:i}=e.useContext(E),{hash:l,pathname:u,search:s}=O(n,{relative:a}),c=u;return"/"!==o&&(c="/"===u?o:r([o,u])),i.createHref({pathname:c,search:s,hash:l})}function R(){return null!=e.useContext(g)}function b(){return R()||t(!1),e.useContext(g).location}function U(){return e.useContext(g).navigationType}function S(r){R()||t(!1);let{pathname:a}=b();return e.useMemo((()=>n(r,a)),[a,r])}function P(t){e.useContext(E).static||e.useLayoutEffect(t)}function B(){let{isDataRoute:n}=e.useContext(x);return n?function(){let{router:t}=z(w.UseNavigateStable),r=$(J.UseNavigateStable),n=e.useRef(!1);return P((()=>{n.current=!0})),e.useCallback(((e,a={})=>{n.current&&("number"==typeof e?t.navigate(e):t.navigate(e,{fromRouteId:r,...a}))}),[t,r])}():function(){R()||t(!1);let n=e.useContext(h),{basename:i,navigator:l}=e.useContext(E),{matches:u}=e.useContext(x),{pathname:s}=b(),c=JSON.stringify(a(u).map((e=>e.pathnameBase))),d=e.useRef(!1);return P((()=>{d.current=!0})),e.useCallback(((e,t={})=>{if(!d.current)return;if("number"==typeof e)return void l.go(e);let a=o(e,JSON.parse(c),s,"path"===t.relative);null==n&&"/"!==i&&(a.pathname="/"===a.pathname?i:r([i,a.pathname])),(t.replace?l.replace:l.push)(a,t.state,t)}),[i,l,c,s,n])}()}const D=e.createContext(null);function N(){return e.useContext(D)}function _(t){let r=e.useContext(x).outlet;return r?e.createElement(D.Provider,{value:t},r):r}function k(){let{matches:t}=e.useContext(x),r=t[t.length-1];return r?r.params:{}}function O(t,{relative:r}={}){let{matches:n}=e.useContext(x),{pathname:i}=b(),l=JSON.stringify(a(n).map((e=>e.pathnameBase)));return e.useMemo((()=>o(t,JSON.parse(l),i,"path"===r)),[t,l,i,r])}function L(e,t){return A(e,t)}function A(n,a,o){R()||t(!1);let{navigator:s}=e.useContext(E),{matches:c}=e.useContext(x),d=c[c.length-1],p=d?d.params:{};!d||d.pathname;let m=d?d.pathnameBase:"/";d&&d.route;let h,f=b();if(a){let e="string"==typeof a?i(a):a;"/"===m||e.pathname?.startsWith(m)||t(!1),h=e}else h=f;let v=h.pathname||"/",C="/"===m?v:v.slice(m.length)||"/",y=l(n,{pathname:C}),U=T(y&&y.map((e=>Object.assign({},e,{params:Object.assign({},p,e.params),pathname:r([m,s.encodeLocation?s.encodeLocation(e.pathname).pathname:e.pathname]),pathnameBase:"/"===e.pathnameBase?m:r([m,s.encodeLocation?s.encodeLocation(e.pathnameBase).pathname:e.pathnameBase])}))),c,o);return a&&U?e.createElement(g.Provider,{value:{location:{pathname:"/",search:"",hash:"",state:null,key:"default",...h},navigationType:u.Pop}},U):U}function F(){let t=X(),r=s(t)?`${t.status} ${t.statusText}`:t instanceof Error?t.message:JSON.stringify(t),n=t instanceof Error?t.stack:null,a={padding:"0.5rem",backgroundColor:"rgba(200,200,200, 0.5)"};return e.createElement(e.Fragment,null,e.createElement("h2",null,"Unexpected Application Error!"),e.createElement("h3",{style:{fontStyle:"italic"}},r),n?e.createElement("pre",{style:a},n):null,null)}const j=e.createElement(F,null);class I extends e.Component{constructor(e){super(e),this.state={location:e.location,revalidation:e.revalidation,error:e.error}}static getDerivedStateFromError(e){return{error:e}}static getDerivedStateFromProps(e,t){return t.location!==e.location||"idle"!==t.revalidation&&"idle"===e.revalidation?{error:e.error,location:e.location,revalidation:e.revalidation}:{error:e.error||t.error,location:t.location,revalidation:e.revalidation||t.revalidation}}componentDidCatch(e,t){console.error("React Router caught the following error during render",e,t)}render(){return this.state.error?e.createElement(x.Provider,{value:this.props.routeContext},e.createElement(C.Provider,{value:this.state.error,children:this.props.component})):this.props.children}}function M({routeContext:t,match:r,children:n}){let a=e.useContext(h);return a&&a.static&&a.staticContext&&(r.route.errorElement||r.route.ErrorBoundary)&&(a.staticContext._deepestRenderedBoundaryId=r.route.id),e.createElement(x.Provider,{value:t},n)}function T(r,n=[],a=null){if(null==r){if(!a?.errors)return null;r=a.matches}let o=r,i=a?.errors;if(null!=i){let e=o.findIndex((e=>e.route.id&&i?.[e.route.id]));e>=0||t(!1),o=o.slice(0,Math.min(o.length,e+1))}return o.reduceRight(((t,r,l)=>{let u=r.route.id?i?.[r.route.id]:null,s=null;a&&(s=r.route.errorElement||j);let c=n.concat(o.slice(0,l+1)),d=()=>{let n;return n=u?s:r.route.Component?e.createElement(r.route.Component,null):r.route.element?r.route.element:t,e.createElement(M,{match:r,routeContext:{outlet:t,matches:c,isDataRoute:null!=a},children:n})};return a&&(r.route.ErrorBoundary||r.route.errorElement||0===l)?e.createElement(I,{location:a.location,revalidation:a.revalidation,component:s,error:u,children:d(),routeContext:{outlet:null,matches:c,isDataRoute:!0}}):d()}),null)}var w,J;function z(r){let n=e.useContext(h);return n||t(!1),n}function H(r){let n=e.useContext(f);return n||t(!1),n}function $(r){let n=function(r){let n=e.useContext(x);return n||t(!1),n}(),a=n.matches[n.matches.length-1];return a.route.id||t(!1),a.route.id}function V(){return $(J.UseRouteId)}function W(){return H(J.UseNavigation).navigation}function Y(){let e=z(w.UseRevalidator),t=H(J.UseRevalidator);return{revalidate:e.router.revalidate,state:t.revalidation}}function q(){let{matches:t,loaderData:r}=H(J.UseMatches);return e.useMemo((()=>t.map((e=>{let{pathname:t,params:n}=e;return{id:e.route.id,pathname:t,params:n,data:r[e.route.id],handle:e.route.handle}}))),[t,r])}function G(){let e=H(J.UseLoaderData),t=$(J.UseLoaderData);if(!e.errors||null==e.errors[t])return e.loaderData[t];console.error(`You cannot \`useLoaderData\` in an errorElement (routeId: ${t})`)}function K(e){return H(J.UseRouteLoaderData).loaderData[e]}function Q(){let r=H(J.UseActionData);return e.useContext(x)||t(!1),Object.values(r?.actionData||{})[0]}function X(){let t=e.useContext(C),r=H(J.UseRouteError),n=$(J.UseRouteError);return t||r.errors?.[n]}function Z(){return e.useContext(v)?._data}function ee(){return e.useContext(v)?._error}!function(e){e.UseBlocker="useBlocker",e.UseRevalidator="useRevalidator",e.UseNavigateStable="useNavigate"}(w||(w={})),function(e){e.UseBlocker="useBlocker",e.UseLoaderData="useLoaderData",e.UseActionData="useActionData",e.UseRouteError="useRouteError",e.UseNavigation="useNavigation",e.UseRouteLoaderData="useRouteLoaderData",e.UseMatches="useMatches",e.UseRevalidator="useRevalidator",e.UseNavigateStable="useNavigate",e.UseRouteId="useRouteId"}(J||(J={}));let te=0;function re(t){let{router:r}=z(w.UseBlocker),n=H(J.UseBlocker),[a]=e.useState((()=>String(++te))),o=e.useCallback((e=>"function"==typeof t?!!t(e):!!t),[t]),i=r.getBlocker(a,o);return e.useEffect((()=>()=>r.deleteBlocker(a)),[r,a]),n.blockers.get(a)||i}function ne({fallbackElement:t,router:r}){let[n,a]=e.useState(r.state);e.useLayoutEffect((()=>r.subscribe(a)),[r,a]);let o=e.useMemo((()=>({createHref:r.createHref,encodeLocation:r.encodeLocation,go:e=>r.navigate(e),push:(e,t,n)=>r.navigate(e,{state:t,preventScrollReset:n?.preventScrollReset}),replace:(e,t,n)=>r.navigate(e,{replace:!0,state:t,preventScrollReset:n?.preventScrollReset})})),[r]),i=r.basename||"/",l=e.useMemo((()=>({router:r,navigator:o,static:!1,basename:i})),[r,o,i]);return e.createElement(e.Fragment,null,e.createElement(h.Provider,{value:l},e.createElement(f.Provider,{value:n},e.createElement(se,{basename:r.basename,location:r.state.location,navigationType:r.state.historyAction,navigator:o},r.state.initialized?e.createElement(ae,{routes:r.routes,state:n}):t))),null)}function ae({routes:e,state:t}){return A(e,void 0,t)}function oe({basename:t,children:r,initialEntries:n,initialIndex:a}){let o=e.useRef();null==o.current&&(o.current=c({initialEntries:n,initialIndex:a,v5Compat:!0}));let i=o.current,[l,u]=e.useState({action:i.action,location:i.location});return e.useLayoutEffect((()=>i.listen(u)),[i]),e.createElement(se,{basename:t,children:r,location:l.location,navigationType:l.action,navigator:i})}function ie({to:r,replace:n,state:i,relative:l}){R()||t(!1);let{matches:u}=e.useContext(x),{pathname:s}=b(),c=B(),d=o(r,a(u).map((e=>e.pathnameBase)),s,"path"===l),p=JSON.stringify(d);return e.useEffect((()=>c(JSON.parse(p),{replace:n,state:i,relative:l})),[c,p,l,n,i]),null}function le(e){return _(e.context)}function ue(e){t(!1)}function se({basename:r="/",children:n=null,location:a,navigationType:o=u.Pop,navigator:l,static:s=!1}){R()&&t(!1);let c=r.replace(/^\/*/,"/"),p=e.useMemo((()=>({basename:c,navigator:l,static:s})),[c,l,s]);"string"==typeof a&&(a=i(a));let{pathname:m="/",search:h="",hash:f="",state:v=null,key:x="default"}=a,C=e.useMemo((()=>{let e=d(m,c);return null==e?null:{location:{pathname:e,search:h,hash:f,state:v,key:x},navigationType:o}}),[c,m,h,f,v,x,o]);return null==C?null:e.createElement(E.Provider,{value:p},e.createElement(g.Provider,{children:n,value:C}))}function ce({children:e,location:t}){return L(ve(e),t)}function de({children:t,errorElement:r,resolve:n}){return e.createElement(he,{resolve:n,errorElement:r},e.createElement(fe,null,t))}var pe;!function(e){e[e.pending=0]="pending",e[e.success=1]="success",e[e.error=2]="error"}(pe||(pe={}));const me=new Promise((()=>{}));class he extends e.Component{constructor(e){super(e),this.state={error:null}}static getDerivedStateFromError(e){return{error:e}}componentDidCatch(e,t){console.error("<Await> caught the following error during render",e,t)}render(){let{children:t,errorElement:r,resolve:n}=this.props,a=null,o=pe.pending;if(n instanceof Promise)if(this.state.error){o=pe.error;let e=this.state.error;a=Promise.reject().catch((()=>{})),Object.defineProperty(a,"_tracked",{get:()=>!0}),Object.defineProperty(a,"_error",{get:()=>e})}else n._tracked?(a=n,o=void 0!==a._error?pe.error:void 0!==a._data?pe.success:pe.pending):(o=pe.pending,Object.defineProperty(n,"_tracked",{get:()=>!0}),a=n.then((e=>Object.defineProperty(n,"_data",{get:()=>e})),(e=>Object.defineProperty(n,"_error",{get:()=>e}))));else o=pe.success,a=Promise.resolve(),Object.defineProperty(a,"_tracked",{get:()=>!0}),Object.defineProperty(a,"_data",{get:()=>n});if(o===pe.error&&a._error instanceof p)throw me;if(o===pe.error&&!r)throw a._error;if(o===pe.error)return e.createElement(v.Provider,{value:a,children:r});if(o===pe.success)return e.createElement(v.Provider,{value:a,children:t});throw a}}function fe({children:t}){let r=Z(),n="function"==typeof t?t(r):t;return e.createElement(e.Fragment,null,n)}function ve(r,n=[]){let a=[];return e.Children.forEach(r,((r,o)=>{if(!e.isValidElement(r))return;let i=[...n,o];if(r.type===e.Fragment)return void a.push.apply(a,ve(r.props.children,i));r.type!==ue&&t(!1),r.props.index&&r.props.children&&t(!1);let l={id:r.props.id||i.join("-"),caseSensitive:r.props.caseSensitive,element:r.props.element,Component:r.props.Component,index:r.props.index,path:r.props.path,loader:r.props.loader,action:r.props.action,errorElement:r.props.errorElement,ErrorBoundary:r.props.ErrorBoundary,hasErrorBoundary:null!=r.props.ErrorBoundary||null!=r.props.errorElement,shouldRevalidate:r.props.shouldRevalidate,handle:r.props.handle,lazy:r.props.lazy};r.props.children&&(l.children=ve(r.props.children,i)),a.push(l)})),a}function Ee(e){return T(e)}function ge(t){let r={hasErrorBoundary:null!=t.ErrorBoundary||null!=t.errorElement};return t.Component&&Object.assign(r,{element:e.createElement(t.Component),Component:void 0}),t.ErrorBoundary&&Object.assign(r,{errorElement:e.createElement(t.ErrorBoundary),ErrorBoundary:void 0}),r}function xe(e,t){return m({basename:t?.basename,future:{...t?.future,v7_prependBasename:!0},history:c({initialEntries:t?.initialEntries,initialIndex:t?.initialIndex}),hydrationData:t?.hydrationData,routes:e,mapRouteProperties:ge}).initialize()}export{de as Await,oe as MemoryRouter,ie as Navigate,le as Outlet,ue as Route,se as Router,ne as RouterProvider,ce as Routes,h as UNSAFE_DataRouterContext,f as UNSAFE_DataRouterStateContext,g as UNSAFE_LocationContext,E as UNSAFE_NavigationContext,x as UNSAFE_RouteContext,ge as UNSAFE_mapRouteProperties,V as UNSAFE_useRouteId,A as UNSAFE_useRoutesImpl,xe as createMemoryRouter,ve as createRoutesFromChildren,ve as createRoutesFromElements,Ee as renderMatches,re as unstable_useBlocker,Q as useActionData,ee as useAsyncError,Z as useAsyncValue,y as useHref,R as useInRouterContext,G as useLoaderData,b as useLocation,S as useMatch,q as useMatches,B as useNavigate,W as useNavigation,U as useNavigationType,_ as useOutlet,N as useOutletContext,k as useParams,O as useResolvedPath,Y as useRevalidator,X as useRouteError,K as useRouteLoaderData,L as useRoutes};
12//# sourceMappingURL=react-router.production.min.js.map