@tanstack/react-router 0.0.1-alpha.10 → 0.0.1-alpha.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tanstack/react-router",
3
3
  "author": "Tanner Linsley",
4
- "version": "0.0.1-alpha.10",
4
+ "version": "0.0.1-alpha.11",
5
5
  "license": "MIT",
6
6
  "repository": "tanstack/router",
7
7
  "homepage": "https://react-router.tanstack.com/",
@@ -40,7 +40,7 @@
40
40
  },
41
41
  "dependencies": {
42
42
  "@babel/runtime": "^7.16.7",
43
- "@tanstack/router-core": "0.0.1-alpha.10",
43
+ "@tanstack/router-core": "0.0.1-alpha.11",
44
44
  "use-sync-external-store": "^1.2.0"
45
45
  },
46
46
  "devDependencies": {
package/src/index.tsx CHANGED
@@ -4,10 +4,15 @@ import { useSyncExternalStore } from 'use-sync-external-store/shim'
4
4
 
5
5
  import {
6
6
  AnyRoute,
7
+ CheckId,
8
+ CheckPath,
9
+ Expand,
10
+ resolvePath,
7
11
  RootRouteId,
8
12
  rootRouteId,
9
13
  Router,
10
14
  RouterState,
15
+ ToIdOption,
11
16
  } from '@tanstack/router-core'
12
17
  import {
13
18
  warning,
@@ -37,19 +42,14 @@ export * from '@tanstack/router-core'
37
42
  declare module '@tanstack/router-core' {
38
43
  interface FrameworkGenerics {
39
44
  Element: React.ReactNode
40
- AsyncElement: (opts: {
41
- params: Record<string, string>
42
- }) => Promise<React.ReactNode>
43
- SyncOrAsyncElement: React.ReactNode | FrameworkGenerics['AsyncElement']
45
+ // Any is required here so import() will work without having to do import().then(d => d.default)
46
+ SyncOrAsyncElement: React.ReactNode | (() => Promise<any>)
44
47
  }
45
48
 
46
49
  interface Router<
47
50
  TRouteConfig extends AnyRouteConfig = RouteConfig,
48
51
  TAllRouteInfo extends AnyAllRouteInfo = AllRouteInfo<TRouteConfig>,
49
- > extends Pick<
50
- Route<TAllRouteInfo, TAllRouteInfo['routeInfoById'][RootRouteId]>,
51
- 'linkProps' | 'Link' | 'MatchRoute'
52
- > {
52
+ > {
53
53
  useState: () => RouterState
54
54
  useRoute: <TId extends keyof TAllRouteInfo['routeInfoById']>(
55
55
  routeId: TId,
@@ -91,6 +91,19 @@ declare module '@tanstack/router-core' {
91
91
  TAllRouteInfo extends AnyAllRouteInfo = DefaultAllRouteInfo,
92
92
  TRouteInfo extends AnyRouteInfo = RouteInfo,
93
93
  > {
94
+ useRoute: <
95
+ TTo extends string = '.',
96
+ TResolved extends string = ResolveRelativePath<
97
+ TRouteInfo['id'],
98
+ NoInfer<TTo>
99
+ >,
100
+ >(
101
+ routeId: CheckId<
102
+ TAllRouteInfo,
103
+ TResolved,
104
+ ToIdOption<TAllRouteInfo, TRouteInfo['id'], TTo>
105
+ >,
106
+ ) => Route<TAllRouteInfo, TAllRouteInfo['routeInfoById'][TResolved]>
94
107
  linkProps: <TTo extends string = '.'>(
95
108
  props: LinkPropsOptions<TAllRouteInfo, TRouteInfo['fullPath'], TTo> &
96
109
  React.AnchorHTMLAttributes<HTMLAnchorElement>,
@@ -179,8 +192,23 @@ export function createReactRouter<
179
192
  const makeRouteExt = (
180
193
  route: AnyRoute,
181
194
  router: Router<any, any>,
182
- ): Pick<AnyRoute, 'linkProps' | 'Link' | 'MatchRoute'> => {
195
+ ): Pick<AnyRoute, 'useRoute' | 'linkProps' | 'Link' | 'MatchRoute'> => {
183
196
  return {
197
+ useRoute: (subRouteId = '.' as any) => {
198
+ const resolvedRouteId = router.resolvePath(
199
+ route.routeId,
200
+ subRouteId as string,
201
+ )
202
+ const resolvedRoute = router.getRoute(resolvedRouteId)
203
+ useRouterSubscription(router)
204
+ invariant(
205
+ resolvedRoute,
206
+ `Could not find a route for route "${
207
+ resolvedRouteId as string
208
+ }"! Did you forget to add it to your route config?`,
209
+ )
210
+ return resolvedRoute
211
+ },
184
212
  linkProps: (options) => {
185
213
  const {
186
214
  // custom props
@@ -319,25 +347,11 @@ export function createReactRouter<
319
347
  const coreRouter = createRouter<TRouteConfig>({
320
348
  ...opts,
321
349
  createRouter: (router) => {
322
- const routerExt: Pick<
323
- Router<any, any>,
324
- 'useRoute' | 'useMatch' | 'useState'
325
- > = {
350
+ const routerExt: Pick<Router<any, any>, 'useMatch' | 'useState'> = {
326
351
  useState: () => {
327
352
  useRouterSubscription(router)
328
353
  return router.state
329
354
  },
330
- useRoute: (routeId) => {
331
- const route = router.getRoute(routeId)
332
- useRouterSubscription(router)
333
- invariant(
334
- route,
335
- `Could not find a route for route "${
336
- routeId as string
337
- }"! Did you forget to add it to your route config?`,
338
- )
339
- return route
340
- },
341
355
  useMatch: (routeId) => {
342
356
  useRouterSubscription(router)
343
357
 
@@ -384,6 +398,20 @@ export function createReactRouter<
384
398
 
385
399
  Object.assign(route, routeExt)
386
400
  },
401
+ createElement: async (element) => {
402
+ if (typeof element === 'function') {
403
+ const res = (await element()) as any
404
+
405
+ // Support direct import() calls
406
+ if (typeof res === 'object' && res.default) {
407
+ return React.createElement(res.default)
408
+ } else {
409
+ return res
410
+ }
411
+ }
412
+
413
+ return element
414
+ },
387
415
  })
388
416
 
389
417
  return coreRouter as any