@tanstack/react-router 0.0.1-beta.9 → 1.0.1

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 +172 -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 +1085 -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 +241 -0
  45. package/build/cjs/utils.js.map +1 -0
  46. package/build/esm/index.js +2302 -2534
  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 +64 -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 -74
  58. package/build/types/lazyRouteComponent.d.ts +2 -0
  59. package/build/types/link.d.ts +93 -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 +283 -0
  65. package/build/types/routeInfo.d.ts +31 -0
  66. package/build/types/router.d.ts +186 -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 +69 -0
  74. package/build/umd/index.development.js +2899 -2493
  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 +12 -10
  79. package/src/CatchBoundary.tsx +101 -0
  80. package/src/Matches.tsx +423 -0
  81. package/src/RouterProvider.tsx +254 -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 -619
  87. package/src/lazyRouteComponent.tsx +33 -0
  88. package/src/link.tsx +603 -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 +882 -0
  94. package/src/routeInfo.ts +84 -0
  95. package/src/router.ts +1671 -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 +360 -0
  103. package/build/cjs/react-router/src/index.js +0 -458
  104. package/build/cjs/react-router/src/index.js.map +0 -1
  105. package/build/cjs/router-core/build/esm/index.js +0 -2524
  106. package/build/cjs/router-core/build/esm/index.js.map +0 -1
