| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 |
|
| 11 | import { ABSOLUTE_URL_REGEX } from "../router/url.js";
|
| 12 | import { createBrowserHistory, createHashHistory, createPath, invariant, warning } from "../router/history.js";
|
| 13 | import { ErrorResponseImpl, SUPPORTED_ERROR_TYPES, defaultMapRouteProperties, joinPaths, matchPath, parseToInfo, resolveTo, stripBasename } from "../router/utils.js";
|
| 14 | import { IDLE_FETCHER, createRouter } from "../router/router.js";
|
| 15 | import { DataRouterContext, DataRouterStateContext, FetchersContext, NavigationContext, RouteContext, ViewTransitionContext } from "../context.js";
|
| 16 | import { useBlocker, useHref, useLocation, useMatches, useNavigate, useNavigation, useResolvedPath, useRouteId } from "../hooks.js";
|
| 17 | import { Router, hydrationRouteProperties } from "../components.js";
|
| 18 | import { createSearchParams, getFormSubmissionInfo, getSearchParamsForLocation, shouldProcessLinkClick } from "./dom.js";
|
| 19 | import { escapeHtml } from "./ssr/markup.js";
|
| 20 | import { FrameworkContext, PrefetchPageLinks, mergeRefs, usePrefetchBehavior } from "./ssr/components.js";
|
| 21 | import * as React$1 from "react";
|
| 22 |
|
| 23 | const isBrowser = typeof window !== "undefined" && typeof window.document !== "undefined" && typeof window.document.createElement !== "undefined";
|
| 24 | try {
|
| 25 | if (isBrowser) window.__reactRouterVersion = "8.0.0";
|
| 26 | } catch (e) {}
|
| 27 | |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 |
|
| 51 | function createBrowserRouter(routes, opts) {
|
| 52 | return createRouter({
|
| 53 | basename: opts?.basename,
|
| 54 | getContext: opts?.getContext,
|
| 55 | future: opts?.future,
|
| 56 | history: createBrowserHistory({ window: opts?.window }),
|
| 57 | hydrationData: opts?.hydrationData || parseHydrationData(),
|
| 58 | routes,
|
| 59 | mapRouteProperties: defaultMapRouteProperties,
|
| 60 | hydrationRouteProperties,
|
| 61 | dataStrategy: opts?.dataStrategy,
|
| 62 | patchRoutesOnNavigation: opts?.patchRoutesOnNavigation,
|
| 63 | window: opts?.window,
|
| 64 | instrumentations: opts?.instrumentations
|
| 65 | }).initialize();
|
| 66 | }
|
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 |
|
| 90 | function createHashRouter(routes, opts) {
|
| 91 | return createRouter({
|
| 92 | basename: opts?.basename,
|
| 93 | getContext: opts?.getContext,
|
| 94 | future: opts?.future,
|
| 95 | history: createHashHistory({ window: opts?.window }),
|
| 96 | hydrationData: opts?.hydrationData || parseHydrationData(),
|
| 97 | routes,
|
| 98 | mapRouteProperties: defaultMapRouteProperties,
|
| 99 | hydrationRouteProperties,
|
| 100 | dataStrategy: opts?.dataStrategy,
|
| 101 | patchRoutesOnNavigation: opts?.patchRoutesOnNavigation,
|
| 102 | window: opts?.window,
|
| 103 | instrumentations: opts?.instrumentations
|
| 104 | }).initialize();
|
| 105 | }
|
| 106 | function parseHydrationData() {
|
| 107 | let state = window?.__staticRouterHydrationData;
|
| 108 | if (state && state.errors) state = {
|
| 109 | ...state,
|
| 110 | errors: deserializeErrors(state.errors)
|
| 111 | };
|
| 112 | return state;
|
| 113 | }
|
| 114 | function deserializeErrors(errors) {
|
| 115 | if (!errors) return null;
|
| 116 | let entries = Object.entries(errors);
|
| 117 | let serialized = {};
|
| 118 | for (let [key, val] of entries) if (val && val.__type === "RouteErrorResponse") serialized[key] = new ErrorResponseImpl(val.status, val.statusText, val.data, val.internal === true);
|
| 119 | else if (val && val.__type === "Error") {
|
| 120 | if (typeof val.__subType === "string" && SUPPORTED_ERROR_TYPES.includes(val.__subType)) {
|
| 121 | let ErrorConstructor = window[val.__subType];
|
| 122 | if (typeof ErrorConstructor === "function") try {
|
| 123 | let error = new ErrorConstructor(val.message);
|
| 124 | error.stack = "";
|
| 125 | serialized[key] = error;
|
| 126 | } catch (e) {}
|
| 127 | }
|
| 128 | if (serialized[key] == null) {
|
| 129 | let error = new Error(val.message);
|
| 130 | error.stack = "";
|
| 131 | serialized[key] = error;
|
| 132 | }
|
| 133 | } else serialized[key] = val;
|
| 134 | return serialized;
|
| 135 | }
|
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | |
| 142 | |
| 143 | |
| 144 | |
| 145 | |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 |
|
| 151 | function BrowserRouter({ basename, children, useTransitions, window }) {
|
| 152 | let historyRef = React$1.useRef(null);
|
| 153 | if (historyRef.current == null) historyRef.current = createBrowserHistory({
|
| 154 | window,
|
| 155 | v5Compat: true
|
| 156 | });
|
| 157 | let history = historyRef.current;
|
| 158 | let [state, setStateImpl] = React$1.useState({
|
| 159 | action: history.action,
|
| 160 | location: history.location
|
| 161 | });
|
| 162 | let setState = React$1.useCallback((newState) => {
|
| 163 | if (useTransitions === false) setStateImpl(newState);
|
| 164 | else React$1.startTransition(() => setStateImpl(newState));
|
| 165 | }, [useTransitions]);
|
| 166 | React$1.useLayoutEffect(() => history.listen(setState), [history, setState]);
|
| 167 | return React$1.createElement(Router, {
|
| 168 | basename,
|
| 169 | children,
|
| 170 | location: state.location,
|
| 171 | navigationType: state.action,
|
| 172 | navigator: history,
|
| 173 | useTransitions
|
| 174 | });
|
| 175 | }
|
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 |
|
| 192 | function HashRouter({ basename, children, useTransitions, window }) {
|
| 193 | let historyRef = React$1.useRef(null);
|
| 194 | if (historyRef.current == null) historyRef.current = createHashHistory({
|
| 195 | window,
|
| 196 | v5Compat: true
|
| 197 | });
|
| 198 | let history = historyRef.current;
|
| 199 | let [state, setStateImpl] = React$1.useState({
|
| 200 | action: history.action,
|
| 201 | location: history.location
|
| 202 | });
|
| 203 | let setState = React$1.useCallback((newState) => {
|
| 204 | if (useTransitions === false) setStateImpl(newState);
|
| 205 | else React$1.startTransition(() => setStateImpl(newState));
|
| 206 | }, [useTransitions]);
|
| 207 | React$1.useLayoutEffect(() => history.listen(setState), [history, setState]);
|
| 208 | return React$1.createElement(Router, {
|
| 209 | basename,
|
| 210 | children,
|
| 211 | location: state.location,
|
| 212 | navigationType: state.action,
|
| 213 | navigator: history,
|
| 214 | useTransitions
|
| 215 | });
|
| 216 | }
|
| 217 | |
| 218 | |
| 219 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 |
|
| 236 | function HistoryRouter({ basename, children, history, useTransitions }) {
|
| 237 | let [state, setStateImpl] = React$1.useState({
|
| 238 | action: history.action,
|
| 239 | location: history.location
|
| 240 | });
|
| 241 | let setState = React$1.useCallback((newState) => {
|
| 242 | if (useTransitions === false) setStateImpl(newState);
|
| 243 | else React$1.startTransition(() => setStateImpl(newState));
|
| 244 | }, [useTransitions]);
|
| 245 | React$1.useLayoutEffect(() => history.listen(setState), [history, setState]);
|
| 246 | return React$1.createElement(Router, {
|
| 247 | basename,
|
| 248 | children,
|
| 249 | location: state.location,
|
| 250 | navigationType: state.action,
|
| 251 | navigator: history,
|
| 252 | useTransitions
|
| 253 | });
|
| 254 | }
|
| 255 | HistoryRouter.displayName = "unstable_HistoryRouter";
|
| 256 | |
| 257 | |
| 258 | |
| 259 | |
| 260 | |
| 261 | |
| 262 | |
| 263 | |
| 264 | |
| 265 | |
| 266 | |
| 267 | |
| 268 | |
| 269 | |
| 270 | |
| 271 | |
| 272 | |
| 273 | |
| 274 | |
| 275 | |
| 276 | |
| 277 | |
| 278 | |
| 279 | |
| 280 | |
| 281 | |
| 282 | |
| 283 | |
| 284 | |
| 285 | |
| 286 |
|
| 287 | const Link = React$1.forwardRef(function LinkWithRef({ onClick, discover = "render", prefetch = "none", relative, reloadDocument, replace, mask, state, target, to, preventScrollReset, viewTransition, defaultShouldRevalidate, ...rest }, forwardedRef) {
|
| 288 | let { basename, navigator, useTransitions } = React$1.useContext(NavigationContext);
|
| 289 | let isAbsolute = typeof to === "string" && ABSOLUTE_URL_REGEX.test(to);
|
| 290 | let parsed = parseToInfo(to, basename);
|
| 291 | to = parsed.to;
|
| 292 | let href = useHref(to, { relative });
|
| 293 | let location = useLocation();
|
| 294 | let maskedHref = null;
|
| 295 | if (mask) {
|
| 296 | let resolved = resolveTo(mask, [], location.mask ? location.mask.pathname : "/", true);
|
| 297 | if (basename !== "/") resolved.pathname = resolved.pathname === "/" ? basename : joinPaths([basename, resolved.pathname]);
|
| 298 | maskedHref = navigator.createHref(resolved);
|
| 299 | }
|
| 300 | let [shouldPrefetch, prefetchRef, prefetchHandlers] = usePrefetchBehavior(prefetch, rest);
|
| 301 | let internalOnClick = useLinkClickHandler(to, {
|
| 302 | replace,
|
| 303 | mask,
|
| 304 | state,
|
| 305 | target,
|
| 306 | preventScrollReset,
|
| 307 | relative,
|
| 308 | viewTransition,
|
| 309 | defaultShouldRevalidate,
|
| 310 | useTransitions
|
| 311 | });
|
| 312 | function handleClick(event) {
|
| 313 | if (onClick) onClick(event);
|
| 314 | if (!event.defaultPrevented) internalOnClick(event);
|
| 315 | }
|
| 316 | let isSpaLink = !(parsed.isExternal || reloadDocument);
|
| 317 | let link = React$1.createElement("a", {
|
| 318 | ...rest,
|
| 319 | ...prefetchHandlers,
|
| 320 | href: (isSpaLink ? maskedHref : void 0) || parsed.absoluteURL || href,
|
| 321 | onClick: isSpaLink ? handleClick : onClick,
|
| 322 | ref: mergeRefs(forwardedRef, prefetchRef),
|
| 323 | target,
|
| 324 | "data-discover": !isAbsolute && discover === "render" ? "true" : void 0
|
| 325 | });
|
| 326 | return shouldPrefetch && !isAbsolute ? React$1.createElement(React$1.Fragment, null, link, React$1.createElement(PrefetchPageLinks, { page: href })) : link;
|
| 327 | });
|
| 328 | Link.displayName = "Link";
|
| 329 | |
| 330 | |
| 331 | |
| 332 | |
| 333 | |
| 334 | |
| 335 | |
| 336 | |
| 337 | |
| 338 | |
| 339 | |
| 340 | |
| 341 | |
| 342 | |
| 343 | |
| 344 | |
| 345 | |
| 346 | |
| 347 | |
| 348 | |
| 349 | |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | |
| 356 | |
| 357 | |
| 358 | |
| 359 | |
| 360 | |
| 361 | |
| 362 | |
| 363 | |
| 364 | |
| 365 | |
| 366 | |
| 367 | |
| 368 | |
| 369 | |
| 370 | |
| 371 |
|
| 372 | const NavLink = React$1.forwardRef(function NavLinkWithRef({ "aria-current": ariaCurrentProp = "page", caseSensitive = false, className: classNameProp = "", end = false, style: styleProp, to, viewTransition, children, ...rest }, ref) {
|
| 373 | let path = useResolvedPath(to, { relative: rest.relative });
|
| 374 | let location = useLocation();
|
| 375 | let routerState = React$1.useContext(DataRouterStateContext);
|
| 376 | let { navigator, basename } = React$1.useContext(NavigationContext);
|
| 377 | let isTransitioning = routerState != null && useViewTransitionState(path) && viewTransition === true;
|
| 378 | let toPathname = navigator.encodeLocation ? navigator.encodeLocation(path).pathname : path.pathname;
|
| 379 | let locationPathname = location.pathname;
|
| 380 | let nextLocationPathname = routerState && routerState.navigation && routerState.navigation.location ? routerState.navigation.location.pathname : null;
|
| 381 | if (!caseSensitive) {
|
| 382 | locationPathname = locationPathname.toLowerCase();
|
| 383 | nextLocationPathname = nextLocationPathname ? nextLocationPathname.toLowerCase() : null;
|
| 384 | toPathname = toPathname.toLowerCase();
|
| 385 | }
|
| 386 | if (nextLocationPathname && basename) nextLocationPathname = stripBasename(nextLocationPathname, basename) || nextLocationPathname;
|
| 387 | const endSlashPosition = toPathname !== "/" && toPathname.endsWith("/") ? toPathname.length - 1 : toPathname.length;
|
| 388 | let isActive = locationPathname === toPathname || !end && locationPathname.startsWith(toPathname) && locationPathname.charAt(endSlashPosition) === "/";
|
| 389 | let isPending = nextLocationPathname != null && (nextLocationPathname === toPathname || !end && nextLocationPathname.startsWith(toPathname) && nextLocationPathname.charAt(toPathname.length) === "/");
|
| 390 | let renderProps = {
|
| 391 | isActive,
|
| 392 | isPending,
|
| 393 | isTransitioning
|
| 394 | };
|
| 395 | let ariaCurrent = isActive ? ariaCurrentProp : void 0;
|
| 396 | let className;
|
| 397 | if (typeof classNameProp === "function") className = classNameProp(renderProps);
|
| 398 | else className = [
|
| 399 | classNameProp,
|
| 400 | isActive ? "active" : null,
|
| 401 | isPending ? "pending" : null,
|
| 402 | isTransitioning ? "transitioning" : null
|
| 403 | ].filter(Boolean).join(" ");
|
| 404 | let style = typeof styleProp === "function" ? styleProp(renderProps) : styleProp;
|
| 405 | return React$1.createElement(Link, {
|
| 406 | ...rest,
|
| 407 | "aria-current": ariaCurrent,
|
| 408 | className,
|
| 409 | ref,
|
| 410 | style,
|
| 411 | to,
|
| 412 | viewTransition
|
| 413 | }, typeof children === "function" ? children(renderProps) : children);
|
| 414 | });
|
| 415 | NavLink.displayName = "NavLink";
|
| 416 | |
| 417 | |
| 418 | |
| 419 | |
| 420 | |
| 421 | |
| 422 | |
| 423 | |
| 424 | |
| 425 | |
| 426 | |
| 427 | |
| 428 | |
| 429 | |
| 430 | |
| 431 | |
| 432 | |
| 433 | |
| 434 | |
| 435 | |
| 436 | |
| 437 | |
| 438 | |
| 439 | |
| 440 | |
| 441 | |
| 442 | |
| 443 | |
| 444 | |
| 445 | |
| 446 | |
| 447 | |
| 448 | |
| 449 | |
| 450 | |
| 451 | |
| 452 | |
| 453 | |
| 454 | |
| 455 | |
| 456 | |
| 457 | |
| 458 | |
| 459 | |
| 460 | |
| 461 | |
| 462 | |
| 463 | |
| 464 | |
| 465 |
|
| 466 | const Form = React$1.forwardRef(({ discover = "render", fetcherKey, navigate, reloadDocument, replace, state, method = "get", action, onSubmit, relative, preventScrollReset, viewTransition, defaultShouldRevalidate, ...props }, forwardedRef) => {
|
| 467 | let { useTransitions } = React$1.useContext(NavigationContext);
|
| 468 | let submit = useSubmit();
|
| 469 | let formAction = useFormAction(action, { relative });
|
| 470 | let formMethod = method.toLowerCase() === "get" ? "get" : "post";
|
| 471 | let isAbsolute = typeof action === "string" && ABSOLUTE_URL_REGEX.test(action);
|
| 472 | let submitHandler = (event) => {
|
| 473 | onSubmit && onSubmit(event);
|
| 474 | if (event.defaultPrevented) return;
|
| 475 | event.preventDefault();
|
| 476 | let submitter = event.nativeEvent.submitter;
|
| 477 | let submitMethod = submitter?.getAttribute("formmethod") || method;
|
| 478 | let doSubmit = () => submit(submitter || event.currentTarget, {
|
| 479 | fetcherKey,
|
| 480 | method: submitMethod,
|
| 481 | navigate,
|
| 482 | replace,
|
| 483 | state,
|
| 484 | relative,
|
| 485 | preventScrollReset,
|
| 486 | viewTransition,
|
| 487 | defaultShouldRevalidate
|
| 488 | });
|
| 489 | if (useTransitions && navigate !== false) React$1.startTransition(() => doSubmit());
|
| 490 | else doSubmit();
|
| 491 | };
|
| 492 | return React$1.createElement("form", {
|
| 493 | ref: forwardedRef,
|
| 494 | method: formMethod,
|
| 495 | action: formAction,
|
| 496 | onSubmit: reloadDocument ? onSubmit : submitHandler,
|
| 497 | ...props,
|
| 498 | "data-discover": !isAbsolute && discover === "render" ? "true" : void 0
|
| 499 | });
|
| 500 | });
|
| 501 | Form.displayName = "Form";
|
| 502 | |
| 503 | |
| 504 | |
| 505 | |
| 506 | |
| 507 | |
| 508 | |
| 509 | |
| 510 | |
| 511 | |
| 512 | |
| 513 | |
| 514 | |
| 515 | |
| 516 | |
| 517 | |
| 518 | |
| 519 | |
| 520 | |
| 521 | |
| 522 | |
| 523 | |
| 524 | |
| 525 | |
| 526 | |
| 527 | |
| 528 | |
| 529 | |
| 530 | |
| 531 | |
| 532 | |
| 533 | |
| 534 | |
| 535 | |
| 536 | |
| 537 | |
| 538 | |
| 539 |
|
| 540 | function ScrollRestoration({ getKey, storageKey, ...props }) {
|
| 541 | let remixContext = React$1.useContext(FrameworkContext);
|
| 542 | let { basename } = React$1.useContext(NavigationContext);
|
| 543 | let location = useLocation();
|
| 544 | let matches = useMatches();
|
| 545 | useScrollRestoration({
|
| 546 | getKey,
|
| 547 | storageKey
|
| 548 | });
|
| 549 | let ssrKey = React$1.useMemo(() => {
|
| 550 | if (!remixContext || !getKey) return null;
|
| 551 | let userKey = getScrollRestorationKey(location, matches, basename, getKey);
|
| 552 | return userKey !== location.key ? userKey : null;
|
| 553 | }, []);
|
| 554 | if (!remixContext || remixContext.isSpaMode) return null;
|
| 555 | let restoreScroll = ((storageKey, restoreKey) => {
|
| 556 | if (!window.history.state || !window.history.state.key) {
|
| 557 | let key = Math.random().toString(32).slice(2);
|
| 558 | window.history.replaceState({ key }, "");
|
| 559 | }
|
| 560 | try {
|
| 561 | let storedY = JSON.parse(sessionStorage.getItem(storageKey) || "{}")[restoreKey || window.history.state.key];
|
| 562 | if (typeof storedY === "number") window.scrollTo(0, storedY);
|
| 563 | } catch (error) {
|
| 564 | console.error(error);
|
| 565 | sessionStorage.removeItem(storageKey);
|
| 566 | }
|
| 567 | }).toString();
|
| 568 | if (props.nonce == null && remixContext?.nonce) props.nonce = remixContext.nonce;
|
| 569 | return React$1.createElement("script", {
|
| 570 | ...props,
|
| 571 | suppressHydrationWarning: true,
|
| 572 | dangerouslySetInnerHTML: { __html: `(${restoreScroll})(${escapeHtml(JSON.stringify(storageKey || SCROLL_RESTORATION_STORAGE_KEY))}, ${escapeHtml(JSON.stringify(ssrKey))})` }
|
| 573 | });
|
| 574 | }
|
| 575 | ScrollRestoration.displayName = "ScrollRestoration";
|
| 576 | function getDataRouterConsoleError(hookName) {
|
| 577 | return `${hookName} must be used within a data router. See https://reactrouter.com/en/main/routers/picking-a-router.`;
|
| 578 | }
|
| 579 | function useDataRouterContext(hookName) {
|
| 580 | let ctx = React$1.useContext(DataRouterContext);
|
| 581 | invariant(ctx, getDataRouterConsoleError(hookName));
|
| 582 | return ctx;
|
| 583 | }
|
| 584 | function useDataRouterState(hookName) {
|
| 585 | let state = React$1.useContext(DataRouterStateContext);
|
| 586 | invariant(state, getDataRouterConsoleError(hookName));
|
| 587 | return state;
|
| 588 | }
|
| 589 | |
| 590 | |
| 591 | |
| 592 | |
| 593 | |
| 594 | |
| 595 | |
| 596 | |
| 597 | |
| 598 | |
| 599 | |
| 600 | |
| 601 | |
| 602 | |
| 603 | |
| 604 | |
| 605 | |
| 606 | |
| 607 | |
| 608 | |
| 609 | |
| 610 | |
| 611 | |
| 612 | |
| 613 | |
| 614 | |
| 615 | |
| 616 | |
| 617 | |
| 618 | |
| 619 |
|
| 620 | function useLinkClickHandler(to, { target, replace: replaceProp, mask, state, preventScrollReset, relative, viewTransition, defaultShouldRevalidate, useTransitions } = {}) {
|
| 621 | let navigate = useNavigate();
|
| 622 | let location = useLocation();
|
| 623 | let path = useResolvedPath(to, { relative });
|
| 624 | return React$1.useCallback((event) => {
|
| 625 | if (shouldProcessLinkClick(event, target)) {
|
| 626 | event.preventDefault();
|
| 627 | let replace = replaceProp !== void 0 ? replaceProp : createPath(location) === createPath(path);
|
| 628 | let doNavigate = () => navigate(to, {
|
| 629 | replace,
|
| 630 | mask,
|
| 631 | state,
|
| 632 | preventScrollReset,
|
| 633 | relative,
|
| 634 | viewTransition,
|
| 635 | defaultShouldRevalidate
|
| 636 | });
|
| 637 | if (useTransitions) React$1.startTransition(() => doNavigate());
|
| 638 | else doNavigate();
|
| 639 | }
|
| 640 | }, [
|
| 641 | location,
|
| 642 | navigate,
|
| 643 | path,
|
| 644 | replaceProp,
|
| 645 | mask,
|
| 646 | state,
|
| 647 | target,
|
| 648 | to,
|
| 649 | preventScrollReset,
|
| 650 | relative,
|
| 651 | viewTransition,
|
| 652 | defaultShouldRevalidate,
|
| 653 | useTransitions
|
| 654 | ]);
|
| 655 | }
|
| 656 | |
| 657 | |
| 658 | |
| 659 | |
| 660 | |
| 661 | |
| 662 | |
| 663 | |
| 664 | |
| 665 | |
| 666 | |
| 667 | |
| 668 | |
| 669 | |
| 670 | |
| 671 | |
| 672 | |
| 673 | |
| 674 | |
| 675 | |
| 676 | |
| 677 | |
| 678 | |
| 679 | |
| 680 | |
| 681 | |
| 682 | |
| 683 | |
| 684 | |
| 685 | |
| 686 | |
| 687 | |
| 688 | |
| 689 | |
| 690 | |
| 691 | |
| 692 | |
| 693 | |
| 694 | |
| 695 | |
| 696 | |
| 697 | |
| 698 | |
| 699 | |
| 700 | |
| 701 | |
| 702 | |
| 703 | |
| 704 | |
| 705 | |
| 706 | |
| 707 | |
| 708 | |
| 709 | |
| 710 | |
| 711 | |
| 712 | |
| 713 | |
| 714 | |
| 715 | |
| 716 | |
| 717 | |
| 718 | |
| 719 | |
| 720 | |
| 721 | |
| 722 | |
| 723 | |
| 724 | |
| 725 | |
| 726 | |
| 727 | |
| 728 | |
| 729 | |
| 730 | |
| 731 | |
| 732 | |
| 733 | |
| 734 | |
| 735 | |
| 736 | |
| 737 | |
| 738 | |
| 739 | |
| 740 | |
| 741 | |
| 742 | |
| 743 | |
| 744 | |
| 745 | |
| 746 | |
| 747 | |
| 748 | |
| 749 | |
| 750 | |
| 751 |
|
| 752 | function useSearchParams(defaultInit) {
|
| 753 | warning(typeof URLSearchParams !== "undefined", "You cannot use the `useSearchParams` hook in a browser that does not support the URLSearchParams API. If you need to support Internet Explorer 11, we recommend you load a polyfill such as https://github.com/ungap/url-search-params.");
|
| 754 | let defaultSearchParamsRef = React$1.useRef(createSearchParams(defaultInit));
|
| 755 | let hasSetSearchParamsRef = React$1.useRef(false);
|
| 756 | let location = useLocation();
|
| 757 | let searchParams = React$1.useMemo(() => getSearchParamsForLocation(location.search, hasSetSearchParamsRef.current ? null : defaultSearchParamsRef.current), [location.search]);
|
| 758 | let navigate = useNavigate();
|
| 759 | return [searchParams, React$1.useCallback((nextInit, navigateOptions) => {
|
| 760 | const newSearchParams = createSearchParams(typeof nextInit === "function" ? nextInit(new URLSearchParams(searchParams)) : nextInit);
|
| 761 | hasSetSearchParamsRef.current = true;
|
| 762 | navigate("?" + newSearchParams, navigateOptions);
|
| 763 | }, [navigate, searchParams])];
|
| 764 | }
|
| 765 | let fetcherId = 0;
|
| 766 | let getUniqueFetcherId = () => `__${String(++fetcherId)}__`;
|
| 767 | |
| 768 | |
| 769 | |
| 770 | |
| 771 | |
| 772 | |
| 773 | |
| 774 | |
| 775 | |
| 776 | |
| 777 | |
| 778 | |
| 779 | |
| 780 | |
| 781 | |
| 782 | |
| 783 | |
| 784 | |
| 785 | |
| 786 |
|
| 787 | function useSubmit() {
|
| 788 | let { router } = useDataRouterContext("useSubmit");
|
| 789 | let { basename } = React$1.useContext(NavigationContext);
|
| 790 | let currentRouteId = useRouteId();
|
| 791 | let routerFetch = router.fetch;
|
| 792 | let routerNavigate = router.navigate;
|
| 793 | return React$1.useCallback(async (target, options = {}) => {
|
| 794 | let { action, method, encType, formData, body } = getFormSubmissionInfo(target, basename);
|
| 795 | if (options.navigate === false) await routerFetch(options.fetcherKey || getUniqueFetcherId(), currentRouteId, options.action || action, {
|
| 796 | defaultShouldRevalidate: options.defaultShouldRevalidate,
|
| 797 | preventScrollReset: options.preventScrollReset,
|
| 798 | formData,
|
| 799 | body,
|
| 800 | formMethod: options.method || method,
|
| 801 | formEncType: options.encType || encType,
|
| 802 | flushSync: options.flushSync
|
| 803 | });
|
| 804 | else await routerNavigate(options.action || action, {
|
| 805 | defaultShouldRevalidate: options.defaultShouldRevalidate,
|
| 806 | preventScrollReset: options.preventScrollReset,
|
| 807 | formData,
|
| 808 | body,
|
| 809 | formMethod: options.method || method,
|
| 810 | formEncType: options.encType || encType,
|
| 811 | replace: options.replace,
|
| 812 | state: options.state,
|
| 813 | fromRouteId: currentRouteId,
|
| 814 | flushSync: options.flushSync,
|
| 815 | viewTransition: options.viewTransition
|
| 816 | });
|
| 817 | }, [
|
| 818 | routerFetch,
|
| 819 | routerNavigate,
|
| 820 | basename,
|
| 821 | currentRouteId
|
| 822 | ]);
|
| 823 | }
|
| 824 | |
| 825 | |
| 826 | |
| 827 | |
| 828 | |
| 829 | |
| 830 | |
| 831 | |
| 832 | |
| 833 | |
| 834 | |
| 835 | |
| 836 | |
| 837 | |
| 838 | |
| 839 | |
| 840 | |
| 841 | |
| 842 | |
| 843 | |
| 844 | |
| 845 | |
| 846 | |
| 847 | |
| 848 | |
| 849 | |
| 850 | |
| 851 | |
| 852 | |
| 853 | |
| 854 | |
| 855 | |
| 856 | |
| 857 | |
| 858 | |
| 859 | |
| 860 |
|
| 861 | function useFormAction(action, { relative } = {}) {
|
| 862 | let { basename } = React$1.useContext(NavigationContext);
|
| 863 | let routeContext = React$1.useContext(RouteContext);
|
| 864 | invariant(routeContext, "useFormAction must be used inside a RouteContext");
|
| 865 | let [match] = routeContext.matches.slice(-1);
|
| 866 | let path = { ...useResolvedPath(action ? action : ".", { relative }) };
|
| 867 | let location = useLocation();
|
| 868 | if (action == null) {
|
| 869 | path.search = location.search;
|
| 870 | let params = new URLSearchParams(path.search);
|
| 871 | let indexValues = params.getAll("index");
|
| 872 | if (indexValues.some((v) => v === "")) {
|
| 873 | params.delete("index");
|
| 874 | indexValues.filter((v) => v).forEach((v) => params.append("index", v));
|
| 875 | let qs = params.toString();
|
| 876 | path.search = qs ? `?${qs}` : "";
|
| 877 | }
|
| 878 | }
|
| 879 | if ((!action || action === ".") && match.route.index) path.search = path.search ? path.search.replace(/^\?/, "?index&") : "?index";
|
| 880 | if (basename !== "/") path.pathname = path.pathname === "/" ? basename : joinPaths([basename, path.pathname]);
|
| 881 | return createPath(path);
|
| 882 | }
|
| 883 | |
| 884 | |
| 885 | |
| 886 | |
| 887 | |
| 888 | |
| 889 | |
| 890 | |
| 891 | |
| 892 | |
| 893 | |
| 894 | |
| 895 | |
| 896 | |
| 897 | |
| 898 | |
| 899 | |
| 900 | |
| 901 | |
| 902 | |
| 903 | |
| 904 | |
| 905 | |
| 906 | |
| 907 | |
| 908 | |
| 909 | |
| 910 | |
| 911 | |
| 912 | |
| 913 | |
| 914 | |
| 915 | |
| 916 | |
| 917 | |
| 918 | |
| 919 | |
| 920 | |
| 921 | |
| 922 | |
| 923 | |
| 924 | |
| 925 | |
| 926 | |
| 927 | |
| 928 | |
| 929 | |
| 930 | |
| 931 | |
| 932 | |
| 933 | |
| 934 | |
| 935 | |
| 936 | |
| 937 | |
| 938 | |
| 939 | |
| 940 | |
| 941 | |
| 942 | |
| 943 | |
| 944 |
|
| 945 | function useFetcher({ key } = {}) {
|
| 946 | let { router } = useDataRouterContext("useFetcher");
|
| 947 | let state = useDataRouterState("useFetcher");
|
| 948 | let fetcherData = React$1.useContext(FetchersContext);
|
| 949 | let route = React$1.useContext(RouteContext);
|
| 950 | let routeId = route.matches[route.matches.length - 1]?.route.id;
|
| 951 | invariant(fetcherData, `useFetcher must be used inside a FetchersContext`);
|
| 952 | invariant(route, `useFetcher must be used inside a RouteContext`);
|
| 953 | invariant(routeId != null, `useFetcher can only be used on routes that contain a unique "id"`);
|
| 954 | let defaultKey = React$1.useId();
|
| 955 | let [fetcherKey, setFetcherKey] = React$1.useState(key || defaultKey);
|
| 956 | if (key && key !== fetcherKey) setFetcherKey(key);
|
| 957 | let { deleteFetcher, getFetcher, resetFetcher, fetch: routerFetch } = router;
|
| 958 | React$1.useEffect(() => {
|
| 959 | getFetcher(fetcherKey);
|
| 960 | return () => deleteFetcher(fetcherKey);
|
| 961 | }, [
|
| 962 | deleteFetcher,
|
| 963 | getFetcher,
|
| 964 | fetcherKey
|
| 965 | ]);
|
| 966 | let load = React$1.useCallback(async (href, opts) => {
|
| 967 | invariant(routeId, "No routeId available for fetcher.load()");
|
| 968 | await routerFetch(fetcherKey, routeId, href, opts);
|
| 969 | }, [
|
| 970 | fetcherKey,
|
| 971 | routeId,
|
| 972 | routerFetch
|
| 973 | ]);
|
| 974 | let submitImpl = useSubmit();
|
| 975 | let submit = React$1.useCallback(async (target, opts) => {
|
| 976 | await submitImpl(target, {
|
| 977 | ...opts,
|
| 978 | navigate: false,
|
| 979 | fetcherKey
|
| 980 | });
|
| 981 | }, [fetcherKey, submitImpl]);
|
| 982 | let reset = React$1.useCallback((opts) => resetFetcher(fetcherKey, opts), [resetFetcher, fetcherKey]);
|
| 983 | let FetcherForm = React$1.useMemo(() => {
|
| 984 | let FetcherForm = React$1.forwardRef((props, ref) => {
|
| 985 | return React$1.createElement(Form, {
|
| 986 | ...props,
|
| 987 | navigate: false,
|
| 988 | fetcherKey,
|
| 989 | ref
|
| 990 | });
|
| 991 | });
|
| 992 | FetcherForm.displayName = "fetcher.Form";
|
| 993 | return FetcherForm;
|
| 994 | }, [fetcherKey]);
|
| 995 | let fetcher = state.fetchers.get(fetcherKey) || IDLE_FETCHER;
|
| 996 | let data = fetcherData.get(fetcherKey);
|
| 997 | return React$1.useMemo(() => ({
|
| 998 | Form: FetcherForm,
|
| 999 | submit,
|
| 1000 | load,
|
| 1001 | reset,
|
| 1002 | ...fetcher,
|
| 1003 | data
|
| 1004 | }), [
|
| 1005 | FetcherForm,
|
| 1006 | submit,
|
| 1007 | load,
|
| 1008 | reset,
|
| 1009 | fetcher,
|
| 1010 | data
|
| 1011 | ]);
|
| 1012 | }
|
| 1013 | |
| 1014 | |
| 1015 | |
| 1016 | |
| 1017 | |
| 1018 | |
| 1019 | |
| 1020 | |
| 1021 | |
| 1022 | |
| 1023 | |
| 1024 | |
| 1025 | |
| 1026 | |
| 1027 | |
| 1028 | |
| 1029 | |
| 1030 | |
| 1031 | |
| 1032 | |
| 1033 | |
| 1034 |
|
| 1035 | function useFetchers() {
|
| 1036 | let state = useDataRouterState("useFetchers");
|
| 1037 | return React$1.useMemo(() => Array.from(state.fetchers.entries()).map(([key, fetcher]) => ({
|
| 1038 | ...fetcher,
|
| 1039 | key
|
| 1040 | })), [state.fetchers]);
|
| 1041 | }
|
| 1042 | const SCROLL_RESTORATION_STORAGE_KEY = "react-router-scroll-positions";
|
| 1043 | let savedScrollPositions = {};
|
| 1044 | function getScrollRestorationKey(location, matches, basename, getKey) {
|
| 1045 | let key = null;
|
| 1046 | if (getKey) if (basename !== "/") key = getKey({
|
| 1047 | ...location,
|
| 1048 | pathname: stripBasename(location.pathname, basename) || location.pathname
|
| 1049 | }, matches);
|
| 1050 | else key = getKey(location, matches);
|
| 1051 | if (key == null) key = location.key;
|
| 1052 | return key;
|
| 1053 | }
|
| 1054 | |
| 1055 | |
| 1056 | |
| 1057 | |
| 1058 | |
| 1059 | |
| 1060 | |
| 1061 | |
| 1062 | |
| 1063 | |
| 1064 | |
| 1065 | |
| 1066 | |
| 1067 | |
| 1068 | |
| 1069 | |
| 1070 | |
| 1071 | |
| 1072 | |
| 1073 | |
| 1074 | |
| 1075 |
|
| 1076 | function useScrollRestoration({ getKey, storageKey } = {}) {
|
| 1077 | let { router } = useDataRouterContext("useScrollRestoration");
|
| 1078 | let { restoreScrollPosition, preventScrollReset } = useDataRouterState("useScrollRestoration");
|
| 1079 | let { basename } = React$1.useContext(NavigationContext);
|
| 1080 | let location = useLocation();
|
| 1081 | let matches = useMatches();
|
| 1082 | let navigation = useNavigation();
|
| 1083 | React$1.useEffect(() => {
|
| 1084 | window.history.scrollRestoration = "manual";
|
| 1085 | return () => {
|
| 1086 | window.history.scrollRestoration = "auto";
|
| 1087 | };
|
| 1088 | }, []);
|
| 1089 | usePageHide(React$1.useCallback(() => {
|
| 1090 | if (navigation.state === "idle") {
|
| 1091 | let key = getScrollRestorationKey(location, matches, basename, getKey);
|
| 1092 | savedScrollPositions[key] = window.scrollY;
|
| 1093 | }
|
| 1094 | try {
|
| 1095 | sessionStorage.setItem(storageKey || SCROLL_RESTORATION_STORAGE_KEY, JSON.stringify(savedScrollPositions));
|
| 1096 | } catch (error) {
|
| 1097 | warning(false, `Failed to save scroll positions in sessionStorage, <ScrollRestoration /> will not work properly (${error}).`);
|
| 1098 | }
|
| 1099 | window.history.scrollRestoration = "auto";
|
| 1100 | }, [
|
| 1101 | navigation.state,
|
| 1102 | getKey,
|
| 1103 | basename,
|
| 1104 | location,
|
| 1105 | matches,
|
| 1106 | storageKey
|
| 1107 | ]));
|
| 1108 | if (typeof document !== "undefined") {
|
| 1109 | React$1.useLayoutEffect(() => {
|
| 1110 | try {
|
| 1111 | let sessionPositions = sessionStorage.getItem(storageKey || SCROLL_RESTORATION_STORAGE_KEY);
|
| 1112 | if (sessionPositions) savedScrollPositions = JSON.parse(sessionPositions);
|
| 1113 | } catch (e) {}
|
| 1114 | }, [storageKey]);
|
| 1115 | React$1.useLayoutEffect(() => {
|
| 1116 | let disableScrollRestoration = router?.enableScrollRestoration(savedScrollPositions, () => window.scrollY, getKey ? (location, matches) => getScrollRestorationKey(location, matches, basename, getKey) : void 0);
|
| 1117 | return () => disableScrollRestoration && disableScrollRestoration();
|
| 1118 | }, [
|
| 1119 | router,
|
| 1120 | basename,
|
| 1121 | getKey
|
| 1122 | ]);
|
| 1123 | React$1.useLayoutEffect(() => {
|
| 1124 | if (restoreScrollPosition === false) return;
|
| 1125 | if (typeof restoreScrollPosition === "number") {
|
| 1126 | window.scrollTo(0, restoreScrollPosition);
|
| 1127 | return;
|
| 1128 | }
|
| 1129 | try {
|
| 1130 | if (location.hash) {
|
| 1131 | let el = document.getElementById(decodeURIComponent(location.hash.slice(1)));
|
| 1132 | if (el) {
|
| 1133 | el.scrollIntoView();
|
| 1134 | return;
|
| 1135 | }
|
| 1136 | }
|
| 1137 | } catch {
|
| 1138 | warning(false, `"${location.hash.slice(1)}" is not a decodable element ID. The view will not scroll to it.`);
|
| 1139 | }
|
| 1140 | if (preventScrollReset === true) return;
|
| 1141 | window.scrollTo(0, 0);
|
| 1142 | }, [
|
| 1143 | location,
|
| 1144 | restoreScrollPosition,
|
| 1145 | preventScrollReset
|
| 1146 | ]);
|
| 1147 | }
|
| 1148 | }
|
| 1149 | |
| 1150 | |
| 1151 | |
| 1152 | |
| 1153 | |
| 1154 | |
| 1155 | |
| 1156 | |
| 1157 | |
| 1158 | |
| 1159 | |
| 1160 |
|
| 1161 | function useBeforeUnload(callback, options) {
|
| 1162 | let { capture } = options || {};
|
| 1163 | React$1.useEffect(() => {
|
| 1164 | let opts = capture != null ? { capture } : void 0;
|
| 1165 | window.addEventListener("beforeunload", callback, opts);
|
| 1166 | return () => {
|
| 1167 | window.removeEventListener("beforeunload", callback, opts);
|
| 1168 | };
|
| 1169 | }, [callback, capture]);
|
| 1170 | }
|
| 1171 | function usePageHide(callback, options) {
|
| 1172 | let { capture } = options || {};
|
| 1173 | React$1.useEffect(() => {
|
| 1174 | let opts = capture != null ? { capture } : void 0;
|
| 1175 | window.addEventListener("pagehide", callback, opts);
|
| 1176 | return () => {
|
| 1177 | window.removeEventListener("pagehide", callback, opts);
|
| 1178 | };
|
| 1179 | }, [callback, capture]);
|
| 1180 | }
|
| 1181 | |
| 1182 | |
| 1183 | |
| 1184 | |
| 1185 | |
| 1186 | |
| 1187 | |
| 1188 | |
| 1189 | |
| 1190 | |
| 1191 | |
| 1192 | |
| 1193 | |
| 1194 | |
| 1195 | |
| 1196 | |
| 1197 | |
| 1198 | |
| 1199 | |
| 1200 | |
| 1201 | |
| 1202 | |
| 1203 | |
| 1204 | |
| 1205 | |
| 1206 | |
| 1207 | |
| 1208 | |
| 1209 | |
| 1210 | |
| 1211 | |
| 1212 | |
| 1213 | |
| 1214 | |
| 1215 | |
| 1216 | |
| 1217 | |
| 1218 | |
| 1219 | |
| 1220 | |
| 1221 | |
| 1222 | |
| 1223 | |
| 1224 | |
| 1225 | |
| 1226 | |
| 1227 | |
| 1228 |
|
| 1229 | function usePrompt({ when, message }) {
|
| 1230 | let blocker = useBlocker(when);
|
| 1231 | React$1.useEffect(() => {
|
| 1232 | if (blocker.state === "blocked") if (window.confirm(message)) setTimeout(blocker.proceed, 0);
|
| 1233 | else blocker.reset();
|
| 1234 | }, [blocker, message]);
|
| 1235 | React$1.useEffect(() => {
|
| 1236 | if (blocker.state === "blocked" && !when) blocker.reset();
|
| 1237 | }, [blocker, when]);
|
| 1238 | }
|
| 1239 | |
| 1240 | |
| 1241 | |
| 1242 | |
| 1243 | |
| 1244 | |
| 1245 | |
| 1246 | |
| 1247 | |
| 1248 | |
| 1249 | |
| 1250 | |
| 1251 | |
| 1252 | |
| 1253 | |
| 1254 | |
| 1255 | |
| 1256 | |
| 1257 | |
| 1258 | |
| 1259 |
|
| 1260 | function useViewTransitionState(to, { relative } = {}) {
|
| 1261 | let vtContext = React$1.useContext(ViewTransitionContext);
|
| 1262 | invariant(vtContext != null, "`useViewTransitionState` must be used within `react-router/dom`'s `RouterProvider`. Did you accidentally import `RouterProvider` from `react-router`?");
|
| 1263 | let { basename } = useDataRouterContext("useViewTransitionState");
|
| 1264 | let path = useResolvedPath(to, { relative });
|
| 1265 | if (!vtContext.isTransitioning) return false;
|
| 1266 | let currentPath = stripBasename(vtContext.currentLocation.pathname, basename) || vtContext.currentLocation.pathname;
|
| 1267 | let nextPath = stripBasename(vtContext.nextLocation.pathname, basename) || vtContext.nextLocation.pathname;
|
| 1268 | return matchPath(path.pathname, nextPath) != null || matchPath(path.pathname, currentPath) != null;
|
| 1269 | }
|
| 1270 |
|
| 1271 | export { BrowserRouter, Form, HashRouter, HistoryRouter, Link, NavLink, ScrollRestoration, createBrowserRouter, createHashRouter, useBeforeUnload, useFetcher, useFetchers, useFormAction, useLinkClickHandler, usePrompt, useScrollRestoration, useSearchParams, useSubmit, useViewTransitionState };
|