@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
@@ -0,0 +1,8 @@
1
+ import * as React from 'react'
2
+
3
+ export const matchContext = React.createContext<string | undefined>(undefined)
4
+
5
+ // N.B. this only exists so we can conditionally call useContext on it when we are not interested in the nearest match
6
+ export const dummyMatchContext = React.createContext<string | undefined>(
7
+ undefined,
8
+ )
package/src/not-found.tsx CHANGED
@@ -1,38 +1,13 @@
1
1
  import * as React from 'react'
2
+ import { isNotFound } from '@tanstack/router-core'
2
3
  import { CatchBoundary } from './CatchBoundary'
3
4
  import { useRouterState } from './useRouterState'
4
- import { RegisteredRouter } from './router'
5
- import { RouteIds } from './routeInfo'
6
-
7
- export type NotFoundError = {
8
- /**
9
- @deprecated
10
- Use `routeId: rootRouteId` instead
11
- */
12
- global?: boolean
13
- /**
14
- @private
15
- Do not use this. It's used internally to indicate a path matching error
16
- */
17
- _global?: boolean
18
- data?: any
19
- throw?: boolean
20
- routeId?: RouteIds<RegisteredRouter['routeTree']>
21
- }
22
-
23
- export function notFound(options: NotFoundError = {}) {
24
- ;(options as any).isNotFound = true
25
- if (options.throw) throw options
26
- return options
27
- }
28
-
29
- export function isNotFound(obj: any): obj is NotFoundError {
30
- return !!obj?.isNotFound
31
- }
5
+ import type { ErrorInfo } from 'react'
6
+ import type { NotFoundError } from '@tanstack/router-core'
32
7
 
