@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.
- package/dist/cjs/ScriptOnce.cjs +1 -1
- package/dist/cjs/ScriptOnce.cjs.map +1 -1
- package/dist/cjs/fileRoute.cjs.map +1 -1
- package/dist/cjs/fileRoute.d.cts +2 -2
- package/dist/cjs/index.cjs +4 -0
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +3 -2
- package/dist/cjs/link.cjs +41 -55
- package/dist/cjs/link.cjs.map +1 -1
- package/dist/cjs/route.cjs.map +1 -1
- package/dist/cjs/route.d.cts +11 -11
- package/dist/cjs/router.cjs.map +1 -1
- package/dist/cjs/router.d.cts +2 -2
- package/dist/cjs/scroll-restoration.cjs +11 -3
- package/dist/cjs/scroll-restoration.cjs.map +1 -1
- package/dist/cjs/ssr/serializer.d.cts +6 -0
- package/dist/cjs/useActiveLocation.cjs +39 -0
- package/dist/cjs/useActiveLocation.cjs.map +1 -0
- package/dist/cjs/useActiveLocation.d.cts +7 -0
- package/dist/cjs/useBlocker.cjs +1 -1
- package/dist/cjs/useBlocker.cjs.map +1 -1
- package/dist/cjs/useNavigate.cjs +6 -9
- package/dist/cjs/useNavigate.cjs.map +1 -1
- package/dist/esm/ScriptOnce.js +1 -1
- package/dist/esm/ScriptOnce.js.map +1 -1
- package/dist/esm/fileRoute.d.ts +2 -2
- package/dist/esm/fileRoute.js.map +1 -1
- package/dist/esm/index.d.ts +3 -2
- package/dist/esm/index.js +2 -1
- package/dist/esm/link.js +41 -55
- package/dist/esm/link.js.map +1 -1
- package/dist/esm/route.d.ts +11 -11
- package/dist/esm/route.js.map +1 -1
- package/dist/esm/router.d.ts +2 -2
- package/dist/esm/router.js.map +1 -1
- package/dist/esm/scroll-restoration.js +11 -3
- package/dist/esm/scroll-restoration.js.map +1 -1
- package/dist/esm/ssr/serializer.d.ts +6 -0
- package/dist/esm/useActiveLocation.d.ts +7 -0
- package/dist/esm/useActiveLocation.js +39 -0
- package/dist/esm/useActiveLocation.js.map +1 -0
- package/dist/esm/useBlocker.js +1 -1
- package/dist/esm/useBlocker.js.map +1 -1
- package/dist/esm/useNavigate.js +6 -9
- package/dist/esm/useNavigate.js.map +1 -1
- package/dist/llms/rules/guide.d.ts +1 -1
- package/dist/llms/rules/guide.js +14 -7
- package/package.json +2 -2
- package/src/ScriptOnce.tsx +1 -1
- package/src/fileRoute.ts +10 -2
- package/src/index.tsx +4 -3
- package/src/link.tsx +46 -56
- package/src/route.tsx +74 -17
- package/src/router.ts +2 -5
- package/src/scroll-restoration.tsx +11 -4
- package/src/ssr/serializer.ts +7 -0
- package/src/useActiveLocation.ts +57 -0
- package/src/useBlocker.tsx +1 -1
- 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,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
|
+
}
|
package/src/useBlocker.tsx
CHANGED
|
@@ -176,7 +176,7 @@ export function useBlocker(
|
|
|
176
176
|
function getLocation(
|
|
177
177
|
location: HistoryLocation,
|
|
178
178
|
): AnyShouldBlockFnLocation {
|
|
179
|
-
const parsedLocation = router.parseLocation(
|
|
179
|
+
const parsedLocation = router.parseLocation(location)
|
|
180
180
|
const matchedRoutes = router.getMatchedRoutes(
|
|
181
181
|
parsedLocation.pathname,
|
|
182
182
|
undefined,
|
package/src/useNavigate.tsx
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import * as React from 'react'
|
|
2
2
|
import { useRouter } from './useRouter'
|
|
3
|
-
import {
|
|
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
|
|
18
|
+
const router = useRouter()
|
|
19
19
|
|
|
20
|
-
|
|
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,
|
|
32
|
+
[_defaultOpts?.from, router, getFromPath, activeLocation],
|
|
41
33
|
) as UseNavigateResult<TDefaultFrom>
|
|
42
34
|
}
|
|
43
35
|
|