@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.
Files changed (320) hide show
  1. package/README.md +31 -0
  2. package/dist/cjs/Asset.cjs +41 -0
  3. package/dist/cjs/Asset.cjs.map +1 -0
  4. package/dist/cjs/Asset.d.cts +2 -0
  5. package/dist/cjs/CatchBoundary.cjs +16 -12
  6. package/dist/cjs/CatchBoundary.cjs.map +1 -1
  7. package/dist/cjs/CatchBoundary.d.cts +8 -32
  8. package/dist/cjs/ClientOnly.cjs +20 -0
  9. package/dist/cjs/ClientOnly.cjs.map +1 -0
  10. package/dist/cjs/ClientOnly.d.cts +29 -0
  11. package/dist/cjs/HeadContent.cjs +155 -0
  12. package/dist/cjs/HeadContent.cjs.map +1 -0
  13. package/dist/cjs/HeadContent.d.cts +7 -0
  14. package/dist/cjs/Match.cjs +252 -0
  15. package/dist/cjs/Match.cjs.map +1 -0
  16. package/dist/cjs/Match.d.cts +8 -0
  17. package/dist/cjs/Matches.cjs +39 -287
  18. package/dist/cjs/Matches.cjs.map +1 -1
  19. package/dist/cjs/Matches.d.cts +23 -83
  20. package/dist/cjs/RouterProvider.cjs +17 -140
  21. package/dist/cjs/RouterProvider.cjs.map +1 -1
  22. package/dist/cjs/RouterProvider.d.cts +8 -27
  23. package/dist/cjs/SafeFragment.cjs +8 -0
  24. package/dist/cjs/SafeFragment.cjs.map +1 -0
  25. package/dist/cjs/SafeFragment.d.cts +1 -0
  26. package/dist/cjs/ScriptOnce.cjs +28 -0
  27. package/dist/cjs/ScriptOnce.cjs.map +1 -0
  28. package/dist/cjs/ScriptOnce.d.cts +5 -0
  29. package/dist/cjs/Scripts.cjs +51 -0
  30. package/dist/cjs/Scripts.cjs.map +1 -0
  31. package/dist/cjs/Scripts.d.cts +1 -0
  32. package/dist/cjs/ScrollRestoration.cjs +39 -0
  33. package/dist/cjs/ScrollRestoration.cjs.map +1 -0
  34. package/dist/cjs/ScrollRestoration.d.cts +14 -0
  35. package/dist/cjs/Transitioner.cjs +115 -0
  36. package/dist/cjs/Transitioner.cjs.map +1 -0
  37. package/dist/cjs/Transitioner.d.cts +1 -0
  38. package/dist/cjs/awaited.cjs +12 -65
  39. package/dist/cjs/awaited.cjs.map +1 -1
  40. package/dist/cjs/awaited.d.cts +4 -4
  41. package/dist/cjs/fileRoute.cjs +41 -15
  42. package/dist/cjs/fileRoute.cjs.map +1 -1
  43. package/dist/cjs/fileRoute.d.cts +33 -108
  44. package/dist/cjs/history.d.cts +1 -0
  45. package/dist/cjs/index.cjs +216 -73
  46. package/dist/cjs/index.cjs.map +1 -1
  47. package/dist/cjs/index.d.cts +52 -29
  48. package/dist/cjs/lazyRouteComponent.cjs +40 -29
  49. package/dist/cjs/lazyRouteComponent.cjs.map +1 -1
  50. package/dist/cjs/lazyRouteComponent.d.cts +1 -1
  51. package/dist/cjs/link.cjs +212 -106
  52. package/dist/cjs/link.cjs.map +1 -1
  53. package/dist/cjs/link.d.cts +41 -86
  54. package/dist/cjs/matchContext.cjs +27 -0
  55. package/dist/cjs/matchContext.cjs.map +1 -0
  56. package/dist/cjs/matchContext.d.cts +3 -0
  57. package/dist/cjs/not-found.cjs +9 -15
  58. package/dist/cjs/not-found.cjs.map +1 -1
  59. package/dist/cjs/not-found.d.cts +5 -22
  60. package/dist/cjs/renderRouteNotFound.cjs +22 -0
  61. package/dist/cjs/renderRouteNotFound.cjs.map +1 -0
  62. package/dist/cjs/renderRouteNotFound.d.cts +2 -0
  63. package/dist/cjs/route.cjs +110 -79
  64. package/dist/cjs/route.cjs.map +1 -1
  65. package/dist/cjs/route.d.cts +64 -361
  66. package/dist/cjs/router.cjs +12 -1237
  67. package/dist/cjs/router.cjs.map +1 -1
  68. package/dist/cjs/router.d.cts +69 -237
  69. package/dist/cjs/routerContext.cjs +1 -1
  70. package/dist/cjs/routerContext.cjs.map +1 -1
  71. package/dist/cjs/routerContext.d.cts +7 -2
  72. package/dist/cjs/scroll-restoration.cjs +16 -177
  73. package/dist/cjs/scroll-restoration.cjs.map +1 -1
  74. package/dist/cjs/scroll-restoration.d.cts +1 -18
  75. package/dist/cjs/serializer.d.cts +6 -0
  76. package/dist/cjs/structuralSharing.d.cts +8 -0
  77. package/dist/cjs/typePrimitives.d.cts +16 -0
  78. package/dist/cjs/useBlocker.cjs +138 -9
  79. package/dist/cjs/useBlocker.cjs.map +1 -1
  80. package/dist/cjs/useBlocker.d.cts +64 -7
  81. package/dist/cjs/useCanGoBack.cjs +8 -0
  82. package/dist/cjs/useCanGoBack.cjs.map +1 -0
  83. package/dist/cjs/useCanGoBack.d.cts +1 -0
  84. package/dist/cjs/useLoaderData.cjs +15 -0
  85. package/dist/cjs/useLoaderData.cjs.map +1 -0
  86. package/dist/cjs/useLoaderData.d.cts +8 -0
  87. package/dist/cjs/useLoaderDeps.cjs +14 -0
  88. package/dist/cjs/useLoaderDeps.cjs.map +1 -0
  89. package/dist/cjs/useLoaderDeps.d.cts +8 -0
  90. package/dist/cjs/useLocation.cjs +10 -0
  91. package/dist/cjs/useLocation.cjs.map +1 -0
  92. package/dist/cjs/useLocation.d.cts +7 -0
  93. package/dist/cjs/useMatch.cjs +47 -0
  94. package/dist/cjs/useMatch.cjs.map +1 -0
  95. package/dist/cjs/useMatch.d.cts +10 -0
  96. package/dist/cjs/useNavigate.cjs +18 -19
  97. package/dist/cjs/useNavigate.cjs.map +1 -1
  98. package/dist/cjs/useNavigate.d.cts +4 -8
  99. package/dist/cjs/useParams.cjs +8 -8
  100. package/dist/cjs/useParams.cjs.map +1 -1
  101. package/dist/cjs/useParams.d.cts +9 -8
  102. package/dist/cjs/useRouteContext.cjs +3 -3
  103. package/dist/cjs/useRouteContext.cjs.map +1 -1
  104. package/dist/cjs/useRouteContext.d.cts +3 -7
  105. package/dist/cjs/useRouter.cjs.map +1 -1
  106. package/dist/cjs/useRouter.d.cts +3 -4
  107. package/dist/cjs/useRouterState.cjs +18 -1
  108. package/dist/cjs/useRouterState.cjs.map +1 -1
  109. package/dist/cjs/useRouterState.d.cts +8 -6
  110. package/dist/cjs/useSearch.cjs +7 -4
  111. package/dist/cjs/useSearch.cjs.map +1 -1
  112. package/dist/cjs/useSearch.d.cts +9 -7
  113. package/dist/cjs/utils.cjs +40 -122
  114. package/dist/cjs/utils.cjs.map +1 -1
  115. package/dist/cjs/utils.d.cts +46 -50
  116. package/dist/esm/Asset.d.ts +2 -0
  117. package/dist/esm/Asset.js +41 -0
  118. package/dist/esm/Asset.js.map +1 -0
  119. package/dist/esm/CatchBoundary.d.ts +8 -32
  120. package/dist/esm/CatchBoundary.js +16 -12
  121. package/dist/esm/CatchBoundary.js.map +1 -1
  122. package/dist/esm/ClientOnly.d.ts +29 -0
  123. package/dist/esm/ClientOnly.js +20 -0
  124. package/dist/esm/ClientOnly.js.map +1 -0
  125. package/dist/esm/HeadContent.d.ts +7 -0
  126. package/dist/esm/HeadContent.js +139 -0
  127. package/dist/esm/HeadContent.js.map +1 -0
  128. package/dist/esm/Match.d.ts +8 -0
  129. package/dist/esm/Match.js +235 -0
  130. package/dist/esm/Match.js.map +1 -0
  131. package/dist/esm/Matches.d.ts +23 -83
  132. package/dist/esm/Matches.js +36 -284
  133. package/dist/esm/Matches.js.map +1 -1
  134. package/dist/esm/RouterProvider.d.ts +8 -27
  135. package/dist/esm/RouterProvider.js +20 -126
  136. package/dist/esm/RouterProvider.js.map +1 -1
  137. package/dist/esm/SafeFragment.d.ts +1 -0
  138. package/dist/esm/SafeFragment.js +8 -0
  139. package/dist/esm/SafeFragment.js.map +1 -0
  140. package/dist/esm/ScriptOnce.d.ts +5 -0
  141. package/dist/esm/ScriptOnce.js +28 -0
  142. package/dist/esm/ScriptOnce.js.map +1 -0
  143. package/dist/esm/Scripts.d.ts +1 -0
  144. package/dist/esm/Scripts.js +51 -0
  145. package/dist/esm/Scripts.js.map +1 -0
  146. package/dist/esm/ScrollRestoration.d.ts +14 -0
  147. package/dist/esm/ScrollRestoration.js +39 -0
  148. package/dist/esm/ScrollRestoration.js.map +1 -0
  149. package/dist/esm/Transitioner.d.ts +1 -0
  150. package/dist/esm/Transitioner.js +98 -0
  151. package/dist/esm/Transitioner.js.map +1 -0
  152. package/dist/esm/awaited.d.ts +4 -4
  153. package/dist/esm/awaited.js +12 -65
  154. package/dist/esm/awaited.js.map +1 -1
  155. package/dist/esm/fileRoute.d.ts +33 -108
  156. package/dist/esm/fileRoute.js +38 -12
  157. package/dist/esm/fileRoute.js.map +1 -1
  158. package/dist/esm/history.d.ts +1 -0
  159. package/dist/esm/index.d.ts +52 -29
  160. package/dist/esm/index.js +41 -29
  161. package/dist/esm/index.js.map +1 -1
  162. package/dist/esm/lazyRouteComponent.d.ts +1 -1
  163. package/dist/esm/lazyRouteComponent.js +40 -29
  164. package/dist/esm/lazyRouteComponent.js.map +1 -1
  165. package/dist/esm/link.d.ts +41 -86
  166. package/dist/esm/link.js +212 -106
  167. package/dist/esm/link.js.map +1 -1
  168. package/dist/esm/matchContext.d.ts +3 -0
  169. package/dist/esm/matchContext.js +10 -0
  170. package/dist/esm/matchContext.js.map +1 -0
  171. package/dist/esm/not-found.d.ts +5 -22
  172. package/dist/esm/not-found.js +9 -15
  173. package/dist/esm/not-found.js.map +1 -1
  174. package/dist/esm/renderRouteNotFound.d.ts +2 -0
  175. package/dist/esm/renderRouteNotFound.js +22 -0
  176. package/dist/esm/renderRouteNotFound.js.map +1 -0
  177. package/dist/esm/route.d.ts +64 -361
  178. package/dist/esm/route.js +103 -72
  179. package/dist/esm/route.js.map +1 -1
  180. package/dist/esm/router.d.ts +69 -237
  181. package/dist/esm/router.js +13 -1238
  182. package/dist/esm/router.js.map +1 -1
  183. package/dist/esm/routerContext.d.ts +7 -2
  184. package/dist/esm/routerContext.js +1 -1
  185. package/dist/esm/routerContext.js.map +1 -1
  186. package/dist/esm/scroll-restoration.d.ts +1 -18
  187. package/dist/esm/scroll-restoration.js +17 -161
  188. package/dist/esm/scroll-restoration.js.map +1 -1
  189. package/dist/esm/serializer.d.ts +6 -0
  190. package/dist/esm/structuralSharing.d.ts +8 -0
  191. package/dist/esm/typePrimitives.d.ts +16 -0
  192. package/dist/esm/useBlocker.d.ts +64 -7
  193. package/dist/esm/useBlocker.js +138 -9
  194. package/dist/esm/useBlocker.js.map +1 -1
  195. package/dist/esm/useCanGoBack.d.ts +1 -0
  196. package/dist/esm/useCanGoBack.js +8 -0
  197. package/dist/esm/useCanGoBack.js.map +1 -0
  198. package/dist/esm/useLoaderData.d.ts +8 -0
  199. package/dist/esm/useLoaderData.js +15 -0
  200. package/dist/esm/useLoaderData.js.map +1 -0
  201. package/dist/esm/useLoaderDeps.d.ts +8 -0
  202. package/dist/esm/useLoaderDeps.js +14 -0
  203. package/dist/esm/useLoaderDeps.js.map +1 -0
  204. package/dist/esm/useLocation.d.ts +7 -0
  205. package/dist/esm/useLocation.js +10 -0
  206. package/dist/esm/useLocation.js.map +1 -0
  207. package/dist/esm/useMatch.d.ts +10 -0
  208. package/dist/esm/useMatch.js +30 -0
  209. package/dist/esm/useMatch.js.map +1 -0
  210. package/dist/esm/useNavigate.d.ts +4 -8
  211. package/dist/esm/useNavigate.js +18 -19
  212. package/dist/esm/useNavigate.js.map +1 -1
  213. package/dist/esm/useParams.d.ts +9 -8
  214. package/dist/esm/useParams.js +8 -8
  215. package/dist/esm/useParams.js.map +1 -1
  216. package/dist/esm/useRouteContext.d.ts +3 -7
  217. package/dist/esm/useRouteContext.js +2 -2
  218. package/dist/esm/useRouteContext.js.map +1 -1
  219. package/dist/esm/useRouter.d.ts +3 -4
  220. package/dist/esm/useRouter.js.map +1 -1
  221. package/dist/esm/useRouterState.d.ts +8 -6
  222. package/dist/esm/useRouterState.js +18 -1
  223. package/dist/esm/useRouterState.js.map +1 -1
  224. package/dist/esm/useSearch.d.ts +9 -7
  225. package/dist/esm/useSearch.js +6 -3
  226. package/dist/esm/useSearch.js.map +1 -1
  227. package/dist/esm/utils.d.ts +46 -50
  228. package/dist/esm/utils.js +41 -123
  229. package/dist/esm/utils.js.map +1 -1
  230. package/package.json +30 -31
  231. package/src/Asset.tsx +40 -0
  232. package/src/CatchBoundary.tsx +35 -19
  233. package/src/ClientOnly.tsx +68 -0
  234. package/src/HeadContent.tsx +174 -0
  235. package/src/Match.tsx +330 -0
  236. package/src/Matches.tsx +149 -558
  237. package/src/RouterProvider.tsx +58 -212
  238. package/src/SafeFragment.tsx +5 -0
  239. package/src/ScriptOnce.tsx +32 -0
  240. package/src/Scripts.tsx +65 -0
  241. package/src/ScrollRestoration.tsx +69 -0
  242. package/src/Transitioner.tsx +130 -0
  243. package/src/awaited.tsx +16 -87
  244. package/src/fileRoute.ts +145 -248
  245. package/src/history.ts +2 -1
  246. package/src/index.tsx +368 -30
  247. package/src/lazyRouteComponent.tsx +68 -54
  248. package/src/link.tsx +397 -522
  249. package/src/matchContext.tsx +8 -0
  250. package/src/not-found.tsx +13 -34
  251. package/src/renderRouteNotFound.tsx +27 -0
  252. package/src/route.tsx +572 -0
  253. package/src/router.ts +99 -2067
  254. package/src/routerContext.tsx +8 -2
  255. package/src/scroll-restoration.tsx +23 -224
  256. package/src/serializer.ts +7 -0
  257. package/src/structuralSharing.ts +47 -0
  258. package/src/typePrimitives.ts +84 -0
  259. package/src/useBlocker.tsx +297 -15
  260. package/src/useCanGoBack.ts +5 -0
  261. package/src/useLoaderData.tsx +80 -0
  262. package/src/useLoaderDeps.tsx +58 -0
  263. package/src/useLocation.tsx +41 -0
  264. package/src/useMatch.tsx +119 -0
  265. package/src/useNavigate.tsx +41 -61
  266. package/src/useParams.tsx +88 -23
  267. package/src/useRouteContext.ts +24 -18
  268. package/src/useRouter.tsx +4 -5
  269. package/src/useRouterState.tsx +52 -10
  270. package/src/useSearch.tsx +87 -24
  271. package/src/utils.ts +97 -312
  272. package/dist/cjs/createServerFn.cjs +0 -40
  273. package/dist/cjs/createServerFn.cjs.map +0 -1
  274. package/dist/cjs/createServerFn.d.cts +0 -44
  275. package/dist/cjs/defer.cjs +0 -30
  276. package/dist/cjs/defer.cjs.map +0 -1
  277. package/dist/cjs/defer.d.cts +0 -25
  278. package/dist/cjs/location.d.cts +0 -12
  279. package/dist/cjs/path.cjs +0 -213
  280. package/dist/cjs/path.cjs.map +0 -1
  281. package/dist/cjs/path.d.cts +0 -24
  282. package/dist/cjs/qss.cjs +0 -45
  283. package/dist/cjs/qss.cjs.map +0 -1
  284. package/dist/cjs/qss.d.cts +0 -2
  285. package/dist/cjs/redirects.cjs +0 -16
  286. package/dist/cjs/redirects.cjs.map +0 -1
  287. package/dist/cjs/redirects.d.cts +0 -18
  288. package/dist/cjs/routeInfo.d.cts +0 -31
  289. package/dist/cjs/searchParams.cjs +0 -63
  290. package/dist/cjs/searchParams.cjs.map +0 -1
  291. package/dist/cjs/searchParams.d.cts +0 -7
  292. package/dist/esm/createServerFn.d.ts +0 -44
  293. package/dist/esm/createServerFn.js +0 -40
  294. package/dist/esm/createServerFn.js.map +0 -1
  295. package/dist/esm/defer.d.ts +0 -25
  296. package/dist/esm/defer.js +0 -30
  297. package/dist/esm/defer.js.map +0 -1
  298. package/dist/esm/location.d.ts +0 -12
  299. package/dist/esm/path.d.ts +0 -24
  300. package/dist/esm/path.js +0 -213
  301. package/dist/esm/path.js.map +0 -1
  302. package/dist/esm/qss.d.ts +0 -2
  303. package/dist/esm/qss.js +0 -45
  304. package/dist/esm/qss.js.map +0 -1
  305. package/dist/esm/redirects.d.ts +0 -18
  306. package/dist/esm/redirects.js +0 -16
  307. package/dist/esm/redirects.js.map +0 -1
  308. package/dist/esm/routeInfo.d.ts +0 -31
  309. package/dist/esm/searchParams.d.ts +0 -7
  310. package/dist/esm/searchParams.js +0 -63
  311. package/dist/esm/searchParams.js.map +0 -1
  312. package/src/createServerFn.ts +0 -107
  313. package/src/defer.ts +0 -70
  314. package/src/location.ts +0 -13
  315. package/src/path.ts +0 -280
  316. package/src/qss.ts +0 -53
  317. package/src/redirects.ts +0 -56
  318. package/src/route.ts +0 -1356
  319. package/src/routeInfo.ts +0 -63
  320. package/src/searchParams.ts +0 -79
