@tanstack/react-router 1.20.1 → 1.20.3-alpha.1
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/README.md +31 -0
- package/dist/cjs/Asset.cjs +41 -0
- package/dist/cjs/Asset.cjs.map +1 -0
- package/dist/cjs/Asset.d.cts +2 -0
- package/dist/cjs/CatchBoundary.cjs +16 -12
- package/dist/cjs/CatchBoundary.cjs.map +1 -1
- package/dist/cjs/CatchBoundary.d.cts +8 -32
- package/dist/cjs/ClientOnly.cjs +20 -0
- package/dist/cjs/ClientOnly.cjs.map +1 -0
- package/dist/cjs/ClientOnly.d.cts +29 -0
- package/dist/cjs/HeadContent.cjs +155 -0
- package/dist/cjs/HeadContent.cjs.map +1 -0
- package/dist/cjs/HeadContent.d.cts +7 -0
- package/dist/cjs/Match.cjs +252 -0
- package/dist/cjs/Match.cjs.map +1 -0
- package/dist/cjs/Match.d.cts +8 -0
- package/dist/cjs/Matches.cjs +39 -287
- package/dist/cjs/Matches.cjs.map +1 -1
- package/dist/cjs/Matches.d.cts +23 -83
- package/dist/cjs/RouterProvider.cjs +17 -140
- package/dist/cjs/RouterProvider.cjs.map +1 -1
- package/dist/cjs/RouterProvider.d.cts +8 -27
- 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 +28 -0
- package/dist/cjs/ScriptOnce.cjs.map +1 -0
- package/dist/cjs/ScriptOnce.d.cts +5 -0
- package/dist/cjs/Scripts.cjs +51 -0
- package/dist/cjs/Scripts.cjs.map +1 -0
- package/dist/cjs/Scripts.d.cts +1 -0
- package/dist/cjs/ScrollRestoration.cjs +39 -0
- package/dist/cjs/ScrollRestoration.cjs.map +1 -0
- package/dist/cjs/ScrollRestoration.d.cts +14 -0
- package/dist/cjs/Transitioner.cjs +115 -0
- package/dist/cjs/Transitioner.cjs.map +1 -0
- package/dist/cjs/Transitioner.d.cts +1 -0
- package/dist/cjs/awaited.cjs +12 -65
- package/dist/cjs/awaited.cjs.map +1 -1
- package/dist/cjs/awaited.d.cts +4 -4
- package/dist/cjs/fileRoute.cjs +41 -15
- package/dist/cjs/fileRoute.cjs.map +1 -1
- package/dist/cjs/fileRoute.d.cts +33 -108
- package/dist/cjs/history.d.cts +1 -0
- package/dist/cjs/index.cjs +216 -73
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +52 -29
- package/dist/cjs/lazyRouteComponent.cjs +40 -29
- package/dist/cjs/lazyRouteComponent.cjs.map +1 -1
- package/dist/cjs/lazyRouteComponent.d.cts +1 -1
- package/dist/cjs/link.cjs +212 -106
- package/dist/cjs/link.cjs.map +1 -1
- package/dist/cjs/link.d.cts +41 -86
- 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 +9 -15
- package/dist/cjs/not-found.cjs.map +1 -1
- package/dist/cjs/not-found.d.cts +5 -22
- package/dist/cjs/renderRouteNotFound.cjs +22 -0
- package/dist/cjs/renderRouteNotFound.cjs.map +1 -0
- package/dist/cjs/renderRouteNotFound.d.cts +2 -0
- package/dist/cjs/route.cjs +110 -79
- package/dist/cjs/route.cjs.map +1 -1
- package/dist/cjs/route.d.cts +64 -361
- package/dist/cjs/router.cjs +12 -1237
- package/dist/cjs/router.cjs.map +1 -1
- package/dist/cjs/router.d.cts +69 -237
- package/dist/cjs/routerContext.cjs +1 -1
- package/dist/cjs/routerContext.cjs.map +1 -1
- package/dist/cjs/routerContext.d.cts +7 -2
- package/dist/cjs/scroll-restoration.cjs +16 -177
- package/dist/cjs/scroll-restoration.cjs.map +1 -1
- package/dist/cjs/scroll-restoration.d.cts +1 -18
- package/dist/cjs/serializer.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 +138 -9
- package/dist/cjs/useBlocker.cjs.map +1 -1
- package/dist/cjs/useBlocker.d.cts +64 -7
- 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 +8 -0
- package/dist/cjs/useLoaderDeps.cjs +14 -0
- package/dist/cjs/useLoaderDeps.cjs.map +1 -0
- package/dist/cjs/useLoaderDeps.d.cts +8 -0
- package/dist/cjs/useLocation.cjs +10 -0
- package/dist/cjs/useLocation.cjs.map +1 -0
- package/dist/cjs/useLocation.d.cts +7 -0
- package/dist/cjs/useMatch.cjs +47 -0
- package/dist/cjs/useMatch.cjs.map +1 -0
- package/dist/cjs/useMatch.d.cts +10 -0
- package/dist/cjs/useNavigate.cjs +18 -19
- package/dist/cjs/useNavigate.cjs.map +1 -1
- package/dist/cjs/useNavigate.d.cts +4 -8
- package/dist/cjs/useParams.cjs +8 -8
- package/dist/cjs/useParams.cjs.map +1 -1
- package/dist/cjs/useParams.d.cts +9 -8
- package/dist/cjs/useRouteContext.cjs +3 -3
- package/dist/cjs/useRouteContext.cjs.map +1 -1
- package/dist/cjs/useRouteContext.d.cts +3 -7
- package/dist/cjs/useRouter.cjs.map +1 -1
- package/dist/cjs/useRouter.d.cts +3 -4
- package/dist/cjs/useRouterState.cjs +18 -1
- package/dist/cjs/useRouterState.cjs.map +1 -1
- package/dist/cjs/useRouterState.d.cts +8 -6
- package/dist/cjs/useSearch.cjs +7 -4
- package/dist/cjs/useSearch.cjs.map +1 -1
- package/dist/cjs/useSearch.d.cts +9 -7
- package/dist/cjs/utils.cjs +40 -122
- package/dist/cjs/utils.cjs.map +1 -1
- package/dist/cjs/utils.d.cts +46 -50
- package/dist/esm/Asset.d.ts +2 -0
- package/dist/esm/Asset.js +41 -0
- package/dist/esm/Asset.js.map +1 -0
- package/dist/esm/CatchBoundary.d.ts +8 -32
- package/dist/esm/CatchBoundary.js +16 -12
- package/dist/esm/CatchBoundary.js.map +1 -1
- package/dist/esm/ClientOnly.d.ts +29 -0
- package/dist/esm/ClientOnly.js +20 -0
- package/dist/esm/ClientOnly.js.map +1 -0
- package/dist/esm/HeadContent.d.ts +7 -0
- package/dist/esm/HeadContent.js +139 -0
- package/dist/esm/HeadContent.js.map +1 -0
- package/dist/esm/Match.d.ts +8 -0
- package/dist/esm/Match.js +235 -0
- package/dist/esm/Match.js.map +1 -0
- package/dist/esm/Matches.d.ts +23 -83
- package/dist/esm/Matches.js +36 -284
- package/dist/esm/Matches.js.map +1 -1
- package/dist/esm/RouterProvider.d.ts +8 -27
- package/dist/esm/RouterProvider.js +20 -126
- package/dist/esm/RouterProvider.js.map +1 -1
- 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 +5 -0
- package/dist/esm/ScriptOnce.js +28 -0
- package/dist/esm/ScriptOnce.js.map +1 -0
- package/dist/esm/Scripts.d.ts +1 -0
- package/dist/esm/Scripts.js +51 -0
- package/dist/esm/Scripts.js.map +1 -0
- package/dist/esm/ScrollRestoration.d.ts +14 -0
- package/dist/esm/ScrollRestoration.js +39 -0
- package/dist/esm/ScrollRestoration.js.map +1 -0
- package/dist/esm/Transitioner.d.ts +1 -0
- package/dist/esm/Transitioner.js +98 -0
- package/dist/esm/Transitioner.js.map +1 -0
- package/dist/esm/awaited.d.ts +4 -4
- package/dist/esm/awaited.js +12 -65
- package/dist/esm/awaited.js.map +1 -1
- package/dist/esm/fileRoute.d.ts +33 -108
- package/dist/esm/fileRoute.js +38 -12
- package/dist/esm/fileRoute.js.map +1 -1
- package/dist/esm/history.d.ts +1 -0
- package/dist/esm/index.d.ts +52 -29
- package/dist/esm/index.js +41 -29
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/lazyRouteComponent.d.ts +1 -1
- package/dist/esm/lazyRouteComponent.js +40 -29
- package/dist/esm/lazyRouteComponent.js.map +1 -1
- package/dist/esm/link.d.ts +41 -86
- package/dist/esm/link.js +212 -106
- package/dist/esm/link.js.map +1 -1
- 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 +5 -22
- package/dist/esm/not-found.js +9 -15
- package/dist/esm/not-found.js.map +1 -1
- package/dist/esm/renderRouteNotFound.d.ts +2 -0
- package/dist/esm/renderRouteNotFound.js +22 -0
- package/dist/esm/renderRouteNotFound.js.map +1 -0
- package/dist/esm/route.d.ts +64 -361
- package/dist/esm/route.js +103 -72
- package/dist/esm/route.js.map +1 -1
- package/dist/esm/router.d.ts +69 -237
- package/dist/esm/router.js +13 -1238
- package/dist/esm/router.js.map +1 -1
- package/dist/esm/routerContext.d.ts +7 -2
- package/dist/esm/routerContext.js +1 -1
- package/dist/esm/routerContext.js.map +1 -1
- package/dist/esm/scroll-restoration.d.ts +1 -18
- package/dist/esm/scroll-restoration.js +17 -161
- package/dist/esm/scroll-restoration.js.map +1 -1
- package/dist/esm/serializer.d.ts +6 -0
- package/dist/esm/structuralSharing.d.ts +8 -0
- package/dist/esm/typePrimitives.d.ts +16 -0
- package/dist/esm/useBlocker.d.ts +64 -7
- package/dist/esm/useBlocker.js +138 -9
- package/dist/esm/useBlocker.js.map +1 -1
- 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 +8 -0
- package/dist/esm/useLoaderData.js +15 -0
- package/dist/esm/useLoaderData.js.map +1 -0
- package/dist/esm/useLoaderDeps.d.ts +8 -0
- package/dist/esm/useLoaderDeps.js +14 -0
- package/dist/esm/useLoaderDeps.js.map +1 -0
- package/dist/esm/useLocation.d.ts +7 -0
- package/dist/esm/useLocation.js +10 -0
- package/dist/esm/useLocation.js.map +1 -0
- package/dist/esm/useMatch.d.ts +10 -0
- package/dist/esm/useMatch.js +30 -0
- package/dist/esm/useMatch.js.map +1 -0
- package/dist/esm/useNavigate.d.ts +4 -8
- package/dist/esm/useNavigate.js +18 -19
- package/dist/esm/useNavigate.js.map +1 -1
- package/dist/esm/useParams.d.ts +9 -8
- package/dist/esm/useParams.js +8 -8
- package/dist/esm/useParams.js.map +1 -1
- package/dist/esm/useRouteContext.d.ts +3 -7
- package/dist/esm/useRouteContext.js +2 -2
- package/dist/esm/useRouteContext.js.map +1 -1
- package/dist/esm/useRouter.d.ts +3 -4
- package/dist/esm/useRouter.js.map +1 -1
- package/dist/esm/useRouterState.d.ts +8 -6
- package/dist/esm/useRouterState.js +18 -1
- package/dist/esm/useRouterState.js.map +1 -1
- package/dist/esm/useSearch.d.ts +9 -7
- package/dist/esm/useSearch.js +6 -3
- package/dist/esm/useSearch.js.map +1 -1
- package/dist/esm/utils.d.ts +46 -50
- package/dist/esm/utils.js +41 -123
- package/dist/esm/utils.js.map +1 -1
- package/package.json +30 -31
- package/src/Asset.tsx +40 -0
- package/src/CatchBoundary.tsx +35 -19
- package/src/ClientOnly.tsx +68 -0
- package/src/HeadContent.tsx +174 -0
- package/src/Match.tsx +330 -0
- package/src/Matches.tsx +149 -558
- package/src/RouterProvider.tsx +58 -212
- package/src/SafeFragment.tsx +5 -0
- package/src/ScriptOnce.tsx +32 -0
- package/src/Scripts.tsx +65 -0
- package/src/ScrollRestoration.tsx +69 -0
- package/src/Transitioner.tsx +130 -0
- package/src/awaited.tsx +16 -87
- package/src/fileRoute.ts +145 -248
- package/src/history.ts +2 -1
- package/src/index.tsx +368 -30
- package/src/lazyRouteComponent.tsx +68 -54
- package/src/link.tsx +397 -522
- package/src/matchContext.tsx +8 -0
- package/src/not-found.tsx +13 -34
- package/src/renderRouteNotFound.tsx +27 -0
- package/src/route.tsx +572 -0
- package/src/router.ts +99 -2067
- package/src/routerContext.tsx +8 -2
- package/src/scroll-restoration.tsx +23 -224
- package/src/serializer.ts +7 -0
- package/src/structuralSharing.ts +47 -0
- package/src/typePrimitives.ts +84 -0
- package/src/useBlocker.tsx +297 -15
- package/src/useCanGoBack.ts +5 -0
- package/src/useLoaderData.tsx +80 -0
- package/src/useLoaderDeps.tsx +58 -0
- package/src/useLocation.tsx +41 -0
- package/src/useMatch.tsx +119 -0
- package/src/useNavigate.tsx +41 -61
- package/src/useParams.tsx +88 -23
- package/src/useRouteContext.ts +24 -18
- package/src/useRouter.tsx +4 -5
- package/src/useRouterState.tsx +52 -10
- package/src/useSearch.tsx +87 -24
- package/src/utils.ts +97 -312
- package/dist/cjs/createServerFn.cjs +0 -40
- package/dist/cjs/createServerFn.cjs.map +0 -1
- package/dist/cjs/createServerFn.d.cts +0 -44
- package/dist/cjs/defer.cjs +0 -30
- package/dist/cjs/defer.cjs.map +0 -1
- package/dist/cjs/defer.d.cts +0 -25
- package/dist/cjs/location.d.cts +0 -12
- package/dist/cjs/path.cjs +0 -213
- package/dist/cjs/path.cjs.map +0 -1
- package/dist/cjs/path.d.cts +0 -24
- package/dist/cjs/qss.cjs +0 -45
- package/dist/cjs/qss.cjs.map +0 -1
- package/dist/cjs/qss.d.cts +0 -2
- package/dist/cjs/redirects.cjs +0 -16
- package/dist/cjs/redirects.cjs.map +0 -1
- package/dist/cjs/redirects.d.cts +0 -18
- package/dist/cjs/routeInfo.d.cts +0 -31
- package/dist/cjs/searchParams.cjs +0 -63
- package/dist/cjs/searchParams.cjs.map +0 -1
- package/dist/cjs/searchParams.d.cts +0 -7
- package/dist/esm/createServerFn.d.ts +0 -44
- package/dist/esm/createServerFn.js +0 -40
- package/dist/esm/createServerFn.js.map +0 -1
- package/dist/esm/defer.d.ts +0 -25
- package/dist/esm/defer.js +0 -30
- package/dist/esm/defer.js.map +0 -1
- package/dist/esm/location.d.ts +0 -12
- package/dist/esm/path.d.ts +0 -24
- package/dist/esm/path.js +0 -213
- package/dist/esm/path.js.map +0 -1
- package/dist/esm/qss.d.ts +0 -2
- package/dist/esm/qss.js +0 -45
- package/dist/esm/qss.js.map +0 -1
- package/dist/esm/redirects.d.ts +0 -18
- package/dist/esm/redirects.js +0 -16
- package/dist/esm/redirects.js.map +0 -1
- package/dist/esm/routeInfo.d.ts +0 -31
- package/dist/esm/searchParams.d.ts +0 -7
- package/dist/esm/searchParams.js +0 -63
- package/dist/esm/searchParams.js.map +0 -1
- package/src/createServerFn.ts +0 -107
- package/src/defer.ts +0 -70
- package/src/location.ts +0 -13
- package/src/path.ts +0 -280
- package/src/qss.ts +0 -53
- package/src/redirects.ts +0 -56
- package/src/route.ts +0 -1356
- package/src/routeInfo.ts +0 -63
- package/src/searchParams.ts +0 -79
package/src/RouterProvider.tsx
CHANGED
|
@@ -1,90 +1,39 @@
|
|
|
1
1
|
import * as React from 'react'
|
|
2
2
|
import { Matches } from './Matches'
|
|
3
|
-
import { NavigateOptions, ToOptions } from './link'
|
|
4
|
-
import { ParsedLocation } from './location'
|
|
5
|
-
import { AnyRoute } from './route'
|
|
6
|
-
import { RoutePaths } from './routeInfo'
|
|
7
|
-
import { RegisteredRouter, Router, RouterOptions, RouterState } from './router'
|
|
8
|
-
import { pick, useLayoutEffect } from './utils'
|
|
9
|
-
|
|
10
|
-
import { RouteMatch } from './Matches'
|
|
11
|
-
import { useRouter } from './useRouter'
|
|
12
|
-
import { useRouterState } from './useRouterState'
|
|
13
3
|
import { getRouterContext } from './routerContext'
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
])
|
|
23
|
-
|
|
24
|
-
export interface CommitLocationOptions {
|
|
25
|
-
replace?: boolean
|
|
26
|
-
resetScroll?: boolean
|
|
27
|
-
startTransition?: boolean
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export interface MatchLocation {
|
|
31
|
-
to?: string | number | null
|
|
32
|
-
fuzzy?: boolean
|
|
33
|
-
caseSensitive?: boolean
|
|
34
|
-
from?: string
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export type NavigateFn = <
|
|
38
|
-
TTo extends string,
|
|
39
|
-
TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
|
|
40
|
-
TFrom extends RoutePaths<TRouteTree> | string = string,
|
|
41
|
-
TMaskFrom extends RoutePaths<TRouteTree> | string = TFrom,
|
|
42
|
-
TMaskTo extends string = '',
|
|
43
|
-
>(
|
|
44
|
-
opts: NavigateOptions<TRouteTree, TFrom, TTo, TMaskFrom, TMaskTo>,
|
|
45
|
-
) => Promise<void>
|
|
46
|
-
|
|
47
|
-
export type BuildLocationFn<TRouteTree extends AnyRoute> = <
|
|
48
|
-
TTo extends string,
|
|
49
|
-
TFrom extends RoutePaths<TRouteTree> | string = string,
|
|
50
|
-
TMaskFrom extends RoutePaths<TRouteTree> | string = TFrom,
|
|
51
|
-
TMaskTo extends string = '',
|
|
52
|
-
>(
|
|
53
|
-
opts: ToOptions<TRouteTree, TFrom, TTo, TMaskFrom, TMaskTo> & {
|
|
54
|
-
leaveParams?: boolean
|
|
55
|
-
},
|
|
56
|
-
) => ParsedLocation
|
|
57
|
-
|
|
58
|
-
export type InjectedHtmlEntry = string | (() => Promise<string> | string)
|
|
59
|
-
|
|
60
|
-
export function RouterProvider<
|
|
61
|
-
TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
|
|
4
|
+
import type {
|
|
5
|
+
AnyRouter,
|
|
6
|
+
RegisteredRouter,
|
|
7
|
+
RouterOptions,
|
|
8
|
+
} from '@tanstack/router-core'
|
|
9
|
+
|
|
10
|
+
export function RouterContextProvider<
|
|
11
|
+
TRouter extends AnyRouter = RegisteredRouter,
|
|
62
12
|
TDehydrated extends Record<string, any> = Record<string, any>,
|
|
63
|
-
>({
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
13
|
+
>({
|
|
14
|
+
router,
|
|
15
|
+
children,
|
|
16
|
+
...rest
|
|
17
|
+
}: RouterProps<TRouter, TDehydrated> & {
|
|
18
|
+
children: React.ReactNode
|
|
19
|
+
}) {
|
|
20
|
+
if (Object.keys(rest).length > 0) {
|
|
21
|
+
// Allow the router to update options on the router instance
|
|
22
|
+
router.update({
|
|
23
|
+
...router.options,
|
|
24
|
+
...rest,
|
|
25
|
+
context: {
|
|
26
|
+
...router.options.context,
|
|
27
|
+
...rest.context,
|
|
28
|
+
},
|
|
29
|
+
} as any)
|
|
30
|
+
}
|
|
81
31
|
|
|
82
32
|
const routerContext = getRouterContext()
|
|
83
33
|
|
|
84
34
|
const provider = (
|
|
85
|
-
<routerContext.Provider value={router}>
|
|
86
|
-
{
|
|
87
|
-
<Transitioner />
|
|
35
|
+
<routerContext.Provider value={router as AnyRouter}>
|
|
36
|
+
{children}
|
|
88
37
|
</routerContext.Provider>
|
|
89
38
|
)
|
|
90
39
|
|
|
@@ -95,141 +44,38 @@ export function RouterProvider<
|
|
|
95
44
|
return provider
|
|
96
45
|
}
|
|
97
46
|
|
|
98
|
-
function
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
router.startReactTransition = startReactTransition
|
|
109
|
-
|
|
110
|
-
React.useEffect(() => {
|
|
111
|
-
if (isTransitioning) {
|
|
112
|
-
router.__store.setState((s) => ({
|
|
113
|
-
...s,
|
|
114
|
-
isTransitioning,
|
|
115
|
-
}))
|
|
116
|
-
}
|
|
117
|
-
}, [isTransitioning])
|
|
118
|
-
|
|
119
|
-
const tryLoad = () => {
|
|
120
|
-
const apply = (cb: () => void) => {
|
|
121
|
-
if (!routerState.isTransitioning) {
|
|
122
|
-
startReactTransition(() => cb())
|
|
123
|
-
} else {
|
|
124
|
-
cb()
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
apply(() => {
|
|
129
|
-
try {
|
|
130
|
-
router.load()
|
|
131
|
-
} catch (err) {
|
|
132
|
-
console.error(err)
|
|
133
|
-
}
|
|
134
|
-
})
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
useLayoutEffect(() => {
|
|
138
|
-
const unsub = router.history.subscribe(() => {
|
|
139
|
-
router.latestLocation = router.parseLocation(router.latestLocation)
|
|
140
|
-
if (router.state.location !== router.latestLocation) {
|
|
141
|
-
tryLoad()
|
|
142
|
-
}
|
|
143
|
-
})
|
|
144
|
-
|
|
145
|
-
const nextLocation = router.buildLocation({
|
|
146
|
-
to: router.latestLocation.pathname,
|
|
147
|
-
search: true,
|
|
148
|
-
params: true,
|
|
149
|
-
hash: true,
|
|
150
|
-
state: true,
|
|
151
|
-
})
|
|
152
|
-
|
|
153
|
-
if (routerState.location.href !== nextLocation.href) {
|
|
154
|
-
router.commitLocation({ ...nextLocation, replace: true })
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
return () => {
|
|
158
|
-
unsub()
|
|
159
|
-
}
|
|
160
|
-
}, [router.history])
|
|
161
|
-
|
|
162
|
-
useLayoutEffect(() => {
|
|
163
|
-
if (
|
|
164
|
-
(React.useTransition as any)
|
|
165
|
-
? routerState.isTransitioning && !isTransitioning
|
|
166
|
-
: true &&
|
|
167
|
-
!routerState.isLoading &&
|
|
168
|
-
routerState.resolvedLocation !== routerState.location
|
|
169
|
-
) {
|
|
170
|
-
router.emit({
|
|
171
|
-
type: 'onResolved',
|
|
172
|
-
fromLocation: routerState.resolvedLocation,
|
|
173
|
-
toLocation: routerState.location,
|
|
174
|
-
pathChanged:
|
|
175
|
-
routerState.location!.href !== routerState.resolvedLocation?.href,
|
|
176
|
-
})
|
|
177
|
-
|
|
178
|
-
if ((document as any).querySelector) {
|
|
179
|
-
if (routerState.location.hash !== '') {
|
|
180
|
-
const el = document.getElementById(
|
|
181
|
-
routerState.location.hash,
|
|
182
|
-
) as HTMLElement | null
|
|
183
|
-
if (el) {
|
|
184
|
-
el.scrollIntoView()
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
router.__store.setState((s) => ({
|
|
190
|
-
...s,
|
|
191
|
-
isTransitioning: false,
|
|
192
|
-
resolvedLocation: s.location,
|
|
193
|
-
}))
|
|
194
|
-
}
|
|
195
|
-
}, [
|
|
196
|
-
routerState.isTransitioning,
|
|
197
|
-
isTransitioning,
|
|
198
|
-
routerState.isLoading,
|
|
199
|
-
routerState.resolvedLocation,
|
|
200
|
-
routerState.location,
|
|
201
|
-
])
|
|
202
|
-
|
|
203
|
-
useLayoutEffect(() => {
|
|
204
|
-
if (
|
|
205
|
-
window.__TSR_DEHYDRATED__ ||
|
|
206
|
-
(mountLoadForRouter.current.router === router &&
|
|
207
|
-
mountLoadForRouter.current.mounted)
|
|
208
|
-
) {
|
|
209
|
-
return
|
|
210
|
-
}
|
|
211
|
-
mountLoadForRouter.current = { router, mounted: true }
|
|
212
|
-
tryLoad()
|
|
213
|
-
}, [router])
|
|
214
|
-
|
|
215
|
-
return null
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
export function getRouteMatch<TRouteTree extends AnyRoute>(
|
|
219
|
-
state: RouterState<TRouteTree>,
|
|
220
|
-
id: string,
|
|
221
|
-
): undefined | RouteMatch<TRouteTree> {
|
|
222
|
-
return [
|
|
223
|
-
...state.cachedMatches,
|
|
224
|
-
...(state.pendingMatches ?? []),
|
|
225
|
-
...state.matches,
|
|
226
|
-
].find((d) => d.id === id)
|
|
47
|
+
export function RouterProvider<
|
|
48
|
+
TRouter extends AnyRouter = RegisteredRouter,
|
|
49
|
+
TDehydrated extends Record<string, any> = Record<string, any>,
|
|
50
|
+
>({ router, ...rest }: RouterProps<TRouter, TDehydrated>) {
|
|
51
|
+
return (
|
|
52
|
+
<RouterContextProvider router={router} {...rest}>
|
|
53
|
+
<Matches />
|
|
54
|
+
</RouterContextProvider>
|
|
55
|
+
)
|
|
227
56
|
}
|
|
228
57
|
|
|
229
58
|
export type RouterProps<
|
|
230
|
-
|
|
59
|
+
TRouter extends AnyRouter = RegisteredRouter,
|
|
231
60
|
TDehydrated extends Record<string, any> = Record<string, any>,
|
|
232
|
-
> = Omit<
|
|
233
|
-
|
|
234
|
-
|
|
61
|
+
> = Omit<
|
|
62
|
+
RouterOptions<
|
|
63
|
+
TRouter['routeTree'],
|
|
64
|
+
NonNullable<TRouter['options']['trailingSlash']>,
|
|
65
|
+
NonNullable<TRouter['options']['defaultStructuralSharing']>,
|
|
66
|
+
TRouter['history'],
|
|
67
|
+
TDehydrated
|
|
68
|
+
>,
|
|
69
|
+
'context'
|
|
70
|
+
> & {
|
|
71
|
+
router: TRouter
|
|
72
|
+
context?: Partial<
|
|
73
|
+
RouterOptions<
|
|
74
|
+
TRouter['routeTree'],
|
|
75
|
+
NonNullable<TRouter['options']['trailingSlash']>,
|
|
76
|
+
NonNullable<TRouter['options']['defaultStructuralSharing']>,
|
|
77
|
+
TRouter['history'],
|
|
78
|
+
TDehydrated
|
|
79
|
+
>['context']
|
|
80
|
+
>
|
|
235
81
|
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import jsesc from 'jsesc'
|
|
2
|
+
|
|
3
|
+
export function ScriptOnce({
|
|
4
|
+
children,
|
|
5
|
+
log,
|
|
6
|
+
}: {
|
|
7
|
+
children: string
|
|
8
|
+
log?: boolean
|
|
9
|
+
sync?: boolean
|
|
10
|
+
}) {
|
|
11
|
+
if (typeof document !== 'undefined') {
|
|
12
|
+
return null
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
return (
|
|
16
|
+
<script
|
|
17
|
+
className="tsr-once"
|
|
18
|
+
dangerouslySetInnerHTML={{
|
|
19
|
+
__html: [
|
|
20
|
+
children,
|
|
21
|
+
(log ?? true) && process.env.NODE_ENV === 'development'
|
|
22
|
+
? `console.info(\`Injected From Server:
|
|
23
|
+
${jsesc(children.toString(), { quotes: 'backtick' })}\`)`
|
|
24
|
+
: '',
|
|
25
|
+
'if (typeof __TSR_SSR__ !== "undefined") __TSR_SSR__.cleanScripts()',
|
|
26
|
+
]
|
|
27
|
+
.filter(Boolean)
|
|
28
|
+
.join('\n'),
|
|
29
|
+
}}
|
|
30
|
+
/>
|
|
31
|
+
)
|
|
32
|
+
}
|
package/src/Scripts.tsx
ADDED
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { Asset } from './Asset'
|
|
2
|
+
import { useRouterState } from './useRouterState'
|
|
3
|
+
import { useRouter } from './useRouter'
|
|
4
|
+
import type { RouterManagedTag } from '@tanstack/router-core'
|
|
5
|
+
|
|
6
|
+
export const Scripts = () => {
|
|
7
|
+
const router = useRouter()
|
|
8
|
+
|
|
9
|
+
const assetScripts = useRouterState({
|
|
10
|
+
select: (state) => {
|
|
11
|
+
const assetScripts: Array<RouterManagedTag> = []
|
|
12
|
+
const manifest = router.ssr?.manifest
|
|
13
|
+
|
|
14
|
+
if (!manifest) {
|
|
15
|
+
return []
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
state.matches
|
|
19
|
+
.map((match) => router.looseRoutesById[match.routeId]!)
|
|
20
|
+
.forEach((route) =>
|
|
21
|
+
manifest.routes[route.id]?.assets
|
|
22
|
+
?.filter((d) => d.tag === 'script')
|
|
23
|
+
.forEach((asset) => {
|
|
24
|
+
assetScripts.push({
|
|
25
|
+
tag: 'script',
|
|
26
|
+
attrs: asset.attrs,
|
|
27
|
+
children: asset.children,
|
|
28
|
+
} as any)
|
|
29
|
+
}),
|
|
30
|
+
)
|
|
31
|
+
|
|
32
|
+
return assetScripts
|
|
33
|
+
},
|
|
34
|
+
structuralSharing: true as any,
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
const { scripts } = useRouterState({
|
|
38
|
+
select: (state) => ({
|
|
39
|
+
scripts: (
|
|
40
|
+
state.matches
|
|
41
|
+
.map((match) => match.scripts!)
|
|
42
|
+
.flat(1)
|
|
43
|
+
.filter(Boolean) as Array<RouterManagedTag>
|
|
44
|
+
).map(({ children, ...script }) => ({
|
|
45
|
+
tag: 'script',
|
|
46
|
+
attrs: {
|
|
47
|
+
...script,
|
|
48
|
+
suppressHydrationWarning: true,
|
|
49
|
+
},
|
|
50
|
+
children,
|
|
51
|
+
})),
|
|
52
|
+
}),
|
|
53
|
+
structuralSharing: true as any,
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
const allScripts = [...scripts, ...assetScripts] as Array<RouterManagedTag>
|
|
57
|
+
|
|
58
|
+
return (
|
|
59
|
+
<>
|
|
60
|
+
{allScripts.map((asset, i) => (
|
|
61
|
+
<Asset {...asset} key={`tsr-scripts-${asset.tag}-${i}`} />
|
|
62
|
+
))}
|
|
63
|
+
</>
|
|
64
|
+
)
|
|
65
|
+
}
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import {
|
|
2
|
+
defaultGetScrollRestorationKey,
|
|
3
|
+
getCssSelector,
|
|
4
|
+
scrollRestorationCache,
|
|
5
|
+
setupScrollRestoration,
|
|
6
|
+
} from '@tanstack/router-core'
|
|
7
|
+
import { useRouter } from './useRouter'
|
|
8
|
+
import type {
|
|
9
|
+
ParsedLocation,
|
|
10
|
+
ScrollRestorationEntry,
|
|
11
|
+
ScrollRestorationOptions,
|
|
12
|
+
} from '@tanstack/router-core'
|
|
13
|
+
|
|
14
|
+
function useScrollRestoration() {
|
|
15
|
+
const router = useRouter()
|
|
16
|
+
setupScrollRestoration(router, true)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* @deprecated use createRouter's `scrollRestoration` option instead
|
|
21
|
+
*/
|
|
22
|
+
export function ScrollRestoration(_props: ScrollRestorationOptions) {
|
|
23
|
+
useScrollRestoration()
|
|
24
|
+
|
|
25
|
+
if (process.env.NODE_ENV === 'development') {
|
|
26
|
+
console.warn(
|
|
27
|
+
"The ScrollRestoration component is deprecated. Use createRouter's `scrollRestoration` option instead.",
|
|
28
|
+
)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
return null
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function useElementScrollRestoration(
|
|
35
|
+
options: (
|
|
36
|
+
| {
|
|
37
|
+
id: string
|
|
38
|
+
getElement?: () => Window | Element | undefined | null
|
|
39
|
+
}
|
|
40
|
+
| {
|
|
41
|
+
id?: string
|
|
42
|
+
getElement: () => Window | Element | undefined | null
|
|
43
|
+
}
|
|
44
|
+
) & {
|
|
45
|
+
getKey?: (location: ParsedLocation) => string
|
|
46
|
+
},
|
|
47
|
+
): ScrollRestorationEntry | undefined {
|
|
48
|
+
useScrollRestoration()
|
|
49
|
+
|
|
50
|
+
const router = useRouter()
|
|
51
|
+
const getKey = options.getKey || defaultGetScrollRestorationKey
|
|
52
|
+
|
|
53
|
+
let elementSelector = ''
|
|
54
|
+
|
|
55
|
+
if (options.id) {
|
|
56
|
+
elementSelector = `[data-scroll-restoration-id="${options.id}"]`
|
|
57
|
+
} else {
|
|
58
|
+
const element = options.getElement?.()
|
|
59
|
+
if (!element) {
|
|
60
|
+
return
|
|
61
|
+
}
|
|
62
|
+
elementSelector =
|
|
63
|
+
element instanceof Window ? 'window' : getCssSelector(element)
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const restoreKey = getKey(router.latestLocation)
|
|
67
|
+
const byKey = scrollRestorationCache.state[restoreKey]
|
|
68
|
+
return byKey?.[elementSelector]
|
|
69
|
+
}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import * as React from 'react'
|
|
2
|
+
import {
|
|
3
|
+
getLocationChangeInfo,
|
|
4
|
+
handleHashScroll,
|
|
5
|
+
trimPathRight,
|
|
6
|
+
} from '@tanstack/router-core'
|
|
7
|
+
import { useLayoutEffect, usePrevious } from './utils'
|
|
8
|
+
import { useRouter } from './useRouter'
|
|
9
|
+
import { useRouterState } from './useRouterState'
|
|
10
|
+
|
|
11
|
+
export function Transitioner() {
|
|
12
|
+
const router = useRouter()
|
|
13
|
+
const mountLoadForRouter = React.useRef({ router, mounted: false })
|
|
14
|
+
const isLoading = useRouterState({
|
|
15
|
+
select: ({ isLoading }) => isLoading,
|
|
16
|
+
})
|
|
17
|
+
|
|
18
|
+
const [isTransitioning, setIsTransitioning] = React.useState(false)
|
|
19
|
+
// Track pending state changes
|
|
20
|
+
const hasPendingMatches = useRouterState({
|
|
21
|
+
select: (s) => s.matches.some((d) => d.status === 'pending'),
|
|
22
|
+
structuralSharing: true,
|
|
23
|
+
})
|
|
24
|
+
|
|
25
|
+
const previousIsLoading = usePrevious(isLoading)
|
|
26
|
+
|
|
27
|
+
const isAnyPending = isLoading || isTransitioning || hasPendingMatches
|
|
28
|
+
const previousIsAnyPending = usePrevious(isAnyPending)
|
|
29
|
+
|
|
30
|
+
const isPagePending = isLoading || hasPendingMatches
|
|
31
|
+
const previousIsPagePending = usePrevious(isPagePending)
|
|
32
|
+
|
|
33
|
+
if (!router.isServer) {
|
|
34
|
+
router.startTransition = (fn: () => void) => {
|
|
35
|
+
setIsTransitioning(true)
|
|
36
|
+
React.startTransition(() => {
|
|
37
|
+
fn()
|
|
38
|
+
setIsTransitioning(false)
|
|
39
|
+
})
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
// Subscribe to location changes
|
|
44
|
+
// and try to load the new location
|
|
45
|
+
React.useEffect(() => {
|
|
46
|
+
const unsub = router.history.subscribe(router.load)
|
|
47
|
+
|
|
48
|
+
const nextLocation = router.buildLocation({
|
|
49
|
+
to: router.latestLocation.pathname,
|
|
50
|
+
search: true,
|
|
51
|
+
params: true,
|
|
52
|
+
hash: true,
|
|
53
|
+
state: true,
|
|
54
|
+
_includeValidateSearch: true,
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
if (
|
|
58
|
+
trimPathRight(router.latestLocation.href) !==
|
|
59
|
+
trimPathRight(nextLocation.href)
|
|
60
|
+
) {
|
|
61
|
+
router.commitLocation({ ...nextLocation, replace: true })
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return () => {
|
|
65
|
+
unsub()
|
|
66
|
+
}
|
|
67
|
+
}, [router, router.history])
|
|
68
|
+
|
|
69
|
+
// Try to load the initial location
|
|
70
|
+
useLayoutEffect(() => {
|
|
71
|
+
if (
|
|
72
|
+
(typeof window !== 'undefined' && router.clientSsr) ||
|
|
73
|
+
(mountLoadForRouter.current.router === router &&
|
|
74
|
+
mountLoadForRouter.current.mounted)
|
|
75
|
+
) {
|
|
76
|
+
return
|
|
77
|
+
}
|
|
78
|
+
mountLoadForRouter.current = { router, mounted: true }
|
|
79
|
+
|
|
80
|
+
const tryLoad = async () => {
|
|
81
|
+
try {
|
|
82
|
+
await router.load()
|
|
83
|
+
} catch (err) {
|
|
84
|
+
console.error(err)
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
tryLoad()
|
|
89
|
+
}, [router])
|
|
90
|
+
|
|
91
|
+
useLayoutEffect(() => {
|
|
92
|
+
// The router was loading and now it's not
|
|
93
|
+
if (previousIsLoading && !isLoading) {
|
|
94
|
+
router.emit({
|
|
95
|
+
type: 'onLoad', // When the new URL has committed, when the new matches have been loaded into state.matches
|
|
96
|
+
...getLocationChangeInfo(router.state),
|
|
97
|
+
})
|
|
98
|
+
}
|
|
99
|
+
}, [previousIsLoading, router, isLoading])
|
|
100
|
+
|
|
101
|
+
useLayoutEffect(() => {
|
|
102
|
+
// emit onBeforeRouteMount
|
|
103
|
+
if (previousIsPagePending && !isPagePending) {
|
|
104
|
+
router.emit({
|
|
105
|
+
type: 'onBeforeRouteMount',
|
|
106
|
+
...getLocationChangeInfo(router.state),
|
|
107
|
+
})
|
|
108
|
+
}
|
|
109
|
+
}, [isPagePending, previousIsPagePending, router])
|
|
110
|
+
|
|
111
|
+
useLayoutEffect(() => {
|
|
112
|
+
// The router was pending and now it's not
|
|
113
|
+
if (previousIsAnyPending && !isAnyPending) {
|
|
114
|
+
router.emit({
|
|
115
|
+
type: 'onResolved',
|
|
116
|
+
...getLocationChangeInfo(router.state),
|
|
117
|
+
})
|
|
118
|
+
|
|
119
|
+
router.__store.setState((s) => ({
|
|
120
|
+
...s,
|
|
121
|
+
status: 'idle',
|
|
122
|
+
resolvedLocation: s.location,
|
|
123
|
+
}))
|
|
124
|
+
|
|
125
|
+
handleHashScroll(router)
|
|
126
|
+
}
|
|
127
|
+
}, [isAnyPending, previousIsAnyPending, router])
|
|
128
|
+
|
|
129
|
+
return null
|
|
130
|
+
}
|