package/src/utils.ts ADDED
@@ -0,0 +1,360 @@
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
+ // from https://github.com/type-challenges/type-challenges/issues/737
137
+ type LastInUnion<U> = UnionToIntersection<
138
+ U extends unknown ? (x: U) => 0 : never
139
+ > extends (x: infer L) => 0
140
+ ? L
141
+ : never
142
+ export type UnionToTuple<U, Last = LastInUnion<U>> = [U] extends [never]
143
+ ? []
144
+ : [...UnionToTuple<Exclude<U, Last>>, Last]
145
+
146
+ //
147
+
148
+ export const isServer = typeof document === 'undefined'
149
+
150
+ export function last<T>(arr: T[]) {
151
+ return arr[arr.length - 1]
152
+ }
153
+
154
+ function isFunction(d: any): d is Function {
155
+ return typeof d === 'function'
156
+ }
157
+
158
+ export function functionalUpdate<TResult>(
159
+ updater: Updater<TResult> | NonNullableUpdater<TResult>,
160
+ previous: TResult,
161
+ ): TResult {
162
+ if (isFunction(updater)) {
163
+ return updater(previous as TResult)
164
+ }
165
+
166
+ return updater
167
+ }
168
+
169
+ export function pick<T, K extends keyof T>(parent: T, keys: K[]): Pick<T, K> {
170
+ return keys.reduce((obj: any, key: K) => {
171
+ obj[key] = parent[key]
172
+ return obj
173
+ }, {} as any)
174
+ }
175
+
176
+ /**
177
+ * This function returns `a` if `b` is deeply equal.
178
+ * If not, it will replace any deeply equal children of `b` with those of `a`.
179
+ * This can be used for structural sharing between immutable JSON values for example.
180
+ * Do not use this with signals
181
+ */
182
+ export function replaceEqualDeep<T>(prev: any, _next: T): T {
183
+ if (prev === _next) {
184
+ return prev
185
+ }
186
+
187
+ const next = _next as any
188
+
189
+ const array = Array.isArray(prev) && Array.isArray(next)
190
+
191
+ if (array || (isPlainObject(prev) && isPlainObject(next))) {
192
+ const prevSize = array ? prev.length : Object.keys(prev).length
193
+ const nextItems = array ? next : Object.keys(next)
194
+ const nextSize = nextItems.length
195
+ const copy: any = array ? [] : {}
196
+
197
+ let equalItems = 0
198
+
199
+ for (let i = 0; i < nextSize; i++) {
200
+ const key = array ? i : nextItems[i]
201
+ copy[key] = replaceEqualDeep(prev[key], next[key])
202
+ if (copy[key] === prev[key]) {
203
+ equalItems++
204
+ }
205
+ }
206
+
207
+ return prevSize === nextSize && equalItems === prevSize ? prev : copy
208
+ }
209
+
210
+ return next
211
+ }
212
+
213
+ // Copied from: https://github.com/jonschlinkert/is-plain-object
214
+ export function isPlainObject(o: any) {
215
+ if (!hasObjectPrototype(o)) {
216
+ return false
217
+ }
218
+
219
+ // If has modified constructor
220
+ const ctor = o.constructor
221
+ if (typeof ctor === 'undefined') {
222
+ return true
223
+ }
224
+
225
+ // If has modified prototype
226
+ const prot = ctor.prototype
227
+ if (!hasObjectPrototype(prot)) {
228
+ return false
229
+ }
230
+
231
+ // If constructor does not have an Object-specific method
232
+ if (!prot.hasOwnProperty('isPrototypeOf')) {
233
+ return false
234
+ }
235
+
236
+ // Most likely a plain Object
237
+ return true
238
+ }
239
+
240
+ function hasObjectPrototype(o: any) {
241
+ return Object.prototype.toString.call(o) === '[object Object]'
242
+ }
243
+
244
+ export function deepEqual(a: any, b: any, partial: boolean = false): boolean {
245
+ if (a === b) {
246
+ return true
247
+ }
248
+
249
+ if (typeof a !== typeof b) {
250
+ return false
251
+ }
252
+
253
+ if (isPlainObject(a) && isPlainObject(b)) {
254
+ const aKeys = Object.keys(a)
255
+ const bKeys = Object.keys(b)
256
+
257
+ if (!partial && aKeys.length !== bKeys.length) {
258
+ return false
259
+ }
260
+
261
+ return !bKeys.some(
262
+ (key) => !(key in a) || !deepEqual(a[key], b[key], partial),
263
+ )
264
+ }
265
+
266
+ if (Array.isArray(a) && Array.isArray(b)) {
267
+ return !a.some((item, index) => !deepEqual(item, b[index], partial))
268
+ }
269
+
270
+ return false
271
+ }
272
+
273
+ export function useStableCallback<T extends (...args: any[]) => any>(fn: T): T {
274
+ const fnRef = React.useRef(fn)
275
+ fnRef.current = fn
276
+
277
+ const ref = React.useRef((...args: any[]) => fnRef.current(...args))
278
+ return ref.current as T
279
+ }
280
+
281
+ export function shallow<T>(objA: T, objB: T) {
282
+ if (Object.is(objA, objB)) {
283
+ return true
284
+ }
285
+
286
+ if (
287
+ typeof objA !== 'object' ||
288
+ objA === null ||
289
+ typeof objB !== 'object' ||
290
+ objB === null
291
+ ) {
292
+ return false
293
+ }
294
+
295
+ const keysA = Object.keys(objA)
296
+ if (keysA.length !== Object.keys(objB).length) {
297
+ return false
298
+ }
299
+
300
+ for (let i = 0; i < keysA.length; i++) {
301
+ if (
302
+ !Object.prototype.hasOwnProperty.call(objB, keysA[i] as string) ||
303
+ !Object.is(objA[keysA[i] as keyof T], objB[keysA[i] as keyof T])
304
+ ) {
305
+ return false
306
+ }
307
+ }
308
+ return true
309
+ }
310
+
311
+ export type StrictOrFrom<TFrom> =
312
+ | {
313
+ from: TFrom
314
+ strict?: true
315
+ }
316
+ | {
317
+ from?: never
318
+ strict: false
319
+ }
320
+
321
+ export type RouteFromIdOrRoute<
322
+ T,
323
+ TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
324
+ > = T extends ParseRoute<TRouteTree>
325
+ ? T
326
+ : T extends RouteIds<TRouteTree>
327
+ ? RoutesById<TRouteTree>[T]
328
+ : T extends string
329
+ ? RouteIds<TRouteTree>
330
+ : never
331
+
332
+ export function useRouteContext<
333
+ TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
334
+ TFrom extends RouteIds<TRouteTree> = RouteIds<TRouteTree>,
335
+ TStrict extends boolean = true,
336
+ TRouteContext = RouteById<TRouteTree, TFrom>['types']['allContext'],
337
+ TSelected = TRouteContext,
338
+ >(
339
+ opts: StrictOrFrom<TFrom> & {
340
+ select?: (search: TRouteContext) => TSelected
341
+ },
342
+ ): TStrict extends true ? TSelected : TSelected | undefined {
343
+ return useMatch({
344
+ ...(opts as any),
345
+ select: (match: RouteMatch) =>
346
+ opts?.select
347
+ ? opts.select(match.context as TRouteContext)
348
+ : match.context,
349
+ })
350
+ }
351
+
352
+ export const useLayoutEffect =
353
+ typeof window !== 'undefined' ? React.useLayoutEffect : React.useEffect
354
+
355
+ export function escapeJSON(jsonString: string) {
356
+ return jsonString
357
+ .replace(/\\/g, '\\\\') // Escape backslashes
358
+ .replace(/'/g, "\\'") // Escape single quotes
359
+ .replace(/"/g, '\\"') // Escape double quotes
360
+ }