@tanstack/react-router 1.132.0-alpha.3 → 1.132.0-alpha.8

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.
Files changed (59) hide show
  1. package/dist/cjs/ScriptOnce.cjs +1 -1
  2. package/dist/cjs/ScriptOnce.cjs.map +1 -1
  3. package/dist/cjs/fileRoute.cjs.map +1 -1
  4. package/dist/cjs/fileRoute.d.cts +2 -2
  5. package/dist/cjs/index.cjs +4 -0
  6. package/dist/cjs/index.cjs.map +1 -1
  7. package/dist/cjs/index.d.cts +3 -2
  8. package/dist/cjs/link.cjs +41 -55
  9. package/dist/cjs/link.cjs.map +1 -1
  10. package/dist/cjs/route.cjs.map +1 -1
  11. package/dist/cjs/route.d.cts +11 -11
  12. package/dist/cjs/router.cjs.map +1 -1
  13. package/dist/cjs/router.d.cts +2 -2
  14. package/dist/cjs/scroll-restoration.cjs +11 -3
  15. package/dist/cjs/scroll-restoration.cjs.map +1 -1
  16. package/dist/cjs/ssr/serializer.d.cts +6 -0
  17. package/dist/cjs/useActiveLocation.cjs +39 -0
  18. package/dist/cjs/useActiveLocation.cjs.map +1 -0
  19. package/dist/cjs/useActiveLocation.d.cts +7 -0
  20. package/dist/cjs/useBlocker.cjs +1 -1
  21. package/dist/cjs/useBlocker.cjs.map +1 -1
  22. package/dist/cjs/useNavigate.cjs +6 -9
  23. package/dist/cjs/useNavigate.cjs.map +1 -1
  24. package/dist/esm/ScriptOnce.js +1 -1
  25. package/dist/esm/ScriptOnce.js.map +1 -1
  26. package/dist/esm/fileRoute.d.ts +2 -2
  27. package/dist/esm/fileRoute.js.map +1 -1
  28. package/dist/esm/index.d.ts +3 -2
  29. package/dist/esm/index.js +2 -1
  30. package/dist/esm/link.js +41 -55
  31. package/dist/esm/link.js.map +1 -1
  32. package/dist/esm/route.d.ts +11 -11
  33. package/dist/esm/route.js.map +1 -1
  34. package/dist/esm/router.d.ts +2 -2
  35. package/dist/esm/router.js.map +1 -1
  36. package/dist/esm/scroll-restoration.js +11 -3
  37. package/dist/esm/scroll-restoration.js.map +1 -1
  38. package/dist/esm/ssr/serializer.d.ts +6 -0
  39. package/dist/esm/useActiveLocation.d.ts +7 -0
  40. package/dist/esm/useActiveLocation.js +39 -0
  41. package/dist/esm/useActiveLocation.js.map +1 -0
  42. package/dist/esm/useBlocker.js +1 -1
  43. package/dist/esm/useBlocker.js.map +1 -1
  44. package/dist/esm/useNavigate.js +6 -9
  45. package/dist/esm/useNavigate.js.map +1 -1
  46. package/dist/llms/rules/guide.d.ts +1 -1
  47. package/dist/llms/rules/guide.js +14 -7
  48. package/package.json +2 -2
  49. package/src/ScriptOnce.tsx +1 -1
  50. package/src/fileRoute.ts +10 -2
  51. package/src/index.tsx +4 -3
  52. package/src/link.tsx +46 -56
  53. package/src/route.tsx +74 -17
  54. package/src/router.ts +2 -5
  55. package/src/scroll-restoration.tsx +11 -4
  56. package/src/ssr/serializer.ts +7 -0
  57. package/src/useActiveLocation.ts +57 -0
  58. package/src/useBlocker.tsx +1 -1
  59. package/src/useNavigate.tsx +6 -14
@@ -8,6 +8,17 @@ import { ScriptOnce } from './ScriptOnce'
8
8
 
