@tanstack/react-router 0.0.1-beta.23 → 0.0.1-beta.230
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/LICENSE +21 -0
- package/build/cjs/CatchBoundary.js +123 -0
- package/build/cjs/CatchBoundary.js.map +1 -0
- package/build/cjs/Matches.js +232 -0
- package/build/cjs/Matches.js.map +1 -0
- package/build/cjs/RouterProvider.js +158 -0
- package/build/cjs/RouterProvider.js.map +1 -0
- package/build/cjs/_virtual/_rollupPluginBabelHelpers.js +2 -22
- package/build/cjs/_virtual/_rollupPluginBabelHelpers.js.map +1 -1
- package/build/cjs/awaited.js +43 -0
- package/build/cjs/awaited.js.map +1 -0
- package/build/cjs/defer.js +37 -0
- package/build/cjs/defer.js.map +1 -0
- package/build/cjs/fileRoute.js +27 -0
- package/build/cjs/fileRoute.js.map +1 -0
- package/build/cjs/index.js +122 -0
- package/build/cjs/index.js.map +1 -0
- package/build/cjs/lazyRouteComponent.js +54 -0
- package/build/cjs/lazyRouteComponent.js.map +1 -0
- package/build/cjs/link.js +148 -0
- package/build/cjs/link.js.map +1 -0
- package/build/cjs/path.js +209 -0
- package/build/cjs/path.js.map +1 -0
- package/build/cjs/qss.js +63 -0
- package/build/cjs/qss.js.map +1 -0
- package/build/cjs/redirects.js +25 -0
- package/build/cjs/redirects.js.map +1 -0
- package/build/cjs/route.js +134 -0
- package/build/cjs/route.js.map +1 -0
- package/build/cjs/router.js +1099 -0
- package/build/cjs/router.js.map +1 -0
- package/build/cjs/scroll-restoration.js +179 -0
- package/build/cjs/scroll-restoration.js.map +1 -0
- package/build/cjs/searchParams.js +81 -0
- package/build/cjs/searchParams.js.map +1 -0
- package/build/cjs/useBlocker.js +61 -0
- package/build/cjs/useBlocker.js.map +1 -0
- package/build/cjs/useNavigate.js +75 -0
- package/build/cjs/useNavigate.js.map +1 -0
- package/build/cjs/useParams.js +26 -0
- package/build/cjs/useParams.js.map +1 -0
- package/build/cjs/useSearch.js +25 -0
- package/build/cjs/useSearch.js.map +1 -0
- package/build/cjs/utils.js +239 -0
- package/build/cjs/utils.js.map +1 -0
- package/build/esm/index.js +2149 -2560
- package/build/esm/index.js.map +1 -1
- package/build/stats-html.html +3498 -2694
- package/build/stats-react.json +927 -42
- package/build/types/CatchBoundary.d.ts +33 -0
- package/build/types/Matches.d.ts +57 -0
- package/build/types/RouterProvider.d.ts +41 -0
- package/build/types/awaited.d.ts +9 -0
- package/build/types/defer.d.ts +19 -0
- package/build/types/fileRoute.d.ts +35 -0
- package/build/types/history.d.ts +7 -0
- package/build/types/index.d.ts +27 -108
- package/build/types/injectHtml.d.ts +0 -0
- package/build/types/lazyRouteComponent.d.ts +2 -0
- package/build/types/link.d.ts +105 -0
- package/build/types/location.d.ts +14 -0
- package/build/types/path.d.ts +16 -0
- package/build/types/qss.d.ts +2 -0
- package/build/types/redirects.d.ts +10 -0
- package/build/types/route.d.ts +278 -0
- package/build/types/routeInfo.d.ts +22 -0
- package/build/types/router.d.ts +167 -0
- package/build/types/scroll-restoration.d.ts +6 -0
- package/build/types/searchParams.d.ts +7 -0
- package/build/types/useBlocker.d.ts +8 -0
- package/build/types/useNavigate.d.ts +20 -0
- package/build/types/useParams.d.ts +7 -0
- package/build/types/useSearch.d.ts +7 -0
- package/build/types/utils.d.ts +66 -0
- package/build/umd/index.development.js +2443 -2515
- package/build/umd/index.development.js.map +1 -1
- package/build/umd/index.production.js +4 -4
- package/build/umd/index.production.js.map +1 -1
- package/package.json +9 -10
- package/src/CatchBoundary.tsx +98 -0
- package/src/Matches.tsx +387 -0
- package/src/RouterProvider.tsx +224 -0
- package/src/awaited.tsx +40 -0
- package/src/defer.ts +55 -0
- package/src/fileRoute.ts +154 -0
- package/src/history.ts +8 -0
- package/src/index.tsx +28 -709
- package/src/injectHtml.ts +28 -0
- package/src/lazyRouteComponent.tsx +33 -0
- package/src/link.tsx +508 -0
- package/src/location.ts +15 -0
- package/src/path.ts +256 -0
- package/src/qss.ts +53 -0
- package/src/redirects.ts +31 -0
- package/src/route.ts +861 -0
- package/src/routeInfo.ts +68 -0
- package/src/router.ts +1660 -0
- package/src/scroll-restoration.tsx +192 -0
- package/src/searchParams.ts +79 -0
- package/src/useBlocker.tsx +34 -0
- package/src/useNavigate.tsx +109 -0
- package/src/useParams.tsx +25 -0
- package/src/useSearch.tsx +25 -0
- package/src/utils.ts +350 -0
- package/build/cjs/react-router/src/index.js +0 -473
- package/build/cjs/react-router/src/index.js.map +0 -1
- package/build/cjs/router-core/build/esm/index.js +0 -2527
- package/build/cjs/router-core/build/esm/index.js.map +0 -1
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import * as React from 'react'
|
|
2
|
+
|
|
3
|
+
const useLayoutEffect =
|
|
4
|
+
typeof window !== 'undefined' ? React.useLayoutEffect : React.useEffect
|
|
5
|
+
|
|
6
|
+
import { ParsedLocation } from './location'
|
|
7
|
+
import { useRouter } from './RouterProvider'
|
|
8
|
+
import { NonNullableUpdater, functionalUpdate } from './utils'
|
|
9
|
+
|
|
10
|
+
const windowKey = 'window'
|
|
11
|
+
const delimiter = '___'
|
|
12
|
+
|
|
13
|
+
let weakScrolledElements = new WeakSet<any>()
|
|
14
|
+
|
|
15
|
+
type CacheValue = Record<string, { scrollX: number; scrollY: number }>
|
|
16
|
+
type CacheState = {
|
|
17
|
+
cached: CacheValue
|
|
18
|
+
next: CacheValue
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
type Cache = {
|
|
22
|
+
state: CacheState
|
|
23
|
+
set: (updater: NonNullableUpdater<CacheState>) => void
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
let cache: Cache
|
|
27
|
+
|
|
28
|
+
const sessionsStorage = typeof window !== 'undefined' && window.sessionStorage
|
|
29
|
+
|
|
30
|
+
export type ScrollRestorationOptions = {
|
|
31
|
+
getKey?: (location: ParsedLocation) => string
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const defaultGetKey = (location: ParsedLocation) => location.state.key!
|
|
35
|
+
|
|
36
|
+
export function useScrollRestoration(options?: ScrollRestorationOptions) {
|
|
37
|
+
const router = useRouter()
|
|
38
|
+
|
|
39
|
+
useLayoutEffect(() => {
|
|
40
|
+
const getKey = options?.getKey || defaultGetKey
|
|
41
|
+
|
|
42
|
+
if (sessionsStorage) {
|
|
43
|
+
if (!cache) {
|
|
44
|
+
cache = (() => {
|
|
45
|
+
const storageKey = 'tsr-scroll-restoration-v2'
|
|
46
|
+
|
|
47
|
+
const state: CacheState = JSON.parse(
|
|
48
|
+
window.sessionStorage.getItem(storageKey) || 'null',
|
|
49
|
+
) || { cached: {}, next: {} }
|
|
50
|
+
|
|
51
|
+
return {
|
|
52
|
+
state,
|
|
53
|
+
set: (updater) => {
|
|
54
|
+
cache.state = functionalUpdate(updater, cache.state)
|
|
55
|
+
window.sessionStorage.setItem(
|
|
56
|
+
storageKey,
|
|
57
|
+
JSON.stringify(cache.state),
|
|
58
|
+
)
|
|
59
|
+
},
|
|
60
|
+
}
|
|
61
|
+
})()
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const { history } = window
|
|
66
|
+
if (history.scrollRestoration) {
|
|
67
|
+
history.scrollRestoration = 'manual'
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
const onScroll = (event: Event) => {
|
|
71
|
+
if (weakScrolledElements.has(event.target)) return
|
|
72
|
+
weakScrolledElements.add(event.target)
|
|
73
|
+
|
|
74
|
+
const elementSelector =
|
|
75
|
+
event.target === document || event.target === window
|
|
76
|
+
? windowKey
|
|
77
|
+
: getCssSelector(event.target)
|
|
78
|
+
|
|
79
|
+
if (!cache.state.next[elementSelector]) {
|
|
80
|
+
cache.set((c) => ({
|
|
81
|
+
...c,
|
|
82
|
+
next: {
|
|
83
|
+
...c.next,
|
|
84
|
+
[elementSelector]: {
|
|
85
|
+
scrollX: NaN,
|
|
86
|
+
scrollY: NaN,
|
|
87
|
+
},
|
|
88
|
+
},
|
|
89
|
+
}))
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const getCssSelector = (el: any): string => {
|
|
94
|
+
let path = [],
|
|
95
|
+
parent
|
|
96
|
+
while ((parent = el.parentNode)) {
|
|
97
|
+
path.unshift(
|
|
98
|
+
`${el.tagName}:nth-child(${
|
|
99
|
+
([].indexOf as any).call(parent.children, el) + 1
|
|
100
|
+
})`,
|
|
101
|
+
)
|
|
102
|
+
el = parent
|
|
103
|
+
}
|
|
104
|
+
return `${path.join(' > ')}`.toLowerCase()
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
if (typeof document !== 'undefined') {
|
|
108
|
+
document.addEventListener('scroll', onScroll, true)
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const unsubOnBeforeLoad = router.subscribe('onBeforeLoad', (event) => {
|
|
112
|
+
if (event.pathChanged) {
|
|
113
|
+
const restoreKey = getKey(event.fromLocation)
|
|
114
|
+
for (const elementSelector in cache.state.next) {
|
|
115
|
+
const entry = cache.state.next[elementSelector]!
|
|
116
|
+
if (elementSelector === windowKey) {
|
|
117
|
+
entry.scrollX = window.scrollX || 0
|
|
118
|
+
entry.scrollY = window.scrollY || 0
|
|
119
|
+
} else if (elementSelector) {
|
|
120
|
+
const element = document.querySelector(elementSelector)
|
|
121
|
+
entry.scrollX = element?.scrollLeft || 0
|
|
122
|
+
entry.scrollY = element?.scrollTop || 0
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
cache.set((c) => {
|
|
126
|
+
const next = { ...c.next }
|
|
127
|
+
delete next[elementSelector]
|
|
128
|
+
|
|
129
|
+
return {
|
|
130
|
+
...c,
|
|
131
|
+
next,
|
|
132
|
+
cached: {
|
|
133
|
+
...c.cached,
|
|
134
|
+
[[restoreKey, elementSelector].join(delimiter)]: entry,
|
|
135
|
+
},
|
|
136
|
+
}
|
|
137
|
+
})
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
})
|
|
141
|
+
|
|
142
|
+
const unsubOnResolved = router.subscribe('onResolved', (event) => {
|
|
143
|
+
if (event.pathChanged) {
|
|
144
|
+
if (!router.resetNextScroll) {
|
|
145
|
+
return
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
router.resetNextScroll = true
|
|
149
|
+
|
|
150
|
+
const getKey = options?.getKey || defaultGetKey
|
|
151
|
+
|
|
152
|
+
const restoreKey = getKey(event.toLocation)
|
|
153
|
+
let windowRestored = false
|
|
154
|
+
|
|
155
|
+
for (const cacheKey in cache.state.cached) {
|
|
156
|
+
const entry = cache.state.cached[cacheKey]!
|
|
157
|
+
const [key, elementSelector] = cacheKey.split(delimiter)
|
|
158
|
+
if (key === restoreKey) {
|
|
159
|
+
if (elementSelector === windowKey) {
|
|
160
|
+
windowRestored = true
|
|
161
|
+
window.scrollTo(entry.scrollX, entry.scrollY)
|
|
162
|
+
} else if (elementSelector) {
|
|
163
|
+
const element = document.querySelector(elementSelector)
|
|
164
|
+
if (element) {
|
|
165
|
+
element.scrollLeft = entry.scrollX
|
|
166
|
+
element.scrollTop = entry.scrollY
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
if (!windowRestored) {
|
|
173
|
+
window.scrollTo(0, 0)
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
cache.set((c) => ({ ...c, next: {} }))
|
|
177
|
+
weakScrolledElements = new WeakSet<any>()
|
|
178
|
+
}
|
|
179
|
+
})
|
|
180
|
+
|
|
181
|
+
return () => {
|
|
182
|
+
document.removeEventListener('scroll', onScroll)
|
|
183
|
+
unsubOnBeforeLoad()
|
|
184
|
+
unsubOnResolved()
|
|
185
|
+
}
|
|
186
|
+
}, [])
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
export function ScrollRestoration(props: ScrollRestorationOptions) {
|
|
190
|
+
useScrollRestoration(props)
|
|
191
|
+
return null
|
|
192
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { decode, encode } from './qss'
|
|
2
|
+
import { AnySearchSchema } from './route'
|
|
3
|
+
|
|
4
|
+
export const defaultParseSearch = parseSearchWith(JSON.parse)
|
|
5
|
+
export const defaultStringifySearch = stringifySearchWith(
|
|
6
|
+
JSON.stringify,
|
|
7
|
+
JSON.parse,
|
|
8
|
+
)
|
|
9
|
+
|
|
10
|
+
export function parseSearchWith(parser: (str: string) => any) {
|
|
11
|
+
return (searchStr: string): AnySearchSchema => {
|
|
12
|
+
if (searchStr.substring(0, 1) === '?') {
|
|
13
|
+
searchStr = searchStr.substring(1)
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
let query: Record<string, unknown> = decode(searchStr)
|
|
17
|
+
|
|
18
|
+
// Try to parse any query params that might be json
|
|
19
|
+
for (let key in query) {
|
|
20
|
+
const value = query[key]
|
|
21
|
+
if (typeof value === 'string') {
|
|
22
|
+
try {
|
|
23
|
+
query[key] = parser(value)
|
|
24
|
+
} catch (err) {
|
|
25
|
+
//
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return query
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function stringifySearchWith(
|
|
35
|
+
stringify: (search: any) => string,
|
|
36
|
+
parser?: (str: string) => any,
|
|
37
|
+
) {
|
|
38
|
+
function stringifyValue(val: any) {
|
|
39
|
+
if (typeof val === 'object' && val !== null) {
|
|
40
|
+
try {
|
|
41
|
+
return stringify(val)
|
|
42
|
+
} catch (err) {
|
|
43
|
+
// silent
|
|
44
|
+
}
|
|
45
|
+
} else if (typeof val === 'string' && typeof parser === 'function') {
|
|
46
|
+
try {
|
|
47
|
+
// Check if it's a valid parseable string.
|
|
48
|
+
// If it is, then stringify it again.
|
|
49
|
+
parser(val)
|
|
50
|
+
return stringify(val)
|
|
51
|
+
} catch (err) {
|
|
52
|
+
// silent
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return val
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return (search: Record<string, any>) => {
|
|
59
|
+
search = { ...search }
|
|
60
|
+
|
|
61
|
+
if (search) {
|
|
62
|
+
Object.keys(search).forEach((key) => {
|
|
63
|
+
const val = search[key]
|
|
64
|
+
if (typeof val === 'undefined' || val === undefined) {
|
|
65
|
+
delete search[key]
|
|
66
|
+
} else {
|
|
67
|
+
search[key] = stringifyValue(val)
|
|
68
|
+
}
|
|
69
|
+
})
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
const searchStr = encode(search as Record<string, string>).toString()
|
|
73
|
+
|
|
74
|
+
return searchStr ? `?${searchStr}` : ''
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export type SearchSerializer = (searchObj: Record<string, any>) => string
|
|
79
|
+
export type SearchParser = (searchStr: string) => Record<string, any>
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import * as React from 'react'
|
|
2
|
+
import { ReactNode } from './route'
|
|
3
|
+
import { useRouter } from './RouterProvider'
|
|
4
|
+
|
|
5
|
+
export function useBlocker(
|
|
6
|
+
message: string,
|
|
7
|
+
condition: boolean | any = true,
|
|
8
|
+
): void {
|
|
9
|
+
const { history } = useRouter()
|
|
10
|
+
|
|
11
|
+
React.useEffect(() => {
|
|
12
|
+
if (!condition) return
|
|
13
|
+
|
|
14
|
+
let unblock = history.block((retry, cancel) => {
|
|
15
|
+
if (window.confirm(message)) {
|
|
16
|
+
unblock()
|
|
17
|
+
retry()
|
|
18
|
+
}
|
|
19
|
+
})
|
|
20
|
+
|
|
21
|
+
return unblock
|
|
22
|
+
})
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function Block({ message, condition, children }: PromptProps) {
|
|
26
|
+
useBlocker(message, condition)
|
|
27
|
+
return (children ?? null) as ReactNode
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export type PromptProps = {
|
|
31
|
+
message: string
|
|
32
|
+
condition?: boolean | any
|
|
33
|
+
children?: ReactNode
|
|
34
|
+
}
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import * as React from 'react'
|
|
2
|
+
import { useMatch } from './Matches'
|
|
3
|
+
import { useRouter } from './RouterProvider'
|
|
4
|
+
import { LinkOptions, NavigateOptions } from './link'
|
|
5
|
+
import { AnyRoute } from './route'
|
|
6
|
+
import { RoutePaths } from './routeInfo'
|
|
7
|
+
import { RegisteredRouter } from './router'
|
|
8
|
+
import { useLayoutEffect } from './utils'
|
|
9
|
+
|
|
10
|
+
export function useNavigate<
|
|
11
|
+
TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
|
|
12
|
+
TDefaultFrom extends RoutePaths<TRouteTree> = '/',
|
|
13
|
+
>(defaultOpts?: { from?: TDefaultFrom }) {
|
|
14
|
+
const { navigate } = useRouter()
|
|
15
|
+
const match = useMatch({
|
|
16
|
+
strict: false,
|
|
17
|
+
})
|
|
18
|
+
return React.useCallback(
|
|
19
|
+
<
|
|
20
|
+
TFrom extends RoutePaths<TRouteTree> = TDefaultFrom,
|
|
21
|
+
TTo extends string = '',
|
|
22
|
+
TMaskFrom extends RoutePaths<TRouteTree> = '/',
|
|
23
|
+
TMaskTo extends string = '',
|
|
24
|
+
>(
|
|
25
|
+
opts?: NavigateOptions<TRouteTree, TFrom, TTo, TMaskFrom, TMaskTo>,
|
|
26
|
+
) => {
|
|
27
|
+
return navigate({
|
|
28
|
+
from: opts?.to ? match.pathname : undefined,
|
|
29
|
+
...defaultOpts,
|
|
30
|
+
...(opts as any),
|
|
31
|
+
})
|
|
32
|
+
},
|
|
33
|
+
[],
|
|
34
|
+
)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export function typedNavigate<
|
|
38
|
+
TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
|
|
39
|
+
TDefaultFrom extends RoutePaths<TRouteTree> = '/',
|
|
40
|
+
>(navigate: (opts: NavigateOptions<any>) => Promise<void>) {
|
|
41
|
+
return navigate as <
|
|
42
|
+
TFrom extends RoutePaths<TRouteTree> = TDefaultFrom,
|
|
43
|
+
TTo extends string = '',
|
|
44
|
+
TMaskFrom extends RoutePaths<TRouteTree> = '/',
|
|
45
|
+
TMaskTo extends string = '',
|
|
46
|
+
>(
|
|
47
|
+
opts?: NavigateOptions<TRouteTree, TFrom, TTo, TMaskFrom, TMaskTo>,
|
|
48
|
+
) => Promise<void>
|
|
49
|
+
} //
|
|
50
|
+
|
|
51
|
+
export function Navigate<
|
|
52
|
+
TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
|
|
53
|
+
TFrom extends RoutePaths<TRouteTree> = '/',
|
|
54
|
+
TTo extends string = '',
|
|
55
|
+
TMaskFrom extends RoutePaths<TRouteTree> = '/',
|
|
56
|
+
TMaskTo extends string = '',
|
|
57
|
+
>(props: NavigateOptions<TRouteTree, TFrom, TTo, TMaskFrom, TMaskTo>): null {
|
|
58
|
+
const { navigate } = useRouter()
|
|
59
|
+
const match = useMatch({ strict: false })
|
|
60
|
+
|
|
61
|
+
useLayoutEffect(() => {
|
|
62
|
+
navigate({
|
|
63
|
+
from: props.to ? match.pathname : undefined,
|
|
64
|
+
...props,
|
|
65
|
+
} as any)
|
|
66
|
+
}, [])
|
|
67
|
+
|
|
68
|
+
return null
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export type MakeLinkPropsOptions<
|
|
72
|
+
TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
|
|
73
|
+
TFrom extends RoutePaths<TRouteTree> = '/',
|
|
74
|
+
TTo extends string = '',
|
|
75
|
+
TMaskFrom extends RoutePaths<TRouteTree> = '/',
|
|
76
|
+
TMaskTo extends string = '',
|
|
77
|
+
> = LinkPropsOptions<TRouteTree, TFrom, TTo, TMaskFrom, TMaskTo> &
|
|
78
|
+
React.AnchorHTMLAttributes<HTMLAnchorElement>
|
|
79
|
+
|
|
80
|
+
export type MakeLinkOptions<
|
|
81
|
+
TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
|
|
82
|
+
TFrom extends RoutePaths<TRouteTree> = '/',
|
|
83
|
+
TTo extends string = '',
|
|
84
|
+
TMaskFrom extends RoutePaths<TRouteTree> = '/',
|
|
85
|
+
TMaskTo extends string = '',
|
|
86
|
+
> = LinkPropsOptions<TRouteTree, TFrom, TTo, TMaskFrom, TMaskTo> &
|
|
87
|
+
Omit<React.AnchorHTMLAttributes<HTMLAnchorElement>, 'children'> & {
|
|
88
|
+
// 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
|
|
89
|
+
children?:
|
|
90
|
+
| React.ReactNode
|
|
91
|
+
| ((state: { isActive: boolean }) => React.ReactNode)
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export type LinkPropsOptions<
|
|
95
|
+
TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
|
|
96
|
+
TFrom extends RoutePaths<TRouteTree> = '/',
|
|
97
|
+
TTo extends string = '',
|
|
98
|
+
TMaskFrom extends RoutePaths<TRouteTree> = '/',
|
|
99
|
+
TMaskTo extends string = '',
|
|
100
|
+
> = LinkOptions<TRouteTree, TFrom, TTo, TMaskFrom, TMaskTo> & {
|
|
101
|
+
// 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)
|
|
102
|
+
activeProps?:
|
|
103
|
+
| React.AnchorHTMLAttributes<HTMLAnchorElement>
|
|
104
|
+
| (() => React.AnchorHTMLAttributes<HTMLAnchorElement>)
|
|
105
|
+
// A function that returns additional props for the `inactive` state of this link. These props override other props passed to the link (`style`'s are merged, `className`'s are concatenated)
|
|
106
|
+
inactiveProps?:
|
|
107
|
+
| React.AnchorHTMLAttributes<HTMLAnchorElement>
|
|
108
|
+
| (() => React.AnchorHTMLAttributes<HTMLAnchorElement>)
|
|
109
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { AnyRoute } from './route'
|
|
2
|
+
import { RouteIds, RouteById, AllParams } from './routeInfo'
|
|
3
|
+
import { RegisteredRouter } from './router'
|
|
4
|
+
import { last } from './utils'
|
|
5
|
+
import { useRouterState } from './RouterProvider'
|
|
6
|
+
import { StrictOrFrom } from './utils'
|
|
7
|
+
|
|
8
|
+
export function useParams<
|
|
9
|
+
TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
|
|
10
|
+
TFrom extends RouteIds<TRouteTree> = RouteIds<TRouteTree>,
|
|
11
|
+
TDefaultSelected = AllParams<TRouteTree> &
|
|
12
|
+
RouteById<TRouteTree, TFrom>['types']['allParams'],
|
|
13
|
+
TSelected = TDefaultSelected,
|
|
14
|
+
>(
|
|
15
|
+
opts: StrictOrFrom<TFrom> & {
|
|
16
|
+
select?: (search: TDefaultSelected) => TSelected
|
|
17
|
+
},
|
|
18
|
+
): TSelected {
|
|
19
|
+
return useRouterState({
|
|
20
|
+
select: (state: any) => {
|
|
21
|
+
const params = (last(state.matches) as any)?.params
|
|
22
|
+
return opts?.select ? opts.select(params) : params
|
|
23
|
+
},
|
|
24
|
+
})
|
|
25
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { AnyRoute } from './route'
|
|
2
|
+
import { RouteIds, RouteById } from './routeInfo'
|
|
3
|
+
import { RegisteredRouter } from './router'
|
|
4
|
+
import { RouteMatch } from './Matches'
|
|
5
|
+
import { useMatch } from './Matches'
|
|
6
|
+
import { StrictOrFrom } from './utils'
|
|
7
|
+
|
|
8
|
+
export function useSearch<
|
|
9
|
+
TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
|
|
10
|
+
TFrom extends RouteIds<TRouteTree> = RouteIds<TRouteTree>,
|
|
11
|
+
TStrict extends boolean = true,
|
|
12
|
+
TSearch = RouteById<TRouteTree, TFrom>['types']['fullSearchSchema'],
|
|
13
|
+
TSelected = TSearch,
|
|
14
|
+
>(
|
|
15
|
+
opts: StrictOrFrom<TFrom> & {
|
|
16
|
+
select?: (search: TSearch) => TSelected
|
|
17
|
+
},
|
|
18
|
+
): TStrict extends true ? TSelected : TSelected | undefined {
|
|
19
|
+
return useMatch({
|
|
20
|
+
...(opts as any),
|
|
21
|
+
select: (match: RouteMatch) => {
|
|
22
|
+
return opts?.select ? opts.select(match.search as TSearch) : match.search
|
|
23
|
+
},
|
|
24
|
+
})
|
|
25
|
+
}
|