UNPKG

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