@tanstack/react-router 1.94.1 → 1.95.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.
@@ -0,0 +1 @@
1
+ export declare function useCanGoBack(): boolean;
@@ -0,0 +1,8 @@
1
+ import { useRouterState } from "./useRouterState.js";
2
+ function useCanGoBack() {
3
+ return useRouterState({ select: (s) => s.location.state.__TSR_index !== 0 });
4
+ }
5
+ export {
6
+ useCanGoBack
7
+ };
8
+ //# sourceMappingURL=useCanGoBack.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useCanGoBack.js","sources":["../../src/useCanGoBack.ts"],"sourcesContent":["import { useRouterState } from './useRouterState'\n\nexport function useCanGoBack() {\n return useRouterState({ select: (s) => s.location.state.__TSR_index !== 0 })\n}\n"],"names":[],"mappings":";AAEO,SAAS,eAAe;AACtB,SAAA,eAAe,EAAE,QAAQ,CAAC,MAAM,EAAE,SAAS,MAAM,gBAAgB,GAAG;AAC7E;"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/react-router",
3
- "version": "1.94.1",
3
+ "version": "1.95.1",
4
4
  "description": "Modern and scalable routing for React applications",
5
5
  "author": "Tanner Linsley",
6
6
  "license": "MIT",
@@ -53,7 +53,7 @@
53
53
  "jsesc": "^3.0.2",
54
54
  "tiny-invariant": "^1.3.3",
55
55
  "tiny-warning": "^1.0.3",
56
- "@tanstack/history": "1.90.0"
56
+ "@tanstack/history": "1.95.0"
57
57
  },
