UNPKG

14.1 kBJavaScriptView Raw
1/**
2 * React Router v6.19.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*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,UNSAFE_convertRouteMatchToUiMatch as s,stripBasename as c,IDLE_BLOCKER as p,isRouteErrorResponse as d,createMemoryHistory as m,AbortedDeferredError as h,createRouter as f}from"@remix-run/router";export{AbortedDeferredError,Action as NavigationType,createPath,defer,generatePath,isRouteErrorResponse,json,matchPath,matchRoutes,parsePath,redirect,redirectDocument,resolvePath}from"@remix-run/router";const v=e.createContext(null),E=e.createContext(null),g=e.createContext(null),x=e.createContext(null),C=e.createContext(null),y=e.createContext({outlet:null,matches:[],isDataRoute:!1}),R=e.createContext(null);function b(n,{relative:a}={}){U()||t(!1);let{basename:o,navigator:i}=e.useContext(x),{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 U(){return null!=e.useContext(C)}function S(){return U()||t(!1),e.useContext(C).location}function D(){return e.useContext(C).navigationType}function P(r){U()||t(!1);let{pathname:a}=S();return e.useMemo((()=>n(r,a)),[a,r])}function B(t){e.useContext(x).static||e.useLayoutEffect(t)}function N(){let{isDataRoute:n}=e.useContext(y);return n?function(){let{router:t}=$(z.UseNavigateStable),r=W(H.UseNavigateStable),n=e.useRef(!1);return B((()=>{n.current=!0})),e.useCallback(((e,a={})=>{n.current&&("number"==typeof e?t.navigate(e):t.navigate(e,{fromRouteId:r,...a}))}),[t,r])}():function(){U()||t(!1);let n=e.useContext(v),{basename:i,navigator:l}=e.useContext(x),{matches:u}=e.useContext(y),{pathname:s}=S(),c=JSON.stringify(a(u).map((e=>e.pathnameBase))),p=e.useRef(!1);return B((()=>{p.current=!0})),e.useCallback(((e,t={})=>{if(!p.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 _=e.createContext(null);function k(){return e.useContext(_)}function L(t){let r=e.useContext(y).outlet;return r?e.createElement(_.Provider,{value:t},r):r}function A(){let{matches:t}=e.useContext(y),r=t[t.length-1];return r?r.params:{}}function O(t,{relative:r}={}){let{matches:n}=e.useContext(y),{pathname:i}=S(),l=JSON.stringify(a(n).map(((e,t)=>t===n.length-1?e.pathname:e.pathnameBase)));return e.useMemo((()=>o(t,JSON.parse(l),i,"path"===r)),[t,l,i,r])}function F(e,t){return j(e,t)}function j(n,a,o){U()||t(!1);let{navigator:s}=e.useContext(x),{matches:c}=e.useContext(y),p=c[c.length-1],d=p?p.params:{};!p||p.pathname;let m=p?p.pathnameBase:"/";p&&p.route;let h,f=S();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||"/",E="/"===m?v:v.slice(m.length)||"/",g=l(n,{pathname:E}),R=J(g&&g.map((e=>Object.assign({},e,{params:Object.assign({},d,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&&R?e.createElement(C.Provider,{value:{location:{pathname:"/",search:"",hash:"",state:null,key:"default",...h},navigationType:u.Pop}},R):R}function I(){let t=ee(),r=d(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 M=e.createElement(I,null);class T 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(y.Provider,{value:this.props.routeContext},e.createElement(R.Provider,{value:this.state.error,children:this.props.component})):this.props.children}}function w({routeContext:t,match:r,children:n}){let a=e.useContext(v);return a&&a.static&&a.staticContext&&(r.route.errorElement||r.route.ErrorBoundary)&&(a.staticContext._deepestRenderedBoundaryId=r.route.id),e.createElement(y.Provider,{value:t},n)}function J(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||M);let c=n.concat(o.slice(0,l+1)),p=()=>{let n;return n=u?s:r.route.Component?e.createElement(r.route.Component,null):r.route.element?r.route.element:t,e.createElement(w,{match:r,routeContext:{outlet:t,matches:c,isDataRoute:null!=a},children:n})};return a&&(r.route.ErrorBoundary||r.route.errorElement||0===l)?e.createElement(T,{location:a.location,revalidation:a.revalidation,component:s,error:u,children:p(),routeContext:{outlet:null,matches:c,isDataRoute:!0}}):p()}),null)}var z=function(e){return e.UseBlocker="useBlocker",e.UseRevalidator="useRevalidator",e.UseNavigateStable="useNavigate",e}(z||{}),H=function(e){return 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",e}(H||{});function $(r){let n=e.useContext(v);return n||t(!1),n}function V(r){let n=e.useContext(E);return n||t(!1),n}function W(r){let n=function(r){let n=e.useContext(y);return n||t(!1),n}(),a=n.matches[n.matches.length-1];return a.route.id||t(!1),a.route.id}function Y(){return W(H.UseRouteId)}function q(){return V(H.UseNavigation).navigation}function G(){let t=$(z.UseRevalidator),r=V(H.UseRevalidator);return e.useMemo((()=>({revalidate:t.router.revalidate,state:r.revalidation})),[t.router.revalidate,r.revalidation])}function K(){let{matches:t,loaderData:r}=V(H.UseMatches);return e.useMemo((()=>t.map((e=>s(e,r)))),[t,r])}function Q(){let e=V(H.UseLoaderData),t=W(H.UseLoaderData);if(!e.errors||null==e.errors[t])return e.loaderData[t];console.error(`You cannot \`useLoaderData\` in an errorElement (routeId: ${t})`)}function X(e){return V(H.UseRouteLoaderData).loaderData[e]}function Z(){let e=V(H.UseActionData),t=W(H.UseLoaderData);return e.actionData?e.actionData[t]:void 0}function ee(){let t=e.useContext(R),r=V(H.UseRouteError),n=W(H.UseRouteError);return t||r.errors?.[n]}function te(){return e.useContext(g)?._data}function re(){return e.useContext(g)?._error}let ne=0;function ae(t){let{router:r,basename:n}=$(z.UseBlocker),a=V(H.UseBlocker),[o,i]=e.useState(""),l=e.useCallback((e=>{if("function"!=typeof t)return!!t;if("/"===n)return t(e);let{currentLocation:r,nextLocation:a,historyAction:o}=e;return t({currentLocation:{...r,pathname:c(r.pathname,n)||r.pathname},nextLocation:{...a,pathname:c(a.pathname,n)||a.pathname},historyAction:o})}),[n,t]);return e.useEffect((()=>{let e=String(++ne);return i(e),()=>r.deleteBlocker(e)}),[r]),e.useEffect((()=>{""!==o&&r.getBlocker(o,l)}),[r,o,l]),o&&a.blockers.has(o)?a.blockers.get(o):p}const oe=e.startTransition;function ie({fallbackElement:t,router:r,future:n}){let[a,o]=e.useState(r.state),{v7_startTransition:i}=n||{},l=e.useCallback((e=>{i&&oe?oe((()=>o(e))):o(e)}),[o,i]);e.useLayoutEffect((()=>r.subscribe(l)),[r,l]);let u=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]),s=r.basename||"/",c=e.useMemo((()=>({router:r,navigator:u,static:!1,basename:s})),[r,u,s]);return e.createElement(e.Fragment,null,e.createElement(v.Provider,{value:c},e.createElement(E.Provider,{value:a},e.createElement(de,{basename:s,location:a.location,navigationType:a.historyAction,navigator:u},a.initialized?e.createElement(le,{routes:r.routes,state:a}):t))),null)}function le({routes:e,state:t}){return j(e,void 0,t)}function ue({basename:t,children:r,initialEntries:n,initialIndex:a,future:o}){let i=e.useRef();null==i.current&&(i.current=m({initialEntries:n,initialIndex:a,v5Compat:!0}));let l=i.current,[u,s]=e.useState({action:l.action,location:l.location}),{v7_startTransition:c}=o||{},p=e.useCallback((e=>{c&&oe?oe((()=>s(e))):s(e)}),[s,c]);return e.useLayoutEffect((()=>l.listen(p)),[l,p]),e.createElement(de,{basename:t,children:r,location:u.location,navigationType:u.action,navigator:l})}function se({to:r,replace:n,state:i,relative:l}){U()||t(!1);let{matches:u}=e.useContext(y),{pathname:s}=S(),c=N(),p=o(r,a(u).map((e=>e.pathnameBase)),s,"path"===l),d=JSON.stringify(p);return e.useEffect((()=>c(JSON.parse(d),{replace:n,state:i,relative:l})),[c,d,l,n,i]),null}function ce(e){return L(e.context)}function pe(e){t(!1)}function de({basename:r="/",children:n=null,location:a,navigationType:o=u.Pop,navigator:l,static:s=!1}){U()&&t(!1);let p=r.replace(/^\/*/,"/"),d=e.useMemo((()=>({basename:p,navigator:l,static:s})),[p,l,s]);"string"==typeof a&&(a=i(a));let{pathname:m="/",search:h="",hash:f="",state:v=null,key:E="default"}=a,g=e.useMemo((()=>{let e=c(m,p);return null==e?null:{location:{pathname:e,search:h,hash:f,state:v,key:E},navigationType:o}}),[p,m,h,f,v,E,o]);return null==g?null:e.createElement(x.Provider,{value:d},e.createElement(C.Provider,{children:n,value:g}))}function me({children:e,location:t}){return F(xe(e),t)}function he({children:t,errorElement:r,resolve:n}){return e.createElement(Ee,{resolve:n,errorElement:r},e.createElement(ge,null,t))}var fe=function(e){return e[e.pending=0]="pending",e[e.success=1]="success",e[e.error=2]="error",e}(fe||{});const ve=new Promise((()=>{}));class Ee 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=fe.pending;if(n instanceof Promise)if(this.state.error){o=fe.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?fe.error:void 0!==a._data?fe.success:fe.pending):(o=fe.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=fe.success,a=Promise.resolve(),Object.defineProperty(a,"_tracked",{get:()=>!0}),Object.defineProperty(a,"_data",{get:()=>n});if(o===fe.error&&a._error instanceof h)throw ve;if(o===fe.error&&!r)throw a._error;if(o===fe.error)return e.createElement(g.Provider,{value:a,children:r});if(o===fe.success)return e.createElement(g.Provider,{value:a,children:t});throw a}}function ge({children:t}){let r=te(),n="function"==typeof t?t(r):t;return e.createElement(e.Fragment,null,n)}function xe(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,xe(r.props.children,i));r.type!==pe&&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=xe(r.props.children,i)),a.push(l)})),a}function Ce(e){return J(e)}function ye(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 Re(e,t){return f({basename:t?.basename,future:{...t?.future,v7_prependBasename:!0},history:m({initialEntries:t?.initialEntries,initialIndex:t?.initialIndex}),hydrationData:t?.hydrationData,routes:e,mapRouteProperties:ye}).initialize()}export{he as Await,ue as MemoryRouter,se as Navigate,ce as Outlet,pe as Route,de as Router,ie as RouterProvider,me as Routes,v as UNSAFE_DataRouterContext,E as UNSAFE_DataRouterStateContext,C as UNSAFE_LocationContext,x as UNSAFE_NavigationContext,y as UNSAFE_RouteContext,ye as UNSAFE_mapRouteProperties,Y as UNSAFE_useRouteId,j as UNSAFE_useRoutesImpl,Re as createMemoryRouter,xe as createRoutesFromChildren,xe as createRoutesFromElements,Ce as renderMatches,Z as useActionData,re as useAsyncError,te as useAsyncValue,ae as useBlocker,b as useHref,U as useInRouterContext,Q as useLoaderData,S as useLocation,P as useMatch,K as useMatches,N as useNavigate,q as useNavigation,D as useNavigationType,L as useOutlet,k as useOutletContext,A as useParams,O as useResolvedPath,G as useRevalidator,ee as useRouteError,X as useRouteLoaderData,F as useRoutes};
12//# sourceMappingURL=react-router.production.min.js.map