33
8
  export function CatchNotFound(props: {
34
9
  fallback?: (error: NotFoundError) => React.ReactElement
35
- onCatch?: (error: any) => void
10
+ onCatch?: (error: Error, errorInfo: ErrorInfo) => void
36
11
  children: React.ReactNode
37
12
  }) {
38
13
  // TODO: Some way for the user to programmatically reset the not-found boundary?
@@ -43,16 +18,20 @@ export function CatchNotFound(props: {
43
18
  return (
44
19
  <CatchBoundary
45
20
  getResetKey={() => resetKey}
46
- onCatch={(error) => {
21
+ onCatch={(error, errorInfo) => {
22
+ if (isNotFound(error)) {
23
+ props.onCatch?.(error, errorInfo)
24
+ } else {
25
+ throw error
26
+ }
27
+ }}
28
+ errorComponent={({ error }: { error: Error }) => {
47
29
  if (isNotFound(error)) {
48
- props.onCatch?.(error)
30
+ return props.fallback?.(error)
49
31
  } else {
50
32
  throw error
51
33
  }
52
34
  }}
53
- errorComponent={({ error }: { error: NotFoundError }) =>
54
- props.fallback?.(error)
55
- }
56
35
  >
57
36
  {props.children}
58
37
  </CatchBoundary>
@@ -0,0 +1,27 @@
1
+ import * as React from 'react'
2
+ import warning from 'tiny-warning'
3
+ import { DefaultGlobalNotFound } from './not-found'
4
+ import type { AnyRoute, AnyRouter } from '@tanstack/router-core'
5
+
6
+ export function renderRouteNotFound(
7
+ router: AnyRouter,
8
+ route: AnyRoute,
9
+ data: any,
10
+ ) {
11
+ if (!route.options.notFoundComponent) {
12
+ if (router.options.defaultNotFoundComponent) {
13
+ return <router.options.defaultNotFoundComponent data={data} />
14
+ }
15
+
16
+ if (process.env.NODE_ENV === 'development') {
17
+ warning(
18
+ route.options.notFoundComponent,
19
+ `A notFoundError was encountered on the route with ID "${route.id}", but a notFoundComponent option was not configured, nor was a router level defaultNotFoundComponent configured. Consider configuring at least one of these to avoid TanStack Router's overly generic defaultNotFoundComponent (<div>Not Found<div>)`,
20
+ )
21
+ }
22
+
23
+ return <DefaultGlobalNotFound />
24
+ }
25
+
26
+ return <route.options.notFoundComponent data={data} />
27
+ }
package/src/route.tsx ADDED
@@ -0,0 +1,572 @@
1
+ import {
2
+ BaseRootRoute,
3
+ BaseRoute,
4
+ BaseRouteApi,
5
+ notFound,
6
+ } from '@tanstack/router-core'
7
+ import React from 'react'
8
+ import { useLoaderData } from './useLoaderData'
9
+ import { useLoaderDeps } from './useLoaderDeps'
10
+ import { useParams } from './useParams'
11
+ import { useSearch } from './useSearch'
12
+ import { useNavigate } from './useNavigate'
13
+ import { useMatch } from './useMatch'
14
+ import { useRouter } from './useRouter'
15
+ import { Link } from './link'
16
+ import type {
17
+ AnyContext,
18
+ AnyRoute,
19
+ AnyRouter,
20
+ ConstrainLiteral,
21
+ ErrorComponentProps,
22
+ NotFoundError,
23
+ NotFoundRouteProps,
24
+ RegisteredRouter,
25
+ ResolveFullPath,
26
+ ResolveId,
27
+ ResolveParams,
28
+ RootRouteId,
29
+ RootRouteOptions,
30
+ RouteConstraints,
31
+ RouteIds,
32
+ RouteMask,
33
+ RouteOptions,
34
+ RouteTypesById,
35
+ RouterCore,
36
+ ToMaskOptions,
37
+ UseNavigateResult,
38
+ } from '@tanstack/router-core'
39
+ import type { UseLoaderDataRoute } from './useLoaderData'
40
+ import type { UseMatchRoute } from './useMatch'
41
+ import type { UseLoaderDepsRoute } from './useLoaderDeps'
42
+ import type { UseParamsRoute } from './useParams'
43
+ import type { UseSearchRoute } from './useSearch'
44
+ import type { UseRouteContextRoute } from './useRouteContext'
45
+ import type { LinkComponentRoute } from './link'
46
+
47
+ declare module '@tanstack/router-core' {
48
+ export interface UpdatableRouteOptionsExtensions {
49
+ component?: RouteComponent
50
+ errorComponent?: false | null | ErrorRouteComponent
51
+ notFoundComponent?: NotFoundRouteComponent
52
+ pendingComponent?: RouteComponent
53
+ }
54
+
55
+ export interface RouteExtensions<
56
+ in out TId extends string,
57
+ in out TFullPath extends string,
58
+ > {
59
+ useMatch: UseMatchRoute<TId>
60
+ useRouteContext: UseRouteContextRoute<TId>
61
+ useSearch: UseSearchRoute<TId>
62
+ useParams: UseParamsRoute<TId>
63
+ useLoaderDeps: UseLoaderDepsRoute<TId>
64
+ useLoaderData: UseLoaderDataRoute<TId>
65
+ useNavigate: () => UseNavigateResult<TFullPath>
66
+ Link: LinkComponentRoute<TFullPath>
67
+ }
68
+ }
69
+
70
+ export function getRouteApi<
71
+ const TId,
72
+ TRouter extends AnyRouter = RegisteredRouter,
73
+ >(id: ConstrainLiteral<TId, RouteIds<TRouter['routeTree']>>) {
74
+ return new RouteApi<TId, TRouter>({ id })
75
+ }
76
+
77
+ export class RouteApi<
78
+ TId,
79
+ TRouter extends AnyRouter = RegisteredRouter,
80
+ > extends BaseRouteApi<TId, TRouter> {
81
+ /**
82
+ * @deprecated Use the `getRouteApi` function instead.
83
+ */
84
+ constructor({ id }: { id: TId }) {
85
+ super({ id })
86
+ }
87
+
88
+ useMatch: UseMatchRoute<TId> = (opts) => {
89
+ return useMatch({
90
+ select: opts?.select,
91
+ from: this.id,
92
+ structuralSharing: opts?.structuralSharing,
93
+ } as any) as any
94
+ }
95
+
96
+ useRouteContext: UseRouteContextRoute<TId> = (opts) => {
97
+ return useMatch({
98
+ from: this.id as any,
99
+ select: (d) => (opts?.select ? opts.select(d.context) : d.context),
100
+ }) as any
101
+ }
102
+
103
+ useSearch: UseSearchRoute<TId> = (opts) => {
104
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
105
+ return useSearch({
106
+ select: opts?.select,
107
+ structuralSharing: opts?.structuralSharing,
108
+ from: this.id,
109
+ } as any) as any
110
+ }
111
+
112
+ useParams: UseParamsRoute<TId> = (opts) => {
113
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
114
+ return useParams({
115
+ select: opts?.select,
116
+ structuralSharing: opts?.structuralSharing,
117
+ from: this.id,
118
+ } as any) as any
119
+ }
120
+
121
+ useLoaderDeps: UseLoaderDepsRoute<TId> = (opts) => {
122
+ return useLoaderDeps({ ...opts, from: this.id, strict: false } as any)
123
+ }
124
+
125
+ useLoaderData: UseLoaderDataRoute<TId> = (opts) => {
126
+ return useLoaderData({ ...opts, from: this.id, strict: false } as any)
127
+ }
128
+
129
+ useNavigate = (): UseNavigateResult<
130
+ RouteTypesById<TRouter, TId>['fullPath']
131
+ > => {
132
+ const router = useRouter()
133
+ return useNavigate({ from: router.routesById[this.id as string].fullPath })
134
+ }
135
+
136
+ notFound = (opts?: NotFoundError) => {
137
+ return notFound({ routeId: this.id as string, ...opts })
138
+ }
139
+
140
+ Link: LinkComponentRoute<RouteTypesById<TRouter, TId>['fullPath']> =
141
+ React.forwardRef((props, ref: React.ForwardedRef<HTMLAnchorElement>) => {
142
+ const router = useRouter()
143
+ const fullPath = router.routesById[this.id as string].fullPath
144
+ return <Link ref={ref} from={fullPath as never} {...props} />
145
+ }) as unknown as LinkComponentRoute<
146
+ RouteTypesById<TRouter, TId>['fullPath']
147
+ >
148
+ }
149
+
150
+ export class Route<
151
+ in out TParentRoute extends RouteConstraints['TParentRoute'] = AnyRoute,
152
+ in out TPath extends RouteConstraints['TPath'] = '/',
153
+ in out TFullPath extends RouteConstraints['TFullPath'] = ResolveFullPath<
154
+ TParentRoute,
155
+ TPath
156
+ >,
157
+ in out TCustomId extends RouteConstraints['TCustomId'] = string,
158
+ in out TId extends RouteConstraints['TId'] = ResolveId<
159
+ TParentRoute,
160
+ TCustomId,
161
+ TPath
162
+ >,
163
+ in out TSearchValidator = undefined,
164
+ in out TParams = ResolveParams<TPath>,
165
+ in out TRouterContext = AnyContext,
166
+ in out TRouteContextFn = AnyContext,
167
+ in out TBeforeLoadFn = AnyContext,
168
+ in out TLoaderDeps extends Record<string, any> = {},
169
+ in out TLoaderFn = undefined,
170
+ in out TChildren = unknown,
171
+ in out TFileRouteTypes = unknown,
172
+ > extends BaseRoute<
173
+ TParentRoute,
174
+ TPath,
175
+ TFullPath,
176
+ TCustomId,
177
+ TId,
178
+ TSearchValidator,
179
+ TParams,
180
+ TRouterContext,
181
+ TRouteContextFn,
182
+ TBeforeLoadFn,
183
+ TLoaderDeps,
184
+ TLoaderFn,
185
+ TChildren,
186
+ TFileRouteTypes
187
+ > {
188
+ /**
189
+ * @deprecated Use the `createRoute` function instead.
190
+ */
191
+ constructor(
192
+ options?: RouteOptions<
193
+ TParentRoute,
194
+ TId,
195
+ TCustomId,
196
+ TFullPath,
197
+ TPath,
198
+ TSearchValidator,
199
+ TParams,
200
+ TLoaderDeps,
201
+ TLoaderFn,
202
+ TRouterContext,
203
+ TRouteContextFn,
204
+ TBeforeLoadFn
205
+ >,
206
+ ) {
207
+ super(options)
208
+ ;(this as any).$$typeof = Symbol.for('react.memo')
209
+ }
210
+
211
+ useMatch: UseMatchRoute<TId> = (opts) => {
212
+ return useMatch({
213
+ select: opts?.select,
214
+ from: this.id,
215
+ structuralSharing: opts?.structuralSharing,
216
+ } as any) as any
217
+ }
218
+
219
+ useRouteContext: UseRouteContextRoute<TId> = (opts?) => {
220
+ return useMatch({
221
+ ...opts,
222
+ from: this.id,
223
+ select: (d) => (opts?.select ? opts.select(d.context) : d.context),
224
+ }) as any
225
+ }
226
+
227
+ useSearch: UseSearchRoute<TId> = (opts) => {
228
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
229
+ return useSearch({
230
+ select: opts?.select,
231
+ structuralSharing: opts?.structuralSharing,
232
+ from: this.id,
233
+ } as any) as any
234
+ }
235
+
236
+ useParams: UseParamsRoute<TId> = (opts) => {
237
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
238
+ return useParams({
239
+ select: opts?.select,
240
+ structuralSharing: opts?.structuralSharing,
241
+ from: this.id,
242
+ } as any) as any
243
+ }
244
+
245
+ useLoaderDeps: UseLoaderDepsRoute<TId> = (opts) => {
246
+ return useLoaderDeps({ ...opts, from: this.id } as any)
247
+ }
248
+
249
+ useLoaderData: UseLoaderDataRoute<TId> = (opts) => {
250
+ return useLoaderData({ ...opts, from: this.id } as any)
251
+ }
252
+
253
+ useNavigate = (): UseNavigateResult<TFullPath> => {
254
+ return useNavigate({ from: this.fullPath })
255
+ }
256
+
257
+ Link: LinkComponentRoute<TFullPath> = React.forwardRef(
258
+ (props, ref: React.ForwardedRef<HTMLAnchorElement>) => {
259
+ return <Link ref={ref} from={this.fullPath as never} {...props} />
260
+ },
261
+ ) as unknown as LinkComponentRoute<TFullPath>
262
+ }
263
+
264
+ export function createRoute<
265
+ TParentRoute extends RouteConstraints['TParentRoute'] = AnyRoute,
266
+ TPath extends RouteConstraints['TPath'] = '/',
267
+ TFullPath extends RouteConstraints['TFullPath'] = ResolveFullPath<
268
+ TParentRoute,
269
+ TPath
270
+ >,
271
+ TCustomId extends RouteConstraints['TCustomId'] = string,
272
+ TId extends RouteConstraints['TId'] = ResolveId<
273
+ TParentRoute,
274
+ TCustomId,
275
+ TPath
276
+ >,
277
+ TSearchValidator = undefined,
278
+ TParams = ResolveParams<TPath>,
279
+ TRouteContextFn = AnyContext,
280
+ TBeforeLoadFn = AnyContext,
281
+ TLoaderDeps extends Record<string, any> = {},
282
+ TLoaderFn = undefined,
283
+ TChildren = unknown,
284
+ >(
285
+ options: RouteOptions<
286
+ TParentRoute,
287
+ TId,
288
+ TCustomId,
289
+ TFullPath,
290
+ TPath,
291
+ TSearchValidator,
292
+ TParams,
293
+ TLoaderDeps,
294
+ TLoaderFn,
295
+ AnyContext,
296
+ TRouteContextFn,
297
+ TBeforeLoadFn
298
+ >,
299
+ ): Route<
300
+ TParentRoute,
301
+ TPath,
302
+ TFullPath,
303
+ TCustomId,
304
+ TId,
305
+ TSearchValidator,
306
+ TParams,
307
+ AnyContext,
308
+ TRouteContextFn,
309
+ TBeforeLoadFn,
310
+ TLoaderDeps,
311
+ TLoaderFn,
312
+ TChildren
313
+ > {
314
+ return new Route<
315
+ TParentRoute,
316
+ TPath,
317
+ TFullPath,
318
+ TCustomId,
319
+ TId,
320
+ TSearchValidator,
321
+ TParams,
322
+ AnyContext,
323
+ TRouteContextFn,
324
+ TBeforeLoadFn,
325
+ TLoaderDeps,
326
+ TLoaderFn,
327
+ TChildren
328
+ >(options)
329
+ }
330
+
331
+ export type AnyRootRoute = RootRoute<any, any, any, any, any, any, any, any>
332
+
333
+ export function createRootRouteWithContext<TRouterContext extends {}>() {
334
+ return <
335
+ TRouteContextFn = AnyContext,
336
+ TBeforeLoadFn = AnyContext,
337
+ TSearchValidator = undefined,
338
+ TLoaderDeps extends Record<string, any> = {},
339
+ TLoaderFn = undefined,
340
+ >(
341
+ options?: RootRouteOptions<
342
+ TSearchValidator,
343
+ TRouterContext,
344
+ TRouteContextFn,
345
+ TBeforeLoadFn,
346
+ TLoaderDeps,
347
+ TLoaderFn
348
+ >,
349
+ ) => {
350
+ return createRootRoute<
351
+ TSearchValidator,
352
+ TRouterContext,
353
+ TRouteContextFn,
354
+ TBeforeLoadFn,
355
+ TLoaderDeps,
356
+ TLoaderFn
357
+ >(options as any)
358
+ }
359
+ }
360
+
361
+ /**
362
+ * @deprecated Use the `createRootRouteWithContext` function instead.
363
+ */
364
+ export const rootRouteWithContext = createRootRouteWithContext
365
+
366
+ export class RootRoute<
367
+ in out TSearchValidator = undefined,
368
+ in out TRouterContext = {},
369
+ in out TRouteContextFn = AnyContext,
370
+ in out TBeforeLoadFn = AnyContext,
371
+ in out TLoaderDeps extends Record<string, any> = {},
372
+ in out TLoaderFn = undefined,
373
+ in out TChildren = unknown,
374
+ in out TFileRouteTypes = unknown,
375
+ > extends BaseRootRoute<
376
+ TSearchValidator,
377
+ TRouterContext,
378
+ TRouteContextFn,
379
+ TBeforeLoadFn,
380
+ TLoaderDeps,
381
+ TLoaderFn,
382
+ TChildren,
383
+ TFileRouteTypes
384
+ > {
385
+ /**
386
+ * @deprecated `RootRoute` is now an internal implementation detail. Use `createRootRoute()` instead.
387
+ */
388
+ constructor(
389
+ options?: RootRouteOptions<
390
+ TSearchValidator,
391
+ TRouterContext,
392
+ TRouteContextFn,
393
+ TBeforeLoadFn,
394
+ TLoaderDeps,
395
+ TLoaderFn
396
+ >,
397
+ ) {
398
+ super(options)
399
+ ;(this as any).$$typeof = Symbol.for('react.memo')
400
+ }
401
+
402
+ useMatch: UseMatchRoute<RootRouteId> = (opts) => {
403
+ return useMatch({
404
+ select: opts?.select,
405
+ from: this.id,
406
+ structuralSharing: opts?.structuralSharing,
407
+ } as any) as any
408
+ }
409
+
410
+ useRouteContext: UseRouteContextRoute<RootRouteId> = (opts) => {
411
+ return useMatch({
412
+ ...opts,
413
+ from: this.id,
414
+ select: (d) => (opts?.select ? opts.select(d.context) : d.context),
415
+ }) as any
416
+ }
417
+
418
+ useSearch: UseSearchRoute<RootRouteId> = (opts) => {
419
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
420
+ return useSearch({
421
+ select: opts?.select,
422
+ structuralSharing: opts?.structuralSharing,
423
+ from: this.id,
424
+ } as any) as any
425
+ }
426
+
427
+ useParams: UseParamsRoute<RootRouteId> = (opts) => {
428
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
429
+ return useParams({
430
+ select: opts?.select,
431
+ structuralSharing: opts?.structuralSharing,
432
+ from: this.id,
433
+ } as any) as any
434
+ }
435
+
436
+ useLoaderDeps: UseLoaderDepsRoute<RootRouteId> = (opts) => {
437
+ return useLoaderDeps({ ...opts, from: this.id } as any)
438
+ }
439
+
440
+ useLoaderData: UseLoaderDataRoute<RootRouteId> = (opts) => {
441
+ return useLoaderData({ ...opts, from: this.id } as any)
442
+ }
443
+
444
+ useNavigate = (): UseNavigateResult<'/'> => {
445
+ return useNavigate({ from: this.fullPath })
446
+ }
447
+
448
+ Link: LinkComponentRoute<'/'> = React.forwardRef(
449
+ (props, ref: React.ForwardedRef<HTMLAnchorElement>) => {
450
+ return <Link ref={ref} from={this.fullPath} {...props} />
451
+ },
452
+ ) as unknown as LinkComponentRoute<'/'>
453
+ }
454
+
455
+ export function createRootRoute<
456
+ TSearchValidator = undefined,
457
+ TRouterContext = {},
458
+ TRouteContextFn = AnyContext,
459
+ TBeforeLoadFn = AnyContext,
460
+ TLoaderDeps extends Record<string, any> = {},
461
+ TLoaderFn = undefined,
462
+ >(
463
+ options?: RootRouteOptions<
464
+ TSearchValidator,
465
+ TRouterContext,
466
+ TRouteContextFn,
467
+ TBeforeLoadFn,
468
+ TLoaderDeps,
469
+ TLoaderFn
470
+ >,
471
+ ): RootRoute<
472
+ TSearchValidator,
473
+ TRouterContext,
474
+ TRouteContextFn,
475
+ TBeforeLoadFn,
476
+ TLoaderDeps,
477
+ TLoaderFn,
478
+ unknown,
479
+ unknown
480
+ > {
481
+ return new RootRoute<
482
+ TSearchValidator,
483
+ TRouterContext,
484
+ TRouteContextFn,
485
+ TBeforeLoadFn,
486
+ TLoaderDeps,
487
+ TLoaderFn
488
+ >(options)
489
+ }
490
+
491
+ export function createRouteMask<
492
+ TRouteTree extends AnyRoute,
493
+ TFrom extends string,
494
+ TTo extends string,
495
+ >(
496
+ opts: {
497
+ routeTree: TRouteTree
498
+ } & ToMaskOptions<RouterCore<TRouteTree, 'never', boolean>, TFrom, TTo>,
499
+ ): RouteMask<TRouteTree> {
500
+ return opts as any
501
+ }
502
+
503
+ export type ReactNode = any
504
+
505
+ export type SyncRouteComponent<TProps> =
506
+ | ((props: TProps) => ReactNode)
507
+ | React.LazyExoticComponent<(props: TProps) => ReactNode>
508
+
509
+ export type AsyncRouteComponent<TProps> = SyncRouteComponent<TProps> & {
510
+ preload?: () => Promise<void>
511
+ }
512
+
513
+ export type RouteComponent<TProps = any> = AsyncRouteComponent<TProps>
514
+
515
+ export type ErrorRouteComponent = RouteComponent<ErrorComponentProps>
516
+
517
+ export type NotFoundRouteComponent = SyncRouteComponent<NotFoundRouteProps>
518
+
519
+ export class NotFoundRoute<
520
+ TParentRoute extends AnyRootRoute,
521
+ TRouterContext = AnyContext,
522
+ TRouteContextFn = AnyContext,
523
+ TBeforeLoadFn = AnyContext,
524
+ TSearchValidator = undefined,
525
+ TLoaderDeps extends Record<string, any> = {},
526
+ TLoaderFn = undefined,
527
+ TChildren = unknown,
528
+ > extends Route<
529
+ TParentRoute,
530
+ '/404',
531
+ '/404',
532
+ '404',
533
+ '404',
534
+ TSearchValidator,
535
+ {},
536
+ TRouterContext,
537
+ TRouteContextFn,
538
+ TBeforeLoadFn,
539
+ TLoaderDeps,
540
+ TLoaderFn,
541
+ TChildren
542
+ > {
543
+ constructor(
544
+ options: Omit<
545
+ RouteOptions<
546
+ TParentRoute,
547
+ string,
548
+ string,
549
+ string,
550
+ string,
551
+ TSearchValidator,
552
+ {},
553
+ TLoaderDeps,
554
+ TLoaderFn,
555
+ TRouterContext,
556
+ TRouteContextFn,
557
+ TBeforeLoadFn
558
+ >,
559
+ | 'caseSensitive'
560
+ | 'parseParams'
561
+ | 'stringifyParams'
562
+ | 'path'
563
+ | 'id'
564
+ | 'params'
565
+ >,
566
+ ) {
567
+ super({
568
+ ...(options as any),
569
+ id: '404',
570
+ })
571
+ }
572
+ }