@tanstack/react-router 0.0.1-beta.28 → 0.0.1-beta.280

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 (106) hide show
  1. package/LICENSE +21 -0
  2. package/build/cjs/CatchBoundary.js +128 -0
  3. package/build/cjs/CatchBoundary.js.map +1 -0
  4. package/build/cjs/Matches.js +233 -0
  5. package/build/cjs/Matches.js.map +1 -0
  6. package/build/cjs/RouterProvider.js +170 -0
  7. package/build/cjs/RouterProvider.js.map +1 -0
  8. package/build/cjs/_virtual/_rollupPluginBabelHelpers.js +2 -22
  9. package/build/cjs/_virtual/_rollupPluginBabelHelpers.js.map +1 -1
  10. package/build/cjs/awaited.js +43 -0
  11. package/build/cjs/awaited.js.map +1 -0
  12. package/build/cjs/defer.js +37 -0
  13. package/build/cjs/defer.js.map +1 -0
  14. package/build/cjs/fileRoute.js +27 -0
  15. package/build/cjs/fileRoute.js.map +1 -0
  16. package/build/cjs/index.js +130 -0
  17. package/build/cjs/index.js.map +1 -0
  18. package/build/cjs/lazyRouteComponent.js +54 -0
  19. package/build/cjs/lazyRouteComponent.js.map +1 -0
  20. package/build/cjs/link.js +223 -0
  21. package/build/cjs/link.js.map +1 -0
  22. package/build/cjs/path.js +214 -0
  23. package/build/cjs/path.js.map +1 -0
  24. package/build/cjs/qss.js +63 -0
  25. package/build/cjs/qss.js.map +1 -0
  26. package/build/cjs/redirects.js +28 -0
  27. package/build/cjs/redirects.js.map +1 -0
  28. package/build/cjs/route.js +191 -0
  29. package/build/cjs/route.js.map +1 -0
  30. package/build/cjs/router.js +1049 -0
  31. package/build/cjs/router.js.map +1 -0
  32. package/build/cjs/scroll-restoration.js +202 -0
  33. package/build/cjs/scroll-restoration.js.map +1 -0
  34. package/build/cjs/searchParams.js +81 -0
  35. package/build/cjs/searchParams.js.map +1 -0
  36. package/build/cjs/useBlocker.js +55 -0
  37. package/build/cjs/useBlocker.js.map +1 -0
  38. package/build/cjs/useNavigate.js +86 -0
  39. package/build/cjs/useNavigate.js.map +1 -0
  40. package/build/cjs/useParams.js +26 -0
  41. package/build/cjs/useParams.js.map +1 -0
  42. package/build/cjs/useSearch.js +25 -0
  43. package/build/cjs/useSearch.js.map +1 -0
  44. package/build/cjs/utils.js +239 -0
  45. package/build/cjs/utils.js.map +1 -0
  46. package/build/esm/index.js +2255 -2571
  47. package/build/esm/index.js.map +1 -1
  48. package/build/stats-html.html +3498 -2694
  49. package/build/stats-react.json +1204 -44
  50. package/build/types/CatchBoundary.d.ts +36 -0
  51. package/build/types/Matches.d.ts +62 -0
  52. package/build/types/RouterProvider.d.ts +35 -0
  53. package/build/types/awaited.d.ts +9 -0
  54. package/build/types/defer.d.ts +19 -0
  55. package/build/types/fileRoute.d.ts +38 -0
  56. package/build/types/history.d.ts +7 -0
  57. package/build/types/index.d.ts +27 -114
  58. package/build/types/lazyRouteComponent.d.ts +2 -0
  59. package/build/types/link.d.ts +91 -0
  60. package/build/types/location.d.ts +12 -0
  61. package/build/types/path.d.ts +17 -0
  62. package/build/types/qss.d.ts +2 -0
  63. package/build/types/redirects.d.ts +11 -0
  64. package/build/types/route.d.ts +282 -0
  65. package/build/types/routeInfo.d.ts +22 -0
  66. package/build/types/router.d.ts +188 -0
  67. package/build/types/scroll-restoration.d.ts +18 -0
  68. package/build/types/searchParams.d.ts +7 -0
  69. package/build/types/useBlocker.d.ts +9 -0
  70. package/build/types/useNavigate.d.ts +19 -0
  71. package/build/types/useParams.d.ts +7 -0
  72. package/build/types/useSearch.d.ts +7 -0
  73. package/build/types/utils.d.ts +66 -0
  74. package/build/umd/index.development.js +2858 -2548
  75. package/build/umd/index.development.js.map +1 -1
  76. package/build/umd/index.production.js +4 -4
  77. package/build/umd/index.production.js.map +1 -1
  78. package/package.json +11 -10
  79. package/src/CatchBoundary.tsx +101 -0
  80. package/src/Matches.tsx +421 -0
  81. package/src/RouterProvider.tsx +252 -0
  82. package/src/awaited.tsx +40 -0
  83. package/src/defer.ts +55 -0
  84. package/src/fileRoute.ts +152 -0
  85. package/src/history.ts +8 -0
  86. package/src/index.tsx +28 -761
  87. package/src/lazyRouteComponent.tsx +33 -0
  88. package/src/link.tsx +598 -0
  89. package/src/location.ts +13 -0
  90. package/src/path.ts +261 -0
  91. package/src/qss.ts +53 -0
  92. package/src/redirects.ts +39 -0
  93. package/src/route.ts +872 -0
  94. package/src/routeInfo.ts +70 -0
  95. package/src/router.ts +1630 -0
  96. package/src/scroll-restoration.tsx +230 -0
  97. package/src/searchParams.ts +79 -0
  98. package/src/useBlocker.tsx +27 -0
  99. package/src/useNavigate.tsx +111 -0
  100. package/src/useParams.tsx +25 -0
  101. package/src/useSearch.tsx +25 -0
  102. package/src/utils.ts +350 -0
  103. package/build/cjs/react-router/src/index.js +0 -508
  104. package/build/cjs/react-router/src/index.js.map +0 -1
  105. package/build/cjs/router-core/build/esm/index.js +0 -2530
  106. package/build/cjs/router-core/build/esm/index.js.map +0 -1
