@tanstack/router-core 1.108.0 → 1.111.3

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/src/link.ts CHANGED
@@ -1,3 +1,34 @@
1
+ import type { HistoryState, ParsedHistoryState } from '@tanstack/history'
2
+ import type {
3
+ AllParams,
4
+ CatchAllPaths,
5
+ CurrentPath,
6
+ FullSearchSchema,
7
+ FullSearchSchemaInput,
8
+ ParentPath,
9
+ RouteByPath,
10
+ RouteByToPath,
11
+ RoutePaths,
12
+ RouteToPath,
13
+ ToPath,
14
+ } from './routeInfo'
15
+ import type {
16
+ AnyRouter,
17
+ RegisteredRouter,
18
+ ViewTransitionOptions,
19
+ } from './router'
20
+ import type {
21
+ ConstrainLiteral,
22
+ Expand,
23
+ MakeDifferenceOptional,
24
+ NoInfer,
25
+ NonNullableUpdater,
26
+ PickRequired,
27
+ Updater,
28
+ WithoutEmpty,
29
+ } from './utils'
30
+ import type { ParsedLocation } from './location'
31
+
1
32
  export type IsRequiredParams<TParams> =
2
33
  Record<never, never> extends TParams ? never : true
3
34
 
@@ -32,43 +63,6 @@ export type RemoveLeadingSlashes<T> = T & `/${string}` extends never
32
63
  ? R
33
64
  : T
34
65
 
35
- export interface ActiveOptions {
36
- exact?: boolean
37
- includeHash?: boolean
38
- includeSearch?: boolean
39
- explicitUndefined?: boolean
40
- }
41
-
42
- export interface LinkOptionsProps {
43
- /**
44
- * The standard anchor tag target attribute
45
- */
46
- target?: HTMLAnchorElement['target']
47
- /**
48
- * Configurable options to determine if the link should be considered active or not
49
- * @default {exact:true,includeHash:true}
50
- */
51
- activeOptions?: ActiveOptions
52
- /**
53
- * The preloading strategy for this link
54
- * - `false` - No preloading
55
- * - `'intent'` - Preload the linked route on hover and cache it for this many milliseconds in hopes that the user will eventually navigate there.
56
- * - `'viewport'` - Preload the linked route when it enters the viewport
57
- */
58
- preload?: false | 'intent' | 'viewport' | 'render'
59
- /**
60
- * When a preload strategy is set, this delays the preload by this many milliseconds.
61
- * If the user exits the link before this delay, the preload will be cancelled.
62
- */
63
- preloadDelay?: number
64
- /**
65
- * Control whether the link should be disabled or not
66
- * If set to `true`, the link will be rendered without an `href` attribute
67
- * @default false
68
- */
69
- disabled?: boolean
70
- }
71
-
72
66
  type JoinPath<TLeft extends string, TRight extends string> = TRight extends ''
73
67
  ? TLeft
74
68
  : TLeft extends ''
@@ -132,6 +126,420 @@ export type ResolveRelativePath<TFrom, TTo = '.'> = string extends TFrom
132
126
  : never
133
127
  : never
134
128
 
