@depup/tanstack__react-router 1.166.4-depup.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +32 -0
- package/dist/cjs/Asset.cjs +177 -0
- package/dist/cjs/Asset.cjs.map +1 -0
- package/dist/cjs/Asset.d.cts +5 -0
- package/dist/cjs/CatchBoundary.cjs +114 -0
- package/dist/cjs/CatchBoundary.cjs.map +1 -0
- package/dist/cjs/CatchBoundary.d.cts +12 -0
- package/dist/cjs/ClientOnly.cjs +21 -0
- package/dist/cjs/ClientOnly.cjs.map +1 -0
- package/dist/cjs/ClientOnly.d.cts +49 -0
- package/dist/cjs/HeadContent.cjs +15 -0
- package/dist/cjs/HeadContent.cjs.map +1 -0
- package/dist/cjs/HeadContent.d.cts +6 -0
- package/dist/cjs/HeadContent.dev.cjs +41 -0
- package/dist/cjs/HeadContent.dev.cjs.map +1 -0
- package/dist/cjs/HeadContent.dev.d.cts +10 -0
- package/dist/cjs/Match.cjs +255 -0
- package/dist/cjs/Match.cjs.map +1 -0
- package/dist/cjs/Match.d.cts +14 -0
- package/dist/cjs/Matches.cjs +138 -0
- package/dist/cjs/Matches.cjs.map +1 -0
- package/dist/cjs/Matches.d.cts +68 -0
- package/dist/cjs/RouterProvider.cjs +32 -0
- package/dist/cjs/RouterProvider.cjs.map +1 -0
- package/dist/cjs/RouterProvider.d.cts +23 -0
- package/dist/cjs/SafeFragment.cjs +8 -0
- package/dist/cjs/SafeFragment.cjs.map +1 -0
- package/dist/cjs/SafeFragment.d.cts +1 -0
- package/dist/cjs/ScriptOnce.cjs +22 -0
- package/dist/cjs/ScriptOnce.cjs.map +1 -0
- package/dist/cjs/ScriptOnce.d.cts +6 -0
- package/dist/cjs/Scripts.cjs +56 -0
- package/dist/cjs/Scripts.cjs.map +1 -0
- package/dist/cjs/Scripts.d.cts +5 -0
- package/dist/cjs/ScrollRestoration.cjs +38 -0
- package/dist/cjs/ScrollRestoration.cjs.map +1 -0
- package/dist/cjs/ScrollRestoration.d.cts +14 -0
- package/dist/cjs/Transitioner.cjs +119 -0
- package/dist/cjs/Transitioner.cjs.map +1 -0
- package/dist/cjs/Transitioner.d.cts +1 -0
- package/dist/cjs/awaited.cjs +51 -0
- package/dist/cjs/awaited.cjs.map +1 -0
- package/dist/cjs/awaited.d.cts +14 -0
- package/dist/cjs/fileRoute.cjs +109 -0
- package/dist/cjs/fileRoute.cjs.map +1 -0
- package/dist/cjs/fileRoute.d.cts +87 -0
- package/dist/cjs/headContentUtils.cjs +185 -0
- package/dist/cjs/headContentUtils.cjs.map +1 -0
- package/dist/cjs/headContentUtils.d.cts +7 -0
- package/dist/cjs/history.d.cts +8 -0
- package/dist/cjs/index.cjs +241 -0
- package/dist/cjs/index.cjs.map +1 -0
- package/dist/cjs/index.d.cts +51 -0
- package/dist/cjs/index.dev.cjs +241 -0
- package/dist/cjs/index.dev.cjs.map +1 -0
- package/dist/cjs/index.dev.d.cts +2 -0
- package/dist/cjs/lazyRouteComponent.cjs +70 -0
- package/dist/cjs/lazyRouteComponent.cjs.map +1 -0
- package/dist/cjs/lazyRouteComponent.d.cts +11 -0
- package/dist/cjs/link.cjs +573 -0
- package/dist/cjs/link.cjs.map +1 -0
- package/dist/cjs/link.d.cts +98 -0
- package/dist/cjs/matchContext.cjs +27 -0
- package/dist/cjs/matchContext.cjs.map +1 -0
- package/dist/cjs/matchContext.d.cts +3 -0
- package/dist/cjs/not-found.cjs +38 -0
- package/dist/cjs/not-found.cjs.map +1 -0
- package/dist/cjs/not-found.d.cts +9 -0
- package/dist/cjs/renderRouteNotFound.cjs +22 -0
- package/dist/cjs/renderRouteNotFound.cjs.map +1 -0
- package/dist/cjs/renderRouteNotFound.d.cts +10 -0
- package/dist/cjs/route.cjs +198 -0
- package/dist/cjs/route.cjs.map +1 -0
- package/dist/cjs/route.d.cts +142 -0
- package/dist/cjs/router.cjs +22 -0
- package/dist/cjs/router.cjs.map +1 -0
- package/dist/cjs/router.d.cts +83 -0
- package/dist/cjs/routerContext.cjs +23 -0
- package/dist/cjs/routerContext.cjs.map +1 -0
- package/dist/cjs/routerContext.d.cts +3 -0
- package/dist/cjs/scroll-restoration.cjs +39 -0
- package/dist/cjs/scroll-restoration.cjs.map +1 -0
- package/dist/cjs/scroll-restoration.d.cts +1 -0
- package/dist/cjs/ssr/RouterClient.cjs +25 -0
- package/dist/cjs/ssr/RouterClient.cjs.map +1 -0
- package/dist/cjs/ssr/RouterClient.d.cts +4 -0
- package/dist/cjs/ssr/RouterServer.cjs +9 -0
- package/dist/cjs/ssr/RouterServer.cjs.map +1 -0
- package/dist/cjs/ssr/RouterServer.d.cts +4 -0
- package/dist/cjs/ssr/client.cjs +12 -0
- package/dist/cjs/ssr/client.cjs.map +1 -0
- package/dist/cjs/ssr/client.d.cts +2 -0
- package/dist/cjs/ssr/defaultRenderHandler.cjs +15 -0
- package/dist/cjs/ssr/defaultRenderHandler.cjs.map +1 -0
- package/dist/cjs/ssr/defaultRenderHandler.d.cts +1 -0
- package/dist/cjs/ssr/defaultStreamHandler.cjs +16 -0
- package/dist/cjs/ssr/defaultStreamHandler.cjs.map +1 -0
- package/dist/cjs/ssr/defaultStreamHandler.d.cts +1 -0
- package/dist/cjs/ssr/renderRouterToStream.cjs +73 -0
- package/dist/cjs/ssr/renderRouterToStream.cjs.map +1 -0
- package/dist/cjs/ssr/renderRouterToStream.d.cts +8 -0
- package/dist/cjs/ssr/renderRouterToString.cjs +31 -0
- package/dist/cjs/ssr/renderRouterToString.cjs.map +1 -0
- package/dist/cjs/ssr/renderRouterToString.d.cts +7 -0
- package/dist/cjs/ssr/serializer.d.cts +6 -0
- package/dist/cjs/ssr/server.cjs +20 -0
- package/dist/cjs/ssr/server.cjs.map +1 -0
- package/dist/cjs/ssr/server.d.cts +6 -0
- package/dist/cjs/structuralSharing.d.cts +8 -0
- package/dist/cjs/typePrimitives.d.cts +16 -0
- package/dist/cjs/useBlocker.cjs +171 -0
- package/dist/cjs/useBlocker.cjs.map +1 -0
- package/dist/cjs/useBlocker.d.cts +66 -0
- package/dist/cjs/useCanGoBack.cjs +8 -0
- package/dist/cjs/useCanGoBack.cjs.map +1 -0
- package/dist/cjs/useCanGoBack.d.cts +1 -0
- package/dist/cjs/useLoaderData.cjs +15 -0
- package/dist/cjs/useLoaderData.cjs.map +1 -0
- package/dist/cjs/useLoaderData.d.cts +19 -0
- package/dist/cjs/useLoaderDeps.cjs +14 -0
- package/dist/cjs/useLoaderDeps.cjs.map +1 -0
- package/dist/cjs/useLoaderDeps.d.cts +19 -0
- package/dist/cjs/useLocation.cjs +10 -0
- package/dist/cjs/useLocation.cjs.map +1 -0
- package/dist/cjs/useLocation.d.cts +18 -0
- package/dist/cjs/useMatch.cjs +47 -0
- package/dist/cjs/useMatch.cjs.map +1 -0
- package/dist/cjs/useMatch.d.cts +14 -0
- package/dist/cjs/useNavigate.cjs +49 -0
- package/dist/cjs/useNavigate.cjs.map +1 -0
- package/dist/cjs/useNavigate.d.cts +28 -0
- package/dist/cjs/useParams.cjs +17 -0
- package/dist/cjs/useParams.cjs.map +1 -0
- package/dist/cjs/useParams.d.cts +21 -0
- package/dist/cjs/useRouteContext.cjs +11 -0
- package/dist/cjs/useRouteContext.cjs.map +1 -0
- package/dist/cjs/useRouteContext.d.cts +3 -0
- package/dist/cjs/useRouter.cjs +32 -0
- package/dist/cjs/useRouter.cjs.map +1 -0
- package/dist/cjs/useRouter.d.cts +14 -0
- package/dist/cjs/useRouterState.cjs +38 -0
- package/dist/cjs/useRouterState.cjs.map +1 -0
- package/dist/cjs/useRouterState.d.cts +20 -0
- package/dist/cjs/useSearch.cjs +16 -0
- package/dist/cjs/useSearch.cjs.map +1 -0
- package/dist/cjs/useSearch.d.cts +21 -0
- package/dist/cjs/utils.cjs +62 -0
- package/dist/cjs/utils.cjs.map +1 -0
- package/dist/cjs/utils.d.cts +54 -0
- package/dist/esm/Asset.d.ts +5 -0
- package/dist/esm/Asset.js +160 -0
- package/dist/esm/Asset.js.map +1 -0
- package/dist/esm/CatchBoundary.d.ts +12 -0
- package/dist/esm/CatchBoundary.js +97 -0
- package/dist/esm/CatchBoundary.js.map +1 -0
- package/dist/esm/ClientOnly.d.ts +49 -0
- package/dist/esm/ClientOnly.js +21 -0
- package/dist/esm/ClientOnly.js.map +1 -0
- package/dist/esm/HeadContent.d.ts +6 -0
- package/dist/esm/HeadContent.dev.d.ts +10 -0
- package/dist/esm/HeadContent.dev.js +25 -0
- package/dist/esm/HeadContent.dev.js.map +1 -0
- package/dist/esm/HeadContent.js +15 -0
- package/dist/esm/HeadContent.js.map +1 -0
- package/dist/esm/Match.d.ts +14 -0
- package/dist/esm/Match.js +238 -0
- package/dist/esm/Match.js.map +1 -0
- package/dist/esm/Matches.d.ts +68 -0
- package/dist/esm/Matches.js +121 -0
- package/dist/esm/Matches.js.map +1 -0
- package/dist/esm/RouterProvider.d.ts +23 -0
- package/dist/esm/RouterProvider.js +32 -0
- package/dist/esm/RouterProvider.js.map +1 -0
- package/dist/esm/SafeFragment.d.ts +1 -0
- package/dist/esm/SafeFragment.js +8 -0
- package/dist/esm/SafeFragment.js.map +1 -0
- package/dist/esm/ScriptOnce.d.ts +6 -0
- package/dist/esm/ScriptOnce.js +22 -0
- package/dist/esm/ScriptOnce.js.map +1 -0
- package/dist/esm/Scripts.d.ts +5 -0
- package/dist/esm/Scripts.js +56 -0
- package/dist/esm/Scripts.js.map +1 -0
- package/dist/esm/ScrollRestoration.d.ts +14 -0
- package/dist/esm/ScrollRestoration.js +38 -0
- package/dist/esm/ScrollRestoration.js.map +1 -0
- package/dist/esm/Transitioner.d.ts +1 -0
- package/dist/esm/Transitioner.js +102 -0
- package/dist/esm/Transitioner.js.map +1 -0
- package/dist/esm/awaited.d.ts +14 -0
- package/dist/esm/awaited.js +34 -0
- package/dist/esm/awaited.js.map +1 -0
- package/dist/esm/fileRoute.d.ts +87 -0
- package/dist/esm/fileRoute.js +109 -0
- package/dist/esm/fileRoute.js.map +1 -0
- package/dist/esm/headContentUtils.d.ts +7 -0
- package/dist/esm/headContentUtils.js +168 -0
- package/dist/esm/headContentUtils.js.map +1 -0
- package/dist/esm/history.d.ts +8 -0
- package/dist/esm/index.d.ts +51 -0
- package/dist/esm/index.dev.d.ts +2 -0
- package/dist/esm/index.dev.js +133 -0
- package/dist/esm/index.dev.js.map +1 -0
- package/dist/esm/index.js +133 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/lazyRouteComponent.d.ts +11 -0
- package/dist/esm/lazyRouteComponent.js +53 -0
- package/dist/esm/lazyRouteComponent.js.map +1 -0
- package/dist/esm/link.d.ts +98 -0
- package/dist/esm/link.js +556 -0
- package/dist/esm/link.js.map +1 -0
- package/dist/esm/matchContext.d.ts +3 -0
- package/dist/esm/matchContext.js +10 -0
- package/dist/esm/matchContext.js.map +1 -0
- package/dist/esm/not-found.d.ts +9 -0
- package/dist/esm/not-found.js +38 -0
- package/dist/esm/not-found.js.map +1 -0
- package/dist/esm/renderRouteNotFound.d.ts +10 -0
- package/dist/esm/renderRouteNotFound.js +22 -0
- package/dist/esm/renderRouteNotFound.js.map +1 -0
- package/dist/esm/route.d.ts +142 -0
- package/dist/esm/route.js +198 -0
- package/dist/esm/route.js.map +1 -0
- package/dist/esm/router.d.ts +83 -0
- package/dist/esm/router.js +22 -0
- package/dist/esm/router.js.map +1 -0
- package/dist/esm/routerContext.d.ts +3 -0
- package/dist/esm/routerContext.js +6 -0
- package/dist/esm/routerContext.js.map +1 -0
- package/dist/esm/scroll-restoration.d.ts +1 -0
- package/dist/esm/scroll-restoration.js +39 -0
- package/dist/esm/scroll-restoration.js.map +1 -0
- package/dist/esm/ssr/RouterClient.d.ts +4 -0
- package/dist/esm/ssr/RouterClient.js +25 -0
- package/dist/esm/ssr/RouterClient.js.map +1 -0
- package/dist/esm/ssr/RouterServer.d.ts +4 -0
- package/dist/esm/ssr/RouterServer.js +9 -0
- package/dist/esm/ssr/RouterServer.js.map +1 -0
- package/dist/esm/ssr/client.d.ts +2 -0
- package/dist/esm/ssr/client.js +6 -0
- package/dist/esm/ssr/client.js.map +1 -0
- package/dist/esm/ssr/defaultRenderHandler.d.ts +1 -0
- package/dist/esm/ssr/defaultRenderHandler.js +15 -0
- package/dist/esm/ssr/defaultRenderHandler.js.map +1 -0
- package/dist/esm/ssr/defaultStreamHandler.d.ts +1 -0
- package/dist/esm/ssr/defaultStreamHandler.js +16 -0
- package/dist/esm/ssr/defaultStreamHandler.js.map +1 -0
- package/dist/esm/ssr/renderRouterToStream.d.ts +8 -0
- package/dist/esm/ssr/renderRouterToStream.js +73 -0
- package/dist/esm/ssr/renderRouterToStream.js.map +1 -0
- package/dist/esm/ssr/renderRouterToString.d.ts +7 -0
- package/dist/esm/ssr/renderRouterToString.js +31 -0
- package/dist/esm/ssr/renderRouterToString.js.map +1 -0
- package/dist/esm/ssr/serializer.d.ts +6 -0
- package/dist/esm/ssr/server.d.ts +6 -0
- package/dist/esm/ssr/server.js +14 -0
- package/dist/esm/ssr/server.js.map +1 -0
- package/dist/esm/structuralSharing.d.ts +8 -0
- package/dist/esm/typePrimitives.d.ts +16 -0
- package/dist/esm/useBlocker.d.ts +66 -0
- package/dist/esm/useBlocker.js +154 -0
- package/dist/esm/useBlocker.js.map +1 -0
- package/dist/esm/useCanGoBack.d.ts +1 -0
- package/dist/esm/useCanGoBack.js +8 -0
- package/dist/esm/useCanGoBack.js.map +1 -0
- package/dist/esm/useLoaderData.d.ts +19 -0
- package/dist/esm/useLoaderData.js +15 -0
- package/dist/esm/useLoaderData.js.map +1 -0
- package/dist/esm/useLoaderDeps.d.ts +19 -0
- package/dist/esm/useLoaderDeps.js +14 -0
- package/dist/esm/useLoaderDeps.js.map +1 -0
- package/dist/esm/useLocation.d.ts +18 -0
- package/dist/esm/useLocation.js +10 -0
- package/dist/esm/useLocation.js.map +1 -0
- package/dist/esm/useMatch.d.ts +14 -0
- package/dist/esm/useMatch.js +30 -0
- package/dist/esm/useMatch.js.map +1 -0
- package/dist/esm/useNavigate.d.ts +28 -0
- package/dist/esm/useNavigate.js +32 -0
- package/dist/esm/useNavigate.js.map +1 -0
- package/dist/esm/useParams.d.ts +21 -0
- package/dist/esm/useParams.js +17 -0
- package/dist/esm/useParams.js.map +1 -0
- package/dist/esm/useRouteContext.d.ts +3 -0
- package/dist/esm/useRouteContext.js +11 -0
- package/dist/esm/useRouteContext.js.map +1 -0
- package/dist/esm/useRouter.d.ts +14 -0
- package/dist/esm/useRouter.js +15 -0
- package/dist/esm/useRouter.js.map +1 -0
- package/dist/esm/useRouterState.d.ts +20 -0
- package/dist/esm/useRouterState.js +38 -0
- package/dist/esm/useRouterState.js.map +1 -0
- package/dist/esm/useSearch.d.ts +21 -0
- package/dist/esm/useSearch.js +16 -0
- package/dist/esm/useSearch.js.map +1 -0
- package/dist/esm/utils.d.ts +54 -0
- package/dist/esm/utils.js +45 -0
- package/dist/esm/utils.js.map +1 -0
- package/dist/llms/index.d.ts +3 -0
- package/dist/llms/index.js +43 -0
- package/dist/llms/rules/api.d.ts +2 -0
- package/dist/llms/rules/api.js +4612 -0
- package/dist/llms/rules/guide.d.ts +2 -0
- package/dist/llms/rules/guide.js +10690 -0
- package/dist/llms/rules/installation.d.ts +2 -0
- package/dist/llms/rules/installation.js +1285 -0
- package/dist/llms/rules/routing.d.ts +2 -0
- package/dist/llms/rules/routing.js +1984 -0
- package/dist/llms/rules/setup-and-architecture.d.ts +2 -0
- package/dist/llms/rules/setup-and-architecture.js +920 -0
- package/package.json +142 -0
- package/src/Asset.tsx +219 -0
- package/src/CatchBoundary.tsx +120 -0
- package/src/ClientOnly.tsx +68 -0
- package/src/HeadContent.dev.tsx +46 -0
- package/src/HeadContent.tsx +22 -0
- package/src/Match.tsx +360 -0
- package/src/Matches.tsx +313 -0
- package/src/RouterProvider.tsx +92 -0
- package/src/SafeFragment.tsx +5 -0
- package/src/ScriptOnce.tsx +21 -0
- package/src/Scripts.tsx +80 -0
- package/src/ScrollRestoration.tsx +69 -0
- package/src/Transitioner.tsx +134 -0
- package/src/awaited.tsx +55 -0
- package/src/fileRoute.ts +313 -0
- package/src/headContentUtils.tsx +217 -0
- package/src/history.ts +9 -0
- package/src/index.dev.tsx +6 -0
- package/src/index.tsx +341 -0
- package/src/lazyRouteComponent.tsx +96 -0
- package/src/link.tsx +984 -0
- package/src/matchContext.tsx +8 -0
- package/src/not-found.tsx +43 -0
- package/src/renderRouteNotFound.tsx +35 -0
- package/src/route.tsx +740 -0
- package/src/router.ts +127 -0
- package/src/routerContext.tsx +4 -0
- package/src/scroll-restoration.tsx +45 -0
- package/src/ssr/RouterClient.tsx +22 -0
- package/src/ssr/RouterServer.tsx +9 -0
- package/src/ssr/client.ts +2 -0
- package/src/ssr/defaultRenderHandler.tsx +12 -0
- package/src/ssr/defaultStreamHandler.tsx +13 -0
- package/src/ssr/renderRouterToStream.tsx +90 -0
- package/src/ssr/renderRouterToString.tsx +36 -0
- package/src/ssr/serializer.ts +7 -0
- package/src/ssr/server.ts +6 -0
- package/src/structuralSharing.ts +47 -0
- package/src/typePrimitives.ts +84 -0
- package/src/useBlocker.tsx +320 -0
- package/src/useCanGoBack.ts +5 -0
- package/src/useLoaderData.tsx +91 -0
- package/src/useLoaderDeps.tsx +69 -0
- package/src/useLocation.tsx +52 -0
- package/src/useMatch.tsx +123 -0
- package/src/useNavigate.tsx +78 -0
- package/src/useParams.tsx +107 -0
- package/src/useRouteContext.ts +30 -0
- package/src/useRouter.tsx +25 -0
- package/src/useRouterState.tsx +86 -0
- package/src/useSearch.tsx +105 -0
- package/src/utils.ts +125 -0
package/src/Match.tsx
ADDED
|
@@ -0,0 +1,360 @@
|
|
|
1
|
+
import * as React from 'react'
|
|
2
|
+
import invariant from 'tiny-invariant'
|
|
3
|
+
import warning from 'tiny-warning'
|
|
4
|
+
import {
|
|
5
|
+
createControlledPromise,
|
|
6
|
+
getLocationChangeInfo,
|
|
7
|
+
isNotFound,
|
|
8
|
+
isRedirect,
|
|
9
|
+
rootRouteId,
|
|
10
|
+
} from '@tanstack/router-core'
|
|
11
|
+
import { isServer } from '@tanstack/router-core/isServer'
|
|
12
|
+
import { CatchBoundary, ErrorComponent } from './CatchBoundary'
|
|
13
|
+
import { useRouterState } from './useRouterState'
|
|
14
|
+
import { useRouter } from './useRouter'
|
|
15
|
+
import { CatchNotFound } from './not-found'
|
|
16
|
+
import { matchContext } from './matchContext'
|
|
17
|
+
import { SafeFragment } from './SafeFragment'
|
|
18
|
+
import { renderRouteNotFound } from './renderRouteNotFound'
|
|
19
|
+
import { ScrollRestoration } from './scroll-restoration'
|
|
20
|
+
import { ClientOnly } from './ClientOnly'
|
|
21
|
+
import type {
|
|
22
|
+
AnyRoute,
|
|
23
|
+
ParsedLocation,
|
|
24
|
+
RootRouteOptions,
|
|
25
|
+
} from '@tanstack/router-core'
|
|
26
|
+
|
|
27
|
+
export const Match = React.memo(function MatchImpl({
|
|
28
|
+
matchId,
|
|
29
|
+
}: {
|
|
30
|
+
matchId: string
|
|
31
|
+
}) {
|
|
32
|
+
const router = useRouter()
|
|
33
|
+
const matchState = useRouterState({
|
|
34
|
+
select: (s) => {
|
|
35
|
+
const matchIndex = s.matches.findIndex((d) => d.id === matchId)
|
|
36
|
+
const match = s.matches[matchIndex]
|
|
37
|
+
invariant(
|
|
38
|
+
match,
|
|
39
|
+
`Could not find match for matchId "${matchId}". Please file an issue!`,
|
|
40
|
+
)
|
|
41
|
+
return {
|
|
42
|
+
routeId: match.routeId,
|
|
43
|
+
ssr: match.ssr,
|
|
44
|
+
_displayPending: match._displayPending,
|
|
45
|
+
resetKey: s.loadedAt,
|
|
46
|
+
parentRouteId: s.matches[matchIndex - 1]?.routeId as string,
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
structuralSharing: true as any,
|
|
50
|
+
})
|
|
51
|
+
|
|
52
|
+
const route: AnyRoute = router.routesById[matchState.routeId]
|
|
53
|
+
|
|
54
|
+
const PendingComponent =
|
|
55
|
+
route.options.pendingComponent ?? router.options.defaultPendingComponent
|
|
56
|
+
|
|
57
|
+
const pendingElement = PendingComponent ? <PendingComponent /> : null
|
|
58
|
+
|
|
59
|
+
const routeErrorComponent =
|
|
60
|
+
route.options.errorComponent ?? router.options.defaultErrorComponent
|
|
61
|
+
|
|
62
|
+
const routeOnCatch = route.options.onCatch ?? router.options.defaultOnCatch
|
|
63
|
+
|
|
64
|
+
const routeNotFoundComponent = route.isRoot
|
|
65
|
+
? // If it's the root route, use the globalNotFound option, with fallback to the notFoundRoute's component
|
|
66
|
+
(route.options.notFoundComponent ??
|
|
67
|
+
router.options.notFoundRoute?.options.component)
|
|
68
|
+
: route.options.notFoundComponent
|
|
69
|
+
|
|
70
|
+
const resolvedNoSsr =
|
|
71
|
+
matchState.ssr === false || matchState.ssr === 'data-only'
|
|
72
|
+
const ResolvedSuspenseBoundary =
|
|
73
|
+
// If we're on the root route, allow forcefully wrapping in suspense
|
|
74
|
+
(!route.isRoot || route.options.wrapInSuspense || resolvedNoSsr) &&
|
|
75
|
+
(route.options.wrapInSuspense ??
|
|
76
|
+
PendingComponent ??
|
|
77
|
+
((route.options.errorComponent as any)?.preload || resolvedNoSsr))
|
|
78
|
+
? React.Suspense
|
|
79
|
+
: SafeFragment
|
|
80
|
+
|
|
81
|
+
const ResolvedCatchBoundary = routeErrorComponent
|
|
82
|
+
? CatchBoundary
|
|
83
|
+
: SafeFragment
|
|
84
|
+
|
|
85
|
+
const ResolvedNotFoundBoundary = routeNotFoundComponent
|
|
86
|
+
? CatchNotFound
|
|
87
|
+
: SafeFragment
|
|
88
|
+
|
|
89
|
+
const ShellComponent = route.isRoot
|
|
90
|
+
? ((route.options as RootRouteOptions).shellComponent ?? SafeFragment)
|
|
91
|
+
: SafeFragment
|
|
92
|
+
return (
|
|
93
|
+
<ShellComponent>
|
|
94
|
+
<matchContext.Provider value={matchId}>
|
|
95
|
+
<ResolvedSuspenseBoundary fallback={pendingElement}>
|
|
96
|
+
<ResolvedCatchBoundary
|
|
97
|
+
getResetKey={() => matchState.resetKey}
|
|
98
|
+
errorComponent={routeErrorComponent || ErrorComponent}
|
|
99
|
+
onCatch={(error, errorInfo) => {
|
|
100
|
+
// Forward not found errors (we don't want to show the error component for these)
|
|
101
|
+
if (isNotFound(error)) throw error
|
|
102
|
+
warning(false, `Error in route match: ${matchId}`)
|
|
103
|
+
routeOnCatch?.(error, errorInfo)
|
|
104
|
+
}}
|
|
105
|
+
>
|
|
106
|
+
<ResolvedNotFoundBoundary
|
|
107
|
+
fallback={(error) => {
|
|
108
|
+
// If the current not found handler doesn't exist or it has a
|
|
109
|
+
// route ID which doesn't match the current route, rethrow the error
|
|
110
|
+
if (
|
|
111
|
+
!routeNotFoundComponent ||
|
|
112
|
+
(error.routeId && error.routeId !== matchState.routeId) ||
|
|
113
|
+
(!error.routeId && !route.isRoot)
|
|
114
|
+
)
|
|
115
|
+
throw error
|
|
116
|
+
|
|
117
|
+
return React.createElement(routeNotFoundComponent, error as any)
|
|
118
|
+
}}
|
|
119
|
+
>
|
|
120
|
+
{resolvedNoSsr || matchState._displayPending ? (
|
|
121
|
+
<ClientOnly fallback={pendingElement}>
|
|
122
|
+
<MatchInner matchId={matchId} />
|
|
123
|
+
</ClientOnly>
|
|
124
|
+
) : (
|
|
125
|
+
<MatchInner matchId={matchId} />
|
|
126
|
+
)}
|
|
127
|
+
</ResolvedNotFoundBoundary>
|
|
128
|
+
</ResolvedCatchBoundary>
|
|
129
|
+
</ResolvedSuspenseBoundary>
|
|
130
|
+
</matchContext.Provider>
|
|
131
|
+
{matchState.parentRouteId === rootRouteId &&
|
|
132
|
+
router.options.scrollRestoration ? (
|
|
133
|
+
<>
|
|
134
|
+
<OnRendered />
|
|
135
|
+
<ScrollRestoration />
|
|
136
|
+
</>
|
|
137
|
+
) : null}
|
|
138
|
+
</ShellComponent>
|
|
139
|
+
)
|
|
140
|
+
})
|
|
141
|
+
|
|
142
|
+
// On Rendered can't happen above the root layout because it actually
|
|
143
|
+
// renders a dummy dom element to track the rendered state of the app.
|
|
144
|
+
// We render a script tag with a key that changes based on the current
|
|
145
|
+
// location state.__TSR_key. Also, because it's below the root layout, it
|
|
146
|
+
// allows us to fire onRendered events even after a hydration mismatch
|
|
147
|
+
// error that occurred above the root layout (like bad head/link tags,
|
|
148
|
+
// which is common).
|
|
149
|
+
function OnRendered() {
|
|
150
|
+
const router = useRouter()
|
|
151
|
+
|
|
152
|
+
const prevLocationRef = React.useRef<undefined | ParsedLocation<{}>>(
|
|
153
|
+
undefined,
|
|
154
|
+
)
|
|
155
|
+
|
|
156
|
+
return (
|
|
157
|
+
<script
|
|
158
|
+
key={router.latestLocation.state.__TSR_key}
|
|
159
|
+
suppressHydrationWarning
|
|
160
|
+
ref={(el) => {
|
|
161
|
+
if (
|
|
162
|
+
el &&
|
|
163
|
+
(prevLocationRef.current === undefined ||
|
|
164
|
+
prevLocationRef.current.href !== router.latestLocation.href)
|
|
165
|
+
) {
|
|
166
|
+
router.emit({
|
|
167
|
+
type: 'onRendered',
|
|
168
|
+
...getLocationChangeInfo(router.state),
|
|
169
|
+
})
|
|
170
|
+
prevLocationRef.current = router.latestLocation
|
|
171
|
+
}
|
|
172
|
+
}}
|
|
173
|
+
/>
|
|
174
|
+
)
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
export const MatchInner = React.memo(function MatchInnerImpl({
|
|
178
|
+
matchId,
|
|
179
|
+
}: {
|
|
180
|
+
matchId: string
|
|
181
|
+
}): any {
|
|
182
|
+
const router = useRouter()
|
|
183
|
+
|
|
184
|
+
const { match, key, routeId } = useRouterState({
|
|
185
|
+
select: (s) => {
|
|
186
|
+
const match = s.matches.find((d) => d.id === matchId)!
|
|
187
|
+
const routeId = match.routeId as string
|
|
188
|
+
|
|
189
|
+
const remountFn =
|
|
190
|
+
(router.routesById[routeId] as AnyRoute).options.remountDeps ??
|
|
191
|
+
router.options.defaultRemountDeps
|
|
192
|
+
const remountDeps = remountFn?.({
|
|
193
|
+
routeId,
|
|
194
|
+
loaderDeps: match.loaderDeps,
|
|
195
|
+
params: match._strictParams,
|
|
196
|
+
search: match._strictSearch,
|
|
197
|
+
})
|
|
198
|
+
const key = remountDeps ? JSON.stringify(remountDeps) : undefined
|
|
199
|
+
|
|
200
|
+
return {
|
|
201
|
+
key,
|
|
202
|
+
routeId,
|
|
203
|
+
match: {
|
|
204
|
+
id: match.id,
|
|
205
|
+
status: match.status,
|
|
206
|
+
error: match.error,
|
|
207
|
+
invalid: match.invalid,
|
|
208
|
+
_forcePending: match._forcePending,
|
|
209
|
+
_displayPending: match._displayPending,
|
|
210
|
+
},
|
|
211
|
+
}
|
|
212
|
+
},
|
|
213
|
+
structuralSharing: true as any,
|
|
214
|
+
})
|
|
215
|
+
|
|
216
|
+
const route = router.routesById[routeId] as AnyRoute
|
|
217
|
+
|
|
218
|
+
const out = React.useMemo(() => {
|
|
219
|
+
const Comp = route.options.component ?? router.options.defaultComponent
|
|
220
|
+
if (Comp) {
|
|
221
|
+
return <Comp key={key} />
|
|
222
|
+
}
|
|
223
|
+
return <Outlet />
|
|
224
|
+
}, [key, route.options.component, router.options.defaultComponent])
|
|
225
|
+
|
|
226
|
+
if (match._displayPending) {
|
|
227
|
+
throw router.getMatch(match.id)?._nonReactive.displayPendingPromise
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
if (match._forcePending) {
|
|
231
|
+
throw router.getMatch(match.id)?._nonReactive.minPendingPromise
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
// see also hydrate() in packages/router-core/src/ssr/ssr-client.ts
|
|
235
|
+
if (match.status === 'pending') {
|
|
236
|
+
// We're pending, and if we have a minPendingMs, we need to wait for it
|
|
237
|
+
const pendingMinMs =
|
|
238
|
+
route.options.pendingMinMs ?? router.options.defaultPendingMinMs
|
|
239
|
+
if (pendingMinMs) {
|
|
240
|
+
const routerMatch = router.getMatch(match.id)
|
|
241
|
+
if (routerMatch && !routerMatch._nonReactive.minPendingPromise) {
|
|
242
|
+
// Create a promise that will resolve after the minPendingMs
|
|
243
|
+
if (!(isServer ?? router.isServer)) {
|
|
244
|
+
const minPendingPromise = createControlledPromise<void>()
|
|
245
|
+
|
|
246
|
+
routerMatch._nonReactive.minPendingPromise = minPendingPromise
|
|
247
|
+
|
|
248
|
+
setTimeout(() => {
|
|
249
|
+
minPendingPromise.resolve()
|
|
250
|
+
// We've handled the minPendingPromise, so we can delete it
|
|
251
|
+
routerMatch._nonReactive.minPendingPromise = undefined
|
|
252
|
+
}, pendingMinMs)
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
throw router.getMatch(match.id)?._nonReactive.loadPromise
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
if (match.status === 'notFound') {
|
|
260
|
+
invariant(isNotFound(match.error), 'Expected a notFound error')
|
|
261
|
+
return renderRouteNotFound(router, route, match.error)
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
if (match.status === 'redirected') {
|
|
265
|
+
// Redirects should be handled by the router transition. If we happen to
|
|
266
|
+
// encounter a redirect here, it's a bug. Let's warn, but render nothing.
|
|
267
|
+
invariant(isRedirect(match.error), 'Expected a redirect error')
|
|
268
|
+
|
|
269
|
+
// warning(
|
|
270
|
+
// false,
|
|
271
|
+
// 'Tried to render a redirected route match! This is a weird circumstance, please file an issue!',
|
|
272
|
+
// )
|
|
273
|
+
throw router.getMatch(match.id)?._nonReactive.loadPromise
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
if (match.status === 'error') {
|
|
277
|
+
// If we're on the server, we need to use React's new and super
|
|
278
|
+
// wonky api for throwing errors from a server side render inside
|
|
279
|
+
// of a suspense boundary. This is the only way to get
|
|
280
|
+
// renderToPipeableStream to not hang indefinitely.
|
|
281
|
+
// We'll serialize the error and rethrow it on the client.
|
|
282
|
+
if (isServer ?? router.isServer) {
|
|
283
|
+
const RouteErrorComponent =
|
|
284
|
+
(route.options.errorComponent ??
|
|
285
|
+
router.options.defaultErrorComponent) ||
|
|
286
|
+
ErrorComponent
|
|
287
|
+
return (
|
|
288
|
+
<RouteErrorComponent
|
|
289
|
+
error={match.error as any}
|
|
290
|
+
reset={undefined as any}
|
|
291
|
+
info={{
|
|
292
|
+
componentStack: '',
|
|
293
|
+
}}
|
|
294
|
+
/>
|
|
295
|
+
)
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
throw match.error
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
return out
|
|
302
|
+
})
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* Render the next child match in the route tree. Typically used inside
|
|
306
|
+
* a route component to render nested routes.
|
|
307
|
+
*
|
|
308
|
+
* @link https://tanstack.com/router/latest/docs/framework/react/api/router/outletComponent
|
|
309
|
+
*/
|
|
310
|
+
export const Outlet = React.memo(function OutletImpl() {
|
|
311
|
+
const router = useRouter()
|
|
312
|
+
const matchId = React.useContext(matchContext)
|
|
313
|
+
const routeId = useRouterState({
|
|
314
|
+
select: (s) => s.matches.find((d) => d.id === matchId)?.routeId as string,
|
|
315
|
+
})
|
|
316
|
+
|
|
317
|
+
const route = router.routesById[routeId]!
|
|
318
|
+
|
|
319
|
+
const parentGlobalNotFound = useRouterState({
|
|
320
|
+
select: (s) => {
|
|
321
|
+
const matches = s.matches
|
|
322
|
+
const parentMatch = matches.find((d) => d.id === matchId)
|
|
323
|
+
invariant(
|
|
324
|
+
parentMatch,
|
|
325
|
+
`Could not find parent match for matchId "${matchId}"`,
|
|
326
|
+
)
|
|
327
|
+
return parentMatch.globalNotFound
|
|
328
|
+
},
|
|
329
|
+
})
|
|
330
|
+
|
|
331
|
+
const childMatchId = useRouterState({
|
|
332
|
+
select: (s) => {
|
|
333
|
+
const matches = s.matches
|
|
334
|
+
const index = matches.findIndex((d) => d.id === matchId)
|
|
335
|
+
return matches[index + 1]?.id
|
|
336
|
+
},
|
|
337
|
+
})
|
|
338
|
+
|
|
339
|
+
const pendingElement = router.options.defaultPendingComponent ? (
|
|
340
|
+
<router.options.defaultPendingComponent />
|
|
341
|
+
) : null
|
|
342
|
+
|
|
343
|
+
if (parentGlobalNotFound) {
|
|
344
|
+
return renderRouteNotFound(router, route, undefined)
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
if (!childMatchId) {
|
|
348
|
+
return null
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
const nextMatch = <Match matchId={childMatchId} />
|
|
352
|
+
|
|
353
|
+
if (routeId === rootRouteId) {
|
|
354
|
+
return (
|
|
355
|
+
<React.Suspense fallback={pendingElement}>{nextMatch}</React.Suspense>
|
|
356
|
+
)
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
return nextMatch
|
|
360
|
+
})
|
package/src/Matches.tsx
ADDED
|
@@ -0,0 +1,313 @@
|
|
|
1
|
+
import * as React from 'react'
|
|
2
|
+
import warning from 'tiny-warning'
|
|
3
|
+
import { rootRouteId } from '@tanstack/router-core'
|
|
4
|
+
import { isServer } from '@tanstack/router-core/isServer'
|
|
5
|
+
import { CatchBoundary, ErrorComponent } from './CatchBoundary'
|
|
6
|
+
import { useRouterState } from './useRouterState'
|
|
7
|
+
import { useRouter } from './useRouter'
|
|
8
|
+
import { Transitioner } from './Transitioner'
|
|
9
|
+
import { matchContext } from './matchContext'
|
|
10
|
+
import { Match } from './Match'
|
|
11
|
+
import { SafeFragment } from './SafeFragment'
|
|
12
|
+
import type {
|
|
13
|
+
StructuralSharingOption,
|
|
14
|
+
ValidateSelected,
|
|
15
|
+
} from './structuralSharing'
|
|
16
|
+
import type {
|
|
17
|
+
AnyRoute,
|
|
18
|
+
AnyRouter,
|
|
19
|
+
DeepPartial,
|
|
20
|
+
Expand,
|
|
21
|
+
MakeOptionalPathParams,
|
|
22
|
+
MakeOptionalSearchParams,
|
|
23
|
+
MakeRouteMatchUnion,
|
|
24
|
+
MaskOptions,
|
|
25
|
+
MatchRouteOptions,
|
|
26
|
+
NoInfer,
|
|
27
|
+
RegisteredRouter,
|
|
28
|
+
ResolveRelativePath,
|
|
29
|
+
ResolveRoute,
|
|
30
|
+
RouteByPath,
|
|
31
|
+
RouterState,
|
|
32
|
+
ToSubOptionsProps,
|
|
33
|
+
} from '@tanstack/router-core'
|
|
34
|
+
|
|
35
|
+
declare module '@tanstack/router-core' {
|
|
36
|
+
export interface RouteMatchExtensions {
|
|
37
|
+
meta?: Array<React.JSX.IntrinsicElements['meta'] | undefined>
|
|
38
|
+
links?: Array<React.JSX.IntrinsicElements['link'] | undefined>
|
|
39
|
+
scripts?: Array<React.JSX.IntrinsicElements['script'] | undefined>
|
|
40
|
+
styles?: Array<React.JSX.IntrinsicElements['style'] | undefined>
|
|
41
|
+
headScripts?: Array<React.JSX.IntrinsicElements['script'] | undefined>
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Internal component that renders the router's active match tree with
|
|
47
|
+
* suspense, error, and not-found boundaries. Rendered by `RouterProvider`.
|
|
48
|
+
*/
|
|
49
|
+
export function Matches() {
|
|
50
|
+
const router = useRouter()
|
|
51
|
+
const rootRoute: AnyRoute = router.routesById[rootRouteId]
|
|
52
|
+
|
|
53
|
+
const PendingComponent =
|
|
54
|
+
rootRoute.options.pendingComponent ?? router.options.defaultPendingComponent
|
|
55
|
+
|
|
56
|
+
const pendingElement = PendingComponent ? <PendingComponent /> : null
|
|
57
|
+
|
|
58
|
+
// Do not render a root Suspense during SSR or hydrating from SSR
|
|
59
|
+
const ResolvedSuspense =
|
|
60
|
+
(isServer ?? router.isServer) ||
|
|
61
|
+
(typeof document !== 'undefined' && router.ssr)
|
|
62
|
+
? SafeFragment
|
|
63
|
+
: React.Suspense
|
|
64
|
+
|
|
65
|
+
const inner = (
|
|
66
|
+
<ResolvedSuspense fallback={pendingElement}>
|
|
67
|
+
{!(isServer ?? router.isServer) && <Transitioner />}
|
|
68
|
+
<MatchesInner />
|
|
69
|
+
</ResolvedSuspense>
|
|
70
|
+
)
|
|
71
|
+
|
|
72
|
+
return router.options.InnerWrap ? (
|
|
73
|
+
<router.options.InnerWrap>{inner}</router.options.InnerWrap>
|
|
74
|
+
) : (
|
|
75
|
+
inner
|
|
76
|
+
)
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function MatchesInner() {
|
|
80
|
+
const router = useRouter()
|
|
81
|
+
const matchId = useRouterState({
|
|
82
|
+
select: (s) => {
|
|
83
|
+
return s.matches[0]?.id
|
|
84
|
+
},
|
|
85
|
+
})
|
|
86
|
+
|
|
87
|
+
const resetKey = useRouterState({
|
|
88
|
+
select: (s) => s.loadedAt,
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
const matchComponent = matchId ? <Match matchId={matchId} /> : null
|
|
92
|
+
|
|
93
|
+
return (
|
|
94
|
+
<matchContext.Provider value={matchId}>
|
|
95
|
+
{router.options.disableGlobalCatchBoundary ? (
|
|
96
|
+
matchComponent
|
|
97
|
+
) : (
|
|
98
|
+
<CatchBoundary
|
|
99
|
+
getResetKey={() => resetKey}
|
|
100
|
+
errorComponent={ErrorComponent}
|
|
101
|
+
onCatch={
|
|
102
|
+
process.env.NODE_ENV !== 'production'
|
|
103
|
+
? (error) => {
|
|
104
|
+
warning(
|
|
105
|
+
false,
|
|
106
|
+
`The following error wasn't caught by any route! At the very least, consider setting an 'errorComponent' in your RootRoute!`,
|
|
107
|
+
)
|
|
108
|
+
warning(false, error.message || error.toString())
|
|
109
|
+
}
|
|
110
|
+
: undefined
|
|
111
|
+
}
|
|
112
|
+
>
|
|
113
|
+
{matchComponent}
|
|
114
|
+
</CatchBoundary>
|
|
115
|
+
)}
|
|
116
|
+
</matchContext.Provider>
|
|
117
|
+
)
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
export type UseMatchRouteOptions<
|
|
121
|
+
TRouter extends AnyRouter = RegisteredRouter,
|
|
122
|
+
TFrom extends string = string,
|
|
123
|
+
TTo extends string | undefined = undefined,
|
|
124
|
+
TMaskFrom extends string = TFrom,
|
|
125
|
+
TMaskTo extends string = '',
|
|
126
|
+
> = ToSubOptionsProps<TRouter, TFrom, TTo> &
|
|
127
|
+
DeepPartial<MakeOptionalSearchParams<TRouter, TFrom, TTo>> &
|
|
128
|
+
DeepPartial<MakeOptionalPathParams<TRouter, TFrom, TTo>> &
|
|
129
|
+
MaskOptions<TRouter, TMaskFrom, TMaskTo> &
|
|
130
|
+
MatchRouteOptions
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Create a matcher function for testing locations against route definitions.
|
|
134
|
+
*
|
|
135
|
+
* The returned function accepts standard navigation options (`to`, `params`,
|
|
136
|
+
* `search`, etc.) and returns either `false` (no match) or the matched params
|
|
137
|
+
* object when the route matches the current or pending location.
|
|
138
|
+
*
|
|
139
|
+
* Useful for conditional rendering and active UI states.
|
|
140
|
+
*
|
|
141
|
+
* @returns A `matchRoute(options)` function that returns `false` or params.
|
|
142
|
+
* @link https://tanstack.com/router/latest/docs/framework/react/api/router/useMatchRouteHook
|
|
143
|
+
*/
|
|
144
|
+
export function useMatchRoute<TRouter extends AnyRouter = RegisteredRouter>() {
|
|
145
|
+
const router = useRouter()
|
|
146
|
+
|
|
147
|
+
useRouterState({
|
|
148
|
+
select: (s) => [s.location.href, s.resolvedLocation?.href, s.status],
|
|
149
|
+
structuralSharing: true as any,
|
|
150
|
+
})
|
|
151
|
+
|
|
152
|
+
return React.useCallback(
|
|
153
|
+
<
|
|
154
|
+
const TFrom extends string = string,
|
|
155
|
+
const TTo extends string | undefined = undefined,
|
|
156
|
+
const TMaskFrom extends string = TFrom,
|
|
157
|
+
const TMaskTo extends string = '',
|
|
158
|
+
>(
|
|
159
|
+
opts: UseMatchRouteOptions<TRouter, TFrom, TTo, TMaskFrom, TMaskTo>,
|
|
160
|
+
):
|
|
161
|
+
| false
|
|
162
|
+
| Expand<ResolveRoute<TRouter, TFrom, TTo>['types']['allParams']> => {
|
|
163
|
+
const { pending, caseSensitive, fuzzy, includeSearch, ...rest } = opts
|
|
164
|
+
|
|
165
|
+
return router.matchRoute(rest as any, {
|
|
166
|
+
pending,
|
|
167
|
+
caseSensitive,
|
|
168
|
+
fuzzy,
|
|
169
|
+
includeSearch,
|
|
170
|
+
})
|
|
171
|
+
},
|
|
172
|
+
[router],
|
|
173
|
+
)
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
export type MakeMatchRouteOptions<
|
|
177
|
+
TRouter extends AnyRouter = RegisteredRouter,
|
|
178
|
+
TFrom extends string = string,
|
|
179
|
+
TTo extends string | undefined = undefined,
|
|
180
|
+
TMaskFrom extends string = TFrom,
|
|
181
|
+
TMaskTo extends string = '',
|
|
182
|
+
> = UseMatchRouteOptions<TRouter, TFrom, TTo, TMaskFrom, TMaskTo> & {
|
|
183
|
+
// If a function is passed as a child, it will be given the `isActive` boolean to aid in further styling on the element it returns
|
|
184
|
+
children?:
|
|
185
|
+
| ((
|
|
186
|
+
params?: RouteByPath<
|
|
187
|
+
TRouter['routeTree'],
|
|
188
|
+
ResolveRelativePath<TFrom, NoInfer<TTo>>
|
|
189
|
+
>['types']['allParams'],
|
|
190
|
+
) => React.ReactNode)
|
|
191
|
+
| React.ReactNode
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Component that conditionally renders its children based on whether a route
|
|
196
|
+
* matches the provided `from`/`to` options. If `children` is a function, it
|
|
197
|
+
* receives the matched params object.
|
|
198
|
+
*
|
|
199
|
+
* @link https://tanstack.com/router/latest/docs/framework/react/api/router/matchRouteComponent
|
|
200
|
+
*/
|
|
201
|
+
export function MatchRoute<
|
|
202
|
+
TRouter extends AnyRouter = RegisteredRouter,
|
|
203
|
+
const TFrom extends string = string,
|
|
204
|
+
const TTo extends string | undefined = undefined,
|
|
205
|
+
const TMaskFrom extends string = TFrom,
|
|
206
|
+
const TMaskTo extends string = '',
|
|
207
|
+
>(props: MakeMatchRouteOptions<TRouter, TFrom, TTo, TMaskFrom, TMaskTo>): any {
|
|
208
|
+
const matchRoute = useMatchRoute()
|
|
209
|
+
const params = matchRoute(props as any) as boolean
|
|
210
|
+
|
|
211
|
+
if (typeof props.children === 'function') {
|
|
212
|
+
return (props.children as any)(params)
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
return params ? props.children : null
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
export interface UseMatchesBaseOptions<
|
|
219
|
+
TRouter extends AnyRouter,
|
|
220
|
+
TSelected,
|
|
221
|
+
TStructuralSharing,
|
|
222
|
+
> {
|
|
223
|
+
select?: (
|
|
224
|
+
matches: Array<MakeRouteMatchUnion<TRouter>>,
|
|
225
|
+
) => ValidateSelected<TRouter, TSelected, TStructuralSharing>
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
export type UseMatchesResult<
|
|
229
|
+
TRouter extends AnyRouter,
|
|
230
|
+
TSelected,
|
|
231
|
+
> = unknown extends TSelected ? Array<MakeRouteMatchUnion<TRouter>> : TSelected
|
|
232
|
+
|
|
233
|
+
export function useMatches<
|
|
234
|
+
TRouter extends AnyRouter = RegisteredRouter,
|
|
235
|
+
TSelected = unknown,
|
|
236
|
+
TStructuralSharing extends boolean = boolean,
|
|
237
|
+
>(
|
|
238
|
+
opts?: UseMatchesBaseOptions<TRouter, TSelected, TStructuralSharing> &
|
|
239
|
+
StructuralSharingOption<TRouter, TSelected, TStructuralSharing>,
|
|
240
|
+
): UseMatchesResult<TRouter, TSelected> {
|
|
241
|
+
return useRouterState({
|
|
242
|
+
select: (state: RouterState<TRouter['routeTree']>) => {
|
|
243
|
+
const matches = state.matches
|
|
244
|
+
return opts?.select
|
|
245
|
+
? opts.select(matches as Array<MakeRouteMatchUnion<TRouter>>)
|
|
246
|
+
: matches
|
|
247
|
+
},
|
|
248
|
+
structuralSharing: opts?.structuralSharing,
|
|
249
|
+
} as any) as UseMatchesResult<TRouter, TSelected>
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Read the full array of active route matches or select a derived subset.
|
|
254
|
+
*
|
|
255
|
+
* Useful for debugging, breadcrumbs, or aggregating metadata across matches.
|
|
256
|
+
*
|
|
257
|
+
* @returns The array of matches (or the selected value).
|
|
258
|
+
* @link https://tanstack.com/router/latest/docs/framework/react/api/router/useMatchesHook
|
|
259
|
+
*/
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Read the full array of active route matches or select a derived subset.
|
|
263
|
+
*
|
|
264
|
+
* Useful for debugging, breadcrumbs, or aggregating metadata across matches.
|
|
265
|
+
*
|
|
266
|
+
* @link https://tanstack.com/router/latest/docs/framework/react/api/router/useMatchesHook
|
|
267
|
+
*/
|
|
268
|
+
export function useParentMatches<
|
|
269
|
+
TRouter extends AnyRouter = RegisteredRouter,
|
|
270
|
+
TSelected = unknown,
|
|
271
|
+
TStructuralSharing extends boolean = boolean,
|
|
272
|
+
>(
|
|
273
|
+
opts?: UseMatchesBaseOptions<TRouter, TSelected, TStructuralSharing> &
|
|
274
|
+
StructuralSharingOption<TRouter, TSelected, TStructuralSharing>,
|
|
275
|
+
): UseMatchesResult<TRouter, TSelected> {
|
|
276
|
+
const contextMatchId = React.useContext(matchContext)
|
|
277
|
+
|
|
278
|
+
return useMatches({
|
|
279
|
+
select: (matches: Array<MakeRouteMatchUnion<TRouter>>) => {
|
|
280
|
+
matches = matches.slice(
|
|
281
|
+
0,
|
|
282
|
+
matches.findIndex((d) => d.id === contextMatchId),
|
|
283
|
+
)
|
|
284
|
+
return opts?.select ? opts.select(matches) : matches
|
|
285
|
+
},
|
|
286
|
+
structuralSharing: opts?.structuralSharing,
|
|
287
|
+
} as any)
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* Read the array of active route matches that are children of the current
|
|
292
|
+
* match (or selected parent) in the match tree.
|
|
293
|
+
*/
|
|
294
|
+
export function useChildMatches<
|
|
295
|
+
TRouter extends AnyRouter = RegisteredRouter,
|
|
296
|
+
TSelected = unknown,
|
|
297
|
+
TStructuralSharing extends boolean = boolean,
|
|
298
|
+
>(
|
|
299
|
+
opts?: UseMatchesBaseOptions<TRouter, TSelected, TStructuralSharing> &
|
|
300
|
+
StructuralSharingOption<TRouter, TSelected, TStructuralSharing>,
|
|
301
|
+
): UseMatchesResult<TRouter, TSelected> {
|
|
302
|
+
const contextMatchId = React.useContext(matchContext)
|
|
303
|
+
|
|
304
|
+
return useMatches({
|
|
305
|
+
select: (matches: Array<MakeRouteMatchUnion<TRouter>>) => {
|
|
306
|
+
matches = matches.slice(
|
|
307
|
+
matches.findIndex((d) => d.id === contextMatchId) + 1,
|
|
308
|
+
)
|
|
309
|
+
return opts?.select ? opts.select(matches) : matches
|
|
310
|
+
},
|
|
311
|
+
structuralSharing: opts?.structuralSharing,
|
|
312
|
+
} as any)
|
|
313
|
+
}
|