@tanstack/react-router 1.20.0 → 1.20.3-alpha.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 (320) hide show
  1. package/README.md +31 -0
  2. package/dist/cjs/Asset.cjs +41 -0
  3. package/dist/cjs/Asset.cjs.map +1 -0
  4. package/dist/cjs/Asset.d.cts +2 -0
  5. package/dist/cjs/CatchBoundary.cjs +16 -12
  6. package/dist/cjs/CatchBoundary.cjs.map +1 -1
  7. package/dist/cjs/CatchBoundary.d.cts +8 -32
  8. package/dist/cjs/ClientOnly.cjs +20 -0
  9. package/dist/cjs/ClientOnly.cjs.map +1 -0
  10. package/dist/cjs/ClientOnly.d.cts +29 -0
  11. package/dist/cjs/HeadContent.cjs +155 -0
  12. package/dist/cjs/HeadContent.cjs.map +1 -0
  13. package/dist/cjs/HeadContent.d.cts +7 -0
  14. package/dist/cjs/Match.cjs +252 -0
  15. package/dist/cjs/Match.cjs.map +1 -0
  16. package/dist/cjs/Match.d.cts +8 -0
  17. package/dist/cjs/Matches.cjs +39 -287
  18. package/dist/cjs/Matches.cjs.map +1 -1
  19. package/dist/cjs/Matches.d.cts +23 -83
  20. package/dist/cjs/RouterProvider.cjs +17 -140
  21. package/dist/cjs/RouterProvider.cjs.map +1 -1
  22. package/dist/cjs/RouterProvider.d.cts +8 -27
  23. package/dist/cjs/SafeFragment.cjs +8 -0
  24. package/dist/cjs/SafeFragment.cjs.map +1 -0
  25. package/dist/cjs/SafeFragment.d.cts +1 -0
  26. package/dist/cjs/ScriptOnce.cjs +28 -0
  27. package/dist/cjs/ScriptOnce.cjs.map +1 -0
  28. package/dist/cjs/ScriptOnce.d.cts +5 -0
  29. package/dist/cjs/Scripts.cjs +51 -0
  30. package/dist/cjs/Scripts.cjs.map +1 -0
  31. package/dist/cjs/Scripts.d.cts +1 -0
  32. package/dist/cjs/ScrollRestoration.cjs +39 -0
  33. package/dist/cjs/ScrollRestoration.cjs.map +1 -0
  34. package/dist/cjs/ScrollRestoration.d.cts +14 -0
  35. package/dist/cjs/Transitioner.cjs +115 -0
  36. package/dist/cjs/Transitioner.cjs.map +1 -0
  37. package/dist/cjs/Transitioner.d.cts +1 -0
  38. package/dist/cjs/awaited.cjs +12 -65
  39. package/dist/cjs/awaited.cjs.map +1 -1
  40. package/dist/cjs/awaited.d.cts +4 -4
  41. package/dist/cjs/fileRoute.cjs +41 -15
  42. package/dist/cjs/fileRoute.cjs.map +1 -1
  43. package/dist/cjs/fileRoute.d.cts +33 -108
  44. package/dist/cjs/history.d.cts +1 -0
  45. package/dist/cjs/index.cjs +216 -73
  46. package/dist/cjs/index.cjs.map +1 -1
  47. package/dist/cjs/index.d.cts +52 -29
  48. package/dist/cjs/lazyRouteComponent.cjs +40 -29
  49. package/dist/cjs/lazyRouteComponent.cjs.map +1 -1
  50. package/dist/cjs/lazyRouteComponent.d.cts +1 -1
  51. package/dist/cjs/link.cjs +212 -106
  52. package/dist/cjs/link.cjs.map +1 -1
  53. package/dist/cjs/link.d.cts +41 -86
  54. package/dist/cjs/matchContext.cjs +27 -0
  55. package/dist/cjs/matchContext.cjs.map +1 -0
  56. package/dist/cjs/matchContext.d.cts +3 -0
  57. package/dist/cjs/not-found.cjs +9 -15
  58. package/dist/cjs/not-found.cjs.map +1 -1
  59. package/dist/cjs/not-found.d.cts +5 -22
  60. package/dist/cjs/renderRouteNotFound.cjs +22 -0
  61. package/dist/cjs/renderRouteNotFound.cjs.map +1 -0
  62. package/dist/cjs/renderRouteNotFound.d.cts +2 -0
  63. package/dist/cjs/route.cjs +110 -79
  64. package/dist/cjs/route.cjs.map +1 -1
  65. package/dist/cjs/route.d.cts +64 -361
  66. package/dist/cjs/router.cjs +12 -1237
  67. package/dist/cjs/router.cjs.map +1 -1
  68. package/dist/cjs/router.d.cts +69 -237
  69. package/dist/cjs/routerContext.cjs +1 -1
  70. package/dist/cjs/routerContext.cjs.map +1 -1
  71. package/dist/cjs/routerContext.d.cts +7 -2
  72. package/dist/cjs/scroll-restoration.cjs +16 -177
  73. package/dist/cjs/scroll-restoration.cjs.map +1 -1
  74. package/dist/cjs/scroll-restoration.d.cts +1 -18
  75. package/dist/cjs/serializer.d.cts +6 -0
  76. package/dist/cjs/structuralSharing.d.cts +8 -0
  77. package/dist/cjs/typePrimitives.d.cts +16 -0
  78. package/dist/cjs/useBlocker.cjs +138 -9
  79. package/dist/cjs/useBlocker.cjs.map +1 -1
  80. package/dist/cjs/useBlocker.d.cts +64 -7
  81. package/dist/cjs/useCanGoBack.cjs +8 -0
  82. package/dist/cjs/useCanGoBack.cjs.map +1 -0
  83. package/dist/cjs/useCanGoBack.d.cts +1 -0
  84. package/dist/cjs/useLoaderData.cjs +15 -0
  85. package/dist/cjs/useLoaderData.cjs.map +1 -0
  86. package/dist/cjs/useLoaderData.d.cts +8 -0
  87. package/dist/cjs/useLoaderDeps.cjs +14 -0
  88. package/dist/cjs/useLoaderDeps.cjs.map +1 -0
  89. package/dist/cjs/useLoaderDeps.d.cts +8 -0
  90. package/dist/cjs/useLocation.cjs +10 -0
  91. package/dist/cjs/useLocation.cjs.map +1 -0
  92. package/dist/cjs/useLocation.d.cts +7 -0
  93. package/dist/cjs/useMatch.cjs +47 -0
  94. package/dist/cjs/useMatch.cjs.map +1 -0
  95. package/dist/cjs/useMatch.d.cts +10 -0
  96. package/dist/cjs/useNavigate.cjs +18 -19
  97. package/dist/cjs/useNavigate.cjs.map +1 -1
  98. package/dist/cjs/useNavigate.d.cts +4 -8
  99. package/dist/cjs/useParams.cjs +8 -8
  100. package/dist/cjs/useParams.cjs.map +1 -1
  101. package/dist/cjs/useParams.d.cts +9 -8
  102. package/dist/cjs/useRouteContext.cjs +3 -3
  103. package/dist/cjs/useRouteContext.cjs.map +1 -1
  104. package/dist/cjs/useRouteContext.d.cts +3 -7
  105. package/dist/cjs/useRouter.cjs.map +1 -1
  106. package/dist/cjs/useRouter.d.cts +3 -4
  107. package/dist/cjs/useRouterState.cjs +18 -1
  108. package/dist/cjs/useRouterState.cjs.map +1 -1
  109. package/dist/cjs/useRouterState.d.cts +8 -6
  110. package/dist/cjs/useSearch.cjs +7 -4
  111. package/dist/cjs/useSearch.cjs.map +1 -1
  112. package/dist/cjs/useSearch.d.cts +9 -7
  113. package/dist/cjs/utils.cjs +40 -122
  114. package/dist/cjs/utils.cjs.map +1 -1
  115. package/dist/cjs/utils.d.cts +46 -50
  116. package/dist/esm/Asset.d.ts +2 -0
  117. package/dist/esm/Asset.js +41 -0
  118. package/dist/esm/Asset.js.map +1 -0
  119. package/dist/esm/CatchBoundary.d.ts +8 -32
  120. package/dist/esm/CatchBoundary.js +16 -12
  121. package/dist/esm/CatchBoundary.js.map +1 -1
  122. package/dist/esm/ClientOnly.d.ts +29 -0
  123. package/dist/esm/ClientOnly.js +20 -0
  124. package/dist/esm/ClientOnly.js.map +1 -0
  125. package/dist/esm/HeadContent.d.ts +7 -0
  126. package/dist/esm/HeadContent.js +139 -0
  127. package/dist/esm/HeadContent.js.map +1 -0
  128. package/dist/esm/Match.d.ts +8 -0
  129. package/dist/esm/Match.js +235 -0
  130. package/dist/esm/Match.js.map +1 -0
  131. package/dist/esm/Matches.d.ts +23 -83
  132. package/dist/esm/Matches.js +36 -284
  133. package/dist/esm/Matches.js.map +1 -1
  134. package/dist/esm/RouterProvider.d.ts +8 -27
  135. package/dist/esm/RouterProvider.js +20 -126
  136. package/dist/esm/RouterProvider.js.map +1 -1
  137. package/dist/esm/SafeFragment.d.ts +1 -0
  138. package/dist/esm/SafeFragment.js +8 -0
  139. package/dist/esm/SafeFragment.js.map +1 -0
  140. package/dist/esm/ScriptOnce.d.ts +5 -0
  141. package/dist/esm/ScriptOnce.js +28 -0
  142. package/dist/esm/ScriptOnce.js.map +1 -0
  143. package/dist/esm/Scripts.d.ts +1 -0
  144. package/dist/esm/Scripts.js +51 -0
  145. package/dist/esm/Scripts.js.map +1 -0
  146. package/dist/esm/ScrollRestoration.d.ts +14 -0
  147. package/dist/esm/ScrollRestoration.js +39 -0
  148. package/dist/esm/ScrollRestoration.js.map +1 -0
  149. package/dist/esm/Transitioner.d.ts +1 -0
  150. package/dist/esm/Transitioner.js +98 -0
  151. package/dist/esm/Transitioner.js.map +1 -0
  152. package/dist/esm/awaited.d.ts +4 -4
  153. package/dist/esm/awaited.js +12 -65
  154. package/dist/esm/awaited.js.map +1 -1
  155. package/dist/esm/fileRoute.d.ts +33 -108
  156. package/dist/esm/fileRoute.js +38 -12
  157. package/dist/esm/fileRoute.js.map +1 -1
  158. package/dist/esm/history.d.ts +1 -0
  159. package/dist/esm/index.d.ts +52 -29
  160. package/dist/esm/index.js +41 -29
  161. package/dist/esm/index.js.map +1 -1
  162. package/dist/esm/lazyRouteComponent.d.ts +1 -1
  163. package/dist/esm/lazyRouteComponent.js +40 -29
  164. package/dist/esm/lazyRouteComponent.js.map +1 -1
  165. package/dist/esm/link.d.ts +41 -86
  166. package/dist/esm/link.js +212 -106
  167. package/dist/esm/link.js.map +1 -1
  168. package/dist/esm/matchContext.d.ts +3 -0
  169. package/dist/esm/matchContext.js +10 -0
  170. package/dist/esm/matchContext.js.map +1 -0
  171. package/dist/esm/not-found.d.ts +5 -22
  172. package/dist/esm/not-found.js +9 -15
  173. package/dist/esm/not-found.js.map +1 -1
  174. package/dist/esm/renderRouteNotFound.d.ts +2 -0
  175. package/dist/esm/renderRouteNotFound.js +22 -0
  176. package/dist/esm/renderRouteNotFound.js.map +1 -0
  177. package/dist/esm/route.d.ts +64 -361
  178. package/dist/esm/route.js +103 -72
  179. package/dist/esm/route.js.map +1 -1
  180. package/dist/esm/router.d.ts +69 -237
  181. package/dist/esm/router.js +13 -1238
  182. package/dist/esm/router.js.map +1 -1
  183. package/dist/esm/routerContext.d.ts +7 -2
  184. package/dist/esm/routerContext.js +1 -1
  185. package/dist/esm/routerContext.js.map +1 -1
  186. package/dist/esm/scroll-restoration.d.ts +1 -18
  187. package/dist/esm/scroll-restoration.js +17 -161
  188. package/dist/esm/scroll-restoration.js.map +1 -1
  189. package/dist/esm/serializer.d.ts +6 -0
  190. package/dist/esm/structuralSharing.d.ts +8 -0
  191. package/dist/esm/typePrimitives.d.ts +16 -0
  192. package/dist/esm/useBlocker.d.ts +64 -7
  193. package/dist/esm/useBlocker.js +138 -9
  194. package/dist/esm/useBlocker.js.map +1 -1
  195. package/dist/esm/useCanGoBack.d.ts +1 -0
  196. package/dist/esm/useCanGoBack.js +8 -0
  197. package/dist/esm/useCanGoBack.js.map +1 -0
  198. package/dist/esm/useLoaderData.d.ts +8 -0
  199. package/dist/esm/useLoaderData.js +15 -0
  200. package/dist/esm/useLoaderData.js.map +1 -0
  201. package/dist/esm/useLoaderDeps.d.ts +8 -0
  202. package/dist/esm/useLoaderDeps.js +14 -0
  203. package/dist/esm/useLoaderDeps.js.map +1 -0
  204. package/dist/esm/useLocation.d.ts +7 -0
  205. package/dist/esm/useLocation.js +10 -0
  206. package/dist/esm/useLocation.js.map +1 -0
  207. package/dist/esm/useMatch.d.ts +10 -0
  208. package/dist/esm/useMatch.js +30 -0
  209. package/dist/esm/useMatch.js.map +1 -0
  210. package/dist/esm/useNavigate.d.ts +4 -8
  211. package/dist/esm/useNavigate.js +18 -19
  212. package/dist/esm/useNavigate.js.map +1 -1
  213. package/dist/esm/useParams.d.ts +9 -8
  214. package/dist/esm/useParams.js +8 -8
  215. package/dist/esm/useParams.js.map +1 -1
  216. package/dist/esm/useRouteContext.d.ts +3 -7
  217. package/dist/esm/useRouteContext.js +2 -2
  218. package/dist/esm/useRouteContext.js.map +1 -1
  219. package/dist/esm/useRouter.d.ts +3 -4
  220. package/dist/esm/useRouter.js.map +1 -1
  221. package/dist/esm/useRouterState.d.ts +8 -6
  222. package/dist/esm/useRouterState.js +18 -1
  223. package/dist/esm/useRouterState.js.map +1 -1
  224. package/dist/esm/useSearch.d.ts +9 -7
  225. package/dist/esm/useSearch.js +6 -3
  226. package/dist/esm/useSearch.js.map +1 -1
  227. package/dist/esm/utils.d.ts +46 -50
  228. package/dist/esm/utils.js +41 -123
  229. package/dist/esm/utils.js.map +1 -1
  230. package/package.json +30 -31
  231. package/src/Asset.tsx +40 -0
  232. package/src/CatchBoundary.tsx +35 -19
  233. package/src/ClientOnly.tsx +68 -0
  234. package/src/HeadContent.tsx +174 -0
  235. package/src/Match.tsx +330 -0
  236. package/src/Matches.tsx +149 -558
  237. package/src/RouterProvider.tsx +58 -212
  238. package/src/SafeFragment.tsx +5 -0
  239. package/src/ScriptOnce.tsx +32 -0
  240. package/src/Scripts.tsx +65 -0
  241. package/src/ScrollRestoration.tsx +69 -0
  242. package/src/Transitioner.tsx +130 -0
  243. package/src/awaited.tsx +16 -87
  244. package/src/fileRoute.ts +145 -248
  245. package/src/history.ts +2 -1
  246. package/src/index.tsx +368 -30
  247. package/src/lazyRouteComponent.tsx +68 -54
  248. package/src/link.tsx +397 -522
  249. package/src/matchContext.tsx +8 -0
  250. package/src/not-found.tsx +13 -34
  251. package/src/renderRouteNotFound.tsx +27 -0
  252. package/src/route.tsx +572 -0
  253. package/src/router.ts +99 -2067
  254. package/src/routerContext.tsx +8 -2
  255. package/src/scroll-restoration.tsx +23 -224
  256. package/src/serializer.ts +7 -0
  257. package/src/structuralSharing.ts +47 -0
  258. package/src/typePrimitives.ts +84 -0
  259. package/src/useBlocker.tsx +297 -15
  260. package/src/useCanGoBack.ts +5 -0
  261. package/src/useLoaderData.tsx +80 -0
  262. package/src/useLoaderDeps.tsx +58 -0
  263. package/src/useLocation.tsx +41 -0
  264. package/src/useMatch.tsx +119 -0
  265. package/src/useNavigate.tsx +41 -61
  266. package/src/useParams.tsx +88 -23
  267. package/src/useRouteContext.ts +24 -18
  268. package/src/useRouter.tsx +4 -5
  269. package/src/useRouterState.tsx +52 -10
  270. package/src/useSearch.tsx +87 -24
  271. package/src/utils.ts +97 -312
  272. package/dist/cjs/createServerFn.cjs +0 -40
  273. package/dist/cjs/createServerFn.cjs.map +0 -1
  274. package/dist/cjs/createServerFn.d.cts +0 -44
  275. package/dist/cjs/defer.cjs +0 -30
  276. package/dist/cjs/defer.cjs.map +0 -1
  277. package/dist/cjs/defer.d.cts +0 -25
  278. package/dist/cjs/location.d.cts +0 -12
  279. package/dist/cjs/path.cjs +0 -213
  280. package/dist/cjs/path.cjs.map +0 -1
  281. package/dist/cjs/path.d.cts +0 -24
  282. package/dist/cjs/qss.cjs +0 -45
  283. package/dist/cjs/qss.cjs.map +0 -1
  284. package/dist/cjs/qss.d.cts +0 -2
  285. package/dist/cjs/redirects.cjs +0 -16
  286. package/dist/cjs/redirects.cjs.map +0 -1
  287. package/dist/cjs/redirects.d.cts +0 -18
  288. package/dist/cjs/routeInfo.d.cts +0 -33
  289. package/dist/cjs/searchParams.cjs +0 -63
  290. package/dist/cjs/searchParams.cjs.map +0 -1
  291. package/dist/cjs/searchParams.d.cts +0 -7
  292. package/dist/esm/createServerFn.d.ts +0 -44
  293. package/dist/esm/createServerFn.js +0 -40
  294. package/dist/esm/createServerFn.js.map +0 -1
  295. package/dist/esm/defer.d.ts +0 -25
  296. package/dist/esm/defer.js +0 -30
  297. package/dist/esm/defer.js.map +0 -1
  298. package/dist/esm/location.d.ts +0 -12
  299. package/dist/esm/path.d.ts +0 -24
  300. package/dist/esm/path.js +0 -213
  301. package/dist/esm/path.js.map +0 -1
  302. package/dist/esm/qss.d.ts +0 -2
  303. package/dist/esm/qss.js +0 -45
  304. package/dist/esm/qss.js.map +0 -1
  305. package/dist/esm/redirects.d.ts +0 -18
  306. package/dist/esm/redirects.js +0 -16
  307. package/dist/esm/redirects.js.map +0 -1
  308. package/dist/esm/routeInfo.d.ts +0 -33
  309. package/dist/esm/searchParams.d.ts +0 -7
  310. package/dist/esm/searchParams.js +0 -63
  311. package/dist/esm/searchParams.js.map +0 -1
  312. package/src/createServerFn.ts +0 -107
  313. package/src/defer.ts +0 -70
  314. package/src/location.ts +0 -13
  315. package/src/path.ts +0 -280
  316. package/src/qss.ts +0 -53
  317. package/src/redirects.ts +0 -56
  318. package/src/route.ts +0 -1356
  319. package/src/routeInfo.ts +0 -62
  320. package/src/searchParams.ts +0 -79