9
9
  export function ScrollRestoration() {
10
10
  const router = useRouter()
11
+ if (!router.isScrollRestoring || !router.isServer) {
12
+ return null
13
+ }
14
+ if (typeof router.options.scrollRestoration === 'function') {
15
+ const shouldRestore = router.options.scrollRestoration({
16
+ location: router.latestLocation,
17
+ })
18
+ if (!shouldRestore) {
19
+ return null
20
+ }
21
+ }
11
22
  const getKey =
12
23
  router.options.getScrollRestorationKey || defaultGetScrollRestorationKey
13
24
  const userKey = getKey(router.latestLocation)
@@ -16,10 +27,6 @@ export function ScrollRestoration() {
16
27
  ? userKey
17
28
  : undefined
18
29
 
19
- if (!router.isScrollRestoring || !router.isServer) {
20
- return null
21
- }
22
-
23
30
  const restoreScrollOptions: Parameters<typeof restoreScroll>[0] = {
24
31
  storageKey,
25
32
  shouldScrollRestoration: true,
@@ -0,0 +1,7 @@
1
+ import type * as React from 'react'
2
+
3
+ declare module '@tanstack/router-core' {
4
+ export interface SerializerExtensions {
5
+ ReadableStream: React.JSX.Element
6
+ }
7
+ }
@@ -0,0 +1,57 @@
1
+ import { last } from '@tanstack/router-core'
2
+ import { useCallback, useEffect, useState } from 'react'
3
+ import { useRouter } from './useRouter'
4
+ import { useMatch } from './useMatch'
5
+ import { useRouterState } from './useRouterState'
6
+ import type { ParsedLocation } from '@tanstack/router-core'
7
+
8
+ export type UseActiveLocationResult = {
9
+ activeLocation: ParsedLocation
10
+ getFromPath: (from?: string) => string
11
+ setActiveLocation: (location?: ParsedLocation) => void
12
+ }
13
+
14
+ export const useActiveLocation = (
15
+ location?: ParsedLocation,
16
+ ): UseActiveLocationResult => {
17
+ const router = useRouter()
18
+ const routerLocation = useRouterState({ select: (state) => state.location })
19
+ const [activeLocation, setActiveLocation] = useState<ParsedLocation>(
20
+ location ?? routerLocation,
21
+ )
22
+ const [customActiveLocation, setCustomActiveLocation] = useState<
23
+ ParsedLocation | undefined
24
+ >(location)
25
+
26
+ useEffect(() => {
27
+ setActiveLocation(customActiveLocation ?? routerLocation)
28
+ }, [routerLocation, customActiveLocation])
29
+
30
+ const matchIndex = useMatch({
31
+ strict: false,
32
+ select: (match) => match.index,
33
+ })
34
+
35
+ const getFromPath = useCallback(
36
+ (from?: string) => {
37
+ const activeLocationMatches = router.matchRoutes(activeLocation, {
38
+ _buildLocation: false,
39
+ })
40
+
41
+ const activeLocationMatch = last(activeLocationMatches)
42
+
43
+ return (
44
+ from ??
45
+ activeLocationMatch?.fullPath ??
46
+ router.state.matches[matchIndex]!.fullPath
47
+ )
48
+ },
49
+ [activeLocation, matchIndex, router],
50
+ )
51
+
52
+ return {
53
+ activeLocation,
54
+ getFromPath,
55
+ setActiveLocation: setCustomActiveLocation,
56
+ }
57
+ }
@@ -176,7 +176,7 @@ export function useBlocker(
176
176
  function getLocation(
177
177
  location: HistoryLocation,
178
178
  ): AnyShouldBlockFnLocation {
179
- const parsedLocation = router.parseLocation(undefined, location)
179
+ const parsedLocation = router.parseLocation(location)
180
180
  const matchedRoutes = router.getMatchedRoutes(
181
181
  parsedLocation.pathname,
182
182
  undefined,
@@ -1,6 +1,6 @@
1
1
  import * as React from 'react'
2
2
  import { useRouter } from './useRouter'
3
- import { useMatch } from './useMatch'
3
+ import { useActiveLocation } from './useActiveLocation'
4
4
  import type {
5
5
  AnyRouter,
6
6
  FromPathOption,
@@ -15,29 +15,21 @@ export function useNavigate<
15
15
  >(_defaultOpts?: {
16
16
  from?: FromPathOption<TRouter, TDefaultFrom>
17
17
  }): UseNavigateResult<TDefaultFrom> {
18
- const { navigate, state } = useRouter()
18
+ const router = useRouter()
19
19
 
20
- // Just get the index of the current match to avoid rerenders
21
- // as much as possible
22
- const matchIndex = useMatch({
23
- strict: false,
24
- select: (match) => match.index,
25
- })
20
+ const { getFromPath, activeLocation } = useActiveLocation()
26
21
 
27
22
  return React.useCallback(
28
23
  (options: NavigateOptions) => {
29
- const from =
30
- options.from ??
31
- _defaultOpts?.from ??
32
- state.matches[matchIndex]!.fullPath
24
+ const from = getFromPath(options.from ?? _defaultOpts?.from)
33
25
 
34
- return navigate({
26
+ return router.navigate({
35
27
  ...options,
36
28
  from,
37
29
  })
38
30
  },
39
31
  // eslint-disable-next-line react-hooks/exhaustive-deps
40
- [_defaultOpts?.from, navigate],
32
+ [_defaultOpts?.from, router, getFromPath, activeLocation],
41
33
  ) as UseNavigateResult<TDefaultFrom>
42
34
  }
43
35