129
+ export type FindDescendantToPaths<
130
+ TRouter extends AnyRouter,
131
+ TPrefix extends string,
132
+ > = `${TPrefix}/${string}` & RouteToPath<TRouter>
133
+
134
+ export type InferDescendantToPaths<
135
+ TRouter extends AnyRouter,
136
+ TPrefix extends string,
137
+ TPaths = FindDescendantToPaths<TRouter, TPrefix>,
138
+ > = TPaths extends `${TPrefix}/`
139
+ ? never
140
+ : TPaths extends `${TPrefix}/${infer TRest}`
141
+ ? TRest
142
+ : never
143
+
144
+ export type RelativeToPath<
145
+ TRouter extends AnyRouter,
146
+ TTo extends string,
147
+ TResolvedPath extends string,
148
+ > =
149
+ | (TResolvedPath & RouteToPath<TRouter> extends never
150
+ ? never
151
+ : ToPath<TRouter, TTo>)
152
+ | `${RemoveTrailingSlashes<TTo>}/${InferDescendantToPaths<TRouter, RemoveTrailingSlashes<TResolvedPath>>}`
153
+
154
+ export type RelativeToParentPath<
155
+ TRouter extends AnyRouter,
156
+ TFrom extends string,
157
+ TTo extends string,
158
+ TResolvedPath extends string = ResolveRelativePath<TFrom, TTo>,
159
+ > =
160
+ | RelativeToPath<TRouter, TTo, TResolvedPath>
161
+ | (TTo extends `${string}..` | `${string}../`
162
+ ? TResolvedPath extends '/' | ''
163
+ ? never
164
+ : FindDescendantToPaths<
165
+ TRouter,
166
+ RemoveTrailingSlashes<TResolvedPath>
167
+ > extends never
168
+ ? never
169
+ : `${RemoveTrailingSlashes<TTo>}/${ParentPath<TRouter>}`
170
+ : never)
171
+
172
+ export type RelativeToCurrentPath<
173
+ TRouter extends AnyRouter,
174
+ TFrom extends string,
175
+ TTo extends string,
176
+ TResolvedPath extends string = ResolveRelativePath<TFrom, TTo>,
177
+ > = RelativeToPath<TRouter, TTo, TResolvedPath> | CurrentPath<TRouter>
178
+
179
+ export type AbsoluteToPath<TRouter extends AnyRouter, TFrom extends string> =
180
+ | (string extends TFrom
181
+ ? CurrentPath<TRouter>
182
+ : TFrom extends `/`
183
+ ? never
184
+ : CurrentPath<TRouter>)
185
+ | (string extends TFrom
186
+ ? ParentPath<TRouter>
187
+ : TFrom extends `/`
188
+ ? never
189
+ : ParentPath<TRouter>)
190
+ | RouteToPath<TRouter>
191
+ | (TFrom extends '/'
192
+ ? never
193
+ : string extends TFrom
194
+ ? never
195
+ : InferDescendantToPaths<TRouter, RemoveTrailingSlashes<TFrom>>)
196
+
197
+ export type RelativeToPathAutoComplete<
198
+ TRouter extends AnyRouter,
199
+ TFrom extends string,
200
+ TTo extends string,
201
+ > = string extends TTo
202
+ ? string
203
+ : string extends TFrom
204
+ ? AbsoluteToPath<TRouter, TFrom>
205
+ : TTo & `..${string}` extends never
206
+ ? TTo & `.${string}` extends never
207
+ ? AbsoluteToPath<TRouter, TFrom>
208
+ : RelativeToCurrentPath<TRouter, TFrom, TTo>
209
+ : RelativeToParentPath<TRouter, TFrom, TTo>
210
+
211
+ export type NavigateOptions<
212
+ TRouter extends AnyRouter = RegisteredRouter,
213
+ TFrom extends string = string,
214
+ TTo extends string | undefined = '.',
215
+ TMaskFrom extends string = TFrom,
216
+ TMaskTo extends string = '.',
217
+ > = ToOptions<TRouter, TFrom, TTo, TMaskFrom, TMaskTo> & NavigateOptionProps
218
+
219
+ export interface NavigateOptionProps {
220
+ // if set to `true`, the router will scroll the element with an id matching the hash into view with default ScrollIntoViewOptions.
221
+ // if set to `false`, the router will not scroll the element with an id matching the hash into view.
222
+ // if set to `ScrollIntoViewOptions`, the router will scroll the element with an id matching the hash into view with the provided options.
223
+ hashScrollIntoView?: boolean | ScrollIntoViewOptions
224
+ // `replace` is a boolean that determines whether the navigation should replace the current history entry or push a new one.
225
+ replace?: boolean
226
+ resetScroll?: boolean
227
+ /** @deprecated All navigations now use startTransition under the hood */
228
+ startTransition?: boolean
229
+ // if set to `true`, the router will wrap the resulting navigation in a document.startViewTransition() call.
230
+ // if set to `ViewTransitionOptions`, the router will pass the `types` field to document.startViewTransition({update: fn, types: viewTransition.types}) call
231
+ viewTransition?: boolean | ViewTransitionOptions
232
+ ignoreBlocker?: boolean
233
+ reloadDocument?: boolean
234
+ href?: string
235
+ }
236
+
237
+ export type ToOptions<
238
+ TRouter extends AnyRouter = RegisteredRouter,
239
+ TFrom extends string = string,
240
+ TTo extends string | undefined = '.',
241
+ TMaskFrom extends string = TFrom,
242
+ TMaskTo extends string = '.',
243
+ > = ToSubOptions<TRouter, TFrom, TTo> & MaskOptions<TRouter, TMaskFrom, TMaskTo>
244
+
245
+ export interface MaskOptions<
246
+ in out TRouter extends AnyRouter,
247
+ in out TMaskFrom extends string,
248
+ in out TMaskTo extends string,
249
+ > {
250
+ _fromLocation?: ParsedLocation
251
+ mask?: ToMaskOptions<TRouter, TMaskFrom, TMaskTo>
252
+ }
253
+
254
+ export type ToMaskOptions<
255
+ TRouter extends AnyRouter = RegisteredRouter,
256
+ TMaskFrom extends string = string,
257
+ TMaskTo extends string = '.',
258
+ > = ToSubOptions<TRouter, TMaskFrom, TMaskTo> & {
259
+ unmaskOnReload?: boolean
260
+ }
261
+
262
+ export type ToSubOptions<
263
+ TRouter extends AnyRouter = RegisteredRouter,
264
+ TFrom extends string = string,
265
+ TTo extends string | undefined = '.',
266
+ > = ToSubOptionsProps<TRouter, TFrom, TTo> &
267
+ SearchParamOptions<TRouter, TFrom, TTo> &
268
+ PathParamOptions<TRouter, TFrom, TTo>
269
+
270
+ export interface RequiredToOptions<
271
+ in out TRouter extends AnyRouter,
272
+ in out TFrom extends string,
273
+ in out TTo extends string | undefined,
274
+ > {
275
+ to: ToPathOption<TRouter, TFrom, TTo> & {}
276
+ }
277
+
278
+ export interface OptionalToOptions<
279
+ in out TRouter extends AnyRouter,
280
+ in out TFrom extends string,
281
+ in out TTo extends string | undefined,
282
+ > {
283
+ to?: ToPathOption<TRouter, TFrom, TTo> & {}
284
+ }
285
+
286
+ export type MakeToRequired<
287
+ TRouter extends AnyRouter,
288
+ TFrom extends string,
289
+ TTo extends string | undefined,
290
+ > = string extends TFrom
291
+ ? string extends TTo
292
+ ? OptionalToOptions<TRouter, TFrom, TTo>
293
+ : TTo & CatchAllPaths<TRouter> extends never
294
+ ? RequiredToOptions<TRouter, TFrom, TTo>
295
+ : OptionalToOptions<TRouter, TFrom, TTo>
296
+ : OptionalToOptions<TRouter, TFrom, TTo>
297
+
298
+ export type ToSubOptionsProps<
299
+ TRouter extends AnyRouter = RegisteredRouter,
300
+ TFrom extends RoutePaths<TRouter['routeTree']> | string = string,
301
+ TTo extends string | undefined = '.',
302
+ > = MakeToRequired<TRouter, TFrom, TTo> & {
303
+ hash?: true | Updater<string>
304
+ state?: true | NonNullableUpdater<ParsedHistoryState, HistoryState>
305
+ from?: FromPathOption<TRouter, TFrom> & {}
306
+ }
307
+
308
+ export type ParamsReducerFn<
309
+ in out TRouter extends AnyRouter,
310
+ in out TParamVariant extends ParamVariant,
311
+ in out TFrom,
312
+ in out TTo,
313
+ > = (
314
+ current: Expand<ResolveFromParams<TRouter, TParamVariant, TFrom>>,
315
+ ) => Expand<ResolveRelativeToParams<TRouter, TParamVariant, TFrom, TTo>>
316
+
317
+ type ParamsReducer<
318
+ TRouter extends AnyRouter,
319
+ TParamVariant extends ParamVariant,
320
+ TFrom,
321
+ TTo,
322
+ > =
323
+ | Expand<ResolveRelativeToParams<TRouter, TParamVariant, TFrom, TTo>>
324
+ | (ParamsReducerFn<TRouter, TParamVariant, TFrom, TTo> & {})
325
+
326
+ type ParamVariant = 'PATH' | 'SEARCH'
327
+
328
+ export type ResolveRoute<
329
+ TRouter extends AnyRouter,
330
+ TFrom,
331
+ TTo,
332
+ TPath = ResolveRelativePath<TFrom, TTo>,
333
+ > = TPath extends string
334
+ ? TFrom extends TPath
335
+ ? RouteByPath<TRouter['routeTree'], TPath>
336
+ : RouteByToPath<TRouter, TPath>
337
+ : never
338
+
339
+ type ResolveFromParamType<TParamVariant extends ParamVariant> =
340
+ TParamVariant extends 'PATH' ? 'allParams' : 'fullSearchSchema'
341
+
342
+ type ResolveFromAllParams<
343
+ TRouter extends AnyRouter,
344
+ TParamVariant extends ParamVariant,
345
+ > = TParamVariant extends 'PATH'
346
+ ? AllParams<TRouter['routeTree']>
347
+ : FullSearchSchema<TRouter['routeTree']>
348
+
349
+ type ResolveFromParams<
350
+ TRouter extends AnyRouter,
351
+ TParamVariant extends ParamVariant,
352
+ TFrom,
353
+ > = string extends TFrom
354
+ ? ResolveFromAllParams<TRouter, TParamVariant>
355
+ : RouteByPath<
356
+ TRouter['routeTree'],
357
+ TFrom
358
+ >['types'][ResolveFromParamType<TParamVariant>]
359
+
360
+ type ResolveToParamType<TParamVariant extends ParamVariant> =
361
+ TParamVariant extends 'PATH' ? 'allParams' : 'fullSearchSchemaInput'
362
+
363
+ type ResolveAllToParams<
364
+ TRouter extends AnyRouter,
365
+ TParamVariant extends ParamVariant,
366
+ > = TParamVariant extends 'PATH'
367
+ ? AllParams<TRouter['routeTree']>
368
+ : FullSearchSchemaInput<TRouter['routeTree']>
369
+
370
+ export type ResolveToParams<
371
+ TRouter extends AnyRouter,
372
+ TParamVariant extends ParamVariant,
373
+ TFrom,
374
+ TTo,
375
+ > =
376
+ ResolveRelativePath<TFrom, TTo> extends infer TPath
377
+ ? undefined extends TPath
378
+ ? never
379
+ : string extends TPath
380
+ ? ResolveAllToParams<TRouter, TParamVariant>
381
+ : TPath extends CatchAllPaths<TRouter>
382
+ ? ResolveAllToParams<TRouter, TParamVariant>
383
+ : ResolveRoute<
384
+ TRouter,
385
+ TFrom,
386
+ TTo
387
+ >['types'][ResolveToParamType<TParamVariant>]
388
+ : never
389
+
390
+ type ResolveRelativeToParams<
391
+ TRouter extends AnyRouter,
392
+ TParamVariant extends ParamVariant,
393
+ TFrom,
394
+ TTo,
395
+ TToParams = ResolveToParams<TRouter, TParamVariant, TFrom, TTo>,
396
+ > = TParamVariant extends 'SEARCH'
397
+ ? TToParams
398
+ : string extends TFrom
399
+ ? TToParams
400
+ : MakeDifferenceOptional<
401
+ ResolveFromParams<TRouter, TParamVariant, TFrom>,
402
+ TToParams
403
+ >
404
+
405
+ export interface MakeOptionalSearchParams<
406
+ in out TRouter extends AnyRouter,
407
+ in out TFrom,
408
+ in out TTo,
409
+ > {
410
+ search?: true | (ParamsReducer<TRouter, 'SEARCH', TFrom, TTo> & {})
411
+ }
412
+
413
+ export interface MakeOptionalPathParams<
414
+ in out TRouter extends AnyRouter,
415
+ in out TFrom,
416
+ in out TTo,
417
+ > {
418
+ params?: true | (ParamsReducer<TRouter, 'PATH', TFrom, TTo> & {})
419
+ }
420
+
421
+ type MakeRequiredParamsReducer<
422
+ TRouter extends AnyRouter,
423
+ TParamVariant extends ParamVariant,
424
+ TFrom,
425
+ TTo,
426
+ > =
427
+ | (string extends TFrom
428
+ ? never
429
+ : ResolveFromParams<TRouter, TParamVariant, TFrom> extends WithoutEmpty<
430
+ PickRequired<
431
+ ResolveRelativeToParams<TRouter, TParamVariant, TFrom, TTo>
432
+ >
433
+ >
434
+ ? true
435
+ : never)
436
+ | (ParamsReducer<TRouter, TParamVariant, TFrom, TTo> & {})
437
+
438
+ export interface MakeRequiredPathParams<
439
+ in out TRouter extends AnyRouter,
440
+ in out TFrom,
441
+ in out TTo,
442
+ > {
443
+ params: MakeRequiredParamsReducer<TRouter, 'PATH', TFrom, TTo> & {}
444
+ }
445
+
446
+ export interface MakeRequiredSearchParams<
447
+ in out TRouter extends AnyRouter,
448
+ in out TFrom,
449
+ in out TTo,
450
+ > {
451
+ search: MakeRequiredParamsReducer<TRouter, 'SEARCH', TFrom, TTo> & {}
452
+ }
453
+
454
+ export type IsRequired<
455
+ TRouter extends AnyRouter,
456
+ TParamVariant extends ParamVariant,
457
+ TFrom,
458
+ TTo,
459
+ > =
460
+ ResolveRelativePath<TFrom, TTo> extends infer TPath
461
+ ? undefined extends TPath
462
+ ? never
463
+ : TPath extends CatchAllPaths<TRouter>
464
+ ? never
465
+ : IsRequiredParams<
466
+ ResolveRelativeToParams<TRouter, TParamVariant, TFrom, TTo>
467
+ >
468
+ : never
469
+
470
+ export type SearchParamOptions<TRouter extends AnyRouter, TFrom, TTo> =
471
+ IsRequired<TRouter, 'SEARCH', TFrom, TTo> extends never
472
+ ? MakeOptionalSearchParams<TRouter, TFrom, TTo>
473
+ : MakeRequiredSearchParams<TRouter, TFrom, TTo>
474
+
475
+ export type PathParamOptions<TRouter extends AnyRouter, TFrom, TTo> =
476
+ IsRequired<TRouter, 'PATH', TFrom, TTo> extends never
477
+ ? MakeOptionalPathParams<TRouter, TFrom, TTo>
478
+ : MakeRequiredPathParams<TRouter, TFrom, TTo>
479
+
480
+ export type ToPathOption<
481
+ TRouter extends AnyRouter = AnyRouter,
482
+ TFrom extends string = string,
483
+ TTo extends string | undefined = string,
484
+ > = ConstrainLiteral<
485
+ TTo,
486
+ RelativeToPathAutoComplete<
487
+ TRouter,
488
+ NoInfer<TFrom> extends string ? NoInfer<TFrom> : '',
489
+ NoInfer<TTo> & string
490
+ >
491
+ >
492
+
493
+ export type FromPathOption<TRouter extends AnyRouter, TFrom> = ConstrainLiteral<
494
+ TFrom,
495
+ RoutePaths<TRouter['routeTree']>
496
+ >
497
+
498
+ export interface ActiveOptions {
499
+ exact?: boolean
500
+ includeHash?: boolean
501
+ includeSearch?: boolean
502
+ explicitUndefined?: boolean
503
+ }
504
+
505
+ export interface LinkOptionsProps {
506
+ /**
507
+ * The standard anchor tag target attribute
508
+ */
509
+ target?: HTMLAnchorElement['target']
510
+ /**
511
+ * Configurable options to determine if the link should be considered active or not
512
+ * @default {exact:true,includeHash:true}
513
+ */
514
+ activeOptions?: ActiveOptions
515
+ /**
516
+ * The preloading strategy for this link
517
+ * - `false` - No preloading
518
+ * - `'intent'` - Preload the linked route on hover and cache it for this many milliseconds in hopes that the user will eventually navigate there.
519
+ * - `'viewport'` - Preload the linked route when it enters the viewport
520
+ */
521
+ preload?: false | 'intent' | 'viewport' | 'render'
522
+ /**
523
+ * When a preload strategy is set, this delays the preload by this many milliseconds.
524
+ * If the user exits the link before this delay, the preload will be cancelled.
525
+ */
526
+ preloadDelay?: number
527
+ /**
528
+ * Control whether the link should be disabled or not
529
+ * If set to `true`, the link will be rendered without an `href` attribute
530
+ * @default false
531
+ */
532
+ disabled?: boolean
533
+ }
534
+
535
+ export type LinkOptions<
536
+ TRouter extends AnyRouter = RegisteredRouter,
537
+ TFrom extends string = string,
538
+ TTo extends string | undefined = '.',
539
+ TMaskFrom extends string = TFrom,
540
+ TMaskTo extends string = '.',
541
+ > = NavigateOptions<TRouter, TFrom, TTo, TMaskFrom, TMaskTo> & LinkOptionsProps
542
+
135
543
  export type LinkCurrentTargetElement = {
136
544
  preloadTimeout?: null | ReturnType<typeof setTimeout>
137
545
  }