package/src/utils.ts CHANGED
@@ -1,334 +1,119 @@
1
1
  import * as React from 'react'
2
2
 
3
- export type NoInfer<T> = [T][T extends any ? 0 : never]
4
- export type IsAny<T, Y, N = T> = 1 extends 0 & T ? Y : N
5
- export type PickAsRequired<T, K extends keyof T> = Omit<T, K> &
6
- Required<Pick<T, K>>
7
-
8
- export type PickRequired<T> = {
9
- [K in keyof T as undefined extends T[K] ? never : K]: T[K]
10
- }
11
-
12
- // from https://stackoverflow.com/a/76458160
13
- export type WithoutEmpty<T> = T extends T ? ({} extends T ? never : T) : never
14
-
15
- // export type Expand<T> = T
16
- export type Expand<T> = T extends object
17
- ? T extends infer O
18
- ? { [K in keyof O]: O[K] }
19
- : never
20
- : T
21
-
22
- export type UnionToIntersection<U> = (
23
- U extends any ? (k: U) => void : never
24
- ) extends (k: infer I) => any
25
- ? I
26
- : never
27
-
28
- export type DeepOptional<T, K extends keyof T> = Pick<DeepPartial<T>, K> &
29
- Omit<T, K>
30
-
31
- export type DeepPartial<T> = T extends object
32
- ? {
33
- [P in keyof T]?: DeepPartial<T[P]>
34
- }
35
- : T
36
-
37
- export type MakeDifferenceOptional<T, U> = Omit<U, keyof T> &
38
- Partial<Pick<U, keyof T & keyof U>> &
39
- PickRequired<Omit<U, keyof PickRequired<T>>>
40
-
41
- // from https://stackoverflow.com/a/53955431
42
- export type IsUnion<T, U extends T = T> = (
43
- T extends any ? (U extends T ? false : true) : never
44
- ) extends false
45
- ? false
46
- : true
47
-
48
- // type Compute<T> = { [K in keyof T]: T[K] } | never
49
-
50
- // type AllKeys<T> = T extends any ? keyof T : never
51
-
52
- // export type MergeUnion<T, Keys extends keyof T = keyof T> = Compute<
53
- // {
54
- // [K in Keys]: T[Keys]
55
- // } & {
56
- // [K in AllKeys<T>]?: T extends any
57
- // ? K extends keyof T
58
- // ? T[K]
59
- // : never
60
- // : never
61
- // }
62
- // >
63
-
64
- export type Assign<Left, Right> = Omit<Left, keyof Right> & Right
65
-
66
- export type AssignAll<T extends any[]> = T extends [infer Left, ...infer Right]
67
- ? Right extends any[]
68
- ? Assign<Left, AssignAll<Right>>
69
- : Left
70
- : {}
71
-
72
- // // Sample types to merge
73
- // type TypeA = {
74
- // shared: string
75
- // onlyInA: string
76
- // nested: {
77
- // shared: string
78
- // aProp: string
79
- // }
80
- // array: string[]
81
- // }
82
-
83
- // type TypeB = {
84
- // shared: number
85
- // onlyInB: number
86
- // nested: {
87
- // shared: number
88
- // bProp: number
89
- // }
90
- // array: number[]
91
- // }
92
-
93
- // type TypeC = {
94
- // shared: boolean
95
- // onlyInC: boolean
96
- // nested: {
97
- // shared: boolean
98
- // cProp: boolean
99
- // }
100
- // array: boolean[]
101
- // }
102
-
103
- // type Test = Expand<Assign<TypeA, TypeB>>
104
-
105
- // // Using DeepMerge to merge TypeA and TypeB
106
- // type MergedType = Expand<AssignAll<[TypeA, TypeB, TypeC]>>
107
-
108
- export type Timeout = ReturnType<typeof setTimeout>
109
-
110
- export type Updater<TPrevious, TResult = TPrevious> =
111
- | TResult
112
- | ((prev?: TPrevious) => TResult)
113
-
114
- export type NonNullableUpdater<TPrevious, TResult = TPrevious> =
115
- | TResult
116
- | ((prev: TPrevious) => TResult)
117
-
118
- // from https://github.com/type-challenges/type-challenges/issues/737
119
- type LastInUnion<U> =
120
- UnionToIntersection<U extends unknown ? (x: U) => 0 : never> extends (
121
- x: infer L,
122
- ) => 0
123
- ? L
124
- : never
125
- export type UnionToTuple<U, Last = LastInUnion<U>> = [U] extends [never]
126
- ? []
127
- : [...UnionToTuple<Exclude<U, Last>>, Last]
128
-
129
- //
130
-
131
- export const isServer = typeof document === 'undefined'
132
-
133
- export function last<T>(arr: T[]) {
134
- return arr[arr.length - 1]
135
- }
136
-
137
- function isFunction(d: any): d is Function {
138
- return typeof d === 'function'
139
- }
140
-
141
- export function functionalUpdate<TResult>(
142
- updater: Updater<TResult> | NonNullableUpdater<TResult>,
143
- previous: TResult,
144
- ): TResult {
145
- if (isFunction(updater)) {
146
- return updater(previous as TResult)
147
- }
3
+ export function useStableCallback<T extends (...args: Array<any>) => any>(
4
+ fn: T,
5
+ ): T {
6
+ const fnRef = React.useRef(fn)
7
+ fnRef.current = fn
148
8
 
149
- return updater
9
+ const ref = React.useRef((...args: Array<any>) => fnRef.current(...args))
10
+ return ref.current as T
150
11
  }
151
12
 
152
- export function pick<T, K extends keyof T>(parent: T, keys: K[]): Pick<T, K> {
153
- return keys.reduce((obj: any, key: K) => {
154
- obj[key] = parent[key]
155
- return obj
156
- }, {} as any)
157
- }
13
+ export const useLayoutEffect =
14
+ typeof window !== 'undefined' ? React.useLayoutEffect : React.useEffect
158
15
 
159
16
  /**
160
- * This function returns `prev` if `_next` is deeply equal.
161
- * If not, it will replace any deeply equal children of `b` with those of `a`.
162
- * This can be used for structural sharing between immutable JSON values for example.
163
- * Do not use this with signals
17
+ * Taken from https://www.developerway.com/posts/implementing-advanced-use-previous-hook#part3
164
18
  */
165
- export function replaceEqualDeep<T>(prev: any, _next: T): T {
166
- if (prev === _next) {
167
- return prev
168
- }
169
-
170
- const next = _next as any
171
-
172
- const array = isPlainArray(prev) && isPlainArray(next)
173
-
174
- if (array || (isPlainObject(prev) && isPlainObject(next))) {
175
- const prevItems = array ? prev : Object.keys(prev)
176
- const prevSize = prevItems.length
177
- const nextItems = array ? next : Object.keys(next)
178
- const nextSize = nextItems.length
179
- const copy: any = array ? [] : {}
180
-
181
- let equalItems = 0
182
-
183
- for (let i = 0; i < nextSize; i++) {
184
- const key = array ? i : nextItems[i]
185
- if (
186
- !array &&
187
- prev[key] === undefined &&
188
- next[key] === undefined &&
189
- prevItems.includes(key)
190
- ) {
191
- copy[key] = undefined
192
- equalItems++
193
- } else {
194
- copy[key] = replaceEqualDeep(prev[key], next[key])
195
- if (copy[key] === prev[key] && prev[key] !== undefined) {
196
- equalItems++
197
- }
198
- }
19
+ export function usePrevious<T>(value: T): T | null {
20
+ // initialise the ref with previous and current values
21
+ const ref = React.useRef<{ value: T; prev: T | null }>({
22
+ value: value,
23
+ prev: null,
24
+ })
25
+
26
+ const current = ref.current.value
27
+
28
+ // if the value passed into hook doesn't match what we store as "current"
29
+ // move the "current" to the "previous"
30
+ // and store the passed value as "current"
31
+ if (value !== current) {
32
+ ref.current = {
33
+ value: value,
34
+ prev: current,
199
35
  }
200
-
201
- return prevSize === nextSize && equalItems === prevSize ? prev : copy
202
36
  }
203
37
 
204
- return next
38
+ // return the previous value only
39
+ return ref.current.prev
205
40
  }
206
41
 
207
- // Copied from: https://github.com/jonschlinkert/is-plain-object
208
- export function isPlainObject(o: any) {
209
- if (!hasObjectPrototype(o)) {
210
- return false
211
- }
212
-
213
- // If has modified constructor
214
- const ctor = o.constructor
215
- if (typeof ctor === 'undefined') {
216
- return true
217
- }
218
-
219
- // If has modified prototype
220
- const prot = ctor.prototype
221
- if (!hasObjectPrototype(prot)) {
222
- return false
223
- }
224
-
225
- // If constructor does not have an Object-specific method
226
- if (!prot.hasOwnProperty('isPrototypeOf')) {
227
- return false
228
- }
229
-
230
- // Most likely a plain Object
231
- return true
232
- }
233
-
234
- function hasObjectPrototype(o: any) {
235
- return Object.prototype.toString.call(o) === '[object Object]'
236
- }
237
-
238
- export function isPlainArray(value: unknown) {
239
- return Array.isArray(value) && value.length === Object.keys(value).length
240
- }
241
-
242
- export function deepEqual(a: any, b: any, partial: boolean = false): boolean {
243
- if (a === b) {
244
- return true
245
- }
246
-
247
- if (typeof a !== typeof b) {
248
- return false
249
- }
250
-
251
- if (isPlainObject(a) && isPlainObject(b)) {
252
- const aKeys = Object.keys(a)
253
- const bKeys = Object.keys(b)
254
-
255
- if (!partial && aKeys.length !== bKeys.length) {
256
- return false
257
- }
258
-
259
- return !bKeys.some(
260
- (key) => !(key in a) || !deepEqual(a[key], b[key], partial),
261
- )
262
- }
263
-
264
- if (Array.isArray(a) && Array.isArray(b)) {
265
- return !a.some((item, index) => !deepEqual(item, b[index], partial))
266
- }
267
-
268
- return false
269
- }
270
-
271
- export function useStableCallback<T extends (...args: any[]) => any>(fn: T): T {
272
- const fnRef = React.useRef(fn)
273
- fnRef.current = fn
274
-
275
- const ref = React.useRef((...args: any[]) => fnRef.current(...args))
276
- return ref.current as T
277
- }
278
-
279
- export function shallow<T>(objA: T, objB: T) {
280
- if (Object.is(objA, objB)) {
281
- return true
282
- }
283
-
284
- if (
285
- typeof objA !== 'object' ||
286
- objA === null ||
287
- typeof objB !== 'object' ||
288
- objB === null
289
- ) {
290
- return false
291
- }
292
-
293
- const keysA = Object.keys(objA)
294
- if (keysA.length !== Object.keys(objB).length) {
295
- return false
296
- }
297
-
298
- for (let i = 0; i < keysA.length; i++) {
42
+ /**
43
+ * React hook to wrap `IntersectionObserver`.
44
+ *
45
+ * This hook will create an `IntersectionObserver` and observe the ref passed to it.
46
+ *
47
+ * When the intersection changes, the callback will be called with the `IntersectionObserverEntry`.
48
+ *
49
+ * @param ref - The ref to observe
50
+ * @param intersectionObserverOptions - The options to pass to the IntersectionObserver
51
+ * @param options - The options to pass to the hook
52
+ * @param callback - The callback to call when the intersection changes
53
+ * @returns The IntersectionObserver instance
54
+ * @example
55
+ * ```tsx
56
+ * const MyComponent = () => {
57
+ * const ref = React.useRef<HTMLDivElement>(null)
58
+ * useIntersectionObserver(
59
+ * ref,
60
+ * (entry) => { doSomething(entry) },
61
+ * { rootMargin: '10px' },
62
+ * { disabled: false }
63
+ * )
64
+ * return <div ref={ref} />
65
+ * ```
66
+ */
67
+ export function useIntersectionObserver<T extends Element>(
68
+ ref: React.RefObject<T | null>,
69
+ callback: (entry: IntersectionObserverEntry | undefined) => void,
70
+ intersectionObserverOptions: IntersectionObserverInit = {},
71
+ options: { disabled?: boolean } = {},
72
+ ): IntersectionObserver | null {
73
+ const isIntersectionObserverAvailable = React.useRef(
74
+ typeof IntersectionObserver === 'function',
75
+ )
76
+
77
+ const observerRef = React.useRef<IntersectionObserver | null>(null)
78
+
79
+ React.useEffect(() => {
299
80
  if (
300
- !Object.prototype.hasOwnProperty.call(objB, keysA[i] as string) ||
301
- !Object.is(objA[keysA[i] as keyof T], objB[keysA[i] as keyof T])
81
+ !ref.current ||
82
+ !isIntersectionObserverAvailable.current ||
83
+ options.disabled
302
84
  ) {
303
- return false
85
+ return
304
86
  }
305
- }
306
- return true
307
- }
308
87
 
309
- export type StringLiteral<T> = T extends string
310
- ? string extends T
311
- ? string
312
- : T
313
- : never
88
+ observerRef.current = new IntersectionObserver(([entry]) => {
89
+ callback(entry)
90
+ }, intersectionObserverOptions)
314
91
 
315
- export type StrictOrFrom<TFrom, TReturnIntersection extends boolean = false> =
316
- | {
317
- from: StringLiteral<TFrom> | TFrom
318
- strict?: true
319
- }
320
- | {
321
- from?: never
322
- strict: false
323
- experimental_returnIntersection?: TReturnIntersection
92
+ observerRef.current.observe(ref.current)
93
+
94
+ return () => {
95
+ observerRef.current?.disconnect()
324
96
  }
97
+ }, [callback, intersectionObserverOptions, options.disabled, ref])
325
98
 
326
- export const useLayoutEffect =
327
- typeof window !== 'undefined' ? React.useLayoutEffect : React.useEffect
99
+ return observerRef.current
100
+ }
328
101
 
329
- export function escapeJSON(jsonString: string) {
330
- return jsonString
331
- .replace(/\\/g, '\\\\') // Escape backslashes
332
- .replace(/'/g, "\\'") // Escape single quotes
333
- .replace(/"/g, '\\"') // Escape double quotes
102
+ /**
103
+ * React hook to take a `React.ForwardedRef` and returns a `ref` that can be used on a DOM element.
104
+ *
105
+ * @param ref - The forwarded ref
106
+ * @returns The inner ref returned by `useRef`
107
+ * @example
108
+ * ```tsx
109
+ * const MyComponent = React.forwardRef((props, ref) => {
110
+ * const innerRef = useForwardedRef(ref)
111
+ * return <div ref={innerRef} />
112
+ * })
113
+ * ```
114
+ */
115
+ export function useForwardedRef<T>(ref?: React.ForwardedRef<T>) {
116
+ const innerRef = React.useRef<T>(null)
117
+ React.useImperativeHandle(ref, () => innerRef.current!, [])
118
+ return innerRef
334
119
  }
@@ -1,40 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- const invariant = require("tiny-invariant");
4
- const serverFnReturnTypeHeader = "server-fn-return-type";
5
- const serverFnPayloadTypeHeader = "server-fn-payload-type";
6
- function createServerFn(method, fn) {
7
- const compiledFn = fn;
8
- invariant(
9
- compiledFn.url,
10
- `createServerFn must be called with a function that is marked with the 'use server' pragma.`
11
- );
12
- return Object.assign(
13
- async (payload, opts) => {
14
- return compiledFn({
15
- method,
16
- payload: payload || void 0,
17
- requestInit: opts == null ? void 0 : opts.requestInit
18
- });
19
- },
20
- {
21
- url: fn.url
22
- }
23
- );
24
- }
25
- function json(payload, opts) {
26
- return new Response(JSON.stringify(payload), {
27
- status: (opts == null ? void 0 : opts.status) || 200,
28
- statusText: (opts == null ? void 0 : opts.statusText) || (opts == null ? void 0 : opts.status) === 200 ? "OK" : "Error",
29
- headers: {
30
- "Content-Type": "application/json",
31
- [serverFnReturnTypeHeader]: "json",
32
- ...opts == null ? void 0 : opts.headers
33
- }
34
- });
35
- }
36
- exports.createServerFn = createServerFn;
37
- exports.json = json;
38
- exports.serverFnPayloadTypeHeader = serverFnPayloadTypeHeader;
39
- exports.serverFnReturnTypeHeader = serverFnReturnTypeHeader;
40
- //# sourceMappingURL=createServerFn.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"createServerFn.cjs","sources":["../../src/createServerFn.ts"],"sourcesContent":["import invariant from 'tiny-invariant'\n\nexport const serverFnReturnTypeHeader = 'server-fn-return-type'\nexport const serverFnPayloadTypeHeader = 'server-fn-payload-type'\n\nexport interface JsonResponse<TData> extends Response {\n json(): Promise<TData>\n}\n\nexport type FetcherOptionsBase = {\n method?: 'GET' | 'POST'\n}\n\nexport type FetcherOptions = FetcherOptionsBase & {\n requestInit?: RequestInit\n}\n\nexport type FetchFnCtx = {\n method: 'GET' | 'POST'\n request: Request\n}\n\nexport type FetchFn<TPayload, TResponse> = {\n (payload: TPayload, ctx: FetchFnCtx): Promise<TResponse> | TResponse\n url?: string\n}\n\nexport type CompiledFetcherFnOptions<TPayload> = {\n method: 'GET' | 'POST'\n payload: TPayload | undefined\n requestInit?: RequestInit\n}\n\nexport type CompiledFetcherFn<TPayload, TResponse> = {\n (opts: CompiledFetcherFnOptions<TPayload>): Promise<TResponse>\n url: string\n}\n\ntype IsPayloadOptional<T> = [T] extends [undefined] ? true : false\n\nexport type Fetcher<TPayload, TResponse> =\n (IsPayloadOptional<TPayload> extends true\n ? {\n (\n payload?: TPayload,\n opts?: FetcherOptions,\n ): Promise<JsonResponseOrPayload<TResponse>>\n }\n : {\n (\n payload: TPayload,\n opts?: FetcherOptions,\n ): Promise<JsonResponseOrPayload<TResponse>>\n }) & {\n url: string\n }\n\nexport type JsonResponseOrPayload<TResponse> =\n TResponse extends JsonResponse<infer TData> ? TData : TResponse\n\nexport function createServerFn<\n TPayload extends any = undefined,\n TResponse = unknown,\n>(\n method: 'GET' | 'POST',\n fn: FetchFn<TPayload, TResponse>,\n): Fetcher<TPayload, TResponse> {\n // Cast the compiled function that will be injected by vinxi\n const compiledFn = fn as unknown as CompiledFetcherFn<TPayload, TResponse>\n\n invariant(\n compiledFn.url,\n `createServerFn must be called with a function that is marked with the 'use server' pragma.`,\n )\n\n return Object.assign(\n async (payload: TPayload, opts?: FetcherOptions) => {\n return compiledFn({\n method,\n payload: payload || undefined,\n requestInit: opts?.requestInit,\n })\n },\n {\n url: fn.url!,\n },\n ) as Fetcher<TPayload, TResponse>\n}\n\nexport function json<TData>(\n payload: TData,\n opts?: {\n status?: number\n statusText?: string\n headers?: HeadersInit\n },\n): JsonResponse<TData> {\n return new Response(JSON.stringify(payload), {\n status: opts?.status || 200,\n statusText: opts?.statusText || opts?.status === 200 ? 'OK' : 'Error',\n headers: {\n 'Content-Type': 'application/json',\n [serverFnReturnTypeHeader]: 'json',\n ...opts?.headers,\n },\n })\n}\n"],"names":[],"mappings":";;;AAEO,MAAM,2BAA2B;AACjC,MAAM,4BAA4B;AAyDzB,SAAA,eAId,QACA,IAC8B;AAE9B,QAAM,aAAa;AAEnB;AAAA,IACE,WAAW;AAAA,IACX;AAAA,EAAA;AAGF,SAAO,OAAO;AAAA,IACZ,OAAO,SAAmB,SAA0B;AAClD,aAAO,WAAW;AAAA,QAChB;AAAA,QACA,SAAS,WAAW;AAAA,QACpB,aAAa,6BAAM;AAAA,MAAA,CACpB;AAAA,IACH;AAAA,IACA;AAAA,MACE,KAAK,GAAG;AAAA,IACV;AAAA,EAAA;AAEJ;AAEgB,SAAA,KACd,SACA,MAKqB;AACrB,SAAO,IAAI,SAAS,KAAK,UAAU,OAAO,GAAG;AAAA,IAC3C,SAAQ,6BAAM,WAAU;AAAA,IACxB,aAAY,6BAAM,gBAAc,6BAAM,YAAW,MAAM,OAAO;AAAA,IAC9D,SAAS;AAAA,MACP,gBAAgB;AAAA,MAChB,CAAC,wBAAwB,GAAG;AAAA,MAC5B,GAAG,6BAAM;AAAA,IACX;AAAA,EAAA,CACD;AACH;;;;;"}
@@ -1,44 +0,0 @@
1
- export declare const serverFnReturnTypeHeader = "server-fn-return-type";
2
- export declare const serverFnPayloadTypeHeader = "server-fn-payload-type";
3
- export interface JsonResponse<TData> extends Response {
4
- json(): Promise<TData>;
5
- }
6
- export type FetcherOptionsBase = {
7
- method?: 'GET' | 'POST';
8
- };
9
- export type FetcherOptions = FetcherOptionsBase & {
10
- requestInit?: RequestInit;
11
- };
12
- export type FetchFnCtx = {
13
- method: 'GET' | 'POST';
14
- request: Request;
15
- };
16
- export type FetchFn<TPayload, TResponse> = {
17
- (payload: TPayload, ctx: FetchFnCtx): Promise<TResponse> | TResponse;
18
- url?: string;
19
- };
20
- export type CompiledFetcherFnOptions<TPayload> = {
21
- method: 'GET' | 'POST';
22
- payload: TPayload | undefined;
23
- requestInit?: RequestInit;
24
- };
25
- export type CompiledFetcherFn<TPayload, TResponse> = {
26
- (opts: CompiledFetcherFnOptions<TPayload>): Promise<TResponse>;
27
- url: string;
28
- };
29
- type IsPayloadOptional<T> = [T] extends [undefined] ? true : false;
30
- export type Fetcher<TPayload, TResponse> = (IsPayloadOptional<TPayload> extends true ? {
31
- (payload?: TPayload, opts?: FetcherOptions): Promise<JsonResponseOrPayload<TResponse>>;
32
- } : {
33
- (payload: TPayload, opts?: FetcherOptions): Promise<JsonResponseOrPayload<TResponse>>;
34
- }) & {
35
- url: string;
36
- };
37
- export type JsonResponseOrPayload<TResponse> = TResponse extends JsonResponse<infer TData> ? TData : TResponse;
38
- export declare function createServerFn<TPayload extends any = undefined, TResponse = unknown>(method: 'GET' | 'POST', fn: FetchFn<TPayload, TResponse>): Fetcher<TPayload, TResponse>;
39
- export declare function json<TData>(payload: TData, opts?: {
40
- status?: number;
41
- statusText?: string;
42
- headers?: HeadersInit;
43
- }): JsonResponse<TData>;
44
- export {};
@@ -1,30 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
3
- const router = require("./router.cjs");
4
- function defer(_promise, options) {
5
- const promise = _promise;
6
- if (!promise.__deferredState) {
7
- promise.__deferredState = {
8
- uid: Math.random().toString(36).slice(2),
9
- status: "pending"
10
- };
11
- const state = promise.__deferredState;
12
- promise.then((data) => {
13
- state.status = "success";
14
- state.data = data;
15
- }).catch((error) => {
16
- state.status = "error";
17
- state.error = {
18
- data: ((options == null ? void 0 : options.serializeError) ?? router.defaultSerializeError)(error),
19
- __isServerError: true
20
- };
21
- });
22
- }
23
- return promise;
24
- }
25
- function isDehydratedDeferred(obj) {
26
- return typeof obj === "object" && obj !== null && !(obj instanceof Promise) && !obj.then && "__deferredState" in obj;
27
- }
28
- exports.defer = defer;
29
- exports.isDehydratedDeferred = isDehydratedDeferred;
30
- //# sourceMappingURL=defer.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"defer.cjs","sources":["../../src/defer.ts"],"sourcesContent":["import { defaultSerializeError } from './router'\n\nexport type DeferredPromiseState<T> = {\n uid: string\n resolve?: () => void\n promise?: Promise<void>\n __resolvePromise?: () => void\n} & (\n | {\n status: 'pending'\n data?: T\n error?: unknown\n }\n | {\n status: 'success'\n data: T\n }\n | {\n status: 'error'\n data?: T\n error: unknown\n }\n)\n\nexport type DeferredPromise<T> = Promise<T> & {\n __deferredState: DeferredPromiseState<T>\n}\n\nexport function defer<T>(\n _promise: Promise<T>,\n options?: {\n serializeError?: typeof defaultSerializeError\n },\n) {\n const promise = _promise as DeferredPromise<T>\n\n if (!promise.__deferredState) {\n promise.__deferredState = {\n uid: Math.random().toString(36).slice(2),\n status: 'pending',\n }\n\n const state = promise.__deferredState\n\n promise\n .then((data) => {\n state.status = 'success' as any\n state.data = data\n })\n .catch((error) => {\n state.status = 'error' as any\n state.error = {\n data: (options?.serializeError ?? defaultSerializeError)(error),\n __isServerError: true,\n }\n })\n }\n\n return promise\n}\n\nexport function isDehydratedDeferred(obj: any): boolean {\n return (\n typeof obj === 'object' &&\n obj !== null &&\n !(obj instanceof Promise) &&\n !obj.then &&\n '__deferredState' in obj\n )\n}\n"],"names":["defaultSerializeError"],"mappings":";;;AA4BgB,SAAA,MACd,UACA,SAGA;AACA,QAAM,UAAU;AAEZ,MAAA,CAAC,QAAQ,iBAAiB;AAC5B,YAAQ,kBAAkB;AAAA,MACxB,KAAK,KAAK,OAAO,EAAE,SAAS,EAAE,EAAE,MAAM,CAAC;AAAA,MACvC,QAAQ;AAAA,IAAA;AAGV,UAAM,QAAQ,QAAQ;AAGnB,YAAA,KAAK,CAAC,SAAS;AACd,YAAM,SAAS;AACf,YAAM,OAAO;AAAA,IAAA,CACd,EACA,MAAM,CAAC,UAAU;AAChB,YAAM,SAAS;AACf,YAAM,QAAQ;AAAA,QACZ,QAAO,mCAAS,mBAAkBA,OAAAA,uBAAuB,KAAK;AAAA,QAC9D,iBAAiB;AAAA,MAAA;AAAA,IACnB,CACD;AAAA,EACL;AAEO,SAAA;AACT;AAEO,SAAS,qBAAqB,KAAmB;AAEpD,SAAA,OAAO,QAAQ,YACf,QAAQ,QACR,EAAE,eAAe,YACjB,CAAC,IAAI,QACL,qBAAqB;AAEzB;;;"}
@@ -1,25 +0,0 @@
1
- import { defaultSerializeError } from './router.cjs';
2
- export type DeferredPromiseState<T> = {
3
- uid: string;
4
- resolve?: () => void;
5
- promise?: Promise<void>;
6
- __resolvePromise?: () => void;
7
- } & ({
8
- status: 'pending';
9
- data?: T;
10
- error?: unknown;
11
- } | {
12
- status: 'success';
13
- data: T;
14
- } | {
15
- status: 'error';
16
- data?: T;
17
- error: unknown;
18
- });
19
- export type DeferredPromise<T> = Promise<T> & {
20
- __deferredState: DeferredPromiseState<T>;
21
- };
22
- export declare function defer<T>(_promise: Promise<T>, options?: {
23
- serializeError?: typeof defaultSerializeError;
24
- }): DeferredPromise<T>;
25
- export declare function isDehydratedDeferred(obj: any): boolean;
@@ -1,12 +0,0 @@
1
- import { HistoryState } from '@tanstack/history';
2
- import { AnySearchSchema } from './route.cjs';
3
- export interface ParsedLocation<TSearchObj extends AnySearchSchema = {}> {
4
- href: string;
5
- pathname: string;
6
- search: TSearchObj;
7
- searchStr: string;
8
- state: HistoryState;
9
- hash: string;
10
- maskedLocation?: ParsedLocation<TSearchObj>;
11
- unmaskOnReload?: boolean;
12
- }