@tanstack/react-router 1.0.7 → 1.0.9

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": "1.0.7",
4
+ "version": "1.0.9",
5
5
  "license": "MIT",
6
6
  "repository": "tanstack/router",
7
7
  "homepage": "https://tanstack.com/router",
@@ -44,7 +44,7 @@
44
44
  "@tanstack/store": "^0.1.3",
45
45
  "tiny-invariant": "^1.3.1",
46
46
  "tiny-warning": "^1.0.3",
47
- "@tanstack/history": "1.0.7"
47
+ "@tanstack/history": "1.0.9"
48
48
  },
49
49
  "scripts": {
50
50
  "build": "rollup --config rollup.config.js"
@@ -40,9 +40,9 @@ export interface MatchLocation {
40
40
  }
41
41
 
42
42
  export type NavigateFn<TRouteTree extends AnyRoute> = <
43
- TFrom extends RoutePaths<TRouteTree> = '/',
43
+ TFrom extends RoutePaths<TRouteTree> | string = string,
44
44
  TTo extends string = '',
45
- TMaskFrom extends RoutePaths<TRouteTree> = TFrom,
45
+ TMaskFrom extends RoutePaths<TRouteTree> | string = TFrom,
46
46
  TMaskTo extends string = '',
47
47
  >(
48
48
  opts: NavigateOptions<TRouteTree, TFrom, TTo, TMaskFrom, TMaskTo>,
package/src/link.tsx CHANGED
@@ -19,6 +19,7 @@ import {
19
19
  PickRequired,
20
20
  UnionToIntersection,
21
21
  Updater,
22
+ WithoutEmpty,
22
23
  deepEqual,
23
24
  functionalUpdate,
24
25
  } from './utils'
@@ -116,9 +117,9 @@ export type RelativeToPathAutoComplete<
116
117
 
117
118
  export type NavigateOptions<
118
119
  TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
119
- TFrom extends RoutePaths<TRouteTree> = '/',
120
+ TFrom extends RoutePaths<TRouteTree> | string = string,
120
121
  TTo extends string = '',
121
- TMaskFrom extends RoutePaths<TRouteTree> = TFrom,
122
+ TMaskFrom extends RoutePaths<TRouteTree> | string = TFrom,
122
123
  TMaskTo extends string = '',
123
124
  > = ToOptions<TRouteTree, TFrom, TTo, TMaskFrom, TMaskTo> & {
124
125
  // `replace` is a boolean that determines whether the navigation should replace the current history entry or push a new one.
@@ -130,9 +131,9 @@ export type NavigateOptions<
130
131
 
131
132
  export type ToOptions<
132
133
  TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
133
- TFrom extends RoutePaths<TRouteTree> = '/',
134
+ TFrom extends RoutePaths<TRouteTree> | string = string,
134
135
  TTo extends string = '',
135
- TMaskFrom extends RoutePaths<TRouteTree> = '/',
136
+ TMaskFrom extends RoutePaths<TRouteTree> | string = TFrom,
136
137
  TMaskTo extends string = '',
137
138
  > = ToSubOptions<TRouteTree, TFrom, TTo> & {
138
139
  mask?: ToMaskOptions<TRouteTree, TMaskFrom, TMaskTo>
@@ -140,7 +141,7 @@ export type ToOptions<
140
141
 
141
142
  export type ToMaskOptions<
142
143
  TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
143
- TMaskFrom extends RoutePaths<TRouteTree> = '/',
144
+ TMaskFrom extends RoutePaths<TRouteTree> | string = string,
144
145
  TMaskTo extends string = '',
145
146
  > = ToSubOptions<TRouteTree, TMaskFrom, TMaskTo> & {
146
147
  unmaskOnReload?: boolean
@@ -148,7 +149,7 @@ export type ToMaskOptions<
148
149
 
149
150
  export type ToSubOptions<
150
151
  TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
151
- TFrom extends RoutePaths<TRouteTree> = '/',
152
+ TFrom extends RoutePaths<TRouteTree> | string = string,
152
153
  TTo extends string = '',
153
154
  TResolved = ResolveRelativePath<TFrom, NoInfer<TTo>>,
154
155
  > = {
@@ -162,71 +163,55 @@ export type ToSubOptions<
162
163
  // // When using relative route paths, this option forces resolution from the current path, instead of the route API's path or `from` path
163
164
  } & CheckPath<TRouteTree, NoInfer<TResolved>, {}> &
164
165
  SearchParamOptions<TRouteTree, TFrom, TTo, TResolved> &
165
- PathParamOptions<TRouteTree, TFrom, TResolved>
166
+ PathParamOptions<TRouteTree, TFrom, TTo, TResolved>
166
167
 
167
- export type SearchParamOptions<
168
+ type ParamsReducer<TFrom, TTo> = TTo | ((current: TFrom) => TTo)
169
+
170
+ export type ParamOptions<
168
171
  TRouteTree extends AnyRoute,
169
172
  TFrom,
170
173
  TTo,
171
- TResolved = ResolveRelativePath<TFrom, NoInfer<TTo>>,
172
- TFromSearchEnsured = '/' extends TFrom
173
- ? FullSearchSchema<TRouteTree>
174
- : Expand<
175
- PickRequired<
176
- RouteByPath<TRouteTree, TFrom>['types']['fullSearchSchema']
177
- >
178
- >,
179
- TFromSearchOptional = Omit<
180
- FullSearchSchema<TRouteTree>,
181
- keyof TFromSearchEnsured
182
- >,
183
- TFromSearch = Expand<TFromSearchEnsured & TFromSearchOptional>,
184
- TToSearch = '' extends TTo
185
- ? FullSearchSchema<TRouteTree>
186
- : Expand<RouteByPath<TRouteTree, TResolved>['types']['fullSearchSchema']>,
187
- > = keyof PickRequired<TToSearch> extends never
188
- ? {
189
- search?: true | SearchReducer<TFromSearch, TToSearch>
190
- }
191
- : {
192
- search: TFromSearchEnsured extends PickRequired<TToSearch>
193
- ? true | SearchReducer<TFromSearch, TToSearch>
194
- : SearchReducer<TFromSearch, TToSearch>
195
- }
174
+ TResolved,
175
+ TParamVariant extends 'allParams' | 'fullSearchSchema',
176
+ TFromParams = Expand<RouteByPath<TRouteTree, TFrom>['types'][TParamVariant]>,
177
+ TToParams = TTo extends ''
178
+ ? TFromParams
179
+ : never extends TResolved
180
+ ? Expand<RouteByPath<TRouteTree, TTo>['types'][TParamVariant]>
181
+ : Expand<RouteByPath<TRouteTree, TResolved>['types'][TParamVariant]>,
182
+ TReducer = ParamsReducer<TFromParams, TToParams>,
183
+ > = Expand<WithoutEmpty<PickRequired<TToParams>>> extends never
184
+ ? Partial<MakeParamOption<TParamVariant, true | TReducer>>
185
+ : TFromParams extends Expand<WithoutEmpty<PickRequired<TToParams>>>
186
+ ? MakeParamOption<TParamVariant, true | TReducer>
187
+ : MakeParamOption<TParamVariant, TReducer>
188
+
189
+ type MakeParamOption<
190
+ TParamVariant extends 'allParams' | 'fullSearchSchema',
191
+ T,
192
+ > = TParamVariant extends 'allParams'
193
+ ? MakePathParamOptions<T>
194
+ : MakeSearchParamOptions<T>
195
+ type MakeSearchParamOptions<T> = { search: T }
196
+ type MakePathParamOptions<T> = { params: T }
196
197
 
197
- type SearchReducer<TFrom, TTo> = TTo | ((current: TFrom) => TTo)
198
+ export type SearchParamOptions<
199
+ TRouteTree extends AnyRoute,
200
+ TFrom,
201
+ TTo,
202
+ TResolved,
203
+ > = ParamOptions<TRouteTree, TFrom, TTo, TResolved, 'fullSearchSchema'>
198
204
 
199
205
  export type PathParamOptions<
200
206
  TRouteTree extends AnyRoute,
201
207
  TFrom,
202
208
  TTo,
203
- TFromParamsEnsured = Expand<
204
- UnionToIntersection<
205
- PickRequired<RouteByPath<TRouteTree, TFrom>['types']['allParams']>
206
- >
207
- >,
208
- TFromParamsOptional = Omit<AllParams<TRouteTree>, keyof TFromParamsEnsured>,
209
- TFromParams = Expand<TFromParamsOptional & TFromParamsEnsured>,
210
- TToParams = Expand<RouteByPath<TRouteTree, TTo>['types']['allParams']>,
211
- > = never extends TToParams
212
- ? {
213
- params?: true | ParamsReducer<Partial<TFromParams>, Partial<TFromParams>>
214
- }
215
- : keyof PickRequired<TToParams> extends never
216
- ? {
217
- params?: true | ParamsReducer<TFromParams, TToParams>
218
- }
219
- : {
220
- params: TFromParamsEnsured extends PickRequired<TToParams>
221
- ? true | ParamsReducer<TFromParams, TToParams>
222
- : ParamsReducer<TFromParams, TToParams>
223
- }
224
-
225
- type ParamsReducer<TFrom, TTo> = TTo | ((current: TFrom) => TTo)
209
+ TResolved,
210
+ > = ParamOptions<TRouteTree, TFrom, TTo, TResolved, 'allParams'>
226
211
 
227
212
  export type ToPathOption<
228
213
  TRouteTree extends AnyRoute = AnyRoute,
229
- TFrom extends RoutePaths<TRouteTree> = '/',
214
+ TFrom extends RoutePaths<TRouteTree> | string = string,
230
215
  TTo extends string = '',
231
216
  > =
232
217
  | TTo
@@ -238,7 +223,7 @@ export type ToPathOption<
238
223
 
239
224
  export type ToIdOption<
240
225
  TRouteTree extends AnyRoute = AnyRoute,
241
- TFrom extends RoutePaths<TRouteTree> = '/',
226
+ TFrom extends RoutePaths<TRouteTree> | undefined = undefined,
242
227
  TTo extends string = '',
243
228
  > =
244
229
  | TTo
@@ -256,9 +241,9 @@ export interface ActiveOptions {
256
241
 
257
242
  export type LinkOptions<
258
243
  TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
259
- TFrom extends RoutePaths<TRouteTree> = '/',
244
+ TFrom extends RoutePaths<TRouteTree> | string = string,
260
245
  TTo extends string = '',
261
- TMaskFrom extends RoutePaths<TRouteTree> = TFrom,
246
+ TMaskFrom extends RoutePaths<TRouteTree> | string = TFrom,
262
247
  TMaskTo extends string = '',
263
248
  > = NavigateOptions<TRouteTree, TFrom, TTo, TMaskFrom, TMaskTo> & {
264
249
  // The standard anchor tag target attribute
@@ -348,20 +333,13 @@ const preloadWarning = 'Error preloading route! ☝️'
348
333
 
349
334
  export function useLinkProps<
350
335
  TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
351
- TFrom extends RoutePaths<TRouteTree> = '/',
336
+ TFrom extends RoutePaths<TRouteTree> | string = string,
352
337
  TTo extends string = '',
353
- TMaskFrom extends RoutePaths<TRouteTree> = '/',
338
+ TMaskFrom extends RoutePaths<TRouteTree> | string = TFrom,
354
339
  TMaskTo extends string = '',
355
- >({
356
- from,
357
- ...options
358
- }: UseLinkPropsOptions<
359
- TRouteTree,
360
- TFrom,
361
- TTo,
362
- TMaskFrom,
363
- TMaskTo
364
- >): React.AnchorHTMLAttributes<HTMLAnchorElement> {
340
+ >(
341
+ options: UseLinkPropsOptions<TRouteTree, TFrom, TTo, TMaskFrom, TMaskTo>,
342
+ ): React.AnchorHTMLAttributes<HTMLAnchorElement> {
365
343
  const router = useRouter()
366
344
  const matchPathname = useMatch({
367
345
  strict: false,
@@ -574,9 +552,9 @@ export function useLinkProps<
574
552
  export interface LinkComponent<TProps extends Record<string, any> = {}> {
575
553
  <
576
554
  TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
577
- TFrom extends RoutePaths<TRouteTree> = '/',
555
+ TFrom extends RoutePaths<TRouteTree> | string = string,
578
556
  TTo extends string = '',
579
- TMaskFrom extends RoutePaths<TRouteTree> = '/',
557
+ TMaskFrom extends RoutePaths<TRouteTree> | string = TFrom,
580
558
  TMaskTo extends string = '',
581
559
  >(
582
560
  props: LinkProps<TRouteTree, TFrom, TTo, TMaskFrom, TMaskTo> &
@@ -5,11 +5,10 @@ import { LinkOptions, NavigateOptions } from './link'
5
5
  import { AnyRoute } from './route'
6
6
  import { RoutePaths } from './routeInfo'
7
7
  import { RegisteredRouter } from './router'
8
- import { useLayoutEffect } from './utils'
9
8
 
10
9
  export function useNavigate<
11
10
  TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
12
- TDefaultFrom extends RoutePaths<TRouteTree> = '/',
11
+ TDefaultFrom extends RoutePaths<TRouteTree> | string = string,
13
12
  >(_defaultOpts?: { from?: TDefaultFrom }) {
14
13
  const { navigate, buildLocation } = useRouter()
15
14
 
@@ -20,9 +19,9 @@ export function useNavigate<
20
19
 
21
20
  return React.useCallback(
22
21
  <
23
- TFrom extends RoutePaths<TRouteTree> = TDefaultFrom,
22
+ TFrom extends RoutePaths<TRouteTree> | string = TDefaultFrom,
24
23
  TTo extends string = '',
25
- TMaskFrom extends RoutePaths<TRouteTree> = '/',
24
+ TMaskFrom extends RoutePaths<TRouteTree>| string = TFrom,
26
25
  TMaskTo extends string = '',
27
26
  >({
28
27
  from,
@@ -54,9 +53,9 @@ export function useNavigate<
54
53
 
55
54
  export function Navigate<
56
55
  TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
57
- TFrom extends RoutePaths<TRouteTree> = '/',
56
+ TFrom extends RoutePaths<TRouteTree> | string = string,
58
57
  TTo extends string = '',
59
- TMaskFrom extends RoutePaths<TRouteTree> = '/',
58
+ TMaskFrom extends RoutePaths<TRouteTree> | string = TFrom,
60
59
  TMaskTo extends string = '',
61
60
  >(props: NavigateOptions<TRouteTree, TFrom, TTo, TMaskFrom, TMaskTo>): null {
62
61
  const { navigate } = useRouter()
@@ -74,18 +73,18 @@ export function Navigate<
74
73
 
75
74
  export type UseLinkPropsOptions<
76
75
  TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
77
- TFrom extends RoutePaths<TRouteTree> = '/',
76
+ TFrom extends RoutePaths<TRouteTree> | string = string,
78
77
  TTo extends string = '',
79
- TMaskFrom extends RoutePaths<TRouteTree> = '/',
78
+ TMaskFrom extends RoutePaths<TRouteTree> | string = TFrom,
80
79
  TMaskTo extends string = '',
81
80
  > = ActiveLinkOptions<TRouteTree, TFrom, TTo, TMaskFrom, TMaskTo> &
82
81
  React.AnchorHTMLAttributes<HTMLAnchorElement>
83
82
 
84
83
  export type LinkProps<
85
84
  TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
86
- TFrom extends RoutePaths<TRouteTree> = '/',
85
+ TFrom extends RoutePaths<TRouteTree> | string = string,
87
86
  TTo extends string = '',
88
- TMaskFrom extends RoutePaths<TRouteTree> = '/',
87
+ TMaskFrom extends RoutePaths<TRouteTree> | string = TFrom,
89
88
  TMaskTo extends string = '',
90
89
  > = ActiveLinkOptions<TRouteTree, TFrom, TTo, TMaskFrom, TMaskTo> &
91
90
  Omit<React.AnchorHTMLAttributes<HTMLAnchorElement>, 'children'> & {
@@ -97,9 +96,9 @@ export type LinkProps<
97
96
 
98
97
  export type ActiveLinkOptions<
99
98
  TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
100
- TFrom extends RoutePaths<TRouteTree> = '/',
99
+ TFrom extends RoutePaths<TRouteTree> | string = string,
101
100
  TTo extends string = '',
102
- TMaskFrom extends RoutePaths<TRouteTree> = '/',
101
+ TMaskFrom extends RoutePaths<TRouteTree> | string = TFrom,
103
102
  TMaskTo extends string = '',
104
103
  > = LinkOptions<TRouteTree, TFrom, TTo, TMaskFrom, TMaskTo> & {
105
104
  // 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)
package/src/utils.ts CHANGED
@@ -26,6 +26,9 @@ export type PickRequired<T> = {
26
26
  [K in keyof T as undefined extends T[K] ? never : K]: T[K]
27
27
  }
28
28
 
29
+ // from https://stackoverflow.com/a/76458160
30
+ export type WithoutEmpty<T> = T extends T ? ({} extends T ? never : T) : never
31
+
29
32
  // export type Expand<T> = T
30
33
  export type Expand<T> = T extends object
31
34
  ? T extends infer O