@@ -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
- const useTransition =
16
- React.useTransition ||
17
- (() => [
18
- false,
19
- (cb) => {
20
- cb()
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
- >({ router, ...rest }: RouterProps<TRouteTree, TDehydrated>) {
64
- // Allow the router to update options on the router instance
65
- router.update({
66
- ...router.options,
67
- ...rest,
68
- context: {
69
- ...router.options.context,
70
- ...rest?.context,
71
- },
72
- } as any)
73
-
74
- const matches = router.options.InnerWrap ? (
75
- <router.options.InnerWrap>
76
- <Matches />
77
- </router.options.InnerWrap>
78
- ) : (
79
- <Matches />
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
- {matches}
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 Transitioner() {
99
- const router = useRouter()
100
- const mountLoadForRouter = React.useRef({ router, mounted: false })
101
- const routerState = useRouterState({
102
- select: (s) =>
103
- pick(s, ['isLoading', 'location', 'resolvedLocation', 'isTransitioning']),
104
- })
105
-
106
- const [isTransitioning, startReactTransition] = useTransition()
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
- TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
59
+ TRouter extends AnyRouter = RegisteredRouter,
231
60
  TDehydrated extends Record<string, any> = Record<string, any>,
232
- > = Omit<RouterOptions<TRouteTree, TDehydrated>, 'context'> & {
233
- router: Router<TRouteTree>
234
- context?: Partial<RouterOptions<TRouteTree, TDehydrated>['context']>
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,5 @@
1
+ import * as React from 'react'
2
+
3
+ export function SafeFragment(props: any) {
4
+ return <>{props.children}</>
5
+ }
@@ -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
+ }
@@ -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
+ }