@tanstack/react-router 0.0.1-beta.20 → 0.0.1-beta.21

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-beta.20",
4
+ "version": "0.0.1-beta.21",
5
5
  "license": "MIT",
6
6
  "repository": "tanstack/router",
7
7
  "homepage": "https://tanstack.com/router/",
@@ -41,7 +41,7 @@
41
41
  },
42
42
  "dependencies": {
43
43
  "@babel/runtime": "^7.16.7",
44
- "@tanstack/router-core": "0.0.1-beta.19",
44
+ "@tanstack/router-core": "0.0.1-beta.21",
45
45
  "use-sync-external-store": "^1.2.0"
46
46
  },
47
47
  "devDependencies": {
package/src/index.tsx CHANGED
@@ -6,6 +6,7 @@ import {
6
6
  AnyRoute,
7
7
  CheckId,
8
8
  rootRouteId,
9
+ Route,
9
10
  RouterState,
10
11
  ToIdOption,
11
12
  } from '@tanstack/router-core'
@@ -39,13 +40,13 @@ export interface RegisterRouter {
39
40
  // router: Router
40
41
  }
41
42
 
42
- export type ResolvedRouter = RegisterRouter extends {
43
+ export type RegisteredRouter = RegisterRouter extends {
43
44
  router: Router<infer TRouteConfig, infer TAllRouteInfo>
44
45
  }
45
46
  ? Router<TRouteConfig, TAllRouteInfo>
46
47
  : Router
47
48
 
48
- export type ResolvedAllRouteInfo = RegisterRouter extends {
49
+ export type RegisteredAllRouteInfo = RegisterRouter extends {
49
50
  router: Router<infer TRouteConfig, infer TAllRouteInfo>
50
51
  }
51
52
  ? TAllRouteInfo
@@ -88,6 +89,58 @@ export function lazy(
88
89
  return finalComp
89
90
  }
90
91
 
92
+ type LinkPropsOptions<
93
+ TAllRouteInfo extends AnyAllRouteInfo,
94
+ TFrom extends ValidFromPath<TAllRouteInfo>,
95
+ TTo extends string,
96
+ > = LinkOptions<TAllRouteInfo, TFrom, TTo> & {
97
+ // A function that returns additional props for the `active` state of this link. These props override other props passed to the link (`style`'s are merged, `className`'s are concatenated)
98
+ activeProps?:
99
+ | React.AnchorHTMLAttributes<HTMLAnchorElement>
100
+ | (() => React.AnchorHTMLAttributes<HTMLAnchorElement>)
101
+ // A function that returns additional props for the `inactive` state of this link. These props override other props passed to the link (`style`'s are merged, `className`'s are concatenated)
102
+ inactiveProps?:
103
+ | React.AnchorHTMLAttributes<HTMLAnchorElement>
104
+ | (() => React.AnchorHTMLAttributes<HTMLAnchorElement>)
105
+ }
106
+
107
+ type MakeMatchRouteOptions<
108
+ TAllRouteInfo extends AnyAllRouteInfo,
109
+ TFrom extends ValidFromPath<TAllRouteInfo>,
110
+ TTo extends string,
111
+ > = ToOptions<TAllRouteInfo, TFrom, TTo> &
112
+ MatchRouteOptions & {
113
+ // If a function is passed as a child, it will be given the `isActive` boolean to aid in further styling on the element it returns
114
+ children?:
115
+ | React.ReactNode
116
+ | ((
117
+ params: RouteInfoByPath<
118
+ TAllRouteInfo,
119
+ ResolveRelativePath<TFrom, NoInfer<TTo>>
120
+ >['allParams'],
121
+ ) => React.ReactNode)
122
+ }
123
+
124
+ type MakeLinkPropsOptions<
125
+ TAllRouteInfo extends AnyAllRouteInfo,
126
+ TFrom extends ValidFromPath<TAllRouteInfo>,
127
+ TTo extends string,
128
+ > = LinkPropsOptions<TAllRouteInfo, TFrom, TTo> &
129
+ React.AnchorHTMLAttributes<HTMLAnchorElement>
130
+
131
+ type MakeLinkOptions<
132
+ TAllRouteInfo extends AnyAllRouteInfo,
133
+ TFrom extends ValidFromPath<TAllRouteInfo>,
134
+ TTo extends string,
135
+ > = LinkPropsOptions<TAllRouteInfo, TFrom, TTo> &
136
+ React.AnchorHTMLAttributes<HTMLAnchorElement> &
137
+ Omit<React.AnchorHTMLAttributes<HTMLAnchorElement>, 'children'> & {
138
+ // If a function is passed as a child, it will be given the `isActive` boolean to aid in further styling on the element it returns
139
+ children?:
140
+ | React.ReactNode
141
+ | ((state: { isActive: boolean }) => React.ReactNode)
142
+ }
143
+
91
144
  declare module '@tanstack/router-core' {
92
145
  interface FrameworkGenerics {
93
146
  Component: RouteComponent
@@ -101,9 +154,10 @@ declare module '@tanstack/router-core' {
101
154
  useRoute: <TId extends keyof TAllRouteInfo['routeInfoById']>(
102
155
  routeId: TId,
103
156
  ) => Route<TAllRouteInfo, TAllRouteInfo['routeInfoById'][TId]>
157
+ useNearestMatch: () => RouteMatch<TAllRouteInfo, RouteInfo>
104
158
  useMatch: <
105
159
  TId extends keyof TAllRouteInfo['routeInfoById'],
106
- TStrict extends true | false = true,
160
+ TStrict extends boolean = true,
107
161
  >(
108
162
  routeId: TId,
109
163
  opts?: { strict?: TStrict },
@@ -113,32 +167,13 @@ declare module '@tanstack/router-core' {
113
167
  | RouteMatch<TAllRouteInfo, TAllRouteInfo['routeInfoById'][TId]>
114
168
  | undefined
115
169
  linkProps: <TTo extends string = '.'>(
116
- props: LinkPropsOptions<TAllRouteInfo, '/', TTo> &
117
- React.AnchorHTMLAttributes<HTMLAnchorElement>,
170
+ props: MakeLinkPropsOptions<TAllRouteInfo, '/', TTo>,
118
171
  ) => React.AnchorHTMLAttributes<HTMLAnchorElement>
119
172
  Link: <TTo extends string = '.'>(
120
- props: LinkPropsOptions<TAllRouteInfo, '/', TTo> &
121
- React.AnchorHTMLAttributes<HTMLAnchorElement> &
122
- Omit<React.AnchorHTMLAttributes<HTMLAnchorElement>, 'children'> & {
123
- // If a function is passed as a child, it will be given the `isActive` boolean to aid in further styling on the element it returns
124
- children?:
125
- | React.ReactNode
126
- | ((state: { isActive: boolean }) => React.ReactNode)
127
- },
173
+ props: MakeLinkOptions<TAllRouteInfo, '/', TTo>,
128
174
  ) => JSX.Element
129
175
  MatchRoute: <TTo extends string = '.'>(
130
- props: ToOptions<TAllRouteInfo, '/', TTo> &
131
- MatchRouteOptions & {
132
- // If a function is passed as a child, it will be given the `isActive` boolean to aid in further styling on the element it returns
133
- children?:
134
- | React.ReactNode
135
- | ((
136
- params: RouteInfoByPath<
137
- TAllRouteInfo,
138
- ResolveRelativePath<'/', NoInfer<TTo>>
139
- >['allParams'],
140
- ) => React.ReactNode)
141
- },
176
+ props: MakeMatchRouteOptions<TAllRouteInfo, '/', TTo>,
142
177
  ) => JSX.Element
143
178
  }
144
179
 
@@ -161,51 +196,17 @@ declare module '@tanstack/router-core' {
161
196
  opts?: { strict?: boolean },
162
197
  ) => Route<TAllRouteInfo, TAllRouteInfo['routeInfoById'][TResolved]>
163
198
  linkProps: <TTo extends string = '.'>(
164
- props: LinkPropsOptions<TAllRouteInfo, TRouteInfo['fullPath'], TTo> &
165
- React.AnchorHTMLAttributes<HTMLAnchorElement>,
199
+ props: MakeLinkPropsOptions<TAllRouteInfo, TRouteInfo['fullPath'], TTo>,
166
200
  ) => React.AnchorHTMLAttributes<HTMLAnchorElement>
167
201
  Link: <TTo extends string = '.'>(
168
- props: LinkPropsOptions<TAllRouteInfo, TRouteInfo['fullPath'], TTo> &
169
- React.AnchorHTMLAttributes<HTMLAnchorElement> &
170
- Omit<React.AnchorHTMLAttributes<HTMLAnchorElement>, 'children'> & {
171
- // If a function is passed as a child, it will be given the `isActive` boolean to aid in further styling on the element it returns
172
- children?:
173
- | React.ReactNode
174
- | ((state: { isActive: boolean }) => React.ReactNode)
175
- },
202
+ props: MakeLinkOptions<TAllRouteInfo, TRouteInfo['fullPath'], TTo>,
176
203
  ) => JSX.Element
177
204
  MatchRoute: <TTo extends string = '.'>(
178
- props: ToOptions<TAllRouteInfo, TRouteInfo['fullPath'], TTo> &
179
- MatchRouteOptions & {
180
- // If a function is passed as a child, it will be given the `isActive` boolean to aid in further styling on the element it returns
181
- children?:
182
- | React.ReactNode
183
- | ((
184
- params: RouteInfoByPath<
185
- TAllRouteInfo,
186
- ResolveRelativePath<TRouteInfo['fullPath'], NoInfer<TTo>>
187
- >['allParams'],
188
- ) => React.ReactNode)
189
- },
205
+ props: MakeMatchRouteOptions<TAllRouteInfo, TRouteInfo['fullPath'], TTo>,
190
206
  ) => JSX.Element
191
207
  }
192
208
  }
193
209
 
194
- type LinkPropsOptions<
195
- TAllRouteInfo extends AnyAllRouteInfo = DefaultAllRouteInfo,
196
- TFrom extends ValidFromPath<TAllRouteInfo> = '/',
197
- TTo extends string = '.',
198
- > = LinkOptions<TAllRouteInfo, TFrom, TTo> & {
199
- // A function that returns additional props for the `active` state of this link. These props override other props passed to the link (`style`'s are merged, `className`'s are concatenated)
200
- activeProps?:
201
- | React.AnchorHTMLAttributes<HTMLAnchorElement>
202
- | (() => React.AnchorHTMLAttributes<HTMLAnchorElement>)
203
- // A function that returns additional props for the `inactive` state of this link. These props override other props passed to the link (`style`'s are merged, `className`'s are concatenated)
204
- inactiveProps?:
205
- | React.AnchorHTMLAttributes<HTMLAnchorElement>
206
- | (() => React.AnchorHTMLAttributes<HTMLAnchorElement>)
207
- }
208
-
209
210
  export type PromptProps = {
210
211
  message: string
211
212
  when?: boolean | any
@@ -215,20 +216,14 @@ export type PromptProps = {
215
216
  //
216
217
 
217
218
  export function Link<TTo extends string = '.'>(
218
- props: LinkPropsOptions<ResolvedAllRouteInfo, '/', TTo> &
219
- React.AnchorHTMLAttributes<HTMLAnchorElement> &
220
- Omit<React.AnchorHTMLAttributes<HTMLAnchorElement>, 'children'> & {
221
- children?:
222
- | React.ReactNode
223
- | ((state: { isActive: boolean }) => React.ReactNode)
224
- },
219
+ props: MakeLinkOptions<RegisteredAllRouteInfo, '/', TTo>,
225
220
  ): JSX.Element {
226
221
  const router = useRouter()
227
222
  return <router.Link {...(props as any)} />
228
223
  }
229
224
 
230
225
  export const matchesContext = React.createContext<RouteMatch[]>(null!)
231
- export const routerContext = React.createContext<{ router: ResolvedRouter }>(
226
+ export const routerContext = React.createContext<{ router: RegisteredRouter }>(
232
227
  null!,
233
228
  )
234
229
 
@@ -303,7 +298,7 @@ export function createReactRouter<
303
298
  ...rest
304
299
  } = options
305
300
 
306
- const linkInfo = route.buildLink(options)
301
+ const linkInfo = route.buildLink(options as any)
307
302
 
308
303
  if (linkInfo.type === 'external') {
309
304
  const { href } = linkInfo
@@ -508,7 +503,7 @@ export function RouterProvider<
508
503
  )
509
504
  }
510
505
 
511
- export function useRouter(): ResolvedRouter {
506
+ export function useRouter(): RegisteredRouter {
512
507
  const value = React.useContext(routerContext)
513
508
  warning(!value, 'useRouter must be used inside a <Router> component!')
514
509
 
@@ -522,23 +517,60 @@ export function useMatches(): RouteMatch[] {
522
517
  }
523
518
 
524
519
  export function useMatch<
525
- TId extends keyof ResolvedAllRouteInfo['routeInfoById'],
526
- TStrict extends true | false = true,
520
+ TId extends keyof RegisteredAllRouteInfo['routeInfoById'],
521
+ TStrict extends boolean = true,
527
522
  >(
528
523
  routeId: TId,
529
524
  opts?: { strict?: TStrict },
530
525
  ): TStrict extends true
531
- ? RouteMatch<ResolvedAllRouteInfo, ResolvedAllRouteInfo['routeInfoById'][TId]>
526
+ ? RouteMatch<
527
+ RegisteredAllRouteInfo,
528
+ RegisteredAllRouteInfo['routeInfoById'][TId]
529
+ >
532
530
  :
533
531
  | RouteMatch<
534
- ResolvedAllRouteInfo,
535
- ResolvedAllRouteInfo['routeInfoById'][TId]
532
+ RegisteredAllRouteInfo,
533
+ RegisteredAllRouteInfo['routeInfoById'][TId]
536
534
  >
537
535
  | undefined {
538
536
  const router = useRouter()
539
537
  return router.useMatch(routeId as any, opts) as any
540
538
  }
541
539
 
540
+ export function useNearestMatch(): RouteMatch<
541
+ RegisteredAllRouteInfo,
542
+ RouteInfo
543
+ > {
544
+ const runtimeMatch = useMatches()?.[0]!
545
+
546
+ invariant(runtimeMatch, `Could not find a nearest match!`)
547
+
548
+ return runtimeMatch as any
549
+ }
550
+
551
+ export function useRoute<
552
+ TId extends keyof RegisteredAllRouteInfo['routeInfoById'],
553
+ >(
554
+ routeId: TId,
555
+ ): Route<RegisteredAllRouteInfo, RegisteredAllRouteInfo['routeInfoById'][TId]> {
556
+ const router = useRouter()
557
+ return router.useRoute(routeId as any) as any
558
+ }
559
+
560
+ export function linkProps<TTo extends string = '.'>(
561
+ props: MakeLinkPropsOptions<RegisteredAllRouteInfo, '/', TTo>,
562
+ ): React.AnchorHTMLAttributes<HTMLAnchorElement> {
563
+ const router = useRouter()
564
+ return router.linkProps(props as any)
565
+ }
566
+
567
+ export function MatchRoute<TTo extends string = '.'>(
568
+ props: MakeMatchRouteOptions<RegisteredAllRouteInfo, '/', TTo>,
569
+ ): JSX.Element {
570
+ const router = useRouter()
571
+ return React.createElement(router.MatchRoute, props as any)
572
+ }
573
+
542
574
  export function Outlet() {
543
575
  const router = useRouter()
544
576
  const matches = useMatches().slice(1)