package/src/utils.ts ADDED
@@ -0,0 +1,350 @@
1
+ import * as React from 'react'
2
+ import { useMatch } from './Matches'
3
+ import { RouteMatch } from './Matches'
4
+ import { AnyRoute } from './route'
5
+ import { ParseRoute, RouteIds, RoutesById, RouteById } from './routeInfo'
6
+ import { RegisteredRouter } from './router'
7
+
8
+ export type NoInfer<T> = [T][T extends any ? 0 : never]
9
+ export type IsAny<T, Y, N = T> = 1 extends 0 & T ? Y : N
10
+ export type IsAnyBoolean<T> = 1 extends 0 & T ? true : false
11
+ export type IsKnown<T, Y, N> = unknown extends T ? N : Y
12
+ export type PickAsRequired<T, K extends keyof T> = Omit<T, K> &
13
+ Required<Pick<T, K>>
14
+ export type PickAsPartial<T, K extends keyof T> = Omit<T, K> &
15
+ Partial<Pick<T, K>>
16
+ export type PickUnsafe<T, K> = K extends keyof T ? Pick<T, K> : never
17
+ export type PickExtra<T, K> = {
18
+ [TKey in keyof K as string extends TKey
19
+ ? never
20
+ : TKey extends keyof T
21
+ ? never
22
+ : TKey]: K[TKey]
23
+ }
24
+
25
+ export type PickRequired<T> = {
26
+ [K in keyof T as undefined extends T[K] ? never : K]: T[K]
27
+ }
28
+
29
+ // export type Expand<T> = T
30
+ export type Expand<T> = T extends object
31
+ ? T extends infer O
32
+ ? { [K in keyof O]: O[K] }
33
+ : never
34
+ : T
35
+
36
+ export type UnionToIntersection<U> = (
37
+ U extends any ? (k: U) => void : never
38
+ ) extends (k: infer I) => any
39
+ ? I
40
+ : never
41
+
42
+ // type Compute<T> = { [K in keyof T]: T[K] } | never
43
+
44
+ // type AllKeys<T> = T extends any ? keyof T : never
45
+
46
+ // export type MergeUnion<T, Keys extends keyof T = keyof T> = Compute<
47
+ // {
48
+ // [K in Keys]: T[Keys]
49
+ // } & {
50
+ // [K in AllKeys<T>]?: T extends any
51
+ // ? K extends keyof T
52
+ // ? T[K]
53
+ // : never
54
+ // : never
55
+ // }
56
+ // >
57
+
58
+ export type Assign<Left, Right> = Omit<Left, keyof Right> & Right
59
+
60
+ export type AssignAll<T extends any[]> = T extends [infer Left, ...infer Right]
61
+ ? Right extends any[]
62
+ ? Assign<Left, AssignAll<Right>>
63
+ : Left
64
+ : {}
65
+
66
+ // // Sample types to merge
67
+ // type TypeA = {
68
+ // shared: string
69
+ // onlyInA: string
70
+ // nested: {
71
+ // shared: string
72
+ // aProp: string
73
+ // }
74
+ // array: string[]
75
+ // }
76
+
77
+ // type TypeB = {
78
+ // shared: number
79
+ // onlyInB: number
80
+ // nested: {
81
+ // shared: number
82
+ // bProp: number
83
+ // }
84
+ // array: number[]
85
+ // }
86
+
87
+ // type TypeC = {
88
+ // shared: boolean
89
+ // onlyInC: boolean
90
+ // nested: {
91
+ // shared: boolean
92
+ // cProp: boolean
93
+ // }
94
+ // array: boolean[]
95
+ // }
96
+
97
+ // type Test = Expand<Assign<TypeA, TypeB>>
98
+
99
+ // // Using DeepMerge to merge TypeA and TypeB
100
+ // type MergedType = Expand<AssignAll<[TypeA, TypeB, TypeC]>>
101
+
102
+ export type Values<O> = O[ValueKeys<O>]
103
+ export type ValueKeys<O> = Extract<keyof O, PropertyKey>
104
+
105
+ export type DeepAwaited<T> = T extends Promise<infer A>
106
+ ? DeepAwaited<A>
107
+ : T extends Record<infer A, Promise<infer B>>
108
+ ? { [K in A]: DeepAwaited<B> }
109
+ : T
110
+
111
+ export type PathParamMask<TRoutePath extends string> =
112
+ TRoutePath extends `${infer L}/$${infer C}/${infer R}`
113
+ ? PathParamMask<`${L}/${string}/${R}`>
114
+ : TRoutePath extends `${infer L}/$${infer C}`
115
+ ? PathParamMask<`${L}/${string}`>
116
+ : TRoutePath
117
+
118
+ export type Timeout = ReturnType<typeof setTimeout>
119
+
120
+ export type Updater<TPrevious, TResult = TPrevious> =
121
+ | TResult
122
+ | ((prev?: TPrevious) => TResult)
123
+
124
+ export type NonNullableUpdater<TPrevious, TResult = TPrevious> =
125
+ | TResult
126
+ | ((prev: TPrevious) => TResult)
127
+
128
+ export type PickExtract<T, U> = {
129
+ [K in keyof T as T[K] extends U ? K : never]: T[K]
130
+ }
131
+
132
+ export type PickExclude<T, U> = {
133
+ [K in keyof T as T[K] extends U ? never : K]: T[K]
134
+ }
135
+
136
+ //
137
+
138
+ export const isServer = typeof document === 'undefined'
139
+
140
+ export function last<T>(arr: T[]) {
141
+ return arr[arr.length - 1]
142
+ }
143
+
144
+ function isFunction(d: any): d is Function {
145
+ return typeof d === 'function'
146
+ }
147
+
148
+ export function functionalUpdate<TResult>(
149
+ updater: Updater<TResult> | NonNullableUpdater<TResult>,
150
+ previous: TResult,
151
+ ): TResult {
152
+ if (isFunction(updater)) {
153
+ return updater(previous as TResult)
154
+ }
155
+
156
+ return updater
157
+ }
158
+
159
+ export function pick<T, K extends keyof T>(parent: T, keys: K[]): Pick<T, K> {
160
+ return keys.reduce((obj: any, key: K) => {
161
+ obj[key] = parent[key]
162
+ return obj
163
+ }, {} as any)
164
+ }
165
+
166
+ /**
167
+ * This function returns `a` if `b` is deeply equal.
168
+ * If not, it will replace any deeply equal children of `b` with those of `a`.
169
+ * This can be used for structural sharing between immutable JSON values for example.
170
+ * Do not use this with signals
171
+ */
172
+ export function replaceEqualDeep<T>(prev: any, _next: T): T {
173
+ if (prev === _next) {
174
+ return prev
175
+ }
176
+
177
+ const next = _next as any
178
+
179
+ const array = Array.isArray(prev) && Array.isArray(next)
180
+
181
+ if (array || (isPlainObject(prev) && isPlainObject(next))) {
182
+ const prevSize = array ? prev.length : Object.keys(prev).length
183
+ const nextItems = array ? next : Object.keys(next)
184
+ const nextSize = nextItems.length
185
+ const copy: any = array ? [] : {}
186
+
187
+ let equalItems = 0
188
+
189
+ for (let i = 0; i < nextSize; i++) {
190
+ const key = array ? i : nextItems[i]
191
+ copy[key] = replaceEqualDeep(prev[key], next[key])
192
+ if (copy[key] === prev[key]) {
193
+ equalItems++
194
+ }
195
+ }
196
+
197
+ return prevSize === nextSize && equalItems === prevSize ? prev : copy
198
+ }
199
+
200
+ return next
201
+ }
202
+
203
+ // Copied from: https://github.com/jonschlinkert/is-plain-object
204
+ export function isPlainObject(o: any) {
205
+ if (!hasObjectPrototype(o)) {
206
+ return false
207
+ }
208
+
209
+ // If has modified constructor
210
+ const ctor = o.constructor
211
+ if (typeof ctor === 'undefined') {
212
+ return true
213
+ }
214
+
215
+ // If has modified prototype
216
+ const prot = ctor.prototype
217
+ if (!hasObjectPrototype(prot)) {
218
+ return false
219
+ }
220
+
221
+ // If constructor does not have an Object-specific method
222
+ if (!prot.hasOwnProperty('isPrototypeOf')) {
223
+ return false
224
+ }
225
+
226
+ // Most likely a plain Object
227
+ return true
228
+ }
229
+
230
+ function hasObjectPrototype(o: any) {
231
+ return Object.prototype.toString.call(o) === '[object Object]'
232
+ }
233
+
234
+ export function deepEqual(a: any, b: any, partial: boolean = false): boolean {
235
+ if (a === b) {
236
+ return true
237
+ }
238
+
239
+ if (typeof a !== typeof b) {
240
+ return false
241
+ }
242
+
243
+ if (isPlainObject(a) && isPlainObject(b)) {
244
+ const aKeys = Object.keys(a)
245
+ const bKeys = Object.keys(b)
246
+
247
+ if (!partial && aKeys.length !== bKeys.length) {
248
+ return false
249
+ }
250
+
251
+ return !bKeys.some(
252
+ (key) => !(key in a) || !deepEqual(a[key], b[key], partial),
253
+ )
254
+ }
255
+
256
+ if (Array.isArray(a) && Array.isArray(b)) {
257
+ return !a.some((item, index) => !deepEqual(item, b[index], partial))
258
+ }
259
+
260
+ return false
261
+ }
262
+
263
+ export function useStableCallback<T extends (...args: any[]) => any>(fn: T): T {
264
+ const fnRef = React.useRef(fn)
265
+ fnRef.current = fn
266
+
267
+ const ref = React.useRef((...args: any[]) => fnRef.current(...args))
268
+ return ref.current as T
269
+ }
270
+
271
+ export function shallow<T>(objA: T, objB: T) {
272
+ if (Object.is(objA, objB)) {
273
+ return true
274
+ }
275
+
276
+ if (
277
+ typeof objA !== 'object' ||
278
+ objA === null ||
279
+ typeof objB !== 'object' ||
280
+ objB === null
281
+ ) {
282
+ return false
283
+ }
284
+
285
+ const keysA = Object.keys(objA)
286
+ if (keysA.length !== Object.keys(objB).length) {
287
+ return false
288
+ }
289
+
290
+ for (let i = 0; i < keysA.length; i++) {
291
+ if (
292
+ !Object.prototype.hasOwnProperty.call(objB, keysA[i] as string) ||
293
+ !Object.is(objA[keysA[i] as keyof T], objB[keysA[i] as keyof T])
294
+ ) {
295
+ return false
296
+ }
297
+ }
298
+ return true
299
+ }
300
+
301
+ export type StrictOrFrom<TFrom> =
302
+ | {
303
+ from: TFrom
304
+ strict?: true
305
+ }
306
+ | {
307
+ from?: never
308
+ strict: false
309
+ }
310
+
311
+ export type RouteFromIdOrRoute<
312
+ T,
313
+ TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
314
+ > = T extends ParseRoute<TRouteTree>
315
+ ? T
316
+ : T extends RouteIds<TRouteTree>
317
+ ? RoutesById<TRouteTree>[T]
318
+ : T extends string
319
+ ? RouteIds<TRouteTree>
320
+ : never
321
+
322
+ export function useRouteContext<
323
+ TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
324
+ TFrom extends RouteIds<TRouteTree> = RouteIds<TRouteTree>,
325
+ TStrict extends boolean = true,
326
+ TRouteContext = RouteById<TRouteTree, TFrom>['types']['allContext'],
327
+ TSelected = TRouteContext,
328
+ >(
329
+ opts: StrictOrFrom<TFrom> & {
330
+ select?: (search: TRouteContext) => TSelected
331
+ },
332
+ ): TStrict extends true ? TSelected : TSelected | undefined {
333
+ return useMatch({
334
+ ...(opts as any),
335
+ select: (match: RouteMatch) =>
336
+ opts?.select
337
+ ? opts.select(match.context as TRouteContext)
338
+ : match.context,
339
+ })
340
+ }
341
+
342
+ export const useLayoutEffect =
343
+ typeof window !== 'undefined' ? React.useLayoutEffect : React.useEffect
344
+
345
+ export function escapeJSON(jsonString: string) {
346
+ return jsonString
347
+ .replace(/\\/g, '\\\\') // Escape backslashes
348
+ .replace(/'/g, "\\'") // Escape single quotes
349
+ .replace(/"/g, '\\"') // Escape double quotes
350
+ }