@tanstack/react-router 0.0.1-beta.209 → 0.0.1-beta.210

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 (72) hide show
  1. package/build/cjs/CatchBoundary.js +125 -0
  2. package/build/cjs/CatchBoundary.js.map +1 -0
  3. package/build/cjs/Matches.js +223 -0
  4. package/build/cjs/Matches.js.map +1 -0
  5. package/build/cjs/RouterProvider.js +99 -53
  6. package/build/cjs/RouterProvider.js.map +1 -1
  7. package/build/cjs/index.js +46 -37
  8. package/build/cjs/index.js.map +1 -1
  9. package/build/cjs/lazyRouteComponent.js +57 -0
  10. package/build/cjs/lazyRouteComponent.js.map +1 -0
  11. package/build/cjs/link.js +150 -0
  12. package/build/cjs/link.js.map +1 -0
  13. package/build/cjs/route.js +9 -5
  14. package/build/cjs/route.js.map +1 -1
  15. package/build/cjs/router.js.map +1 -1
  16. package/build/cjs/searchParams.js.map +1 -1
  17. package/build/cjs/useBlocker.js +64 -0
  18. package/build/cjs/useBlocker.js.map +1 -0
  19. package/build/cjs/useNavigate.js +78 -0
  20. package/build/cjs/useNavigate.js.map +1 -0
  21. package/build/cjs/useParams.js +28 -0
  22. package/build/cjs/useParams.js.map +1 -0
  23. package/build/cjs/useSearch.js +27 -0
  24. package/build/cjs/useSearch.js.map +1 -0
  25. package/build/cjs/utils.js +30 -1
  26. package/build/cjs/utils.js.map +1 -1
  27. package/build/esm/index.js +491 -514
  28. package/build/esm/index.js.map +1 -1
  29. package/build/stats-html.html +1 -1
  30. package/build/stats-react.json +484 -208
  31. package/build/types/CatchBoundary.d.ts +33 -0
  32. package/build/types/Matches.d.ts +31 -0
  33. package/build/types/RouterProvider.d.ts +42 -18
  34. package/build/types/fileRoute.d.ts +7 -7
  35. package/build/types/index.d.ts +13 -7
  36. package/build/types/injectHtml.d.ts +0 -0
  37. package/build/types/lazyRouteComponent.d.ts +2 -0
  38. package/build/types/link.d.ts +10 -3
  39. package/build/types/route.d.ts +39 -7
  40. package/build/types/router.d.ts +6 -7
  41. package/build/types/useBlocker.d.ts +8 -0
  42. package/build/types/useNavigate.d.ts +20 -0
  43. package/build/types/useParams.d.ts +7 -0
  44. package/build/types/useSearch.d.ts +7 -0
  45. package/build/types/utils.d.ts +17 -0
  46. package/build/umd/index.development.js +492 -513
  47. package/build/umd/index.development.js.map +1 -1
  48. package/build/umd/index.production.js +1 -1
  49. package/build/umd/index.production.js.map +1 -1
  50. package/package.json +2 -2
  51. package/src/CatchBoundary.tsx +97 -0
  52. package/src/Matches.tsx +315 -0
  53. package/src/RouterProvider.tsx +317 -251
  54. package/src/index.tsx +17 -8
  55. package/src/injectHtml.ts +28 -0
  56. package/src/lazyRouteComponent.tsx +33 -0
  57. package/src/{link.ts → link.tsx} +163 -3
  58. package/src/location.ts +1 -0
  59. package/src/route.ts +86 -16
  60. package/src/router.ts +6 -7
  61. package/src/searchParams.ts +1 -0
  62. package/src/useBlocker.tsx +34 -0
  63. package/src/useNavigate.tsx +109 -0
  64. package/src/useParams.tsx +25 -0
  65. package/src/useSearch.tsx +25 -0
  66. package/src/utils.ts +83 -3
  67. package/build/cjs/react.js +0 -620
  68. package/build/cjs/react.js.map +0 -1
  69. package/build/types/RouteMatch.d.ts +0 -23
  70. package/build/types/react.d.ts +0 -141
  71. package/src/RouteMatch.ts +0 -28
  72. package/src/react.tsx +0 -1013
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tanstack/react-router",
3
3
  "author": "Tanner Linsley",