58
58
  "devDependencies": {
59
59
  "@testing-library/jest-dom": "^6.6.3",
package/src/Matches.tsx CHANGED
@@ -13,7 +13,14 @@ import type {
13
13
  } from './structuralSharing'
14
14
  import type { AnyRoute, ReactNode, StaticDataRouteOption } from './route'
15
15
  import type { AnyRouter, RegisteredRouter, RouterState } from './router'
16
- import type { ResolveRelativePath, ResolveRoute, ToOptions } from './link'
16
+ import type {
17
+ MakeOptionalPathParams,
18
+ MakeOptionalSearchParams,
19
+ MaskOptions,
20
+ ResolveRelativePath,
21
+ ResolveRoute,
22
+ ToSubOptionsProps,
23
+ } from './link'
17
24
  import type {
18
25
  AllContext,
19
26
  AllLoaderData,
@@ -23,7 +30,6 @@ import type {
23
30
  RouteById,
24
31
  RouteByPath,
25
32
  RouteIds,
26
- RoutePaths,
27
33
  } from './routeInfo'
28
34
  import type {
29
35
  Constrain,
@@ -270,22 +276,15 @@ export interface MatchRouteOptions {
270
276
 
271
277
  export type UseMatchRouteOptions<
272
278
  TRouter extends AnyRouter = RegisteredRouter,
273
- TFrom extends RoutePaths<TRouter['routeTree']> | string = RoutePaths<
274
- TRouter['routeTree']
275
- >,
276
- TTo extends string | undefined = '',
277
- TMaskFrom extends RoutePaths<TRouter['routeTree']> | string = TFrom,
279
+ TFrom extends string = string,
280
+ TTo extends string | undefined = undefined,
281
+ TMaskFrom extends string = TFrom,
278
282
  TMaskTo extends string = '',
279
- TOptions extends ToOptions<
280
- TRouter,
281
- TFrom,
282
- TTo,
283
- TMaskFrom,
284
- TMaskTo
285
- > = ToOptions<TRouter, TFrom, TTo, TMaskFrom, TMaskTo>,
286
- TRelaxedOptions = Omit<TOptions, 'search' | 'params'> &
287
- DeepPartial<Pick<TOptions, 'search' | 'params'>>,
288
- > = TRelaxedOptions & MatchRouteOptions
283
+ > = ToSubOptionsProps<TRouter, TFrom, TTo> &
284
+ DeepPartial<MakeOptionalSearchParams<TRouter, TFrom, TTo>> &
285
+ DeepPartial<MakeOptionalPathParams<TRouter, TFrom, TTo>> &
286
+ MaskOptions<TRouter, TMaskFrom, TMaskTo> &
287
+ MatchRouteOptions
289
288
 
290
289
  export function useMatchRoute<TRouter extends AnyRouter = RegisteredRouter>() {
291
290
  const router = useRouter()
package/src/index.tsx CHANGED
@@ -47,11 +47,11 @@ export type {
47
47
  ParsePathParams,
48
48
  RemoveTrailingSlashes,
49
49
  RemoveLeadingSlashes,
50
- SearchPaths,
51
- SearchRelativePathAutoComplete,
52
- RelativeToParentPathAutoComplete,
53
- RelativeToCurrentPathAutoComplete,
54
- AbsolutePathAutoComplete,
50
+ InferDescendantToPaths,
51
+ RelativeToPath,
52
+ RelativeToParentPath,
53
+ RelativeToCurrentPath,
54
+ AbsoluteToPath,
55
55
  RelativeToPathAutoComplete,
56
56
  NavigateOptions,
57
57
  ToOptions,
@@ -317,6 +317,8 @@ export { useRouterState } from './useRouterState'
317
317
 
318
318
  export { useLocation } from './useLocation'
319
319
 
320
+ export { useCanGoBack } from './useCanGoBack'
321
+
320
322
  export {
321
323
  escapeJSON, // SSR
322
324
  useLayoutEffect, // SSR
package/src/link.tsx CHANGED
@@ -27,7 +27,6 @@ import type {
27
27
  RoutePaths,
28
28
  RouteToPath,
29
29
  ToPath,
30
- TrailingSlashOptionByRouter,
31
30
  } from './routeInfo'
32
31
  import type {
33
32
  AnyRouter,
@@ -80,78 +79,73 @@ export type RemoveLeadingSlashes<T> = T extends `/${string}`
80
79
  : T
81
80
  : T
82
81
 
83
- export type FindDescendantPaths<
82
+ export type FindDescendantToPaths<
84
83
  TRouter extends AnyRouter,
85
84
  TPrefix extends string,
86
85
  > = `${TPrefix}/${string}` & RouteToPath<TRouter>
87
86
 
88
- export type SearchPaths<
87
+ export type InferDescendantToPaths<
89
88
  TRouter extends AnyRouter,
90
89
  TPrefix extends string,
91
- TPaths = FindDescendantPaths<TRouter, TPrefix>,
90
+ TPaths = FindDescendantToPaths<TRouter, TPrefix>,
92
91
  > = TPaths extends `${TPrefix}/`
93
92
  ? never
94
93
  : TPaths extends `${TPrefix}/${infer TRest}`
95
94
  ? TRest
96
95
  : never
97
96
 
98
- export type SearchRelativePathAutoComplete<
97
+ export type RelativeToPath<
99
98
  TRouter extends AnyRouter,
100
99
  TTo extends string,
101
- TSearchPath extends string,
100
+ TResolvedPath extends string,
102
101
  > =
103
- | (TSearchPath & RouteToPath<TRouter> extends never
102
+ | (TResolvedPath & RouteToPath<TRouter> extends never
104
103
  ? never
105
- : ToPath<TrailingSlashOptionByRouter<TRouter>, TTo>)
106
- | `${TTo}/${SearchPaths<TRouter, RemoveTrailingSlashes<TSearchPath>>}`
104
+ : ToPath<TRouter, TTo>)
105
+ | `${RemoveTrailingSlashes<TTo>}/${InferDescendantToPaths<TRouter, RemoveTrailingSlashes<TResolvedPath>>}`
107
106
 
108
- export type RelativeToParentPathAutoComplete<
107
+ export type RelativeToParentPath<
109
108
  TRouter extends AnyRouter,
110
109
  TFrom extends string,
111
110
  TTo extends string,
112
111
  TResolvedPath extends string = ResolveRelativePath<TFrom, TTo>,
113
112
  > =
114
- | SearchRelativePathAutoComplete<TRouter, TTo, TResolvedPath>
113
+ | RelativeToPath<TRouter, TTo, TResolvedPath>
115
114
  | (TTo extends `${string}..` | `${string}../`
116
115
  ? TResolvedPath extends '/' | ''
117
116
  ? never
118
- : FindDescendantPaths<
117
+ : FindDescendantToPaths<
119
118
  TRouter,
120
119
  RemoveTrailingSlashes<TResolvedPath>
121
120
  > extends never
122
121
  ? never
123
- : `${TTo}/${ParentPath<TrailingSlashOptionByRouter<TRouter>>}`
122
+ : `${RemoveTrailingSlashes<TTo>}/${ParentPath<TRouter>}`
124
123
  : never)
125
124
 
126
- export type RelativeToCurrentPathAutoComplete<
125
+ export type RelativeToCurrentPath<
127
126
  TRouter extends AnyRouter,
128
127
  TFrom extends string,
129
128
  TTo extends string,
130
129
  TResolvedPath extends string = ResolveRelativePath<TFrom, TTo>,
131
- > =
132
- | SearchRelativePathAutoComplete<TRouter, TTo, TResolvedPath>
133
- | CurrentPath<TrailingSlashOptionByRouter<TRouter>>
130
+ > = RelativeToPath<TRouter, TTo, TResolvedPath> | CurrentPath<TRouter>
134
131
 
135
- export type AbsolutePathAutoComplete<
136
- TRouter extends AnyRouter,
137
- TFrom extends string,
138
- > =
132
+ export type AbsoluteToPath<TRouter extends AnyRouter, TFrom extends string> =
139
133
  | (string extends TFrom
140
- ? CurrentPath<TrailingSlashOptionByRouter<TRouter>>
134
+ ? CurrentPath<TRouter>
141
135
  : TFrom extends `/`
142
136
  ? never
143
- : CurrentPath<TrailingSlashOptionByRouter<TRouter>>)
137
+ : CurrentPath<TRouter>)
144
138
  | (string extends TFrom
145
- ? ParentPath<TrailingSlashOptionByRouter<TRouter>>
139
+ ? ParentPath<TRouter>
146
140
  : TFrom extends `/`
147
141
  ? never
148
- : ParentPath<TrailingSlashOptionByRouter<TRouter>>)
142
+ : ParentPath<TRouter>)
149
143
  | RouteToPath<TRouter>
150
144
  | (TFrom extends '/'
151
145
  ? never
152
146
  : string extends TFrom
153
147
  ? never
154
- : SearchPaths<TRouter, RemoveTrailingSlashes<TFrom>>)
148
+ : InferDescendantToPaths<TRouter, RemoveTrailingSlashes<TFrom>>)
155
149
 
156
150
  export type RelativeToPathAutoComplete<
157
151
  TRouter extends AnyRouter,
@@ -160,20 +154,12 @@ export type RelativeToPathAutoComplete<
160
154
  > = string extends TTo
161
155
  ? string
162
156
  : string extends TFrom
163
- ? AbsolutePathAutoComplete<TRouter, TFrom>
157
+ ? AbsoluteToPath<TRouter, TFrom>
164
158
  : TTo & `..${string}` extends never
165
159
  ? TTo & `.${string}` extends never
166
- ? AbsolutePathAutoComplete<TRouter, TFrom>
167
- : RelativeToCurrentPathAutoComplete<
168
- TRouter,
169
- TFrom,
170
- RemoveTrailingSlashes<TTo>
171
- >
172
- : RelativeToParentPathAutoComplete<
173
- TRouter,
174
- TFrom,
175
- RemoveTrailingSlashes<TTo>
176
- >
160
+ ? AbsoluteToPath<TRouter, TFrom>
161
+ : RelativeToCurrentPath<TRouter, TFrom, TTo>
162
+ : RelativeToParentPath<TRouter, TFrom, TTo>
177
163
 
178
164
  export type NavigateOptions<
179
165
  TRouter extends AnyRouter = RegisteredRouter,
@@ -234,15 +220,41 @@ export type ToSubOptions<
234
220
  SearchParamOptions<TRouter, TFrom, TTo> &
235
221
  PathParamOptions<TRouter, TFrom, TTo>
236
222
 
237
- export interface ToSubOptionsProps<
238
- in out TRouter extends AnyRouter = RegisteredRouter,
239
- in out TFrom extends RoutePaths<TRouter['routeTree']> | string = string,
240
- in out TTo extends string | undefined = '.',
223
+ export interface RequiredToOptions<
224
+ in out TRouter extends AnyRouter,
225
+ in out TFrom extends string,
226
+ in out TTo extends string | undefined,
227
+ > {
228
+ to: ToPathOption<TRouter, TFrom, TTo> & {}
229
+ }
230
+
231
+ export interface OptionalToOptions<
232
+ in out TRouter extends AnyRouter,
233
+ in out TFrom extends string,
234
+ in out TTo extends string | undefined,
241
235
  > {
242
236
  to?: ToPathOption<TRouter, TFrom, TTo> & {}
237
+ }
238
+
239
+ export type MakeToRequired<
240
+ TRouter extends AnyRouter,
241
+ TFrom extends string,
242
+ TTo extends string | undefined,
243
+ > = string extends TFrom
244
+ ? string extends TTo
245
+ ? OptionalToOptions<TRouter, TFrom, TTo>
246
+ : TTo & CatchAllPaths<TRouter> extends never
247
+ ? RequiredToOptions<TRouter, TFrom, TTo>
248
+ : OptionalToOptions<TRouter, TFrom, TTo>
249
+ : OptionalToOptions<TRouter, TFrom, TTo>
250
+
251
+ export type ToSubOptionsProps<
252
+ TRouter extends AnyRouter = RegisteredRouter,
253
+ TFrom extends RoutePaths<TRouter['routeTree']> | string = string,
254
+ TTo extends string | undefined = '.',
255
+ > = MakeToRequired<TRouter, TFrom, TTo> & {
243
256
  hash?: true | Updater<string>
244
257
  state?: true | NonNullableUpdater<HistoryState>
245
- // The source route path. This is automatically set when using route-level APIs, but for type-safe relative routing on the router itself, this is required
246
258
  from?: FromPathOption<TRouter, TFrom> & {}
247
259
  }
248
260
 
@@ -319,7 +331,7 @@ export type ResolveToParams<
319
331
  ? never
320
332
  : string extends TPath
321
333
  ? ResolveAllToParams<TRouter, TParamVariant>
322
- : TPath extends CatchAllPaths<TrailingSlashOptionByRouter<TRouter>>
334
+ : TPath extends CatchAllPaths<TRouter>
323
335
  ? ResolveAllToParams<TRouter, TParamVariant>
324
336
  : ResolveRoute<
325
337
  TRouter,
@@ -404,7 +416,7 @@ export type IsRequired<
404
416
  ResolveRelativePath<TFrom, TTo> extends infer TPath
405
417
  ? undefined extends TPath
406
418
  ? never
407
- : TPath extends CatchAllPaths<TrailingSlashOptionByRouter<TRouter>>
419
+ : TPath extends CatchAllPaths<TRouter>
408
420
  ? never
409
421
  : IsRequiredParams<
410
422
  ResolveRelativeToParams<TRouter, TParamVariant, TFrom, TTo>
@@ -1014,7 +1026,11 @@ export function createLink<const TComp>(
1014
1026
  export const Link: LinkComponent<'a'> = React.forwardRef<Element, any>(
1015
1027
  (props, ref) => {
1016
1028
  const { _asChild, ...rest } = props
1017
- const { type: _type, ref: innerRef, ...linkProps } = useLinkProps(rest, ref)
1029
+ const {
1030
+ type: _type,
1031
+ ref: innerRef,
1032
+ ...linkProps
1033
+ } = useLinkProps(rest as any, ref)
1018
1034
 
1019
1035
  const children =
1020
1036
  typeof rest.children === 'function'
package/src/routeInfo.ts CHANGED
@@ -59,25 +59,30 @@ export type RouteIds<TRouteTree extends AnyRoute> =
59
59
  ? CodeRouteIds<TRouteTree>
60
60
  : InferFileRouteTypes<TRouteTree>['id']
61
61
 
62
- export type ParentPath<TOption> = 'always' extends TOption
63
- ? '../'
64
- : 'never' extends TOption
65
- ? '..'
66
- : '../' | '..'
67
-
68
- export type CurrentPath<TOption> = 'always' extends TOption
69
- ? './'
70
- : 'never' extends TOption
71
- ? '.'
72
- : './' | '.'
73
-
74
- export type ToPath<TOption, TTo extends string> = 'always' extends TOption
75
- ? `${TTo}/`
76
- : 'never' extends TOption
77
- ? TTo
78
- : TTo | `${TTo}/`
79
-
80
- export type CatchAllPaths<TOption> = CurrentPath<TOption> | ParentPath<TOption>
62
+ export type ParentPath<TRouter extends AnyRouter> =
63
+ TrailingSlashOptionByRouter<TRouter> extends 'always'
64
+ ? '../'
65
+ : TrailingSlashOptionByRouter<TRouter> extends 'never'
66
+ ? '..'
67
+ : '../' | '..'
68
+
69
+ export type CurrentPath<TRouter extends AnyRouter> =
70
+ TrailingSlashOptionByRouter<TRouter> extends 'always'
71
+ ? './'
72
+ : TrailingSlashOptionByRouter<TRouter> extends 'never'
73
+ ? '.'
74
+ : './' | '.'
75
+
76
+ export type ToPath<TRouter extends AnyRouter, TTo extends string> =
77
+ TrailingSlashOptionByRouter<TRouter> extends 'always'
78
+ ? AddTrailingSlash<TTo>
79
+ : TrailingSlashOptionByRouter<TRouter> extends 'never'
80
+ ? RemoveTrailingSlashes<TTo>
81
+ : AddTrailingSlash<TTo> | RemoveTrailingSlashes<TTo>
82
+
83
+ export type CatchAllPaths<TRouter extends AnyRouter> =
84
+ | CurrentPath<TRouter>
85
+ | ParentPath<TRouter>
81
86
 
82
87
  export type CodeRoutesByPath<TRouteTree extends AnyRoute> =
83
88
  ParseRoute<TRouteTree> extends infer TRoutes extends AnyRoute
package/src/router.ts CHANGED
@@ -1893,7 +1893,10 @@ export class Router<
1893
1893
  ...rest
1894
1894
  }: BuildNextOptions & CommitLocationOptions = {}) => {
1895
1895
  if (href) {
1896
- const parsed = parseHref(href, {})
1896
+ const currentIndex = this.history.location.state.__TSR_index
1897
+ const parsed = parseHref(href, {
1898
+ __TSR_index: replace ? currentIndex : currentIndex + 1,
1899
+ })
1897
1900
  rest.to = parsed.pathname
1898
1901
  rest.search = this.options.parseSearch(parsed.search)
1899
1902
  // remove the leading `#` from the hash
@@ -0,0 +1,5 @@
1
+ import { useRouterState } from './useRouterState'
2
+
3
+ export function useCanGoBack() {
4
+ return useRouterState({ select: (s) => s.location.state.__TSR_index !== 0 })
5
+ }