@tanstack/react-router 1.20.0 → 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 -33
  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 -33
  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 -62
  320. package/src/searchParams.ts +0 -79
package/src/awaited.tsx CHANGED
@@ -1,98 +1,26 @@
1
1
  import * as React from 'react'
2
- import { useRouter } from './useRouter'
3
- import { defaultSerializeError } from './router'
4
- import { DeferredPromise, isDehydratedDeferred } from './defer'
5
- import { defaultDeserializeError, isServerSideError } from './Matches'
6
2
 
7
- import warning from 'tiny-warning'
3
+ import { TSR_DEFERRED_PROMISE, defer } from '@tanstack/router-core'
4
+ import type { DeferredPromise } from '@tanstack/router-core'
8
5
 
9
6
  export type AwaitOptions<T> = {
10
- promise: DeferredPromise<T>
7
+ promise: Promise<T>
11
8
  }
12
9
 
13
- export function useAwaited<T>({ promise }: AwaitOptions<T>): [T] {
14
- const router = useRouter()
15
- // const rerender = React.useReducer((x) => x + 1, 0)[1]
10
+ export function useAwaited<T>({
11
+ promise: _promise,
12
+ }: AwaitOptions<T>): [T, DeferredPromise<T>] {
13
+ const promise = defer(_promise)
16
14
 
17
- const state = promise.__deferredState
18
-
19
- // Dehydrated promises only
20
- // Successful or errored deferred promises mean they
21
- // were resolved on the server and no further action is needed
22
- if (isDehydratedDeferred(promise) && state.status === 'pending') {
23
- const streamedData = (window as any)[`__TSR__DEFERRED__${state.uid}`]
24
-
25
- if (streamedData) {
26
- Object.assign(state, router.options.transformer.parse(streamedData))
27
- } else {
28
- let token = router.registeredDeferredsIds.get(state.uid)
29
-
30
- // If we haven't yet, create a promise and resolver that our streamed HTML can use
31
- // when the client-side data is streamed in and ready.
32
- if (!token) {
33
- token = {}
34
- router.registeredDeferredsIds.set(state.uid, token)
35
- router.registeredDeferreds.set(token, state)
36
-
37
- Object.assign(state, {
38
- resolve: () => {
39
- state.__resolvePromise?.()
40
- // rerender()
41
- },
42
- promise: new Promise((r) => {
43
- state.__resolvePromise = r as any
44
- }),
45
- __resolvePromise: () => {},
46
- })
47
- }
48
- }
15
+ if (promise[TSR_DEFERRED_PROMISE].status === 'pending') {
16
+ throw promise
49
17
  }
50
18
 
51
- // If the promise is pending, always throw the state.promise
52
- // For originating promises, this will be the original promise
53
- // For dehydrated promises, this will be the placeholder promise
54
- // that will be resolved when the server sends the real data
55
- if (state.status === 'pending') {
56
- throw isDehydratedDeferred(promise) ? state.promise : promise
19
+ if (promise[TSR_DEFERRED_PROMISE].status === 'error') {
20
+ throw promise[TSR_DEFERRED_PROMISE].error
57
21
  }
58
22
 
59
- // If we are the originator of the promise,
60
- // inject the state into the HTML stream
61
- if (!isDehydratedDeferred(promise)) {
62
- router.injectHtml(`<script class='tsr_deferred_data'>window.__TSR__DEFERRED__${state.uid} = ${JSON.stringify(router.options.transformer.stringify(state))}</script>
63
- <script class='tsr_deferred_handler'>
64
- if (window.__TSR__ROUTER__) {
65
- let deferred = window.__TSR__ROUTER__.getDeferred('${state.uid}')
66
- if (deferred) deferred.resolve(window.__TSR__DEFERRED__${state.uid})
67
- }
68
- document.querySelectorAll('.tsr_deferred_handler').forEach((el) => el.parentElement.removeChild(el))
69
- </script>`)
70
- }
71
-
72
- if (state.status === 'error') {
73
- if (typeof document !== 'undefined') {
74
- if (isServerSideError(state.error)) {
75
- throw (
76
- router.options.errorSerializer?.deserialize ?? defaultDeserializeError
77
- )(state.error.data as any)
78
- } else {
79
- warning(
80
- false,
81
- "Encountered a server-side error that doesn't fit the expected shape",
82
- )
83
- throw state.error
84
- }
85
- } else {
86
- throw {
87
- data: (
88
- router.options.errorSerializer?.serialize ?? defaultSerializeError
89
- )(state.error),
90
- __isServerError: true,
91
- }
92
- }
93
- }
94
-
95
- return [promise.__deferredState.data as any]
23
+ return [promise[TSR_DEFERRED_PROMISE].data, promise]
96
24
  }
97
25
 
98
26
  export function Await<T>(
@@ -113,7 +41,8 @@ function AwaitInner<T>(
113
41
  fallback?: React.ReactNode
114
42
  children: (result: T) => React.ReactNode
115
43
  },
116
- ) {
117
- const awaited = useAwaited(props)
118
- return props.children(...awaited)
44
+ ): React.JSX.Element {
45
+ const [data] = useAwaited(props)
46
+
47
+ return props.children(data) as React.JSX.Element
119
48
  }
package/src/fileRoute.ts CHANGED
@@ -1,123 +1,54 @@
1
- import { NoInfer } from '@tanstack/react-store'
2
- import { ParsePathParams } from './link'
3
- import {
4
- AnyRoute,
5
- ResolveFullPath,
6
- ResolveFullSearchSchema,
7
- MergeFromFromParent,
8
- RouteContext,
1
+ import warning from 'tiny-warning'
2
+ import { createRoute } from './route'
3
+
4
+ import { useMatch } from './useMatch'
5
+ import { useLoaderDeps } from './useLoaderDeps'
6
+ import { useLoaderData } from './useLoaderData'
7
+ import { useSearch } from './useSearch'
8
+ import { useParams } from './useParams'
9
+ import { useNavigate } from './useNavigate'
10
+ import { useRouter } from './useRouter'
11
+ import type { UseParamsRoute } from './useParams'
12
+ import type { UseMatchRoute } from './useMatch'
13
+ import type { UseSearchRoute } from './useSearch'
14
+ import type {
9
15
  AnyContext,
10
- RouteOptions,
11
- UpdatableRouteOptions,
16
+ AnyRoute,
17
+ AnyRouter,
18
+ Constrain,
19
+ ConstrainLiteral,
20
+ FileBaseRouteOptions,
21
+ FileRoutesByPath,
22
+ LazyRouteOptions,
23
+ RegisteredRouter,
24
+ ResolveParams,
12
25
  Route,
13
- createRoute,
14
- RootRouteId,
15
- TrimPathLeft,
26
+ RouteById,
16
27
  RouteConstraints,
17
- ResolveFullSearchSchemaInput,
18
- SearchSchemaInput,
28
+ RouteIds,
19
29
  RouteLoaderFn,
20
- AnyPathParams,
21
- AnySearchSchema,
22
- FileBaseRouteOptions,
23
- } from './route'
24
- import { Assign, Expand, IsAny } from './utils'
25
- import { useMatch, useLoaderDeps, useLoaderData, RouteMatch } from './Matches'
26
- import { useSearch } from './useSearch'
27
- import { useParams } from './useParams'
28
- import warning from 'tiny-warning'
29
- import { RegisteredRouter } from './router'
30
- import { RouteById, RouteIds } from './routeInfo'
31
-
32
- export interface FileRoutesByPath {
33
- // '/': {
34
- // parentRoute: typeof rootRoute
35
- // }
36
- }
37
-
38
- type Replace<
39
- S extends string,
40
- From extends string,
41
- To extends string,
42
- > = S extends `${infer Start}${From}${infer Rest}`
43
- ? `${Start}${To}${Replace<Rest, From, To>}`
44
- : S
45
-
46
- export type TrimLeft<
47
- T extends string,
48
- S extends string,
49
- > = T extends `${S}${infer U}` ? U : T
50
-
51
- export type TrimRight<
52
- T extends string,
53
- S extends string,
54
- > = T extends `${infer U}${S}` ? U : T
55
-
56
- export type Trim<T extends string, S extends string> = TrimLeft<
57
- TrimRight<T, S>,
58
- S
59
- >
60
-
61
- export type RemoveUnderScores<T extends string> = Replace<
62
- Replace<TrimRight<TrimLeft<T, '/_'>, '_'>, '_/', '/'>,
63
- '/_',
64
- '/'
65
- >
66
-
67
- type RemoveRouteGroups<S extends string> =
68
- S extends `${infer Before}(${infer RouteGroup})${infer After}`
69
- ? RemoveRouteGroups<`${Before}${After}`>
70
- : S
71
-
72
- type NormalizeSlashes<S extends string> =
73
- S extends `${infer Before}//${infer After}`
74
- ? NormalizeSlashes<`${Before}/${After}`>
75
- : S
76
-
77
- type ReplaceFirstOccurrence<
78
- T extends string,
79
- Search extends string,
80
- Replacement extends string,
81
- > = T extends `${infer Prefix}${Search}${infer Suffix}`
82
- ? `${Prefix}${Replacement}${Suffix}`
83
- : T
84
-
85
- export type ResolveFilePath<
86
- TParentRoute extends AnyRoute,
87
- TFilePath extends string,
88
- > = TParentRoute['id'] extends RootRouteId
89
- ? TrimPathLeft<TFilePath>
90
- : ReplaceFirstOccurrence<
91
- TrimPathLeft<TFilePath>,
92
- TrimPathLeft<TParentRoute['types']['customId']>,
93
- ''
94
- >
95
-
96
- export type FileRoutePath<
97
- TParentRoute extends AnyRoute,
98
- TFilePath extends string,
99
- > =
100
- ResolveFilePath<TParentRoute, TFilePath> extends `_${infer _}`
101
- ? ''
102
- : ResolveFilePath<TParentRoute, TFilePath> extends `/_${infer _}`
103
- ? ''
104
- : ResolveFilePath<TParentRoute, TFilePath>
30
+ UpdatableRouteOptions,
31
+ UseNavigateResult,
32
+ } from '@tanstack/router-core'
33
+ import type { UseLoaderDepsRoute } from './useLoaderDeps'
34
+ import type { UseLoaderDataRoute } from './useLoaderData'
35
+ import type { UseRouteContextRoute } from './useRouteContext'
105
36
 
106
37
  export function createFileRoute<
107
38
  TFilePath extends keyof FileRoutesByPath,
108
39
  TParentRoute extends AnyRoute = FileRoutesByPath[TFilePath]['parentRoute'],
109
- TId extends RouteConstraints['TId'] = NormalizeSlashes<
110
- RemoveRouteGroups<TFilePath>
111
- >,
112
- TPath extends RouteConstraints['TPath'] = FileRoutePath<
113
- TParentRoute,
114
- TFilePath
115
- >,
116
- TFullPath extends RouteConstraints['TFullPath'] = ResolveFullPath<
117
- TParentRoute,
118
- NormalizeSlashes<RemoveRouteGroups<RemoveUnderScores<TPath>>>
119
- >,
120
- >(path: TFilePath) {
40
+ TId extends RouteConstraints['TId'] = FileRoutesByPath[TFilePath]['id'],
41
+ TPath extends RouteConstraints['TPath'] = FileRoutesByPath[TFilePath]['path'],
42
+ TFullPath extends
43
+ RouteConstraints['TFullPath'] = FileRoutesByPath[TFilePath]['fullPath'],
44
+ >(
45
+ path?: TFilePath,
46
+ ): FileRoute<TFilePath, TParentRoute, TId, TPath, TFullPath>['createRoute'] {
47
+ if (typeof path === 'object') {
48
+ return new FileRoute<TFilePath, TParentRoute, TId, TPath, TFullPath>(path, {
49
+ silent: true,
50
+ }).createRoute(path) as any
51
+ }
121
52
  return new FileRoute<TFilePath, TParentRoute, TId, TPath, TFullPath>(path, {
122
53
  silent: true,
123
54
  }).createRoute
@@ -130,110 +61,68 @@ export function createFileRoute<
130
61
  export class FileRoute<
131
62
  TFilePath extends keyof FileRoutesByPath,
132
63
  TParentRoute extends AnyRoute = FileRoutesByPath[TFilePath]['parentRoute'],
133
- TId extends RouteConstraints['TId'] = TFilePath,
134
- TPath extends RouteConstraints['TPath'] = FileRoutePath<
135
- TParentRoute,
136
- TFilePath
137
- >,
138
- TFullPath extends RouteConstraints['TFullPath'] = ResolveFullPath<
139
- TParentRoute,
140
- RemoveUnderScores<TPath>
141
- >,
64
+ TId extends RouteConstraints['TId'] = FileRoutesByPath[TFilePath]['id'],
65
+ TPath extends RouteConstraints['TPath'] = FileRoutesByPath[TFilePath]['path'],
66
+ TFullPath extends
67
+ RouteConstraints['TFullPath'] = FileRoutesByPath[TFilePath]['fullPath'],
142
68
  > {
143
69
  silent?: boolean
144
70
 
145
71
  constructor(
146
- public path: TFilePath,
72
+ public path?: TFilePath,
147
73
  _opts?: { silent: boolean },
148
74
  ) {
149
75
  this.silent = _opts?.silent
150
76
  }
151
77
 
152
78
  createRoute = <
153
- TSearchSchemaInput extends RouteConstraints['TSearchSchema'] = {},
154
- TSearchSchema extends RouteConstraints['TSearchSchema'] = {},
155
- TSearchSchemaUsed extends Record<
156
- string,
157
- any
158
- > = TSearchSchemaInput extends SearchSchemaInput
159
- ? Omit<TSearchSchemaInput, keyof SearchSchemaInput>
160
- : TSearchSchema,
161
- TFullSearchSchemaInput extends
162
- RouteConstraints['TFullSearchSchema'] = ResolveFullSearchSchemaInput<
163
- TParentRoute,
164
- TSearchSchemaUsed
165
- >,
166
- TFullSearchSchema extends
167
- RouteConstraints['TFullSearchSchema'] = ResolveFullSearchSchema<
168
- TParentRoute,
169
- TSearchSchema
170
- >,
171
- TParams extends RouteConstraints['TParams'] = Expand<
172
- Record<ParsePathParams<TPath>, string>
173
- >,
174
- TAllParams extends RouteConstraints['TAllParams'] = MergeFromFromParent<
175
- TParentRoute['types']['allParams'],
176
- TParams
177
- >,
178
- TRouteContextReturn extends
179
- RouteConstraints['TRouteContext'] = RouteContext,
180
- TRouteContext extends RouteConstraints['TRouteContext'] = [
181
- TRouteContextReturn,
182
- ] extends [never]
183
- ? RouteContext
184
- : TRouteContextReturn,
185
- TAllContext extends Expand<
186
- Assign<IsAny<TParentRoute['types']['allContext'], {}>, TRouteContext>
187
- > = Expand<
188
- Assign<IsAny<TParentRoute['types']['allContext'], {}>, TRouteContext>
189
- >,
190
- TRouterContext extends RouteConstraints['TRouterContext'] = AnyContext,
79
+ TSearchValidator = undefined,
80
+ TParams = ResolveParams<TPath>,
81
+ TRouteContextFn = AnyContext,
82
+ TBeforeLoadFn = AnyContext,
191
83
  TLoaderDeps extends Record<string, any> = {},
192
- TLoaderDataReturn extends any = unknown,
193
- TLoaderData extends any = [TLoaderDataReturn] extends [never]
194
- ? undefined
195
- : TLoaderDataReturn,
196
- TChildren extends RouteConstraints['TChildren'] = unknown,
197
- TRouteTree extends RouteConstraints['TRouteTree'] = AnyRoute,
84
+ TLoaderFn = undefined,
85
+ TChildren = unknown,
198
86
  >(
199
87
  options?: FileBaseRouteOptions<
200
88
  TParentRoute,
89
+ TId,
201
90
  TPath,
202
- TSearchSchemaInput,
203
- TSearchSchema,
204
- TFullSearchSchema,
91
+ TSearchValidator,
205
92
  TParams,
206
- TAllParams,
207
- TRouteContextReturn,
208
- TRouteContext,
209
- TRouterContext,
210
- TAllContext,
211
93
  TLoaderDeps,
212
- TLoaderDataReturn
94
+ TLoaderFn,
95
+ AnyContext,
96
+ TRouteContextFn,
97
+ TBeforeLoadFn
213
98
  > &
214
- UpdatableRouteOptions<TAllParams, TFullSearchSchema, TLoaderData>,
99
+ UpdatableRouteOptions<
100
+ TParentRoute,
101
+ TId,
102
+ TFullPath,
103
+ TParams,
104
+ TSearchValidator,
105
+ TLoaderFn,
106
+ TLoaderDeps,
107
+ AnyContext,
108
+ TRouteContextFn,
109
+ TBeforeLoadFn
110
+ >,
215
111
  ): Route<
216
112
  TParentRoute,
217
113
  TPath,
218
114
  TFullPath,
219
115
  TFilePath,
220
116
  TId,
221
- TSearchSchemaInput,
222
- TSearchSchema,
223
- TSearchSchemaUsed,
224
- TFullSearchSchemaInput,
225
- TFullSearchSchema,
117
+ TSearchValidator,
226
118
  TParams,
227
- TAllParams,
228
- TRouteContextReturn,
229
- TRouteContext,
230
- TAllContext,
231
- TRouterContext,
119
+ AnyContext,
120
+ TRouteContextFn,
121
+ TBeforeLoadFn,
232
122
  TLoaderDeps,
233
- TLoaderDataReturn,
234
- TLoaderData,
123
+ TLoaderFn,
235
124
  TChildren,
236
- TRouteTree
125
+ unknown
237
126
  > => {
238
127
  warning(
239
128
  this.silent,
@@ -255,32 +144,38 @@ export function FileRouteLoader<
255
144
  TRoute extends FileRoutesByPath[TFilePath]['preLoaderRoute'],
256
145
  >(
257
146
  _path: TFilePath,
258
- ): <TLoaderData extends any>(
259
- loaderFn: RouteLoaderFn<
260
- TRoute['types']['allParams'],
261
- TRoute['types']['loaderDeps'],
262
- TRoute['types']['allContext'],
263
- TRoute['types']['routeContext'],
264
- TLoaderData
147
+ ): <TLoaderFn>(
148
+ loaderFn: Constrain<
149
+ TLoaderFn,
150
+ RouteLoaderFn<
151
+ TRoute['parentRoute'],
152
+ TRoute['types']['id'],
153
+ TRoute['types']['params'],
154
+ TRoute['types']['loaderDeps'],
155
+ TRoute['types']['routerContext'],
156
+ TRoute['types']['routeContextFn'],
157
+ TRoute['types']['beforeLoadFn']
158
+ >
265
159
  >,
266
- ) => RouteLoaderFn<
267
- TRoute['types']['allParams'],
268
- TRoute['types']['loaderDeps'],
269
- TRoute['types']['allContext'],
270
- TRoute['types']['routeContext'],
271
- NoInfer<TLoaderData>
272
- > {
160
+ ) => TLoaderFn {
273
161
  warning(
274
162
  false,
275
163
  `FileRouteLoader is deprecated and will be removed in the next major version. Please place the loader function in the the main route file, inside the \`createFileRoute('/path/to/file')(options)\` options`,
276
164
  )
277
- return (loaderFn) => loaderFn
165
+ return (loaderFn) => loaderFn as any
278
166
  }
279
167
 
280
- export type LazyRouteOptions = Pick<
281
- UpdatableRouteOptions<AnyPathParams, AnySearchSchema, any>,
282
- 'component' | 'errorComponent' | 'pendingComponent' | 'notFoundComponent'
283
- >
168
+ declare module '@tanstack/router-core' {
169
+ export interface LazyRoute<in out TRoute extends AnyRoute> {
170
+ useMatch: UseMatchRoute<TRoute['id']>
171
+ useRouteContext: UseRouteContextRoute<TRoute['id']>
172
+ useSearch: UseSearchRoute<TRoute['id']>
173
+ useParams: UseParamsRoute<TRoute['id']>
174
+ useLoaderDeps: UseLoaderDepsRoute<TRoute['id']>
175
+ useLoaderData: UseLoaderDataRoute<TRoute['id']>
176
+ useNavigate: () => UseNavigateResult<TRoute['fullPath']>
177
+ }
178
+ }
284
179
 
285
180
  export class LazyRoute<TRoute extends AnyRoute> {
286
181
  options: {
@@ -296,71 +191,73 @@ export class LazyRoute<TRoute extends AnyRoute> {
296
191
  ;(this as any).$$typeof = Symbol.for('react.memo')
297
192
  }
298
193
 
299
- useMatch = <
300
- TRouteMatchState = RouteMatch<
301
- TRoute['types']['routeTree'],
302
- TRoute['types']['id']
303
- >,
304
- TSelected = TRouteMatchState,
305
- >(opts?: {
306
- select?: (match: TRouteMatchState) => TSelected
307
- }): TSelected => {
308
- return useMatch({ select: opts?.select, from: this.options.id })
194
+ useMatch: UseMatchRoute<TRoute['id']> = (opts) => {
195
+ return useMatch({
196
+ select: opts?.select,
197
+ from: this.options.id,
198
+ structuralSharing: opts?.structuralSharing,
199
+ } as any) as any
309
200
  }
310
201
 
311
- useRouteContext = <TSelected = TRoute['types']['allContext']>(opts?: {
312
- select?: (s: TRoute['types']['allContext']) => TSelected
313
- }): TSelected => {
202
+ useRouteContext: UseRouteContextRoute<TRoute['id']> = (opts) => {
314
203
  return useMatch({
315
204
  from: this.options.id,
316
205
  select: (d: any) => (opts?.select ? opts.select(d.context) : d.context),
317
- })
206
+ }) as any
318
207
  }
319
208
 
320
- useSearch = <TSelected = TRoute['types']['fullSearchSchema']>(opts?: {
321
- select?: (s: TRoute['types']['fullSearchSchema']) => TSelected
322
- }): TSelected => {
323
- return useSearch({ ...opts, from: this.options.id })
209
+ useSearch: UseSearchRoute<TRoute['id']> = (opts) => {
210
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
211
+ return useSearch({
212
+ select: opts?.select,
213
+ structuralSharing: opts?.structuralSharing,
214
+ from: this.options.id,
215
+ } as any) as any
324
216
  }
325
217
 
326
- useParams = <TSelected = TRoute['types']['allParams']>(opts?: {
327
- select?: (s: TRoute['types']['allParams']) => TSelected
328
- }): TSelected => {
329
- return useParams({ ...opts, from: this.options.id })
218
+ useParams: UseParamsRoute<TRoute['id']> = (opts) => {
219
+ // eslint-disable-next-line @typescript-eslint/no-unnecessary-type-assertion
220
+ return useParams({
221
+ select: opts?.select,
222
+ structuralSharing: opts?.structuralSharing,
223
+ from: this.options.id,
224
+ } as any) as any
330
225
  }
331
226
 
332
- useLoaderDeps = <TSelected = TRoute['types']['loaderDeps']>(opts?: {
333
- select?: (s: TRoute['types']['loaderDeps']) => TSelected
334
- }): TSelected => {
227
+ useLoaderDeps: UseLoaderDepsRoute<TRoute['id']> = (opts) => {
335
228
  return useLoaderDeps({ ...opts, from: this.options.id } as any)
336
229
  }
337
230
 
338
- useLoaderData = <TSelected = TRoute['types']['loaderData']>(opts?: {
339
- select?: (s: TRoute['types']['loaderData']) => TSelected
340
- }): TSelected => {
231
+ useLoaderData: UseLoaderDataRoute<TRoute['id']> = (opts) => {
341
232
  return useLoaderData({ ...opts, from: this.options.id } as any)
342
233
  }
234
+
235
+ useNavigate = (): UseNavigateResult<TRoute['fullPath']> => {
236
+ const router = useRouter()
237
+ return useNavigate({ from: router.routesById[this.options.id].fullPath })
238
+ }
343
239
  }
344
240
 
345
241
  export function createLazyRoute<
346
- TId extends RouteIds<RegisteredRouter['routeTree']>,
347
- TRoute extends AnyRoute = RouteById<RegisteredRouter['routeTree'], TId>,
348
- >(id: TId) {
242
+ TRouter extends AnyRouter = RegisteredRouter,
243
+ TId extends string = string,
244
+ TRoute extends AnyRoute = RouteById<TRouter['routeTree'], TId>,
245
+ >(id: ConstrainLiteral<TId, RouteIds<TRouter['routeTree']>>) {
349
246
  return (opts: LazyRouteOptions) => {
350
- return new LazyRoute<TRoute>({ id: id as any, ...opts })
247
+ return new LazyRoute<TRoute>({
248
+ id: id,
249
+ ...opts,
250
+ })
351
251
  }
352
252
  }
353
253
 
354
254
  export function createLazyFileRoute<
355
255
  TFilePath extends keyof FileRoutesByPath,
356
256
  TRoute extends FileRoutesByPath[TFilePath]['preLoaderRoute'],
357
- >(path: TFilePath) {
358
- const id = removeGroups(path)
359
- return (opts: LazyRouteOptions) => new LazyRoute<TRoute>({ id, ...opts })
360
- }
361
-
362
- const routeGroupPatternRegex = /\(.+\)/g
257
+ >(id: TFilePath): (opts: LazyRouteOptions) => LazyRoute<TRoute> {
258
+ if (typeof id === 'object') {
259
+ return new LazyRoute<TRoute>(id) as any
260
+ }
363
261
 
364
- function removeGroups(s: string) {
365
- return s.replaceAll(routeGroupPatternRegex, '').replaceAll('//', '/')
262
+ return (opts: LazyRouteOptions) => new LazyRoute<TRoute>({ id, ...opts })
366
263
  }
package/src/history.ts CHANGED
@@ -1,8 +1,9 @@
1
- import { HistoryLocation } from '@tanstack/history'
1
+ import type { HistoryLocation } from '@tanstack/history'
2
2
 
3
3
  declare module '@tanstack/history' {
4
4
  interface HistoryState {
5
5
  __tempLocation?: HistoryLocation
6
6
  __tempKey?: string
7
+ __hashScrollIntoViewOptions?: boolean | ScrollIntoViewOptions
7
8
  }
8
9
  }