4
- "version": "0.0.1-beta.209",
4
+ "version": "0.0.1-beta.210",
5
5
  "license": "MIT",
6
6
  "repository": "tanstack/router",
7
7
  "homepage": "https://tanstack.com/router",
@@ -42,7 +42,7 @@
42
42
  "@babel/runtime": "^7.16.7",
43
43
  "tiny-invariant": "^1.3.1",
44
44
  "tiny-warning": "^1.0.3",
45
- "@tanstack/history": "0.0.1-beta.209"
45
+ "@tanstack/history": "0.0.1-beta.210"
46
46
  },
47
47
  "scripts": {
48
48
  "build": "rollup --config rollup.config.js"
@@ -0,0 +1,97 @@
1
+ import * as React from 'react'
2
+
3
+ export function CatchBoundary(props: {
4
+ resetKey: string
5
+ children: any
6
+ errorComponent?: any
7
+ onCatch: (error: any) => void
8
+ }) {
9
+ const errorComponent = props.errorComponent ?? ErrorComponent
10
+
11
+ return (
12
+ <CatchBoundaryImpl
13
+ resetKey={props.resetKey}
14
+ onCatch={props.onCatch}
15
+ children={({ error }) => {
16
+ if (error) {
17
+ return React.createElement(errorComponent, {
18
+ error,
19
+ })
20
+ }
21
+
22
+ return props.children
23
+ }}
24
+ />
25
+ )
26
+ }
27
+
28
+ export class CatchBoundaryImpl extends React.Component<{
29
+ resetKey: string
30
+ children: (props: { error: any; reset: () => void }) => any
31
+ onCatch?: (error: any) => void
32
+ }> {
33
+ state = { error: null } as any
34
+ static getDerivedStateFromError(error: any) {
35
+ return { error }
36
+ }
37
+ componentDidUpdate(
38
+ prevProps: Readonly<{
39
+ resetKey: string
40
+ children: (props: { error: any; reset: () => void }) => any
41
+ onCatch?: ((error: any, info: any) => void) | undefined
42
+ }>,
43
+ prevState: any,
44
+ ): void {
45
+ if (prevState.error && prevProps.resetKey !== this.props.resetKey) {
46
+ this.setState({ error: null })
47
+ }
48
+ }
49
+ componentDidCatch(error: any) {
50
+ this.props.onCatch?.(error)
51
+ }
52
+ render() {
53
+ return this.props.children(this.state)
54
+ }
55
+ }
56
+
57
+ export function ErrorComponent({ error }: { error: any }) {
58
+ const [show, setShow] = React.useState(process.env.NODE_ENV !== 'production')
59
+
60
+ return (
61
+ <div style={{ padding: '.5rem', maxWidth: '100%' }}>
62
+ <div style={{ display: 'flex', alignItems: 'center', gap: '.5rem' }}>
63
+ <strong style={{ fontSize: '1rem' }}>Something went wrong!</strong>
64
+ <button
65
+ style={{
66
+ appearance: 'none',
67
+ fontSize: '.6em',
68
+ border: '1px solid currentColor',
69
+ padding: '.1rem .2rem',
70
+ fontWeight: 'bold',
71
+ borderRadius: '.25rem',
72
+ }}
73
+ onClick={() => setShow((d) => !d)}
74
+ >
75
+ {show ? 'Hide Error' : 'Show Error'}
76
+ </button>
77
+ </div>
78
+ <div style={{ height: '.25rem' }} />
79
+ {show ? (
80
+ <div>
81
+ <pre
82
+ style={{
83
+ fontSize: '.7em',
84
+ border: '1px solid red',
85
+ borderRadius: '.25rem',
86
+ padding: '.3rem',
87
+ color: 'red',
88
+ overflow: 'auto',
89
+ }}
90
+ >
91
+ {error.message ? <code>{error.message}</code> : null}
92
+ </pre>
93
+ </div>
94
+ ) : null}
95
+ </div>
96
+ )
97
+ }
@@ -0,0 +1,315 @@
1
+ import * as React from 'react'
2
+ import invariant from 'tiny-invariant'
3
+ import warning from 'tiny-warning'
4
+ import { CatchBoundary, ErrorComponent } from './CatchBoundary'
5
+ import { RouteMatch } from './RouterProvider'
6
+ import { useRouter, useRouterState } from './RouterProvider'
7
+ import { ResolveRelativePath, ToOptions } from './link'
8
+ import { AnyRoute, ReactNode, rootRouteId } from './route'
9
+ import { RouteById, RouteByPath, RouteIds, RoutePaths } from './routeInfo'
10
+ import { RegisteredRouter } from './router'
11
+ import { NoInfer, StrictOrFrom } from './utils'
12
+
13
+ export function Matches() {
14
+ const { routesById, state } = useRouter()
15
+ const { matches } = state
16
+
17
+ const locationKey = useRouterState().location.state.key
18
+
19
+ const route = routesById[rootRouteId]
20
+
21
+ const errorComponent = React.useCallback(
22
+ (props: any) => {
23
+ return React.createElement(ErrorComponent, {
24
+ ...props,
25
+ useMatch: route.useMatch,
26
+ useRouteContext: route.useRouteContext,
27
+ useSearch: route.useSearch,
28
+ useParams: route.useParams,
29
+ })
30
+ },
31
+ [route],
32
+ )
33
+
34
+ return (
35
+ <matchesContext.Provider value={matches}>
36
+ <CatchBoundary
37
+ resetKey={locationKey}
38
+ errorComponent={errorComponent}
39
+ onCatch={() => {
40
+ warning(
41
+ false,
42
+ `Error in router! Consider setting an 'errorComponent' in your RootRoute! 👍`,
43
+ )
44
+ }}
45
+ >
46
+ {matches.length ? <Match matches={matches} /> : null}
47
+ </CatchBoundary>
48
+ </matchesContext.Provider>
49
+ )
50
+ }
51
+
52
+ const defaultPending = () => null
53
+ function SafeFragment(props: any) {
54
+ return <>{props.children}</>
55
+ }
56
+
57
+ export function Match({ matches }: { matches: RouteMatch[] }) {
58
+ const { options, routesById } = useRouter()
59
+ const match = matches[0]!
60
+ const routeId = match?.routeId
61
+ const route = routesById[routeId]
62
+ const locationKey = useRouterState().location.state?.key
63
+
64
+ const PendingComponent = (route.options.pendingComponent ??
65
+ options.defaultPendingComponent ??
66
+ defaultPending) as any
67
+
68
+ const routeErrorComponent =
69
+ route.options.errorComponent ??
70
+ options.defaultErrorComponent ??
71
+ ErrorComponent
72
+
73
+ const ResolvedSuspenseBoundary =
74
+ route.options.wrapInSuspense ?? React.Suspense
75
+ // const ResolvedSuspenseBoundary = SafeFragment
76
+
77
+ const errorComponent = React.useCallback(
78
+ (props: any) => {
79
+ return React.createElement(routeErrorComponent, {
80
+ ...props,
81
+ useMatch: route.useMatch,
82
+ useRouteContext: route.useRouteContext,
83
+ useSearch: route.useSearch,
84
+ useParams: route.useParams,
85
+ })
86
+ },
87
+ [route],
88
+ )
89
+
90
+ return (
91
+ <matchesContext.Provider value={matches}>
92
+ <ResolvedSuspenseBoundary
93
+ fallback={React.createElement(PendingComponent, {
94
+ useMatch: route.useMatch,
95
+ useRouteContext: route.useRouteContext,
96
+ useSearch: route.useSearch,
97
+ useParams: route.useParams,
98
+ })}
99
+ >
100
+ <CatchBoundary
101
+ resetKey={locationKey}
102
+ errorComponent={errorComponent}
103
+ onCatch={() => {
104
+ warning(false, `Error in route match: ${match.id}`)
105
+ }}
106
+ >
107
+ <MatchInner match={match} />
108
+ </CatchBoundary>
109
+ </ResolvedSuspenseBoundary>
110
+ </matchesContext.Provider>
111
+ )
112
+ }
113
+ function MatchInner({ match }: { match: RouteMatch }): any {
114
+ const { options, routesById } = useRouter()
115
+ const route = routesById[match.routeId]
116
+
117
+ if (match.status === 'error') {
118
+ throw match.error
119
+ }
120
+
121
+ if (match.status === 'pending') {
122
+ throw match.loadPromise
123
+ }
124
+
125
+ if (match.status === 'success') {
126
+ let comp = route.options.component ?? options.defaultComponent
127
+
128
+ if (comp) {
129
+ return React.createElement(comp, {
130
+ useMatch: route.useMatch,
131
+ useRouteContext: route.useRouteContext as any,
132
+ useSearch: route.useSearch,
133
+ useParams: route.useParams as any,
134
+ } as any)
135
+ }
136
+
137
+ return <Outlet />
138
+ }
139
+
140
+ invariant(
141
+ false,
142
+ 'Idle routeMatch status encountered during rendering! You should never see this. File an issue!',
143
+ )
144
+ }
145
+
146
+ export function Outlet() {
147
+ const matches = React.useContext(matchesContext).slice(1)
148
+
149
+ if (!matches[0]) {
150
+ return null
151
+ }
152
+
153
+ return <Match matches={matches} />
154
+ }
155
+
156
+ export interface MatchRouteOptions {
157
+ pending?: boolean
158
+ caseSensitive?: boolean
159
+ includeSearch?: boolean
160
+ fuzzy?: boolean
161
+ }
162
+
163
+ export type MakeUseMatchRouteOptions<
164
+ TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
165
+ TFrom extends RoutePaths<TRouteTree> = '/',
166
+ TTo extends string = '',
167
+ TMaskFrom extends RoutePaths<TRouteTree> = '/',
168
+ TMaskTo extends string = '',
169
+ > = ToOptions<AnyRoute, TFrom, TTo, TMaskFrom, TMaskTo> & MatchRouteOptions
170
+
171
+ export function useMatchRoute<
172
+ TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
173
+ >() {
174
+ const { matchRoute } = useRouter()
175
+
176
+ return React.useCallback(
177
+ <
178
+ TFrom extends RoutePaths<TRouteTree> = '/',
179
+ TTo extends string = '',
180
+ TMaskFrom extends RoutePaths<TRouteTree> = '/',
181
+ TMaskTo extends string = '',
182
+ TResolved extends string = ResolveRelativePath<TFrom, NoInfer<TTo>>,
183
+ >(
184
+ opts: MakeUseMatchRouteOptions<
185
+ TRouteTree,
186
+ TFrom,
187
+ TTo,
188
+ TMaskFrom,
189
+ TMaskTo
190
+ >,
191
+ ): false | RouteById<TRouteTree, TResolved>['types']['allParams'] => {
192
+ const { pending, caseSensitive, ...rest } = opts
193
+
194
+ return matchRoute(rest as any, {
195
+ pending,
196
+ caseSensitive,
197
+ })
198
+ },
199
+ [],
200
+ )
201
+ }
202
+
203
+ export type MakeMatchRouteOptions<
204
+ TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
205
+ TFrom extends RoutePaths<TRouteTree> = '/',
206
+ TTo extends string = '',
207
+ TMaskFrom extends RoutePaths<TRouteTree> = '/',
208
+ TMaskTo extends string = '',
209
+ > = ToOptions<TRouteTree, TFrom, TTo, TMaskFrom, TMaskTo> &
210
+ MatchRouteOptions & {
211
+ // 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
212
+ children?:
213
+ | ((
214
+ params?: RouteByPath<
215
+ TRouteTree,
216
+ ResolveRelativePath<TFrom, NoInfer<TTo>>
217
+ >['types']['allParams'],
218
+ ) => ReactNode)
219
+ | React.ReactNode
220
+ }
221
+
222
+ export function MatchRoute<
223
+ TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
224
+ TFrom extends RoutePaths<TRouteTree> = '/',
225
+ TTo extends string = '',
226
+ TMaskFrom extends RoutePaths<TRouteTree> = '/',
227
+ TMaskTo extends string = '',
228
+ >(
229
+ props: MakeMatchRouteOptions<TRouteTree, TFrom, TTo, TMaskFrom, TMaskTo>,
230
+ ): any {
231
+ const matchRoute = useMatchRoute()
232
+ const params = matchRoute(props as any)
233
+
234
+ if (typeof props.children === 'function') {
235
+ return (props.children as any)(params)
236
+ }
237
+
238
+ return !!params ? props.children : null
239
+ }
240
+
241
+ export function useMatch<
242
+ TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
243
+ TFrom extends RouteIds<TRouteTree> = RouteIds<TRouteTree>,
244
+ TStrict extends boolean = true,
245
+ TRouteMatchState = RouteMatch<TRouteTree, TFrom>,
246
+ TSelected = TRouteMatchState,
247
+ >(
248
+ opts: StrictOrFrom<TFrom> & {
249
+ select?: (match: TRouteMatchState) => TSelected
250
+ },
251
+ ): TStrict extends true ? TRouteMatchState : TRouteMatchState | undefined {
252
+ const nearestMatch = React.useContext(matchesContext)[0]!
253
+ const nearestMatchRouteId = nearestMatch?.routeId
254
+
255
+ const matchRouteId = useRouterState({
256
+ select: (state) => {
257
+ const match = opts?.from
258
+ ? state.matches.find((d) => d.routeId === opts?.from)
259
+ : state.matches.find((d) => d.id === nearestMatch.id)
260
+
261
+ return match!.routeId
262
+ },
263
+ })
264
+
265
+ if (opts?.strict ?? true) {
266
+ invariant(
267
+ nearestMatchRouteId == matchRouteId,
268
+ `useMatch("${
269
+ matchRouteId as string
270
+ }") is being called in a component that is meant to render the '${nearestMatchRouteId}' route. Did you mean to 'useMatch("${
271
+ matchRouteId as string
272
+ }", { strict: false })' or 'useRoute("${
273
+ matchRouteId as string
274
+ }")' instead?`,
275
+ )
276
+ }
277
+
278
+ const matchSelection = useRouterState({
279
+ select: (state) => {
280
+ const match = opts?.from
281
+ ? state.matches.find((d) => d.routeId === opts?.from)
282
+ : state.matches.find((d) => d.id === nearestMatch.id)
283
+
284
+ invariant(
285
+ match,
286
+ `Could not find ${
287
+ opts?.from
288
+ ? `an active match from "${opts.from}"`
289
+ : 'a nearest match!'
290
+ }`,
291
+ )
292
+
293
+ return opts?.select ? opts.select(match as any) : match
294
+ },
295
+ })
296
+
297
+ return matchSelection as any
298
+ }
299
+
300
+ export const matchesContext = React.createContext<RouteMatch[]>(null!)
301
+
302
+ export function useMatches<T = RouteMatch[]>(opts?: {
303
+ select?: (matches: RouteMatch[]) => T
304
+ }): T {
305
+ const contextMatches = React.useContext(matchesContext)
306
+
307
+ return useRouterState({
308
+ select: (state) => {
309
+ const matches = state.matches.slice(
310
+ state.matches.findIndex((d) => d.id === contextMatches[0]?.id),
311
+ )
312
+ return opts?.select ? opts.select(matches) : (matches as T)
313
+ },
314
+ })
315
+ }