1 | /**
|
---|
2 | * React Router v6.26.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 | */
|
---|
11 | import * as React from 'react';
|
---|
12 | import { UNSAFE_invariant, joinPaths, matchPath, UNSAFE_decodePath, UNSAFE_getResolveToMatches, UNSAFE_warning, resolveTo, parsePath, matchRoutes, Action, UNSAFE_convertRouteMatchToUiMatch, stripBasename, IDLE_BLOCKER, isRouteErrorResponse, createMemoryHistory, AbortedDeferredError, createRouter } from '@remix-run/router';
|
---|
13 | export { AbortedDeferredError, Action as NavigationType, createPath, defer, generatePath, isRouteErrorResponse, json, matchPath, matchRoutes, parsePath, redirect, redirectDocument, replace, resolvePath } from '@remix-run/router';
|
---|
14 |
|
---|
15 | function _extends() {
|
---|
16 | _extends = Object.assign ? Object.assign.bind() : function (target) {
|
---|
17 | for (var i = 1; i < arguments.length; i++) {
|
---|
18 | var source = arguments[i];
|
---|
19 | for (var key in source) {
|
---|
20 | if (Object.prototype.hasOwnProperty.call(source, key)) {
|
---|
21 | target[key] = source[key];
|
---|
22 | }
|
---|
23 | }
|
---|
24 | }
|
---|
25 | return target;
|
---|
26 | };
|
---|
27 | return _extends.apply(this, arguments);
|
---|
28 | }
|
---|
29 |
|
---|
30 | // Create react-specific types from the agnostic types in @remix-run/router to
|
---|
31 | // export from react-router
|
---|
32 | const DataRouterContext = /*#__PURE__*/React.createContext(null);
|
---|
33 | if (process.env.NODE_ENV !== "production") {
|
---|
34 | DataRouterContext.displayName = "DataRouter";
|
---|
35 | }
|
---|
36 | const DataRouterStateContext = /*#__PURE__*/React.createContext(null);
|
---|
37 | if (process.env.NODE_ENV !== "production") {
|
---|
38 | DataRouterStateContext.displayName = "DataRouterState";
|
---|
39 | }
|
---|
40 | const AwaitContext = /*#__PURE__*/React.createContext(null);
|
---|
41 | if (process.env.NODE_ENV !== "production") {
|
---|
42 | AwaitContext.displayName = "Await";
|
---|
43 | }
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * A Navigator is a "location changer"; it's how you get to different locations.
|
---|
47 | *
|
---|
48 | * Every history instance conforms to the Navigator interface, but the
|
---|
49 | * distinction is useful primarily when it comes to the low-level `<Router>` API
|
---|
50 | * where both the location and a navigator must be provided separately in order
|
---|
51 | * to avoid "tearing" that may occur in a suspense-enabled app if the action
|
---|
52 | * and/or location were to be read directly from the history instance.
|
---|
53 | */
|
---|
54 |
|
---|
55 | const NavigationContext = /*#__PURE__*/React.createContext(null);
|
---|
56 | if (process.env.NODE_ENV !== "production") {
|
---|
57 | NavigationContext.displayName = "Navigation";
|
---|
58 | }
|
---|
59 | const LocationContext = /*#__PURE__*/React.createContext(null);
|
---|
60 | if (process.env.NODE_ENV !== "production") {
|
---|
61 | LocationContext.displayName = "Location";
|
---|
62 | }
|
---|
63 | const RouteContext = /*#__PURE__*/React.createContext({
|
---|
64 | outlet: null,
|
---|
65 | matches: [],
|
---|
66 | isDataRoute: false
|
---|
67 | });
|
---|
68 | if (process.env.NODE_ENV !== "production") {
|
---|
69 | RouteContext.displayName = "Route";
|
---|
70 | }
|
---|
71 | const RouteErrorContext = /*#__PURE__*/React.createContext(null);
|
---|
72 | if (process.env.NODE_ENV !== "production") {
|
---|
73 | RouteErrorContext.displayName = "RouteError";
|
---|
74 | }
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * Returns the full href for the given "to" value. This is useful for building
|
---|
78 | * custom links that are also accessible and preserve right-click behavior.
|
---|
79 | *
|
---|
80 | * @see https://reactrouter.com/hooks/use-href
|
---|
81 | */
|
---|
82 | function useHref(to, _temp) {
|
---|
83 | let {
|
---|
84 | relative
|
---|
85 | } = _temp === void 0 ? {} : _temp;
|
---|
86 | !useInRouterContext() ? process.env.NODE_ENV !== "production" ? UNSAFE_invariant(false, // TODO: This error is probably because they somehow have 2 versions of the
|
---|
87 | // router loaded. We can help them understand how to avoid that.
|
---|
88 | "useHref() may be used only in the context of a <Router> component.") : UNSAFE_invariant(false) : void 0;
|
---|
89 | let {
|
---|
90 | basename,
|
---|
91 | navigator
|
---|
92 | } = React.useContext(NavigationContext);
|
---|
93 | let {
|
---|
94 | hash,
|
---|
95 | pathname,
|
---|
96 | search
|
---|
97 | } = useResolvedPath(to, {
|
---|
98 | relative
|
---|
99 | });
|
---|
100 | let joinedPathname = pathname;
|
---|
101 |
|
---|
102 | // If we're operating within a basename, prepend it to the pathname prior
|
---|
103 | // to creating the href. If this is a root navigation, then just use the raw
|
---|
104 | // basename which allows the basename to have full control over the presence
|
---|
105 | // of a trailing slash on root links
|
---|
106 | if (basename !== "/") {
|
---|
107 | joinedPathname = pathname === "/" ? basename : joinPaths([basename, pathname]);
|
---|
108 | }
|
---|
109 | return navigator.createHref({
|
---|
110 | pathname: joinedPathname,
|
---|
111 | search,
|
---|
112 | hash
|
---|
113 | });
|
---|
114 | }
|
---|
115 |
|
---|
116 | /**
|
---|
117 | * Returns true if this component is a descendant of a `<Router>`.
|
---|
118 | *
|
---|
119 | * @see https://reactrouter.com/hooks/use-in-router-context
|
---|
120 | */
|
---|
121 | function useInRouterContext() {
|
---|
122 | return React.useContext(LocationContext) != null;
|
---|
123 | }
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * Returns the current location object, which represents the current URL in web
|
---|
127 | * browsers.
|
---|
128 | *
|
---|
129 | * Note: If you're using this it may mean you're doing some of your own
|
---|
130 | * "routing" in your app, and we'd like to know what your use case is. We may
|
---|
131 | * be able to provide something higher-level to better suit your needs.
|
---|
132 | *
|
---|
133 | * @see https://reactrouter.com/hooks/use-location
|
---|
134 | */
|
---|
135 | function useLocation() {
|
---|
136 | !useInRouterContext() ? process.env.NODE_ENV !== "production" ? UNSAFE_invariant(false, // TODO: This error is probably because they somehow have 2 versions of the
|
---|
137 | // router loaded. We can help them understand how to avoid that.
|
---|
138 | "useLocation() may be used only in the context of a <Router> component.") : UNSAFE_invariant(false) : void 0;
|
---|
139 | return React.useContext(LocationContext).location;
|
---|
140 | }
|
---|
141 |
|
---|
142 | /**
|
---|
143 | * Returns the current navigation action which describes how the router came to
|
---|
144 | * the current location, either by a pop, push, or replace on the history stack.
|
---|
145 | *
|
---|
146 | * @see https://reactrouter.com/hooks/use-navigation-type
|
---|
147 | */
|
---|
148 | function useNavigationType() {
|
---|
149 | return React.useContext(LocationContext).navigationType;
|
---|
150 | }
|
---|
151 |
|
---|
152 | /**
|
---|
153 | * Returns a PathMatch object if the given pattern matches the current URL.
|
---|
154 | * This is useful for components that need to know "active" state, e.g.
|
---|
155 | * `<NavLink>`.
|
---|
156 | *
|
---|
157 | * @see https://reactrouter.com/hooks/use-match
|
---|
158 | */
|
---|
159 | function useMatch(pattern) {
|
---|
160 | !useInRouterContext() ? process.env.NODE_ENV !== "production" ? UNSAFE_invariant(false, // TODO: This error is probably because they somehow have 2 versions of the
|
---|
161 | // router loaded. We can help them understand how to avoid that.
|
---|
162 | "useMatch() may be used only in the context of a <Router> component.") : UNSAFE_invariant(false) : void 0;
|
---|
163 | let {
|
---|
164 | pathname
|
---|
165 | } = useLocation();
|
---|
166 | return React.useMemo(() => matchPath(pattern, UNSAFE_decodePath(pathname)), [pathname, pattern]);
|
---|
167 | }
|
---|
168 |
|
---|
169 | /**
|
---|
170 | * The interface for the navigate() function returned from useNavigate().
|
---|
171 | */
|
---|
172 |
|
---|
173 | const navigateEffectWarning = "You should call navigate() in a React.useEffect(), not when " + "your component is first rendered.";
|
---|
174 |
|
---|
175 | // Mute warnings for calls to useNavigate in SSR environments
|
---|
176 | function useIsomorphicLayoutEffect(cb) {
|
---|
177 | let isStatic = React.useContext(NavigationContext).static;
|
---|
178 | if (!isStatic) {
|
---|
179 | // We should be able to get rid of this once react 18.3 is released
|
---|
180 | // See: https://github.com/facebook/react/pull/26395
|
---|
181 | // eslint-disable-next-line react-hooks/rules-of-hooks
|
---|
182 | React.useLayoutEffect(cb);
|
---|
183 | }
|
---|
184 | }
|
---|
185 |
|
---|
186 | /**
|
---|
187 | * Returns an imperative method for changing the location. Used by `<Link>`s, but
|
---|
188 | * may also be used by other elements to change the location.
|
---|
189 | *
|
---|
190 | * @see https://reactrouter.com/hooks/use-navigate
|
---|
191 | */
|
---|
192 | function useNavigate() {
|
---|
193 | let {
|
---|
194 | isDataRoute
|
---|
195 | } = React.useContext(RouteContext);
|
---|
196 | // Conditional usage is OK here because the usage of a data router is static
|
---|
197 | // eslint-disable-next-line react-hooks/rules-of-hooks
|
---|
198 | return isDataRoute ? useNavigateStable() : useNavigateUnstable();
|
---|
199 | }
|
---|
200 | function useNavigateUnstable() {
|
---|
201 | !useInRouterContext() ? process.env.NODE_ENV !== "production" ? UNSAFE_invariant(false, // TODO: This error is probably because they somehow have 2 versions of the
|
---|
202 | // router loaded. We can help them understand how to avoid that.
|
---|
203 | "useNavigate() may be used only in the context of a <Router> component.") : UNSAFE_invariant(false) : void 0;
|
---|
204 | let dataRouterContext = React.useContext(DataRouterContext);
|
---|
205 | let {
|
---|
206 | basename,
|
---|
207 | future,
|
---|
208 | navigator
|
---|
209 | } = React.useContext(NavigationContext);
|
---|
210 | let {
|
---|
211 | matches
|
---|
212 | } = React.useContext(RouteContext);
|
---|
213 | let {
|
---|
214 | pathname: locationPathname
|
---|
215 | } = useLocation();
|
---|
216 | let routePathnamesJson = JSON.stringify(UNSAFE_getResolveToMatches(matches, future.v7_relativeSplatPath));
|
---|
217 | let activeRef = React.useRef(false);
|
---|
218 | useIsomorphicLayoutEffect(() => {
|
---|
219 | activeRef.current = true;
|
---|
220 | });
|
---|
221 | let navigate = React.useCallback(function (to, options) {
|
---|
222 | if (options === void 0) {
|
---|
223 | options = {};
|
---|
224 | }
|
---|
225 | process.env.NODE_ENV !== "production" ? UNSAFE_warning(activeRef.current, navigateEffectWarning) : void 0;
|
---|
226 |
|
---|
227 | // Short circuit here since if this happens on first render the navigate
|
---|
228 | // is useless because we haven't wired up our history listener yet
|
---|
229 | if (!activeRef.current) return;
|
---|
230 | if (typeof to === "number") {
|
---|
231 | navigator.go(to);
|
---|
232 | return;
|
---|
233 | }
|
---|
234 | let path = resolveTo(to, JSON.parse(routePathnamesJson), locationPathname, options.relative === "path");
|
---|
235 |
|
---|
236 | // If we're operating within a basename, prepend it to the pathname prior
|
---|
237 | // to handing off to history (but only if we're not in a data router,
|
---|
238 | // otherwise it'll prepend the basename inside of the router).
|
---|
239 | // If this is a root navigation, then we navigate to the raw basename
|
---|
240 | // which allows the basename to have full control over the presence of a
|
---|
241 | // trailing slash on root links
|
---|
242 | if (dataRouterContext == null && basename !== "/") {
|
---|
243 | path.pathname = path.pathname === "/" ? basename : joinPaths([basename, path.pathname]);
|
---|
244 | }
|
---|
245 | (!!options.replace ? navigator.replace : navigator.push)(path, options.state, options);
|
---|
246 | }, [basename, navigator, routePathnamesJson, locationPathname, dataRouterContext]);
|
---|
247 | return navigate;
|
---|
248 | }
|
---|
249 | const OutletContext = /*#__PURE__*/React.createContext(null);
|
---|
250 |
|
---|
251 | /**
|
---|
252 | * Returns the context (if provided) for the child route at this level of the route
|
---|
253 | * hierarchy.
|
---|
254 | * @see https://reactrouter.com/hooks/use-outlet-context
|
---|
255 | */
|
---|
256 | function useOutletContext() {
|
---|
257 | return React.useContext(OutletContext);
|
---|
258 | }
|
---|
259 |
|
---|
260 | /**
|
---|
261 | * Returns the element for the child route at this level of the route
|
---|
262 | * hierarchy. Used internally by `<Outlet>` to render child routes.
|
---|
263 | *
|
---|
264 | * @see https://reactrouter.com/hooks/use-outlet
|
---|
265 | */
|
---|
266 | function useOutlet(context) {
|
---|
267 | let outlet = React.useContext(RouteContext).outlet;
|
---|
268 | if (outlet) {
|
---|
269 | return /*#__PURE__*/React.createElement(OutletContext.Provider, {
|
---|
270 | value: context
|
---|
271 | }, outlet);
|
---|
272 | }
|
---|
273 | return outlet;
|
---|
274 | }
|
---|
275 |
|
---|
276 | /**
|
---|
277 | * Returns an object of key/value pairs of the dynamic params from the current
|
---|
278 | * URL that were matched by the route path.
|
---|
279 | *
|
---|
280 | * @see https://reactrouter.com/hooks/use-params
|
---|
281 | */
|
---|
282 | function useParams() {
|
---|
283 | let {
|
---|
284 | matches
|
---|
285 | } = React.useContext(RouteContext);
|
---|
286 | let routeMatch = matches[matches.length - 1];
|
---|
287 | return routeMatch ? routeMatch.params : {};
|
---|
288 | }
|
---|
289 |
|
---|
290 | /**
|
---|
291 | * Resolves the pathname of the given `to` value against the current location.
|
---|
292 | *
|
---|
293 | * @see https://reactrouter.com/hooks/use-resolved-path
|
---|
294 | */
|
---|
295 | function useResolvedPath(to, _temp2) {
|
---|
296 | let {
|
---|
297 | relative
|
---|
298 | } = _temp2 === void 0 ? {} : _temp2;
|
---|
299 | let {
|
---|
300 | future
|
---|
301 | } = React.useContext(NavigationContext);
|
---|
302 | let {
|
---|
303 | matches
|
---|
304 | } = React.useContext(RouteContext);
|
---|
305 | let {
|
---|
306 | pathname: locationPathname
|
---|
307 | } = useLocation();
|
---|
308 | let routePathnamesJson = JSON.stringify(UNSAFE_getResolveToMatches(matches, future.v7_relativeSplatPath));
|
---|
309 | return React.useMemo(() => resolveTo(to, JSON.parse(routePathnamesJson), locationPathname, relative === "path"), [to, routePathnamesJson, locationPathname, relative]);
|
---|
310 | }
|
---|
311 |
|
---|
312 | /**
|
---|
313 | * Returns the element of the route that matched the current location, prepared
|
---|
314 | * with the correct context to render the remainder of the route tree. Route
|
---|
315 | * elements in the tree must render an `<Outlet>` to render their child route's
|
---|
316 | * element.
|
---|
317 | *
|
---|
318 | * @see https://reactrouter.com/hooks/use-routes
|
---|
319 | */
|
---|
320 | function useRoutes(routes, locationArg) {
|
---|
321 | return useRoutesImpl(routes, locationArg);
|
---|
322 | }
|
---|
323 |
|
---|
324 | // Internal implementation with accept optional param for RouterProvider usage
|
---|
325 | function useRoutesImpl(routes, locationArg, dataRouterState, future) {
|
---|
326 | !useInRouterContext() ? process.env.NODE_ENV !== "production" ? UNSAFE_invariant(false, // TODO: This error is probably because they somehow have 2 versions of the
|
---|
327 | // router loaded. We can help them understand how to avoid that.
|
---|
328 | "useRoutes() may be used only in the context of a <Router> component.") : UNSAFE_invariant(false) : void 0;
|
---|
329 | let {
|
---|
330 | navigator
|
---|
331 | } = React.useContext(NavigationContext);
|
---|
332 | let {
|
---|
333 | matches: parentMatches
|
---|
334 | } = React.useContext(RouteContext);
|
---|
335 | let routeMatch = parentMatches[parentMatches.length - 1];
|
---|
336 | let parentParams = routeMatch ? routeMatch.params : {};
|
---|
337 | let parentPathname = routeMatch ? routeMatch.pathname : "/";
|
---|
338 | let parentPathnameBase = routeMatch ? routeMatch.pathnameBase : "/";
|
---|
339 | let parentRoute = routeMatch && routeMatch.route;
|
---|
340 | if (process.env.NODE_ENV !== "production") {
|
---|
341 | // You won't get a warning about 2 different <Routes> under a <Route>
|
---|
342 | // without a trailing *, but this is a best-effort warning anyway since we
|
---|
343 | // cannot even give the warning unless they land at the parent route.
|
---|
344 | //
|
---|
345 | // Example:
|
---|
346 | //
|
---|
347 | // <Routes>
|
---|
348 | // {/* This route path MUST end with /* because otherwise
|
---|
349 | // it will never match /blog/post/123 */}
|
---|
350 | // <Route path="blog" element={<Blog />} />
|
---|
351 | // <Route path="blog/feed" element={<BlogFeed />} />
|
---|
352 | // </Routes>
|
---|
353 | //
|
---|
354 | // function Blog() {
|
---|
355 | // return (
|
---|
356 | // <Routes>
|
---|
357 | // <Route path="post/:id" element={<Post />} />
|
---|
358 | // </Routes>
|
---|
359 | // );
|
---|
360 | // }
|
---|
361 | let parentPath = parentRoute && parentRoute.path || "";
|
---|
362 | warningOnce(parentPathname, !parentRoute || parentPath.endsWith("*"), "You rendered descendant <Routes> (or called `useRoutes()`) at " + ("\"" + parentPathname + "\" (under <Route path=\"" + parentPath + "\">) but the ") + "parent route path has no trailing \"*\". This means if you navigate " + "deeper, the parent won't match anymore and therefore the child " + "routes will never render.\n\n" + ("Please change the parent <Route path=\"" + parentPath + "\"> to <Route ") + ("path=\"" + (parentPath === "/" ? "*" : parentPath + "/*") + "\">."));
|
---|
363 | }
|
---|
364 | let locationFromContext = useLocation();
|
---|
365 | let location;
|
---|
366 | if (locationArg) {
|
---|
367 | var _parsedLocationArg$pa;
|
---|
368 | let parsedLocationArg = typeof locationArg === "string" ? parsePath(locationArg) : locationArg;
|
---|
369 | !(parentPathnameBase === "/" || ((_parsedLocationArg$pa = parsedLocationArg.pathname) == null ? void 0 : _parsedLocationArg$pa.startsWith(parentPathnameBase))) ? process.env.NODE_ENV !== "production" ? UNSAFE_invariant(false, "When overriding the location using `<Routes location>` or `useRoutes(routes, location)`, " + "the location pathname must begin with the portion of the URL pathname that was " + ("matched by all parent routes. The current pathname base is \"" + parentPathnameBase + "\" ") + ("but pathname \"" + parsedLocationArg.pathname + "\" was given in the `location` prop.")) : UNSAFE_invariant(false) : void 0;
|
---|
370 | location = parsedLocationArg;
|
---|
371 | } else {
|
---|
372 | location = locationFromContext;
|
---|
373 | }
|
---|
374 | let pathname = location.pathname || "/";
|
---|
375 | let remainingPathname = pathname;
|
---|
376 | if (parentPathnameBase !== "/") {
|
---|
377 | // Determine the remaining pathname by removing the # of URL segments the
|
---|
378 | // parentPathnameBase has, instead of removing based on character count.
|
---|
379 | // This is because we can't guarantee that incoming/outgoing encodings/
|
---|
380 | // decodings will match exactly.
|
---|
381 | // We decode paths before matching on a per-segment basis with
|
---|
382 | // decodeURIComponent(), but we re-encode pathnames via `new URL()` so they
|
---|
383 | // match what `window.location.pathname` would reflect. Those don't 100%
|
---|
384 | // align when it comes to encoded URI characters such as % and &.
|
---|
385 | //
|
---|
386 | // So we may end up with:
|
---|
387 | // pathname: "/descendant/a%25b/match"
|
---|
388 | // parentPathnameBase: "/descendant/a%b"
|
---|
389 | //
|
---|
390 | // And the direct substring removal approach won't work :/
|
---|
391 | let parentSegments = parentPathnameBase.replace(/^\//, "").split("/");
|
---|
392 | let segments = pathname.replace(/^\//, "").split("/");
|
---|
393 | remainingPathname = "/" + segments.slice(parentSegments.length).join("/");
|
---|
394 | }
|
---|
395 | let matches = matchRoutes(routes, {
|
---|
396 | pathname: remainingPathname
|
---|
397 | });
|
---|
398 | if (process.env.NODE_ENV !== "production") {
|
---|
399 | process.env.NODE_ENV !== "production" ? UNSAFE_warning(parentRoute || matches != null, "No routes matched location \"" + location.pathname + location.search + location.hash + "\" ") : void 0;
|
---|
400 | process.env.NODE_ENV !== "production" ? UNSAFE_warning(matches == null || matches[matches.length - 1].route.element !== undefined || matches[matches.length - 1].route.Component !== undefined || matches[matches.length - 1].route.lazy !== undefined, "Matched leaf route at location \"" + location.pathname + location.search + location.hash + "\" " + "does not have an element or Component. This means it will render an <Outlet /> with a " + "null value by default resulting in an \"empty\" page.") : void 0;
|
---|
401 | }
|
---|
402 | let renderedMatches = _renderMatches(matches && matches.map(match => Object.assign({}, match, {
|
---|
403 | params: Object.assign({}, parentParams, match.params),
|
---|
404 | pathname: joinPaths([parentPathnameBase,
|
---|
405 | // Re-encode pathnames that were decoded inside matchRoutes
|
---|
406 | navigator.encodeLocation ? navigator.encodeLocation(match.pathname).pathname : match.pathname]),
|
---|
407 | pathnameBase: match.pathnameBase === "/" ? parentPathnameBase : joinPaths([parentPathnameBase,
|
---|
408 | // Re-encode pathnames that were decoded inside matchRoutes
|
---|
409 | navigator.encodeLocation ? navigator.encodeLocation(match.pathnameBase).pathname : match.pathnameBase])
|
---|
410 | })), parentMatches, dataRouterState, future);
|
---|
411 |
|
---|
412 | // When a user passes in a `locationArg`, the associated routes need to
|
---|
413 | // be wrapped in a new `LocationContext.Provider` in order for `useLocation`
|
---|
414 | // to use the scoped location instead of the global location.
|
---|
415 | if (locationArg && renderedMatches) {
|
---|
416 | return /*#__PURE__*/React.createElement(LocationContext.Provider, {
|
---|
417 | value: {
|
---|
418 | location: _extends({
|
---|
419 | pathname: "/",
|
---|
420 | search: "",
|
---|
421 | hash: "",
|
---|
422 | state: null,
|
---|
423 | key: "default"
|
---|
424 | }, location),
|
---|
425 | navigationType: Action.Pop
|
---|
426 | }
|
---|
427 | }, renderedMatches);
|
---|
428 | }
|
---|
429 | return renderedMatches;
|
---|
430 | }
|
---|
431 | function DefaultErrorComponent() {
|
---|
432 | let error = useRouteError();
|
---|
433 | let message = isRouteErrorResponse(error) ? error.status + " " + error.statusText : error instanceof Error ? error.message : JSON.stringify(error);
|
---|
434 | let stack = error instanceof Error ? error.stack : null;
|
---|
435 | let lightgrey = "rgba(200,200,200, 0.5)";
|
---|
436 | let preStyles = {
|
---|
437 | padding: "0.5rem",
|
---|
438 | backgroundColor: lightgrey
|
---|
439 | };
|
---|
440 | let codeStyles = {
|
---|
441 | padding: "2px 4px",
|
---|
442 | backgroundColor: lightgrey
|
---|
443 | };
|
---|
444 | let devInfo = null;
|
---|
445 | if (process.env.NODE_ENV !== "production") {
|
---|
446 | console.error("Error handled by React Router default ErrorBoundary:", error);
|
---|
447 | devInfo = /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("p", null, "\uD83D\uDCBF Hey developer \uD83D\uDC4B"), /*#__PURE__*/React.createElement("p", null, "You can provide a way better UX than this when your app throws errors by providing your own ", /*#__PURE__*/React.createElement("code", {
|
---|
448 | style: codeStyles
|
---|
449 | }, "ErrorBoundary"), " or", " ", /*#__PURE__*/React.createElement("code", {
|
---|
450 | style: codeStyles
|
---|
451 | }, "errorElement"), " prop on your route."));
|
---|
452 | }
|
---|
453 | return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("h2", null, "Unexpected Application Error!"), /*#__PURE__*/React.createElement("h3", {
|
---|
454 | style: {
|
---|
455 | fontStyle: "italic"
|
---|
456 | }
|
---|
457 | }, message), stack ? /*#__PURE__*/React.createElement("pre", {
|
---|
458 | style: preStyles
|
---|
459 | }, stack) : null, devInfo);
|
---|
460 | }
|
---|
461 | const defaultErrorElement = /*#__PURE__*/React.createElement(DefaultErrorComponent, null);
|
---|
462 | class RenderErrorBoundary extends React.Component {
|
---|
463 | constructor(props) {
|
---|
464 | super(props);
|
---|
465 | this.state = {
|
---|
466 | location: props.location,
|
---|
467 | revalidation: props.revalidation,
|
---|
468 | error: props.error
|
---|
469 | };
|
---|
470 | }
|
---|
471 | static getDerivedStateFromError(error) {
|
---|
472 | return {
|
---|
473 | error: error
|
---|
474 | };
|
---|
475 | }
|
---|
476 | static getDerivedStateFromProps(props, state) {
|
---|
477 | // When we get into an error state, the user will likely click "back" to the
|
---|
478 | // previous page that didn't have an error. Because this wraps the entire
|
---|
479 | // application, that will have no effect--the error page continues to display.
|
---|
480 | // This gives us a mechanism to recover from the error when the location changes.
|
---|
481 | //
|
---|
482 | // Whether we're in an error state or not, we update the location in state
|
---|
483 | // so that when we are in an error state, it gets reset when a new location
|
---|
484 | // comes in and the user recovers from the error.
|
---|
485 | if (state.location !== props.location || state.revalidation !== "idle" && props.revalidation === "idle") {
|
---|
486 | return {
|
---|
487 | error: props.error,
|
---|
488 | location: props.location,
|
---|
489 | revalidation: props.revalidation
|
---|
490 | };
|
---|
491 | }
|
---|
492 |
|
---|
493 | // If we're not changing locations, preserve the location but still surface
|
---|
494 | // any new errors that may come through. We retain the existing error, we do
|
---|
495 | // this because the error provided from the app state may be cleared without
|
---|
496 | // the location changing.
|
---|
497 | return {
|
---|
498 | error: props.error !== undefined ? props.error : state.error,
|
---|
499 | location: state.location,
|
---|
500 | revalidation: props.revalidation || state.revalidation
|
---|
501 | };
|
---|
502 | }
|
---|
503 | componentDidCatch(error, errorInfo) {
|
---|
504 | console.error("React Router caught the following error during render", error, errorInfo);
|
---|
505 | }
|
---|
506 | render() {
|
---|
507 | return this.state.error !== undefined ? /*#__PURE__*/React.createElement(RouteContext.Provider, {
|
---|
508 | value: this.props.routeContext
|
---|
509 | }, /*#__PURE__*/React.createElement(RouteErrorContext.Provider, {
|
---|
510 | value: this.state.error,
|
---|
511 | children: this.props.component
|
---|
512 | })) : this.props.children;
|
---|
513 | }
|
---|
514 | }
|
---|
515 | function RenderedRoute(_ref) {
|
---|
516 | let {
|
---|
517 | routeContext,
|
---|
518 | match,
|
---|
519 | children
|
---|
520 | } = _ref;
|
---|
521 | let dataRouterContext = React.useContext(DataRouterContext);
|
---|
522 |
|
---|
523 | // Track how deep we got in our render pass to emulate SSR componentDidCatch
|
---|
524 | // in a DataStaticRouter
|
---|
525 | if (dataRouterContext && dataRouterContext.static && dataRouterContext.staticContext && (match.route.errorElement || match.route.ErrorBoundary)) {
|
---|
526 | dataRouterContext.staticContext._deepestRenderedBoundaryId = match.route.id;
|
---|
527 | }
|
---|
528 | return /*#__PURE__*/React.createElement(RouteContext.Provider, {
|
---|
529 | value: routeContext
|
---|
530 | }, children);
|
---|
531 | }
|
---|
532 | function _renderMatches(matches, parentMatches, dataRouterState, future) {
|
---|
533 | var _dataRouterState;
|
---|
534 | if (parentMatches === void 0) {
|
---|
535 | parentMatches = [];
|
---|
536 | }
|
---|
537 | if (dataRouterState === void 0) {
|
---|
538 | dataRouterState = null;
|
---|
539 | }
|
---|
540 | if (future === void 0) {
|
---|
541 | future = null;
|
---|
542 | }
|
---|
543 | if (matches == null) {
|
---|
544 | var _future;
|
---|
545 | if (!dataRouterState) {
|
---|
546 | return null;
|
---|
547 | }
|
---|
548 | if (dataRouterState.errors) {
|
---|
549 | // Don't bail if we have data router errors so we can render them in the
|
---|
550 | // boundary. Use the pre-matched (or shimmed) matches
|
---|
551 | matches = dataRouterState.matches;
|
---|
552 | } else if ((_future = future) != null && _future.v7_partialHydration && parentMatches.length === 0 && !dataRouterState.initialized && dataRouterState.matches.length > 0) {
|
---|
553 | // Don't bail if we're initializing with partial hydration and we have
|
---|
554 | // router matches. That means we're actively running `patchRoutesOnMiss`
|
---|
555 | // so we should render down the partial matches to the appropriate
|
---|
556 | // `HydrateFallback`. We only do this if `parentMatches` is empty so it
|
---|
557 | // only impacts the root matches for `RouterProvider` and no descendant
|
---|
558 | // `<Routes>`
|
---|
559 | matches = dataRouterState.matches;
|
---|
560 | } else {
|
---|
561 | return null;
|
---|
562 | }
|
---|
563 | }
|
---|
564 | let renderedMatches = matches;
|
---|
565 |
|
---|
566 | // If we have data errors, trim matches to the highest error boundary
|
---|
567 | let errors = (_dataRouterState = dataRouterState) == null ? void 0 : _dataRouterState.errors;
|
---|
568 | if (errors != null) {
|
---|
569 | let errorIndex = renderedMatches.findIndex(m => m.route.id && (errors == null ? void 0 : errors[m.route.id]) !== undefined);
|
---|
570 | !(errorIndex >= 0) ? process.env.NODE_ENV !== "production" ? UNSAFE_invariant(false, "Could not find a matching route for errors on route IDs: " + Object.keys(errors).join(",")) : UNSAFE_invariant(false) : void 0;
|
---|
571 | renderedMatches = renderedMatches.slice(0, Math.min(renderedMatches.length, errorIndex + 1));
|
---|
572 | }
|
---|
573 |
|
---|
574 | // If we're in a partial hydration mode, detect if we need to render down to
|
---|
575 | // a given HydrateFallback while we load the rest of the hydration data
|
---|
576 | let renderFallback = false;
|
---|
577 | let fallbackIndex = -1;
|
---|
578 | if (dataRouterState && future && future.v7_partialHydration) {
|
---|
579 | for (let i = 0; i < renderedMatches.length; i++) {
|
---|
580 | let match = renderedMatches[i];
|
---|
581 | // Track the deepest fallback up until the first route without data
|
---|
582 | if (match.route.HydrateFallback || match.route.hydrateFallbackElement) {
|
---|
583 | fallbackIndex = i;
|
---|
584 | }
|
---|
585 | if (match.route.id) {
|
---|
586 | let {
|
---|
587 | loaderData,
|
---|
588 | errors
|
---|
589 | } = dataRouterState;
|
---|
590 | let needsToRunLoader = match.route.loader && loaderData[match.route.id] === undefined && (!errors || errors[match.route.id] === undefined);
|
---|
591 | if (match.route.lazy || needsToRunLoader) {
|
---|
592 | // We found the first route that's not ready to render (waiting on
|
---|
593 | // lazy, or has a loader that hasn't run yet). Flag that we need to
|
---|
594 | // render a fallback and render up until the appropriate fallback
|
---|
595 | renderFallback = true;
|
---|
596 | if (fallbackIndex >= 0) {
|
---|
597 | renderedMatches = renderedMatches.slice(0, fallbackIndex + 1);
|
---|
598 | } else {
|
---|
599 | renderedMatches = [renderedMatches[0]];
|
---|
600 | }
|
---|
601 | break;
|
---|
602 | }
|
---|
603 | }
|
---|
604 | }
|
---|
605 | }
|
---|
606 | return renderedMatches.reduceRight((outlet, match, index) => {
|
---|
607 | // Only data routers handle errors/fallbacks
|
---|
608 | let error;
|
---|
609 | let shouldRenderHydrateFallback = false;
|
---|
610 | let errorElement = null;
|
---|
611 | let hydrateFallbackElement = null;
|
---|
612 | if (dataRouterState) {
|
---|
613 | error = errors && match.route.id ? errors[match.route.id] : undefined;
|
---|
614 | errorElement = match.route.errorElement || defaultErrorElement;
|
---|
615 | if (renderFallback) {
|
---|
616 | if (fallbackIndex < 0 && index === 0) {
|
---|
617 | warningOnce("route-fallback", false, "No `HydrateFallback` element provided to render during initial hydration");
|
---|
618 | shouldRenderHydrateFallback = true;
|
---|
619 | hydrateFallbackElement = null;
|
---|
620 | } else if (fallbackIndex === index) {
|
---|
621 | shouldRenderHydrateFallback = true;
|
---|
622 | hydrateFallbackElement = match.route.hydrateFallbackElement || null;
|
---|
623 | }
|
---|
624 | }
|
---|
625 | }
|
---|
626 | let matches = parentMatches.concat(renderedMatches.slice(0, index + 1));
|
---|
627 | let getChildren = () => {
|
---|
628 | let children;
|
---|
629 | if (error) {
|
---|
630 | children = errorElement;
|
---|
631 | } else if (shouldRenderHydrateFallback) {
|
---|
632 | children = hydrateFallbackElement;
|
---|
633 | } else if (match.route.Component) {
|
---|
634 | // Note: This is a de-optimized path since React won't re-use the
|
---|
635 | // ReactElement since it's identity changes with each new
|
---|
636 | // React.createElement call. We keep this so folks can use
|
---|
637 | // `<Route Component={...}>` in `<Routes>` but generally `Component`
|
---|
638 | // usage is only advised in `RouterProvider` when we can convert it to
|
---|
639 | // `element` ahead of time.
|
---|
640 | children = /*#__PURE__*/React.createElement(match.route.Component, null);
|
---|
641 | } else if (match.route.element) {
|
---|
642 | children = match.route.element;
|
---|
643 | } else {
|
---|
644 | children = outlet;
|
---|
645 | }
|
---|
646 | return /*#__PURE__*/React.createElement(RenderedRoute, {
|
---|
647 | match: match,
|
---|
648 | routeContext: {
|
---|
649 | outlet,
|
---|
650 | matches,
|
---|
651 | isDataRoute: dataRouterState != null
|
---|
652 | },
|
---|
653 | children: children
|
---|
654 | });
|
---|
655 | };
|
---|
656 | // Only wrap in an error boundary within data router usages when we have an
|
---|
657 | // ErrorBoundary/errorElement on this route. Otherwise let it bubble up to
|
---|
658 | // an ancestor ErrorBoundary/errorElement
|
---|
659 | return dataRouterState && (match.route.ErrorBoundary || match.route.errorElement || index === 0) ? /*#__PURE__*/React.createElement(RenderErrorBoundary, {
|
---|
660 | location: dataRouterState.location,
|
---|
661 | revalidation: dataRouterState.revalidation,
|
---|
662 | component: errorElement,
|
---|
663 | error: error,
|
---|
664 | children: getChildren(),
|
---|
665 | routeContext: {
|
---|
666 | outlet: null,
|
---|
667 | matches,
|
---|
668 | isDataRoute: true
|
---|
669 | }
|
---|
670 | }) : getChildren();
|
---|
671 | }, null);
|
---|
672 | }
|
---|
673 | var DataRouterHook = /*#__PURE__*/function (DataRouterHook) {
|
---|
674 | DataRouterHook["UseBlocker"] = "useBlocker";
|
---|
675 | DataRouterHook["UseRevalidator"] = "useRevalidator";
|
---|
676 | DataRouterHook["UseNavigateStable"] = "useNavigate";
|
---|
677 | return DataRouterHook;
|
---|
678 | }(DataRouterHook || {});
|
---|
679 | var DataRouterStateHook = /*#__PURE__*/function (DataRouterStateHook) {
|
---|
680 | DataRouterStateHook["UseBlocker"] = "useBlocker";
|
---|
681 | DataRouterStateHook["UseLoaderData"] = "useLoaderData";
|
---|
682 | DataRouterStateHook["UseActionData"] = "useActionData";
|
---|
683 | DataRouterStateHook["UseRouteError"] = "useRouteError";
|
---|
684 | DataRouterStateHook["UseNavigation"] = "useNavigation";
|
---|
685 | DataRouterStateHook["UseRouteLoaderData"] = "useRouteLoaderData";
|
---|
686 | DataRouterStateHook["UseMatches"] = "useMatches";
|
---|
687 | DataRouterStateHook["UseRevalidator"] = "useRevalidator";
|
---|
688 | DataRouterStateHook["UseNavigateStable"] = "useNavigate";
|
---|
689 | DataRouterStateHook["UseRouteId"] = "useRouteId";
|
---|
690 | return DataRouterStateHook;
|
---|
691 | }(DataRouterStateHook || {});
|
---|
692 | function getDataRouterConsoleError(hookName) {
|
---|
693 | return hookName + " must be used within a data router. See https://reactrouter.com/routers/picking-a-router.";
|
---|
694 | }
|
---|
695 | function useDataRouterContext(hookName) {
|
---|
696 | let ctx = React.useContext(DataRouterContext);
|
---|
697 | !ctx ? process.env.NODE_ENV !== "production" ? UNSAFE_invariant(false, getDataRouterConsoleError(hookName)) : UNSAFE_invariant(false) : void 0;
|
---|
698 | return ctx;
|
---|
699 | }
|
---|
700 | function useDataRouterState(hookName) {
|
---|
701 | let state = React.useContext(DataRouterStateContext);
|
---|
702 | !state ? process.env.NODE_ENV !== "production" ? UNSAFE_invariant(false, getDataRouterConsoleError(hookName)) : UNSAFE_invariant(false) : void 0;
|
---|
703 | return state;
|
---|
704 | }
|
---|
705 | function useRouteContext(hookName) {
|
---|
706 | let route = React.useContext(RouteContext);
|
---|
707 | !route ? process.env.NODE_ENV !== "production" ? UNSAFE_invariant(false, getDataRouterConsoleError(hookName)) : UNSAFE_invariant(false) : void 0;
|
---|
708 | return route;
|
---|
709 | }
|
---|
710 |
|
---|
711 | // Internal version with hookName-aware debugging
|
---|
712 | function useCurrentRouteId(hookName) {
|
---|
713 | let route = useRouteContext(hookName);
|
---|
714 | let thisRoute = route.matches[route.matches.length - 1];
|
---|
715 | !thisRoute.route.id ? process.env.NODE_ENV !== "production" ? UNSAFE_invariant(false, hookName + " can only be used on routes that contain a unique \"id\"") : UNSAFE_invariant(false) : void 0;
|
---|
716 | return thisRoute.route.id;
|
---|
717 | }
|
---|
718 |
|
---|
719 | /**
|
---|
720 | * Returns the ID for the nearest contextual route
|
---|
721 | */
|
---|
722 | function useRouteId() {
|
---|
723 | return useCurrentRouteId(DataRouterStateHook.UseRouteId);
|
---|
724 | }
|
---|
725 |
|
---|
726 | /**
|
---|
727 | * Returns the current navigation, defaulting to an "idle" navigation when
|
---|
728 | * no navigation is in progress
|
---|
729 | */
|
---|
730 | function useNavigation() {
|
---|
731 | let state = useDataRouterState(DataRouterStateHook.UseNavigation);
|
---|
732 | return state.navigation;
|
---|
733 | }
|
---|
734 |
|
---|
735 | /**
|
---|
736 | * Returns a revalidate function for manually triggering revalidation, as well
|
---|
737 | * as the current state of any manual revalidations
|
---|
738 | */
|
---|
739 | function useRevalidator() {
|
---|
740 | let dataRouterContext = useDataRouterContext(DataRouterHook.UseRevalidator);
|
---|
741 | let state = useDataRouterState(DataRouterStateHook.UseRevalidator);
|
---|
742 | return React.useMemo(() => ({
|
---|
743 | revalidate: dataRouterContext.router.revalidate,
|
---|
744 | state: state.revalidation
|
---|
745 | }), [dataRouterContext.router.revalidate, state.revalidation]);
|
---|
746 | }
|
---|
747 |
|
---|
748 | /**
|
---|
749 | * Returns the active route matches, useful for accessing loaderData for
|
---|
750 | * parent/child routes or the route "handle" property
|
---|
751 | */
|
---|
752 | function useMatches() {
|
---|
753 | let {
|
---|
754 | matches,
|
---|
755 | loaderData
|
---|
756 | } = useDataRouterState(DataRouterStateHook.UseMatches);
|
---|
757 | return React.useMemo(() => matches.map(m => UNSAFE_convertRouteMatchToUiMatch(m, loaderData)), [matches, loaderData]);
|
---|
758 | }
|
---|
759 |
|
---|
760 | /**
|
---|
761 | * Returns the loader data for the nearest ancestor Route loader
|
---|
762 | */
|
---|
763 | function useLoaderData() {
|
---|
764 | let state = useDataRouterState(DataRouterStateHook.UseLoaderData);
|
---|
765 | let routeId = useCurrentRouteId(DataRouterStateHook.UseLoaderData);
|
---|
766 | if (state.errors && state.errors[routeId] != null) {
|
---|
767 | console.error("You cannot `useLoaderData` in an errorElement (routeId: " + routeId + ")");
|
---|
768 | return undefined;
|
---|
769 | }
|
---|
770 | return state.loaderData[routeId];
|
---|
771 | }
|
---|
772 |
|
---|
773 | /**
|
---|
774 | * Returns the loaderData for the given routeId
|
---|
775 | */
|
---|
776 | function useRouteLoaderData(routeId) {
|
---|
777 | let state = useDataRouterState(DataRouterStateHook.UseRouteLoaderData);
|
---|
778 | return state.loaderData[routeId];
|
---|
779 | }
|
---|
780 |
|
---|
781 | /**
|
---|
782 | * Returns the action data for the nearest ancestor Route action
|
---|
783 | */
|
---|
784 | function useActionData() {
|
---|
785 | let state = useDataRouterState(DataRouterStateHook.UseActionData);
|
---|
786 | let routeId = useCurrentRouteId(DataRouterStateHook.UseLoaderData);
|
---|
787 | return state.actionData ? state.actionData[routeId] : undefined;
|
---|
788 | }
|
---|
789 |
|
---|
790 | /**
|
---|
791 | * Returns the nearest ancestor Route error, which could be a loader/action
|
---|
792 | * error or a render error. This is intended to be called from your
|
---|
793 | * ErrorBoundary/errorElement to display a proper error message.
|
---|
794 | */
|
---|
795 | function useRouteError() {
|
---|
796 | var _state$errors;
|
---|
797 | let error = React.useContext(RouteErrorContext);
|
---|
798 | let state = useDataRouterState(DataRouterStateHook.UseRouteError);
|
---|
799 | let routeId = useCurrentRouteId(DataRouterStateHook.UseRouteError);
|
---|
800 |
|
---|
801 | // If this was a render error, we put it in a RouteError context inside
|
---|
802 | // of RenderErrorBoundary
|
---|
803 | if (error !== undefined) {
|
---|
804 | return error;
|
---|
805 | }
|
---|
806 |
|
---|
807 | // Otherwise look for errors from our data router state
|
---|
808 | return (_state$errors = state.errors) == null ? void 0 : _state$errors[routeId];
|
---|
809 | }
|
---|
810 |
|
---|
811 | /**
|
---|
812 | * Returns the happy-path data from the nearest ancestor `<Await />` value
|
---|
813 | */
|
---|
814 | function useAsyncValue() {
|
---|
815 | let value = React.useContext(AwaitContext);
|
---|
816 | return value == null ? void 0 : value._data;
|
---|
817 | }
|
---|
818 |
|
---|
819 | /**
|
---|
820 | * Returns the error from the nearest ancestor `<Await />` value
|
---|
821 | */
|
---|
822 | function useAsyncError() {
|
---|
823 | let value = React.useContext(AwaitContext);
|
---|
824 | return value == null ? void 0 : value._error;
|
---|
825 | }
|
---|
826 | let blockerId = 0;
|
---|
827 |
|
---|
828 | /**
|
---|
829 | * Allow the application to block navigations within the SPA and present the
|
---|
830 | * user a confirmation dialog to confirm the navigation. Mostly used to avoid
|
---|
831 | * using half-filled form data. This does not handle hard-reloads or
|
---|
832 | * cross-origin navigations.
|
---|
833 | */
|
---|
834 | function useBlocker(shouldBlock) {
|
---|
835 | let {
|
---|
836 | router,
|
---|
837 | basename
|
---|
838 | } = useDataRouterContext(DataRouterHook.UseBlocker);
|
---|
839 | let state = useDataRouterState(DataRouterStateHook.UseBlocker);
|
---|
840 | let [blockerKey, setBlockerKey] = React.useState("");
|
---|
841 | let blockerFunction = React.useCallback(arg => {
|
---|
842 | if (typeof shouldBlock !== "function") {
|
---|
843 | return !!shouldBlock;
|
---|
844 | }
|
---|
845 | if (basename === "/") {
|
---|
846 | return shouldBlock(arg);
|
---|
847 | }
|
---|
848 |
|
---|
849 | // If they provided us a function and we've got an active basename, strip
|
---|
850 | // it from the locations we expose to the user to match the behavior of
|
---|
851 | // useLocation
|
---|
852 | let {
|
---|
853 | currentLocation,
|
---|
854 | nextLocation,
|
---|
855 | historyAction
|
---|
856 | } = arg;
|
---|
857 | return shouldBlock({
|
---|
858 | currentLocation: _extends({}, currentLocation, {
|
---|
859 | pathname: stripBasename(currentLocation.pathname, basename) || currentLocation.pathname
|
---|
860 | }),
|
---|
861 | nextLocation: _extends({}, nextLocation, {
|
---|
862 | pathname: stripBasename(nextLocation.pathname, basename) || nextLocation.pathname
|
---|
863 | }),
|
---|
864 | historyAction
|
---|
865 | });
|
---|
866 | }, [basename, shouldBlock]);
|
---|
867 |
|
---|
868 | // This effect is in charge of blocker key assignment and deletion (which is
|
---|
869 | // tightly coupled to the key)
|
---|
870 | React.useEffect(() => {
|
---|
871 | let key = String(++blockerId);
|
---|
872 | setBlockerKey(key);
|
---|
873 | return () => router.deleteBlocker(key);
|
---|
874 | }, [router]);
|
---|
875 |
|
---|
876 | // This effect handles assigning the blockerFunction. This is to handle
|
---|
877 | // unstable blocker function identities, and happens only after the prior
|
---|
878 | // effect so we don't get an orphaned blockerFunction in the router with a
|
---|
879 | // key of "". Until then we just have the IDLE_BLOCKER.
|
---|
880 | React.useEffect(() => {
|
---|
881 | if (blockerKey !== "") {
|
---|
882 | router.getBlocker(blockerKey, blockerFunction);
|
---|
883 | }
|
---|
884 | }, [router, blockerKey, blockerFunction]);
|
---|
885 |
|
---|
886 | // Prefer the blocker from `state` not `router.state` since DataRouterContext
|
---|
887 | // is memoized so this ensures we update on blocker state updates
|
---|
888 | return blockerKey && state.blockers.has(blockerKey) ? state.blockers.get(blockerKey) : IDLE_BLOCKER;
|
---|
889 | }
|
---|
890 |
|
---|
891 | /**
|
---|
892 | * Stable version of useNavigate that is used when we are in the context of
|
---|
893 | * a RouterProvider.
|
---|
894 | */
|
---|
895 | function useNavigateStable() {
|
---|
896 | let {
|
---|
897 | router
|
---|
898 | } = useDataRouterContext(DataRouterHook.UseNavigateStable);
|
---|
899 | let id = useCurrentRouteId(DataRouterStateHook.UseNavigateStable);
|
---|
900 | let activeRef = React.useRef(false);
|
---|
901 | useIsomorphicLayoutEffect(() => {
|
---|
902 | activeRef.current = true;
|
---|
903 | });
|
---|
904 | let navigate = React.useCallback(function (to, options) {
|
---|
905 | if (options === void 0) {
|
---|
906 | options = {};
|
---|
907 | }
|
---|
908 | process.env.NODE_ENV !== "production" ? UNSAFE_warning(activeRef.current, navigateEffectWarning) : void 0;
|
---|
909 |
|
---|
910 | // Short circuit here since if this happens on first render the navigate
|
---|
911 | // is useless because we haven't wired up our router subscriber yet
|
---|
912 | if (!activeRef.current) return;
|
---|
913 | if (typeof to === "number") {
|
---|
914 | router.navigate(to);
|
---|
915 | } else {
|
---|
916 | router.navigate(to, _extends({
|
---|
917 | fromRouteId: id
|
---|
918 | }, options));
|
---|
919 | }
|
---|
920 | }, [router, id]);
|
---|
921 | return navigate;
|
---|
922 | }
|
---|
923 | const alreadyWarned = {};
|
---|
924 | function warningOnce(key, cond, message) {
|
---|
925 | if (!cond && !alreadyWarned[key]) {
|
---|
926 | alreadyWarned[key] = true;
|
---|
927 | process.env.NODE_ENV !== "production" ? UNSAFE_warning(false, message) : void 0;
|
---|
928 | }
|
---|
929 | }
|
---|
930 |
|
---|
931 | /**
|
---|
932 | Webpack + React 17 fails to compile on any of the following because webpack
|
---|
933 | complains that `startTransition` doesn't exist in `React`:
|
---|
934 | * import { startTransition } from "react"
|
---|
935 | * import * as React from from "react";
|
---|
936 | "startTransition" in React ? React.startTransition(() => setState()) : setState()
|
---|
937 | * import * as React from from "react";
|
---|
938 | "startTransition" in React ? React["startTransition"](() => setState()) : setState()
|
---|
939 |
|
---|
940 | Moving it to a constant such as the following solves the Webpack/React 17 issue:
|
---|
941 | * import * as React from from "react";
|
---|
942 | const START_TRANSITION = "startTransition";
|
---|
943 | START_TRANSITION in React ? React[START_TRANSITION](() => setState()) : setState()
|
---|
944 |
|
---|
945 | However, that introduces webpack/terser minification issues in production builds
|
---|
946 | in React 18 where minification/obfuscation ends up removing the call of
|
---|
947 | React.startTransition entirely from the first half of the ternary. Grabbing
|
---|
948 | this exported reference once up front resolves that issue.
|
---|
949 |
|
---|
950 | See https://github.com/remix-run/react-router/issues/10579
|
---|
951 | */
|
---|
952 | const START_TRANSITION = "startTransition";
|
---|
953 | const startTransitionImpl = React[START_TRANSITION];
|
---|
954 |
|
---|
955 | /**
|
---|
956 | * Given a Remix Router instance, render the appropriate UI
|
---|
957 | */
|
---|
958 | function RouterProvider(_ref) {
|
---|
959 | let {
|
---|
960 | fallbackElement,
|
---|
961 | router,
|
---|
962 | future
|
---|
963 | } = _ref;
|
---|
964 | let [state, setStateImpl] = React.useState(router.state);
|
---|
965 | let {
|
---|
966 | v7_startTransition
|
---|
967 | } = future || {};
|
---|
968 | let setState = React.useCallback(newState => {
|
---|
969 | if (v7_startTransition && startTransitionImpl) {
|
---|
970 | startTransitionImpl(() => setStateImpl(newState));
|
---|
971 | } else {
|
---|
972 | setStateImpl(newState);
|
---|
973 | }
|
---|
974 | }, [setStateImpl, v7_startTransition]);
|
---|
975 |
|
---|
976 | // Need to use a layout effect here so we are subscribed early enough to
|
---|
977 | // pick up on any render-driven redirects/navigations (useEffect/<Navigate>)
|
---|
978 | React.useLayoutEffect(() => router.subscribe(setState), [router, setState]);
|
---|
979 | React.useEffect(() => {
|
---|
980 | process.env.NODE_ENV !== "production" ? UNSAFE_warning(fallbackElement == null || !router.future.v7_partialHydration, "`<RouterProvider fallbackElement>` is deprecated when using " + "`v7_partialHydration`, use a `HydrateFallback` component instead") : void 0;
|
---|
981 | // Only log this once on initial mount
|
---|
982 | // eslint-disable-next-line react-hooks/exhaustive-deps
|
---|
983 | }, []);
|
---|
984 | let navigator = React.useMemo(() => {
|
---|
985 | return {
|
---|
986 | createHref: router.createHref,
|
---|
987 | encodeLocation: router.encodeLocation,
|
---|
988 | go: n => router.navigate(n),
|
---|
989 | push: (to, state, opts) => router.navigate(to, {
|
---|
990 | state,
|
---|
991 | preventScrollReset: opts == null ? void 0 : opts.preventScrollReset
|
---|
992 | }),
|
---|
993 | replace: (to, state, opts) => router.navigate(to, {
|
---|
994 | replace: true,
|
---|
995 | state,
|
---|
996 | preventScrollReset: opts == null ? void 0 : opts.preventScrollReset
|
---|
997 | })
|
---|
998 | };
|
---|
999 | }, [router]);
|
---|
1000 | let basename = router.basename || "/";
|
---|
1001 | let dataRouterContext = React.useMemo(() => ({
|
---|
1002 | router,
|
---|
1003 | navigator,
|
---|
1004 | static: false,
|
---|
1005 | basename
|
---|
1006 | }), [router, navigator, basename]);
|
---|
1007 |
|
---|
1008 | // The fragment and {null} here are important! We need them to keep React 18's
|
---|
1009 | // useId happy when we are server-rendering since we may have a <script> here
|
---|
1010 | // containing the hydrated server-side staticContext (from StaticRouterProvider).
|
---|
1011 | // useId relies on the component tree structure to generate deterministic id's
|
---|
1012 | // so we need to ensure it remains the same on the client even though
|
---|
1013 | // we don't need the <script> tag
|
---|
1014 | return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(DataRouterContext.Provider, {
|
---|
1015 | value: dataRouterContext
|
---|
1016 | }, /*#__PURE__*/React.createElement(DataRouterStateContext.Provider, {
|
---|
1017 | value: state
|
---|
1018 | }, /*#__PURE__*/React.createElement(Router, {
|
---|
1019 | basename: basename,
|
---|
1020 | location: state.location,
|
---|
1021 | navigationType: state.historyAction,
|
---|
1022 | navigator: navigator,
|
---|
1023 | future: {
|
---|
1024 | v7_relativeSplatPath: router.future.v7_relativeSplatPath
|
---|
1025 | }
|
---|
1026 | }, state.initialized || router.future.v7_partialHydration ? /*#__PURE__*/React.createElement(DataRoutes, {
|
---|
1027 | routes: router.routes,
|
---|
1028 | future: router.future,
|
---|
1029 | state: state
|
---|
1030 | }) : fallbackElement))), null);
|
---|
1031 | }
|
---|
1032 | function DataRoutes(_ref2) {
|
---|
1033 | let {
|
---|
1034 | routes,
|
---|
1035 | future,
|
---|
1036 | state
|
---|
1037 | } = _ref2;
|
---|
1038 | return useRoutesImpl(routes, undefined, state, future);
|
---|
1039 | }
|
---|
1040 | /**
|
---|
1041 | * A `<Router>` that stores all entries in memory.
|
---|
1042 | *
|
---|
1043 | * @see https://reactrouter.com/router-components/memory-router
|
---|
1044 | */
|
---|
1045 | function MemoryRouter(_ref3) {
|
---|
1046 | let {
|
---|
1047 | basename,
|
---|
1048 | children,
|
---|
1049 | initialEntries,
|
---|
1050 | initialIndex,
|
---|
1051 | future
|
---|
1052 | } = _ref3;
|
---|
1053 | let historyRef = React.useRef();
|
---|
1054 | if (historyRef.current == null) {
|
---|
1055 | historyRef.current = createMemoryHistory({
|
---|
1056 | initialEntries,
|
---|
1057 | initialIndex,
|
---|
1058 | v5Compat: true
|
---|
1059 | });
|
---|
1060 | }
|
---|
1061 | let history = historyRef.current;
|
---|
1062 | let [state, setStateImpl] = React.useState({
|
---|
1063 | action: history.action,
|
---|
1064 | location: history.location
|
---|
1065 | });
|
---|
1066 | let {
|
---|
1067 | v7_startTransition
|
---|
1068 | } = future || {};
|
---|
1069 | let setState = React.useCallback(newState => {
|
---|
1070 | v7_startTransition && startTransitionImpl ? startTransitionImpl(() => setStateImpl(newState)) : setStateImpl(newState);
|
---|
1071 | }, [setStateImpl, v7_startTransition]);
|
---|
1072 | React.useLayoutEffect(() => history.listen(setState), [history, setState]);
|
---|
1073 | return /*#__PURE__*/React.createElement(Router, {
|
---|
1074 | basename: basename,
|
---|
1075 | children: children,
|
---|
1076 | location: state.location,
|
---|
1077 | navigationType: state.action,
|
---|
1078 | navigator: history,
|
---|
1079 | future: future
|
---|
1080 | });
|
---|
1081 | }
|
---|
1082 | /**
|
---|
1083 | * Changes the current location.
|
---|
1084 | *
|
---|
1085 | * Note: This API is mostly useful in React.Component subclasses that are not
|
---|
1086 | * able to use hooks. In functional components, we recommend you use the
|
---|
1087 | * `useNavigate` hook instead.
|
---|
1088 | *
|
---|
1089 | * @see https://reactrouter.com/components/navigate
|
---|
1090 | */
|
---|
1091 | function Navigate(_ref4) {
|
---|
1092 | let {
|
---|
1093 | to,
|
---|
1094 | replace,
|
---|
1095 | state,
|
---|
1096 | relative
|
---|
1097 | } = _ref4;
|
---|
1098 | !useInRouterContext() ? process.env.NODE_ENV !== "production" ? UNSAFE_invariant(false, // TODO: This error is probably because they somehow have 2 versions of
|
---|
1099 | // the router loaded. We can help them understand how to avoid that.
|
---|
1100 | "<Navigate> may be used only in the context of a <Router> component.") : UNSAFE_invariant(false) : void 0;
|
---|
1101 | let {
|
---|
1102 | future,
|
---|
1103 | static: isStatic
|
---|
1104 | } = React.useContext(NavigationContext);
|
---|
1105 | process.env.NODE_ENV !== "production" ? UNSAFE_warning(!isStatic, "<Navigate> must not be used on the initial render in a <StaticRouter>. " + "This is a no-op, but you should modify your code so the <Navigate> is " + "only ever rendered in response to some user interaction or state change.") : void 0;
|
---|
1106 | let {
|
---|
1107 | matches
|
---|
1108 | } = React.useContext(RouteContext);
|
---|
1109 | let {
|
---|
1110 | pathname: locationPathname
|
---|
1111 | } = useLocation();
|
---|
1112 | let navigate = useNavigate();
|
---|
1113 |
|
---|
1114 | // Resolve the path outside of the effect so that when effects run twice in
|
---|
1115 | // StrictMode they navigate to the same place
|
---|
1116 | let path = resolveTo(to, UNSAFE_getResolveToMatches(matches, future.v7_relativeSplatPath), locationPathname, relative === "path");
|
---|
1117 | let jsonPath = JSON.stringify(path);
|
---|
1118 | React.useEffect(() => navigate(JSON.parse(jsonPath), {
|
---|
1119 | replace,
|
---|
1120 | state,
|
---|
1121 | relative
|
---|
1122 | }), [navigate, jsonPath, relative, replace, state]);
|
---|
1123 | return null;
|
---|
1124 | }
|
---|
1125 | /**
|
---|
1126 | * Renders the child route's element, if there is one.
|
---|
1127 | *
|
---|
1128 | * @see https://reactrouter.com/components/outlet
|
---|
1129 | */
|
---|
1130 | function Outlet(props) {
|
---|
1131 | return useOutlet(props.context);
|
---|
1132 | }
|
---|
1133 | /**
|
---|
1134 | * Declares an element that should be rendered at a certain URL path.
|
---|
1135 | *
|
---|
1136 | * @see https://reactrouter.com/components/route
|
---|
1137 | */
|
---|
1138 | function Route(_props) {
|
---|
1139 | process.env.NODE_ENV !== "production" ? UNSAFE_invariant(false, "A <Route> is only ever to be used as the child of <Routes> element, " + "never rendered directly. Please wrap your <Route> in a <Routes>.") : UNSAFE_invariant(false) ;
|
---|
1140 | }
|
---|
1141 | /**
|
---|
1142 | * Provides location context for the rest of the app.
|
---|
1143 | *
|
---|
1144 | * Note: You usually won't render a `<Router>` directly. Instead, you'll render a
|
---|
1145 | * router that is more specific to your environment such as a `<BrowserRouter>`
|
---|
1146 | * in web browsers or a `<StaticRouter>` for server rendering.
|
---|
1147 | *
|
---|
1148 | * @see https://reactrouter.com/router-components/router
|
---|
1149 | */
|
---|
1150 | function Router(_ref5) {
|
---|
1151 | let {
|
---|
1152 | basename: basenameProp = "/",
|
---|
1153 | children = null,
|
---|
1154 | location: locationProp,
|
---|
1155 | navigationType = Action.Pop,
|
---|
1156 | navigator,
|
---|
1157 | static: staticProp = false,
|
---|
1158 | future
|
---|
1159 | } = _ref5;
|
---|
1160 | !!useInRouterContext() ? process.env.NODE_ENV !== "production" ? UNSAFE_invariant(false, "You cannot render a <Router> inside another <Router>." + " You should never have more than one in your app.") : UNSAFE_invariant(false) : void 0;
|
---|
1161 |
|
---|
1162 | // Preserve trailing slashes on basename, so we can let the user control
|
---|
1163 | // the enforcement of trailing slashes throughout the app
|
---|
1164 | let basename = basenameProp.replace(/^\/*/, "/");
|
---|
1165 | let navigationContext = React.useMemo(() => ({
|
---|
1166 | basename,
|
---|
1167 | navigator,
|
---|
1168 | static: staticProp,
|
---|
1169 | future: _extends({
|
---|
1170 | v7_relativeSplatPath: false
|
---|
1171 | }, future)
|
---|
1172 | }), [basename, future, navigator, staticProp]);
|
---|
1173 | if (typeof locationProp === "string") {
|
---|
1174 | locationProp = parsePath(locationProp);
|
---|
1175 | }
|
---|
1176 | let {
|
---|
1177 | pathname = "/",
|
---|
1178 | search = "",
|
---|
1179 | hash = "",
|
---|
1180 | state = null,
|
---|
1181 | key = "default"
|
---|
1182 | } = locationProp;
|
---|
1183 | let locationContext = React.useMemo(() => {
|
---|
1184 | let trailingPathname = stripBasename(pathname, basename);
|
---|
1185 | if (trailingPathname == null) {
|
---|
1186 | return null;
|
---|
1187 | }
|
---|
1188 | return {
|
---|
1189 | location: {
|
---|
1190 | pathname: trailingPathname,
|
---|
1191 | search,
|
---|
1192 | hash,
|
---|
1193 | state,
|
---|
1194 | key
|
---|
1195 | },
|
---|
1196 | navigationType
|
---|
1197 | };
|
---|
1198 | }, [basename, pathname, search, hash, state, key, navigationType]);
|
---|
1199 | process.env.NODE_ENV !== "production" ? UNSAFE_warning(locationContext != null, "<Router basename=\"" + basename + "\"> is not able to match the URL " + ("\"" + pathname + search + hash + "\" because it does not start with the ") + "basename, so the <Router> won't render anything.") : void 0;
|
---|
1200 | if (locationContext == null) {
|
---|
1201 | return null;
|
---|
1202 | }
|
---|
1203 | return /*#__PURE__*/React.createElement(NavigationContext.Provider, {
|
---|
1204 | value: navigationContext
|
---|
1205 | }, /*#__PURE__*/React.createElement(LocationContext.Provider, {
|
---|
1206 | children: children,
|
---|
1207 | value: locationContext
|
---|
1208 | }));
|
---|
1209 | }
|
---|
1210 | /**
|
---|
1211 | * A container for a nested tree of `<Route>` elements that renders the branch
|
---|
1212 | * that best matches the current location.
|
---|
1213 | *
|
---|
1214 | * @see https://reactrouter.com/components/routes
|
---|
1215 | */
|
---|
1216 | function Routes(_ref6) {
|
---|
1217 | let {
|
---|
1218 | children,
|
---|
1219 | location
|
---|
1220 | } = _ref6;
|
---|
1221 | return useRoutes(createRoutesFromChildren(children), location);
|
---|
1222 | }
|
---|
1223 | /**
|
---|
1224 | * Component to use for rendering lazily loaded data from returning defer()
|
---|
1225 | * in a loader function
|
---|
1226 | */
|
---|
1227 | function Await(_ref7) {
|
---|
1228 | let {
|
---|
1229 | children,
|
---|
1230 | errorElement,
|
---|
1231 | resolve
|
---|
1232 | } = _ref7;
|
---|
1233 | return /*#__PURE__*/React.createElement(AwaitErrorBoundary, {
|
---|
1234 | resolve: resolve,
|
---|
1235 | errorElement: errorElement
|
---|
1236 | }, /*#__PURE__*/React.createElement(ResolveAwait, null, children));
|
---|
1237 | }
|
---|
1238 | var AwaitRenderStatus = /*#__PURE__*/function (AwaitRenderStatus) {
|
---|
1239 | AwaitRenderStatus[AwaitRenderStatus["pending"] = 0] = "pending";
|
---|
1240 | AwaitRenderStatus[AwaitRenderStatus["success"] = 1] = "success";
|
---|
1241 | AwaitRenderStatus[AwaitRenderStatus["error"] = 2] = "error";
|
---|
1242 | return AwaitRenderStatus;
|
---|
1243 | }(AwaitRenderStatus || {});
|
---|
1244 | const neverSettledPromise = new Promise(() => {});
|
---|
1245 | class AwaitErrorBoundary extends React.Component {
|
---|
1246 | constructor(props) {
|
---|
1247 | super(props);
|
---|
1248 | this.state = {
|
---|
1249 | error: null
|
---|
1250 | };
|
---|
1251 | }
|
---|
1252 | static getDerivedStateFromError(error) {
|
---|
1253 | return {
|
---|
1254 | error
|
---|
1255 | };
|
---|
1256 | }
|
---|
1257 | componentDidCatch(error, errorInfo) {
|
---|
1258 | console.error("<Await> caught the following error during render", error, errorInfo);
|
---|
1259 | }
|
---|
1260 | render() {
|
---|
1261 | let {
|
---|
1262 | children,
|
---|
1263 | errorElement,
|
---|
1264 | resolve
|
---|
1265 | } = this.props;
|
---|
1266 | let promise = null;
|
---|
1267 | let status = AwaitRenderStatus.pending;
|
---|
1268 | if (!(resolve instanceof Promise)) {
|
---|
1269 | // Didn't get a promise - provide as a resolved promise
|
---|
1270 | status = AwaitRenderStatus.success;
|
---|
1271 | promise = Promise.resolve();
|
---|
1272 | Object.defineProperty(promise, "_tracked", {
|
---|
1273 | get: () => true
|
---|
1274 | });
|
---|
1275 | Object.defineProperty(promise, "_data", {
|
---|
1276 | get: () => resolve
|
---|
1277 | });
|
---|
1278 | } else if (this.state.error) {
|
---|
1279 | // Caught a render error, provide it as a rejected promise
|
---|
1280 | status = AwaitRenderStatus.error;
|
---|
1281 | let renderError = this.state.error;
|
---|
1282 | promise = Promise.reject().catch(() => {}); // Avoid unhandled rejection warnings
|
---|
1283 | Object.defineProperty(promise, "_tracked", {
|
---|
1284 | get: () => true
|
---|
1285 | });
|
---|
1286 | Object.defineProperty(promise, "_error", {
|
---|
1287 | get: () => renderError
|
---|
1288 | });
|
---|
1289 | } else if (resolve._tracked) {
|
---|
1290 | // Already tracked promise - check contents
|
---|
1291 | promise = resolve;
|
---|
1292 | status = "_error" in promise ? AwaitRenderStatus.error : "_data" in promise ? AwaitRenderStatus.success : AwaitRenderStatus.pending;
|
---|
1293 | } else {
|
---|
1294 | // Raw (untracked) promise - track it
|
---|
1295 | status = AwaitRenderStatus.pending;
|
---|
1296 | Object.defineProperty(resolve, "_tracked", {
|
---|
1297 | get: () => true
|
---|
1298 | });
|
---|
1299 | promise = resolve.then(data => Object.defineProperty(resolve, "_data", {
|
---|
1300 | get: () => data
|
---|
1301 | }), error => Object.defineProperty(resolve, "_error", {
|
---|
1302 | get: () => error
|
---|
1303 | }));
|
---|
1304 | }
|
---|
1305 | if (status === AwaitRenderStatus.error && promise._error instanceof AbortedDeferredError) {
|
---|
1306 | // Freeze the UI by throwing a never resolved promise
|
---|
1307 | throw neverSettledPromise;
|
---|
1308 | }
|
---|
1309 | if (status === AwaitRenderStatus.error && !errorElement) {
|
---|
1310 | // No errorElement, throw to the nearest route-level error boundary
|
---|
1311 | throw promise._error;
|
---|
1312 | }
|
---|
1313 | if (status === AwaitRenderStatus.error) {
|
---|
1314 | // Render via our errorElement
|
---|
1315 | return /*#__PURE__*/React.createElement(AwaitContext.Provider, {
|
---|
1316 | value: promise,
|
---|
1317 | children: errorElement
|
---|
1318 | });
|
---|
1319 | }
|
---|
1320 | if (status === AwaitRenderStatus.success) {
|
---|
1321 | // Render children with resolved value
|
---|
1322 | return /*#__PURE__*/React.createElement(AwaitContext.Provider, {
|
---|
1323 | value: promise,
|
---|
1324 | children: children
|
---|
1325 | });
|
---|
1326 | }
|
---|
1327 |
|
---|
1328 | // Throw to the suspense boundary
|
---|
1329 | throw promise;
|
---|
1330 | }
|
---|
1331 | }
|
---|
1332 |
|
---|
1333 | /**
|
---|
1334 | * @private
|
---|
1335 | * Indirection to leverage useAsyncValue for a render-prop API on `<Await>`
|
---|
1336 | */
|
---|
1337 | function ResolveAwait(_ref8) {
|
---|
1338 | let {
|
---|
1339 | children
|
---|
1340 | } = _ref8;
|
---|
1341 | let data = useAsyncValue();
|
---|
1342 | let toRender = typeof children === "function" ? children(data) : children;
|
---|
1343 | return /*#__PURE__*/React.createElement(React.Fragment, null, toRender);
|
---|
1344 | }
|
---|
1345 |
|
---|
1346 | ///////////////////////////////////////////////////////////////////////////////
|
---|
1347 | // UTILS
|
---|
1348 | ///////////////////////////////////////////////////////////////////////////////
|
---|
1349 |
|
---|
1350 | /**
|
---|
1351 | * Creates a route config from a React "children" object, which is usually
|
---|
1352 | * either a `<Route>` element or an array of them. Used internally by
|
---|
1353 | * `<Routes>` to create a route config from its children.
|
---|
1354 | *
|
---|
1355 | * @see https://reactrouter.com/utils/create-routes-from-children
|
---|
1356 | */
|
---|
1357 | function createRoutesFromChildren(children, parentPath) {
|
---|
1358 | if (parentPath === void 0) {
|
---|
1359 | parentPath = [];
|
---|
1360 | }
|
---|
1361 | let routes = [];
|
---|
1362 | React.Children.forEach(children, (element, index) => {
|
---|
1363 | if (! /*#__PURE__*/React.isValidElement(element)) {
|
---|
1364 | // Ignore non-elements. This allows people to more easily inline
|
---|
1365 | // conditionals in their route config.
|
---|
1366 | return;
|
---|
1367 | }
|
---|
1368 | let treePath = [...parentPath, index];
|
---|
1369 | if (element.type === React.Fragment) {
|
---|
1370 | // Transparently support React.Fragment and its children.
|
---|
1371 | routes.push.apply(routes, createRoutesFromChildren(element.props.children, treePath));
|
---|
1372 | return;
|
---|
1373 | }
|
---|
1374 | !(element.type === Route) ? process.env.NODE_ENV !== "production" ? UNSAFE_invariant(false, "[" + (typeof element.type === "string" ? element.type : element.type.name) + "] is not a <Route> component. All component children of <Routes> must be a <Route> or <React.Fragment>") : UNSAFE_invariant(false) : void 0;
|
---|
1375 | !(!element.props.index || !element.props.children) ? process.env.NODE_ENV !== "production" ? UNSAFE_invariant(false, "An index route cannot have child routes.") : UNSAFE_invariant(false) : void 0;
|
---|
1376 | let route = {
|
---|
1377 | id: element.props.id || treePath.join("-"),
|
---|
1378 | caseSensitive: element.props.caseSensitive,
|
---|
1379 | element: element.props.element,
|
---|
1380 | Component: element.props.Component,
|
---|
1381 | index: element.props.index,
|
---|
1382 | path: element.props.path,
|
---|
1383 | loader: element.props.loader,
|
---|
1384 | action: element.props.action,
|
---|
1385 | errorElement: element.props.errorElement,
|
---|
1386 | ErrorBoundary: element.props.ErrorBoundary,
|
---|
1387 | hasErrorBoundary: element.props.ErrorBoundary != null || element.props.errorElement != null,
|
---|
1388 | shouldRevalidate: element.props.shouldRevalidate,
|
---|
1389 | handle: element.props.handle,
|
---|
1390 | lazy: element.props.lazy
|
---|
1391 | };
|
---|
1392 | if (element.props.children) {
|
---|
1393 | route.children = createRoutesFromChildren(element.props.children, treePath);
|
---|
1394 | }
|
---|
1395 | routes.push(route);
|
---|
1396 | });
|
---|
1397 | return routes;
|
---|
1398 | }
|
---|
1399 |
|
---|
1400 | /**
|
---|
1401 | * Renders the result of `matchRoutes()` into a React element.
|
---|
1402 | */
|
---|
1403 | function renderMatches(matches) {
|
---|
1404 | return _renderMatches(matches);
|
---|
1405 | }
|
---|
1406 |
|
---|
1407 | function mapRouteProperties(route) {
|
---|
1408 | let updates = {
|
---|
1409 | // Note: this check also occurs in createRoutesFromChildren so update
|
---|
1410 | // there if you change this -- please and thank you!
|
---|
1411 | hasErrorBoundary: route.ErrorBoundary != null || route.errorElement != null
|
---|
1412 | };
|
---|
1413 | if (route.Component) {
|
---|
1414 | if (process.env.NODE_ENV !== "production") {
|
---|
1415 | if (route.element) {
|
---|
1416 | process.env.NODE_ENV !== "production" ? UNSAFE_warning(false, "You should not include both `Component` and `element` on your route - " + "`Component` will be used.") : void 0;
|
---|
1417 | }
|
---|
1418 | }
|
---|
1419 | Object.assign(updates, {
|
---|
1420 | element: /*#__PURE__*/React.createElement(route.Component),
|
---|
1421 | Component: undefined
|
---|
1422 | });
|
---|
1423 | }
|
---|
1424 | if (route.HydrateFallback) {
|
---|
1425 | if (process.env.NODE_ENV !== "production") {
|
---|
1426 | if (route.hydrateFallbackElement) {
|
---|
1427 | process.env.NODE_ENV !== "production" ? UNSAFE_warning(false, "You should not include both `HydrateFallback` and `hydrateFallbackElement` on your route - " + "`HydrateFallback` will be used.") : void 0;
|
---|
1428 | }
|
---|
1429 | }
|
---|
1430 | Object.assign(updates, {
|
---|
1431 | hydrateFallbackElement: /*#__PURE__*/React.createElement(route.HydrateFallback),
|
---|
1432 | HydrateFallback: undefined
|
---|
1433 | });
|
---|
1434 | }
|
---|
1435 | if (route.ErrorBoundary) {
|
---|
1436 | if (process.env.NODE_ENV !== "production") {
|
---|
1437 | if (route.errorElement) {
|
---|
1438 | process.env.NODE_ENV !== "production" ? UNSAFE_warning(false, "You should not include both `ErrorBoundary` and `errorElement` on your route - " + "`ErrorBoundary` will be used.") : void 0;
|
---|
1439 | }
|
---|
1440 | }
|
---|
1441 | Object.assign(updates, {
|
---|
1442 | errorElement: /*#__PURE__*/React.createElement(route.ErrorBoundary),
|
---|
1443 | ErrorBoundary: undefined
|
---|
1444 | });
|
---|
1445 | }
|
---|
1446 | return updates;
|
---|
1447 | }
|
---|
1448 | function createMemoryRouter(routes, opts) {
|
---|
1449 | return createRouter({
|
---|
1450 | basename: opts == null ? void 0 : opts.basename,
|
---|
1451 | future: _extends({}, opts == null ? void 0 : opts.future, {
|
---|
1452 | v7_prependBasename: true
|
---|
1453 | }),
|
---|
1454 | history: createMemoryHistory({
|
---|
1455 | initialEntries: opts == null ? void 0 : opts.initialEntries,
|
---|
1456 | initialIndex: opts == null ? void 0 : opts.initialIndex
|
---|
1457 | }),
|
---|
1458 | hydrationData: opts == null ? void 0 : opts.hydrationData,
|
---|
1459 | routes,
|
---|
1460 | mapRouteProperties,
|
---|
1461 | unstable_dataStrategy: opts == null ? void 0 : opts.unstable_dataStrategy,
|
---|
1462 | unstable_patchRoutesOnMiss: opts == null ? void 0 : opts.unstable_patchRoutesOnMiss
|
---|
1463 | }).initialize();
|
---|
1464 | }
|
---|
1465 |
|
---|
1466 | export { Await, MemoryRouter, Navigate, Outlet, Route, Router, RouterProvider, Routes, DataRouterContext as UNSAFE_DataRouterContext, DataRouterStateContext as UNSAFE_DataRouterStateContext, LocationContext as UNSAFE_LocationContext, NavigationContext as UNSAFE_NavigationContext, RouteContext as UNSAFE_RouteContext, mapRouteProperties as UNSAFE_mapRouteProperties, useRouteId as UNSAFE_useRouteId, useRoutesImpl as UNSAFE_useRoutesImpl, createMemoryRouter, createRoutesFromChildren, createRoutesFromChildren as createRoutesFromElements, renderMatches, useActionData, useAsyncError, useAsyncValue, useBlocker, useHref, useInRouterContext, useLoaderData, useLocation, useMatch, useMatches, useNavigate, useNavigation, useNavigationType, useOutlet, useOutletContext, useParams, useResolvedPath, useRevalidator, useRouteError, useRouteLoaderData, useRoutes };
|
---|
1467 | //# sourceMappingURL=index.js.map
|
---|