@tanstack/react-router 0.0.1-beta.67 → 0.0.1-beta.69

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/index.tsx CHANGED
@@ -30,6 +30,8 @@ import {
30
30
  AnySearchSchema,
31
31
  AnyPathParams,
32
32
  AnyRouteMatch,
33
+ NavigateOptions,
34
+ RouterConstructorOptions,
33
35
  } from '@tanstack/router'
34
36
  import { useStore } from '@tanstack/react-store'
35
37
 
@@ -68,7 +70,7 @@ export function lazy(
68
70
 
69
71
  export type LinkPropsOptions<
70
72
  TFrom extends RegisteredRoutesInfo['routePaths'] = '/',
71
- TTo extends string = '.',
73
+ TTo extends string = '',
72
74
  > = LinkOptions<RegisteredRoutesInfo, TFrom, TTo> & {
73
75
  // 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)
74
76
  activeProps?:
@@ -82,12 +84,12 @@ export type LinkPropsOptions<
82
84
 
83
85
  export type MakeUseMatchRouteOptions<
84
86
  TFrom extends RegisteredRoutesInfo['routePaths'] = '/',
85
- TTo extends string = '.',
87
+ TTo extends string = '',
86
88
  > = ToOptions<RegisteredRoutesInfo, TFrom, TTo> & MatchRouteOptions
87
89
 
88
90
  export type MakeMatchRouteOptions<
89
91
  TFrom extends RegisteredRoutesInfo['routePaths'] = '/',
90
- TTo extends string = '.',
92
+ TTo extends string = '',
91
93
  > = ToOptions<RegisteredRoutesInfo, TFrom, TTo> &
92
94
  MatchRouteOptions & {
93
95
  // 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
@@ -103,12 +105,12 @@ export type MakeMatchRouteOptions<
103
105
 
104
106
  export type MakeLinkPropsOptions<
105
107
  TFrom extends ValidFromPath<RegisteredRoutesInfo> = '/',
106
- TTo extends string = '.',
108
+ TTo extends string = '',
107
109
  > = LinkPropsOptions<TFrom, TTo> & React.AnchorHTMLAttributes<HTMLAnchorElement>
108
110
 
109
111
  export type MakeLinkOptions<
110
112
  TFrom extends RegisteredRoutesInfo['routePaths'] = '/',
111
- TTo extends string = '.',
113
+ TTo extends string = '',
112
114
  > = LinkPropsOptions<TFrom, TTo> &
113
115
  React.AnchorHTMLAttributes<HTMLAnchorElement> &
114
116
  Omit<React.AnchorHTMLAttributes<HTMLAnchorElement>, 'children'> & {
@@ -144,7 +146,7 @@ export type PromptProps = {
144
146
 
145
147
  export function useLinkProps<
146
148
  TFrom extends ValidFromPath<RegisteredRoutesInfo> = '/',
147
- TTo extends string = '.',
149
+ TTo extends string = '',
148
150
  >(
149
151
  options: MakeLinkPropsOptions<TFrom, TTo>,
150
152
  ): React.AnchorHTMLAttributes<HTMLAnchorElement> {
@@ -175,7 +177,6 @@ export function useLinkProps<
175
177
  onMouseEnter,
176
178
  onMouseLeave,
177
179
  onTouchStart,
178
- onTouchEnd,
179
180
  ...rest
180
181
  } = options
181
182
 
@@ -186,8 +187,15 @@ export function useLinkProps<
186
187
  return { href }
187
188
  }
188
189
 
189
- const { handleClick, handleFocus, handleEnter, handleLeave, isActive, next } =
190
- linkInfo
190
+ const {
191
+ handleClick,
192
+ handleFocus,
193
+ handleEnter,
194
+ handleLeave,
195
+ handleTouchStart,
196
+ isActive,
197
+ next,
198
+ } = linkInfo
191
199
 
192
200
  const reactHandleClick = (e: Event) => {
193
201
  if (React.startTransition) {
@@ -228,6 +236,7 @@ export function useLinkProps<
228
236
  onFocus: composeHandlers([onFocus, handleFocus]),
229
237
  onMouseEnter: composeHandlers([onMouseEnter, handleEnter]),
230
238
  onMouseLeave: composeHandlers([onMouseLeave, handleLeave]),
239
+ onTouchStart: composeHandlers([onTouchStart, handleTouchStart]),
231
240
  target,
232
241
  style: {
233
242
  ...style,
@@ -254,7 +263,7 @@ export function useLinkProps<
254
263
 
255
264
  export interface LinkFn<
256
265
  TDefaultFrom extends RegisteredRoutesInfo['routePaths'] = '/',
257
- TDefaultTo extends string = '.',
266
+ TDefaultTo extends string = '',
258
267
  > {
259
268
  <
260
269
  TFrom extends RegisteredRoutesInfo['routePaths'] = TDefaultFrom,
@@ -283,6 +292,19 @@ export const Link: LinkFn = React.forwardRef((props: any, ref) => {
283
292
  )
284
293
  }) as any
285
294
 
295
+ export function Navigate<
296
+ TFrom extends RegisteredRoutesInfo['routePaths'] = '/',
297
+ TTo extends string = '',
298
+ >(props: NavigateOptions<RegisteredRoutesInfo, TFrom, TTo>): null {
299
+ const router = useRouterContext()
300
+
301
+ React.useLayoutEffect(() => {
302
+ router.navigate(props as any)
303
+ }, [])
304
+
305
+ return null
306
+ }
307
+
286
308
  type MatchesContextValue = AnyRouteMatch[]
287
309
 
288
310
  export const matchesContext = React.createContext<MatchesContextValue>(null!)
@@ -299,7 +321,7 @@ export class ReactRouter<
299
321
  TRouteConfig extends AnyRootRoute = RootRoute,
300
322
  TRoutesInfo extends AnyRoutesInfo = RoutesInfo<TRouteConfig>,
301
323
  > extends Router<TRouteConfig, TRoutesInfo> {
302
- constructor(opts: RouterOptions<TRouteConfig>) {
324
+ constructor(opts: RouterConstructorOptions<TRouteConfig>) {
303
325
  super({
304
326
  ...opts,
305
327
  loadComponent: async (component) => {
@@ -423,7 +445,7 @@ export function useRoute<
423
445
  resolvedRoute,
424
446
  `Could not find a route for route "${
425
447
  routeId as string
426
- }"! Did you forget to add it to your route config?`,
448
+ }"! Did you forget to add it to your route?`,
427
449
  )
428
450
 
429
451
  return resolvedRoute as any
@@ -475,37 +497,43 @@ export function useNavigate<
475
497
  TDefaultFrom extends keyof RegisteredRoutesInfo['routesById'] = '/',
476
498
  >(defaultOpts?: { from?: TDefaultFrom }) {
477
499
  const router = useRouterContext()
478
- return <
479
- TFrom extends keyof RegisteredRoutesInfo['routesById'] = TDefaultFrom,
480
- TTo extends string = '.',
481
- >(
482
- opts?: MakeLinkOptions<TFrom, TTo>,
483
- ) => {
484
- return router.navigate({ ...defaultOpts, ...(opts as any) })
485
- }
500
+ return React.useCallback(
501
+ <
502
+ TFrom extends keyof RegisteredRoutesInfo['routesById'] = TDefaultFrom,
503
+ TTo extends string = '',
504
+ >(
505
+ opts?: MakeLinkOptions<TFrom, TTo>,
506
+ ) => {
507
+ return router.navigate({ ...defaultOpts, ...(opts as any) })
508
+ },
509
+ [],
510
+ )
486
511
  }
487
512
 
488
513
  export function useMatchRoute() {
489
514
  const router = useRouterContext()
490
515
 
491
- return <
492
- TFrom extends ValidFromPath<RegisteredRoutesInfo> = '/',
493
- TTo extends string = '.',
494
- >(
495
- opts: MakeUseMatchRouteOptions<TFrom, TTo>,
496
- ) => {
497
- const { pending, caseSensitive, ...rest } = opts
498
-
499
- return router.matchRoute(rest as any, {
500
- pending,
501
- caseSensitive,
502
- })
503
- }
516
+ return React.useCallback(
517
+ <
518
+ TFrom extends ValidFromPath<RegisteredRoutesInfo> = '/',
519
+ TTo extends string = '',
520
+ >(
521
+ opts: MakeUseMatchRouteOptions<TFrom, TTo>,
522
+ ) => {
523
+ const { pending, caseSensitive, ...rest } = opts
524
+
525
+ return router.matchRoute(rest as any, {
526
+ pending,
527
+ caseSensitive,
528
+ })
529
+ },
530
+ [],
531
+ )
504
532
  }
505
533
 
506
534
  export function MatchRoute<
507
535
  TFrom extends ValidFromPath<RegisteredRoutesInfo> = '/',
508
- TTo extends string = '.',
536
+ TTo extends string = '',
509
537
  >(props: MakeMatchRouteOptions<TFrom, TTo>): any {
510
538
  const matchRoute = useMatchRoute()
511
539
  const params = matchRoute(props)