kiru 0.54.3 → 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 (335) hide show
  1. package/dist/{appContext.d.ts → appHandle.d.ts} +4 -4
  2. package/dist/appHandle.d.ts.map +1 -0
  3. package/dist/{appContext.js → appHandle.js} +12 -9
  4. package/dist/appHandle.js.map +1 -0
  5. package/dist/components/derive.d.ts +10 -8
  6. package/dist/components/derive.d.ts.map +1 -1
  7. package/dist/components/derive.js +50 -47
  8. package/dist/components/derive.js.map +1 -1
  9. package/dist/components/index.d.ts +0 -1
  10. package/dist/components/index.d.ts.map +1 -1
  11. package/dist/components/index.js +0 -1
  12. package/dist/components/index.js.map +1 -1
  13. package/dist/components/lazy.d.ts.map +1 -1
  14. package/dist/components/lazy.js +5 -4
  15. package/dist/components/lazy.js.map +1 -1
  16. package/dist/components/portal.d.ts.map +1 -1
  17. package/dist/components/portal.js +2 -3
  18. package/dist/components/portal.js.map +1 -1
  19. package/dist/components/transition.d.ts +3 -2
  20. package/dist/components/transition.d.ts.map +1 -1
  21. package/dist/components/transition.js +29 -26
  22. package/dist/components/transition.js.map +1 -1
  23. package/dist/constants.d.ts +1 -5
  24. package/dist/constants.d.ts.map +1 -1
  25. package/dist/constants.js +1 -5
  26. package/dist/constants.js.map +1 -1
  27. package/dist/context.d.ts +1 -1
  28. package/dist/context.d.ts.map +1 -1
  29. package/dist/context.js +25 -19
  30. package/dist/context.js.map +1 -1
  31. package/dist/devtools.d.ts +7 -0
  32. package/dist/devtools.d.ts.map +1 -0
  33. package/dist/devtools.js +15 -0
  34. package/dist/devtools.js.map +1 -0
  35. package/dist/dom.d.ts.map +1 -1
  36. package/dist/dom.js +25 -58
  37. package/dist/dom.js.map +1 -1
  38. package/dist/globalContext.d.ts +15 -16
  39. package/dist/globalContext.d.ts.map +1 -1
  40. package/dist/globalContext.js +36 -46
  41. package/dist/globalContext.js.map +1 -1
  42. package/dist/globals.d.ts +1 -4
  43. package/dist/globals.d.ts.map +1 -1
  44. package/dist/globals.js +1 -4
  45. package/dist/globals.js.map +1 -1
  46. package/dist/headlessRender.d.ts +6 -0
  47. package/dist/headlessRender.d.ts.map +1 -0
  48. package/dist/{recursiveRender.js → headlessRender.js} +17 -16
  49. package/dist/headlessRender.js.map +1 -0
  50. package/dist/hmr.d.ts +5 -7
  51. package/dist/hmr.d.ts.map +1 -1
  52. package/dist/hmr.js +27 -32
  53. package/dist/hmr.js.map +1 -1
  54. package/dist/hooks/index.d.ts +3 -14
  55. package/dist/hooks/index.d.ts.map +1 -1
  56. package/dist/hooks/index.js +3 -14
  57. package/dist/hooks/index.js.map +1 -1
  58. package/dist/hooks/onBeforeMount.d.ts +9 -0
  59. package/dist/hooks/onBeforeMount.d.ts.map +1 -0
  60. package/dist/hooks/onBeforeMount.js +12 -0
  61. package/dist/hooks/onBeforeMount.js.map +1 -0
  62. package/dist/hooks/onCleanup.d.ts +8 -0
  63. package/dist/hooks/onCleanup.d.ts.map +1 -0
  64. package/dist/hooks/onCleanup.js +15 -0
  65. package/dist/hooks/onCleanup.js.map +1 -0
  66. package/dist/hooks/onMount.d.ts +9 -0
  67. package/dist/hooks/onMount.d.ts.map +1 -0
  68. package/dist/hooks/onMount.js +12 -0
  69. package/dist/hooks/onMount.js.map +1 -0
  70. package/dist/hooks/utils.d.ts +2 -62
  71. package/dist/hooks/utils.d.ts.map +1 -1
  72. package/dist/hooks/utils.js +22 -144
  73. package/dist/hooks/utils.js.map +1 -1
  74. package/dist/index.d.ts +8 -4
  75. package/dist/index.d.ts.map +1 -1
  76. package/dist/index.js +8 -6
  77. package/dist/index.js.map +1 -1
  78. package/dist/profiling.d.ts +15 -14
  79. package/dist/profiling.d.ts.map +1 -1
  80. package/dist/profiling.js +9 -4
  81. package/dist/profiling.js.map +1 -1
  82. package/dist/reconciler.d.ts.map +1 -1
  83. package/dist/reconciler.js +12 -25
  84. package/dist/reconciler.js.map +1 -1
  85. package/dist/ref.d.ts +4 -0
  86. package/dist/ref.d.ts.map +1 -0
  87. package/dist/ref.js +4 -0
  88. package/dist/ref.js.map +1 -0
  89. package/dist/renderToString.js +1 -1
  90. package/dist/renderToString.js.map +1 -1
  91. package/dist/router/context.d.ts.map +1 -1
  92. package/dist/router/context.js +1 -2
  93. package/dist/router/context.js.map +1 -1
  94. package/dist/router/fileRouter.d.ts +1 -1
  95. package/dist/router/fileRouter.d.ts.map +1 -1
  96. package/dist/router/fileRouter.js +17 -11
  97. package/dist/router/fileRouter.js.map +1 -1
  98. package/dist/router/fileRouterController.d.ts.map +1 -1
  99. package/dist/router/fileRouterController.js +68 -55
  100. package/dist/router/fileRouterController.js.map +1 -1
  101. package/dist/router/link.d.ts.map +1 -1
  102. package/dist/router/link.js +19 -23
  103. package/dist/router/link.js.map +1 -1
  104. package/dist/router/server/index.d.ts.map +1 -1
  105. package/dist/router/server/index.js +14 -11
  106. package/dist/router/server/index.js.map +1 -1
  107. package/dist/router/types.d.ts +11 -6
  108. package/dist/router/types.d.ts.map +1 -1
  109. package/dist/scheduler.d.ts +1 -0
  110. package/dist/scheduler.d.ts.map +1 -1
  111. package/dist/scheduler.js +65 -52
  112. package/dist/scheduler.js.map +1 -1
  113. package/dist/signals/base.d.ts +0 -1
  114. package/dist/signals/base.d.ts.map +1 -1
  115. package/dist/signals/base.js +13 -36
  116. package/dist/signals/base.js.map +1 -1
  117. package/dist/signals/computed.d.ts +0 -2
  118. package/dist/signals/computed.d.ts.map +1 -1
  119. package/dist/signals/computed.js +1 -40
  120. package/dist/signals/computed.js.map +1 -1
  121. package/dist/signals/effect.d.ts +15 -14
  122. package/dist/signals/effect.d.ts.map +1 -1
  123. package/dist/signals/effect.js +65 -37
  124. package/dist/signals/effect.js.map +1 -1
  125. package/dist/signals/globals.d.ts +0 -5
  126. package/dist/signals/globals.d.ts.map +1 -1
  127. package/dist/signals/globals.js +0 -6
  128. package/dist/signals/globals.js.map +1 -1
  129. package/dist/signals/index.d.ts +4 -4
  130. package/dist/signals/index.d.ts.map +1 -1
  131. package/dist/signals/index.js +4 -4
  132. package/dist/signals/index.js.map +1 -1
  133. package/dist/signals/{for.d.ts → jsx.d.ts} +8 -1
  134. package/dist/signals/jsx.d.ts.map +1 -0
  135. package/dist/signals/{for.js → jsx.js} +4 -1
  136. package/dist/signals/jsx.js.map +1 -0
  137. package/dist/signals/tracking.d.ts +23 -0
  138. package/dist/signals/tracking.d.ts.map +1 -0
  139. package/dist/signals/tracking.js +51 -0
  140. package/dist/signals/tracking.js.map +1 -0
  141. package/dist/signals/types.d.ts +1 -1
  142. package/dist/signals/types.d.ts.map +1 -1
  143. package/dist/signals/utils.d.ts +2 -1
  144. package/dist/signals/utils.d.ts.map +1 -1
  145. package/dist/signals/utils.js +9 -2
  146. package/dist/signals/utils.js.map +1 -1
  147. package/dist/ssr/client.d.ts +3 -3
  148. package/dist/ssr/client.d.ts.map +1 -1
  149. package/dist/ssr/client.js.map +1 -1
  150. package/dist/ssr/server.js +1 -1
  151. package/dist/ssr/server.js.map +1 -1
  152. package/dist/statefulPromise.d.ts +22 -0
  153. package/dist/statefulPromise.d.ts.map +1 -0
  154. package/dist/statefulPromise.js +94 -0
  155. package/dist/statefulPromise.js.map +1 -0
  156. package/dist/types.d.ts +36 -50
  157. package/dist/types.d.ts.map +1 -1
  158. package/dist/types.dom.d.ts +4 -7
  159. package/dist/types.dom.d.ts.map +1 -1
  160. package/dist/types.utils.d.ts +3 -4
  161. package/dist/types.utils.d.ts.map +1 -1
  162. package/dist/utils/vdom.d.ts +8 -6
  163. package/dist/utils/vdom.d.ts.map +1 -1
  164. package/dist/utils/vdom.js +32 -9
  165. package/dist/utils/vdom.js.map +1 -1
  166. package/dist/viewTransitions.d.ts +7 -0
  167. package/dist/viewTransitions.d.ts.map +1 -0
  168. package/dist/viewTransitions.js +71 -0
  169. package/dist/viewTransitions.js.map +1 -0
  170. package/package.json +1 -1
  171. package/src/{appContext.ts → appHandle.ts} +21 -16
  172. package/src/components/derive.ts +74 -69
  173. package/src/components/index.ts +0 -1
  174. package/src/components/lazy.ts +5 -4
  175. package/src/components/portal.ts +2 -3
  176. package/src/components/transition.ts +33 -35
  177. package/src/constants.ts +0 -8
  178. package/src/context.ts +30 -23
  179. package/src/devtools.ts +16 -0
  180. package/src/dom.ts +29 -63
  181. package/src/globalContext.ts +57 -74
  182. package/src/globals.ts +1 -5
  183. package/src/{recursiveRender.ts → headlessRender.ts} +18 -18
  184. package/src/hmr.ts +29 -38
  185. package/src/hooks/index.ts +3 -14
  186. package/src/hooks/onBeforeMount.ts +12 -0
  187. package/src/hooks/onCleanup.ts +15 -0
  188. package/src/hooks/onMount.ts +12 -0
  189. package/src/hooks/utils.ts +28 -238
  190. package/src/index.ts +14 -6
  191. package/src/profiling.ts +22 -20
  192. package/src/reconciler.ts +18 -30
  193. package/src/ref.ts +6 -0
  194. package/src/renderToString.ts +1 -1
  195. package/src/router/context.ts +1 -2
  196. package/src/router/fileRouter.ts +23 -13
  197. package/src/router/fileRouterController.ts +72 -64
  198. package/src/router/link.ts +11 -25
  199. package/src/router/server/index.ts +24 -13
  200. package/src/router/types.ts +15 -8
  201. package/src/scheduler.ts +74 -71
  202. package/src/signals/base.ts +12 -41
  203. package/src/signals/computed.ts +1 -62
  204. package/src/signals/effect.ts +95 -48
  205. package/src/signals/globals.ts +0 -7
  206. package/src/signals/index.ts +4 -4
  207. package/src/signals/{for.ts → jsx.ts} +10 -0
  208. package/src/signals/tracking.ts +69 -0
  209. package/src/signals/types.ts +1 -1
  210. package/src/signals/utils.ts +9 -1
  211. package/src/ssr/client.ts +4 -4
  212. package/src/ssr/server.ts +2 -2
  213. package/src/statefulPromise.ts +136 -0
  214. package/src/types.dom.ts +4 -8
  215. package/src/types.ts +45 -58
  216. package/src/types.utils.ts +3 -4
  217. package/src/utils/vdom.ts +44 -15
  218. package/src/viewTransitions.ts +88 -0
  219. package/dist/appContext.d.ts.map +0 -1
  220. package/dist/appContext.js.map +0 -1
  221. package/dist/components/memo.d.ts +0 -10
  222. package/dist/components/memo.d.ts.map +0 -1
  223. package/dist/components/memo.js +0 -23
  224. package/dist/components/memo.js.map +0 -1
  225. package/dist/form/index.d.ts +0 -4
  226. package/dist/form/index.d.ts.map +0 -1
  227. package/dist/form/index.js +0 -518
  228. package/dist/form/index.js.map +0 -1
  229. package/dist/form/types.d.ts +0 -122
  230. package/dist/form/types.d.ts.map +0 -1
  231. package/dist/form/types.js +0 -2
  232. package/dist/form/types.js.map +0 -1
  233. package/dist/form/utils.d.ts +0 -3
  234. package/dist/form/utils.d.ts.map +0 -1
  235. package/dist/form/utils.js +0 -16
  236. package/dist/form/utils.js.map +0 -1
  237. package/dist/hooks/useAsync.d.ts +0 -18
  238. package/dist/hooks/useAsync.d.ts.map +0 -1
  239. package/dist/hooks/useAsync.js +0 -96
  240. package/dist/hooks/useAsync.js.map +0 -1
  241. package/dist/hooks/useCallback.d.ts +0 -7
  242. package/dist/hooks/useCallback.d.ts.map +0 -1
  243. package/dist/hooks/useCallback.js +0 -30
  244. package/dist/hooks/useCallback.js.map +0 -1
  245. package/dist/hooks/useContext.d.ts +0 -7
  246. package/dist/hooks/useContext.d.ts.map +0 -1
  247. package/dist/hooks/useContext.js +0 -59
  248. package/dist/hooks/useContext.js.map +0 -1
  249. package/dist/hooks/useEffect.d.ts +0 -8
  250. package/dist/hooks/useEffect.d.ts.map +0 -1
  251. package/dist/hooks/useEffect.js +0 -34
  252. package/dist/hooks/useEffect.js.map +0 -1
  253. package/dist/hooks/useEffectEvent.d.ts +0 -8
  254. package/dist/hooks/useEffectEvent.d.ts.map +0 -1
  255. package/dist/hooks/useEffectEvent.js +0 -23
  256. package/dist/hooks/useEffectEvent.js.map +0 -1
  257. package/dist/hooks/useId.d.ts +0 -8
  258. package/dist/hooks/useId.d.ts.map +0 -1
  259. package/dist/hooks/useId.js +0 -35
  260. package/dist/hooks/useId.js.map +0 -1
  261. package/dist/hooks/useLayoutEffect.d.ts +0 -8
  262. package/dist/hooks/useLayoutEffect.d.ts.map +0 -1
  263. package/dist/hooks/useLayoutEffect.js +0 -34
  264. package/dist/hooks/useLayoutEffect.js.map +0 -1
  265. package/dist/hooks/useMemo.d.ts +0 -8
  266. package/dist/hooks/useMemo.d.ts.map +0 -1
  267. package/dist/hooks/useMemo.js +0 -31
  268. package/dist/hooks/useMemo.js.map +0 -1
  269. package/dist/hooks/usePromise.d.ts +0 -8
  270. package/dist/hooks/usePromise.d.ts.map +0 -1
  271. package/dist/hooks/usePromise.js +0 -90
  272. package/dist/hooks/usePromise.js.map +0 -1
  273. package/dist/hooks/useReducer.d.ts +0 -7
  274. package/dist/hooks/useReducer.d.ts.map +0 -1
  275. package/dist/hooks/useReducer.js +0 -44
  276. package/dist/hooks/useReducer.js.map +0 -1
  277. package/dist/hooks/useRef.d.ts +0 -10
  278. package/dist/hooks/useRef.d.ts.map +0 -1
  279. package/dist/hooks/useRef.js +0 -29
  280. package/dist/hooks/useRef.js.map +0 -1
  281. package/dist/hooks/useState.d.ts +0 -7
  282. package/dist/hooks/useState.d.ts.map +0 -1
  283. package/dist/hooks/useState.js +0 -54
  284. package/dist/hooks/useState.js.map +0 -1
  285. package/dist/hooks/useSyncExternalStore.d.ts +0 -8
  286. package/dist/hooks/useSyncExternalStore.d.ts.map +0 -1
  287. package/dist/hooks/useSyncExternalStore.js +0 -50
  288. package/dist/hooks/useSyncExternalStore.js.map +0 -1
  289. package/dist/hooks/useViewTransition.d.ts +0 -10
  290. package/dist/hooks/useViewTransition.d.ts.map +0 -1
  291. package/dist/hooks/useViewTransition.js +0 -27
  292. package/dist/hooks/useViewTransition.js.map +0 -1
  293. package/dist/recursiveRender.d.ts +0 -7
  294. package/dist/recursiveRender.d.ts.map +0 -1
  295. package/dist/recursiveRender.js.map +0 -1
  296. package/dist/signals/for.d.ts.map +0 -1
  297. package/dist/signals/for.js.map +0 -1
  298. package/dist/signals/watch.d.ts +0 -21
  299. package/dist/signals/watch.d.ts.map +0 -1
  300. package/dist/signals/watch.js +0 -86
  301. package/dist/signals/watch.js.map +0 -1
  302. package/dist/store.d.ts +0 -28
  303. package/dist/store.d.ts.map +0 -1
  304. package/dist/store.js +0 -166
  305. package/dist/store.js.map +0 -1
  306. package/dist/swr.d.ts +0 -63
  307. package/dist/swr.d.ts.map +0 -1
  308. package/dist/swr.js +0 -236
  309. package/dist/swr.js.map +0 -1
  310. package/dist/utils/promise.d.ts +0 -16
  311. package/dist/utils/promise.d.ts.map +0 -1
  312. package/dist/utils/promise.js +0 -14
  313. package/dist/utils/promise.js.map +0 -1
  314. package/src/components/memo.ts +0 -39
  315. package/src/form/index.ts +0 -676
  316. package/src/form/types.ts +0 -262
  317. package/src/form/utils.ts +0 -19
  318. package/src/hooks/useAsync.ts +0 -121
  319. package/src/hooks/useCallback.ts +0 -32
  320. package/src/hooks/useContext.ts +0 -79
  321. package/src/hooks/useEffect.ts +0 -40
  322. package/src/hooks/useEffectEvent.ts +0 -24
  323. package/src/hooks/useId.ts +0 -42
  324. package/src/hooks/useLayoutEffect.ts +0 -43
  325. package/src/hooks/useMemo.ts +0 -34
  326. package/src/hooks/usePromise.ts +0 -126
  327. package/src/hooks/useReducer.ts +0 -50
  328. package/src/hooks/useRef.ts +0 -41
  329. package/src/hooks/useState.ts +0 -62
  330. package/src/hooks/useSyncExternalStore.ts +0 -59
  331. package/src/hooks/useViewTransition.ts +0 -25
  332. package/src/signals/watch.ts +0 -139
  333. package/src/store.ts +0 -245
  334. package/src/swr.ts +0 -351
  335. package/src/utils/promise.ts +0 -26
package/src/form/types.ts DELETED
@@ -1,262 +0,0 @@
1
- type ObjectValuesArray<T extends object> = T[keyof T][]
2
-
3
- type IsObject<T> = T extends object ? (T extends any[] ? false : true) : false
4
-
5
- type InferKeyWithIndices<T, Prefix extends string = ""> = {
6
- [K in keyof T & string]: T[K] extends Array<infer U> | undefined
7
- ? IsObject<U> extends true
8
- ? // If it's an array of objects, infer the index and recurse into the object properties
9
- | `${Prefix}${K}`
10
- | `${Prefix}${K}.${number}`
11
- | InferKeyWithIndices<U, `${Prefix}${K}.${number}.`>
12
- : `${Prefix}${K}` | `${Prefix}${K}.${number}` // For arrays of primitives, only support indices
13
- : IsObject<T[K]> extends true
14
- ? // If it's an object, recurse into its properties
15
- `${Prefix}${K}` | InferKeyWithIndices<T[K], `${Prefix}${K}.`>
16
- : `${Prefix}${K}` // If it's a primitive, return the key
17
- }[keyof T & string]
18
-
19
- export type RecordKey<T extends Record<string, unknown>> =
20
- InferKeyWithIndices<T>
21
-
22
- type InferValue<
23
- T,
24
- Path extends string
25
- > = Path extends `${infer K}.${infer Rest}`
26
- ? K extends keyof T
27
- ? InferValue<T[K], Rest> // Recursively resolve objects
28
- : K extends `${number}` // Check if it's a numeric index for an array
29
- ? T extends Array<infer U> | undefined
30
- ? InferValue<U, Rest> // Recursively resolve array elements
31
- : never
32
- : never
33
- : Path extends keyof T
34
- ? T[Path] // Direct lookup for simple keys
35
- : Path extends `${number}` // If the path is numeric (array index)
36
- ? T extends Array<infer U> | undefined
37
- ? U // Resolve the array element type
38
- : never
39
- : never
40
-
41
- export type InferRecordKeyValue<
42
- T extends Record<string, unknown>,
43
- K extends RecordKey<T>
44
- > = InferValue<T, K>
45
-
46
- export type ValidatorContext<T> = {
47
- value: T
48
- }
49
-
50
- type FormFieldValidator<T extends unknown> = (
51
- context: ValidatorContext<T>
52
- ) => any
53
-
54
- export type AsyncValidatorContext<T> = {
55
- value: T
56
- abortSignal: AbortSignal
57
- }
58
-
59
- type AsyncFormFieldValidator<T extends unknown> = (
60
- context: AsyncValidatorContext<T>
61
- ) => Promise<any>
62
-
63
- export type FormFieldValidators<RecordKey extends string, Value> = {
64
- dependentOn: RecordKey[]
65
- onBlur: FormFieldValidator<Value>
66
- onChange: FormFieldValidator<Value>
67
- onChangeAsyncDebounceMs: number
68
- onChangeAsync: AsyncFormFieldValidator<Value>
69
- onMount: FormFieldValidator<Value>
70
- onSubmit: FormFieldValidator<Value>
71
- }
72
-
73
- type Falsy = false | null | undefined
74
-
75
- type InferValidatorReturn<T> = T extends Falsy
76
- ? never
77
- : T extends Promise<infer U>
78
- ? InferValidatorReturn<U>
79
- : T
80
-
81
- type InferFormFieldErrors<
82
- RecordKey extends string,
83
- T extends FormFieldValidators<RecordKey, any>
84
- > = ObjectValuesArray<{
85
- [K in keyof T]: T[K] extends FormFieldValidator<any>
86
- ? InferValidatorReturn<ReturnType<T[K]>>
87
- : never
88
- }>
89
-
90
- export type FormFieldContext<
91
- T extends Record<string, unknown>,
92
- Name extends RecordKey<T>,
93
- Validators extends FormFieldValidators<
94
- RecordKey<T>,
95
- InferRecordKeyValue<T, Name>
96
- >,
97
- IsArray extends Boolean
98
- > = {
99
- name: Name
100
- state: {
101
- value: InferRecordKeyValue<T, Name>
102
- errors: InferFormFieldErrors<RecordKey<T>, Validators>
103
- isTouched: boolean
104
- isValidating: boolean
105
- }
106
- handleChange: (value: InferRecordKeyValue<T, Name>) => void
107
- handleBlur: () => void
108
- } & (IsArray extends true
109
- ? {
110
- items: {
111
- replace: (
112
- index: number,
113
- value: InferRecordKeyValue<T, Name> extends Array<infer U> ? U : any
114
- ) => void
115
- push: (
116
- value: InferRecordKeyValue<T, Name> extends Array<infer U> ? U : any
117
- ) => void
118
- remove: (index: number) => void
119
- }
120
- }
121
- : {})
122
-
123
- export type AnyFormFieldContext<
124
- T extends Record<string, any> = Record<string, any>
125
- > = FormFieldContext<T, any, any, any>
126
-
127
- export type FormFieldState<
128
- T extends Record<string, any>,
129
- K extends RecordKey<T>
130
- > = {
131
- value: InferRecordKeyValue<T, K>
132
- errors: InferFormFieldErrors<
133
- K,
134
- FormFieldValidators<K, InferRecordKeyValue<T, K>>
135
- >
136
- isTouched: boolean
137
- isValidating: boolean
138
- }
139
-
140
- export type FormFieldProps<
141
- T extends Record<string, unknown>,
142
- Name extends RecordKey<T>,
143
- Validators extends FormFieldValidators<
144
- RecordKey<T>,
145
- InferRecordKeyValue<T, Name>
146
- >,
147
- IsArray extends boolean
148
- > = {
149
- name: Name
150
- validators?: Partial<Validators>
151
-
152
- array?: IsArray
153
- children: (
154
- context: FormFieldContext<T, Name, Validators, IsArray>
155
- ) => JSX.Element
156
- }
157
-
158
- export type FormFieldComponent<T extends Record<string, unknown>> = <
159
- Name extends RecordKey<T>,
160
- Validators extends FormFieldValidators<
161
- RecordKey<T>,
162
- InferRecordKeyValue<T, Name>
163
- >,
164
- IsArray extends boolean
165
- >(
166
- props: FormFieldProps<T, Name, Validators, IsArray>
167
- ) => JSX.Element
168
-
169
- export type SelectorState<T extends Record<string, unknown>> = {
170
- values: T
171
- canSubmit: boolean
172
- isSubmitting: boolean
173
- }
174
-
175
- export type FormSubscribeProps<
176
- T extends Record<string, unknown>,
177
- SelectorFunction extends (state: SelectorState<T>) => unknown,
178
- U
179
- > = {
180
- selector: SelectorFunction
181
- children: (selection: U) => JSX.Element
182
- }
183
-
184
- type FormSubscribeComponent<T extends Record<string, unknown>> = <
185
- Selector extends (state: SelectorState<T>) => unknown
186
- >(
187
- props: FormSubscribeProps<T, Selector, ReturnType<Selector>>
188
- ) => JSX.Element
189
-
190
- export type UseFormState<T extends Record<string, unknown>> = {
191
- Field: FormFieldComponent<T>
192
- Subscribe: FormSubscribeComponent<T>
193
- getFieldState: <K extends RecordKey<T>>(name: K) => FormFieldState<T, K>
194
- handleSubmit: () => Promise<void>
195
- reset: () => void
196
- }
197
-
198
- export type FormContext<T extends Record<string, unknown>> = {
199
- state: T
200
- validateForm: () => Promise<any[]>
201
- resetField: (name: RecordKey<T>) => void
202
- deleteField: (name: RecordKey<T>) => void
203
- setFieldValue: <K extends RecordKey<T>>(
204
- name: K,
205
- value: InferRecordKeyValue<T, K>
206
- ) => void
207
- }
208
-
209
- export type UseFormConfig<T extends Record<string, unknown>> = {
210
- initialValues?: T
211
- onSubmit?: (ctx: FormContext<T>) => void | Promise<void>
212
- onSubmitInvalid?: (ctx: FormContext<T>) => void | Promise<void>
213
- }
214
-
215
- export interface FormStateSubscriber<T extends Record<string, unknown>> {
216
- selector: (state: SelectorState<T>) => unknown
217
- selection: ReturnType<this["selector"]>
218
- update: () => void
219
- }
220
-
221
- export type UseFormInternalState<T extends Record<string, unknown>> = {
222
- Field: FormFieldComponent<T>
223
- Subscribe: FormSubscribeComponent<T>
224
- formController: FormController<T>
225
- }
226
-
227
- export type FormController<T extends Record<string, unknown>> = {
228
- subscribers: Set<FormStateSubscriber<T>>
229
- state: T
230
- validateField: (
231
- name: RecordKey<T>,
232
- type: "onChange" | "onSubmit" | "onMount" | "onBlur"
233
- ) => Promise<void>
234
- getFieldState: <K extends RecordKey<T>>(name: K) => FormFieldState<T, K>
235
- setFieldValue: (
236
- name: RecordKey<T>,
237
- value: InferRecordKeyValue<T, RecordKey<T>>
238
- ) => void
239
- arrayFieldReplace: <K extends RecordKey<T>>(
240
- name: K,
241
- index: number,
242
- value: InferRecordKeyValue<T, K> extends Array<infer U> ? U : any
243
- ) => void
244
- arrayFieldPush: <K extends RecordKey<T>>(
245
- name: K,
246
- value: InferRecordKeyValue<T, K> extends Array<infer U> ? U : any
247
- ) => void
248
- arrayFieldRemove: (name: RecordKey<T>, index: number) => void
249
- connectField: (name: RecordKey<T>, update: () => void) => void
250
- disconnectField: (name: RecordKey<T>, update: () => void) => void
251
- setFieldValidators: <K extends RecordKey<T>>(
252
- name: K,
253
- validators: Partial<
254
- FormFieldValidators<RecordKey<T>, InferRecordKeyValue<T, K>>
255
- >
256
- ) => void
257
- getSelectorState: () => any
258
- validateForm: () => Promise<string[]>
259
- getFormContext: () => FormContext<T>
260
- reset: (values?: T) => void
261
- setSubmitting: (isSubmitting: boolean) => void
262
- }
package/src/form/utils.ts DELETED
@@ -1,19 +0,0 @@
1
- export const objSet = (
2
- obj: Record<string, any>,
3
- path: string[],
4
- value: any
5
- ) => {
6
- let o = obj
7
- for (let i = 0; i < path.length; i++) {
8
- const key = path[i]
9
- if (i === path.length - 1) {
10
- o[key] = value
11
- } else {
12
- o = o[key]
13
- }
14
- }
15
- }
16
-
17
- export const objGet = <T>(obj: Record<string, any>, path: string[]): T => {
18
- return path.reduce((o, key) => o[key], obj) as T
19
- }
@@ -1,121 +0,0 @@
1
- import { __DEV__ } from "../env.js"
2
- import { noop, sideEffectsEnabled } from "../utils/index.js"
3
- import { depsRequireChange, useHook } from "./utils.js"
4
- import type { AsyncTaskState } from "../types.utils.js"
5
-
6
- export type UseAsyncState<T> = AsyncTaskState<T, UseAsyncError> & {
7
- invalidate: () => void
8
- }
9
-
10
- export type UseAsyncCallbackContext = {
11
- abortSignal: AbortSignal
12
- }
13
-
14
- export class UseAsyncError extends Error {
15
- constructor(message: unknown) {
16
- super(message instanceof Error ? message.message : String(message))
17
- this.name = "UseAsyncError"
18
- this.cause = message
19
- }
20
- }
21
-
22
- type InternalTaskState<T> = {
23
- data: T | null
24
- loading: boolean
25
- error: Error | null
26
- abortController: AbortController
27
- }
28
- /**
29
- * Runs an asynchronous function on initial render, or when a value provided in the [dependency
30
- * array](https://kirujs.dev/docs/hooks/dependency-arrays) has changed.
31
- *
32
- * @see https://kirujs.dev/docs/hooks/useAsync
33
- */
34
- export function useAsync<T>(
35
- func: (ctx: UseAsyncCallbackContext) => Promise<T>,
36
- deps: unknown[]
37
- ): UseAsyncState<T> {
38
- if (!sideEffectsEnabled())
39
- return {
40
- data: null,
41
- loading: true,
42
- error: null,
43
- invalidate: noop,
44
- }
45
- return useHook(
46
- "useAsync",
47
- {
48
- deps,
49
- id: 0,
50
- task: null as any as InternalTaskState<T>,
51
- load: noop as (
52
- func: (ctx: UseAsyncCallbackContext) => Promise<T>
53
- ) => void,
54
- },
55
- ({ hook, isInit, isHMR, update }) => {
56
- if (__DEV__) {
57
- hook.dev = { devtools: { get: () => ({ value: hook.task }) } }
58
- if (isHMR) {
59
- isInit = true
60
- }
61
- }
62
- if (isInit) {
63
- hook.cleanup = () => abortTask(hook.task)
64
- hook.load = (func) => {
65
- let invalidated = false
66
- const abortController = new AbortController()
67
- abortController.signal.addEventListener("abort", () => {
68
- invalidated = true
69
- })
70
- const id = ++hook.id
71
- const task: InternalTaskState<T> = (hook.task = {
72
- abortController,
73
- data: null,
74
- loading: true,
75
- error: null,
76
- })
77
- func({ abortSignal: abortController.signal })
78
- .then((result: T) => {
79
- if (id !== hook.id) abortTask(task)
80
- if (invalidated) return
81
-
82
- task.data = result
83
- task.loading = false
84
- task.error = null
85
- update()
86
- })
87
- .catch((error) => {
88
- if (id !== hook.id) abortTask(task)
89
- if (invalidated) return
90
-
91
- task.data = null
92
- task.loading = false
93
- task.error = new UseAsyncError(error)
94
- update()
95
- })
96
- }
97
- }
98
-
99
- if (isInit || depsRequireChange(deps, hook.deps)) {
100
- abortTask(hook.task)
101
- hook.deps = deps
102
- hook.load(func)
103
- }
104
-
105
- const { abortController, ...rest } = hook.task
106
- return {
107
- ...rest,
108
- invalidate: () => {
109
- abortTask(hook.task)
110
- hook.load(func)
111
- update()
112
- },
113
- } as UseAsyncState<T>
114
- }
115
- )
116
- }
117
-
118
- function abortTask<T>(task: InternalTaskState<T> | null): void {
119
- if (task === null || task.abortController.signal.aborted) return
120
- task.abortController.abort()
121
- }
@@ -1,32 +0,0 @@
1
- import { __DEV__ } from "../env.js"
2
- import { sideEffectsEnabled } from "../utils/index.js"
3
- import { depsRequireChange, useHook } from "./utils.js"
4
-
5
- /**
6
- * Creates a memoized callback function.
7
- *
8
- * @see https://kirujs.dev/docs/hooks/useCallback
9
- */
10
- export function useCallback<T extends Function>(
11
- callback: T,
12
- deps: unknown[]
13
- ): T {
14
- if (!sideEffectsEnabled()) return callback
15
- return useHook("useCallback", { callback, deps }, ({ hook, isHMR }) => {
16
- if (__DEV__) {
17
- hook.dev = {
18
- devtools: {
19
- get: () => ({ callback: hook.callback, dependencies: hook.deps }),
20
- },
21
- }
22
- if (isHMR) {
23
- hook.deps = []
24
- }
25
- }
26
- if (depsRequireChange(deps, hook.deps)) {
27
- hook.deps = deps
28
- hook.callback = callback
29
- }
30
- return hook.callback
31
- })
32
- }
@@ -1,79 +0,0 @@
1
- import type { ContextProviderNode } from "../types.utils.js"
2
- import { type HookCallbackState, useHook } from "./utils.js"
3
- import { __DEV__ } from "../env.js"
4
- import { $CONTEXT_PROVIDER } from "../constants.js"
5
-
6
- type UseContextHookState<T> = {
7
- provider?: ContextProviderNode<T>
8
- context: Kiru.Context<T>
9
- warnIfNotFound: boolean
10
- }
11
-
12
- /**
13
- * Gets the current value of a context provider created by the context.
14
- *
15
- * @see https://kirujs.dev/docs/hooks/useContext
16
- */
17
- export function useContext<T>(
18
- context: Kiru.Context<T>,
19
- warnIfNotFound = true
20
- ): T {
21
- return useHook(
22
- "useContext",
23
- {
24
- context,
25
- warnIfNotFound,
26
- } satisfies UseContextHookState<T>,
27
- useContextCallback as typeof useContextCallback<T>
28
- )
29
- }
30
-
31
- const useContextCallback = <T>({
32
- hook,
33
- isInit,
34
- vNode,
35
- }: HookCallbackState<UseContextHookState<T>>) => {
36
- if (__DEV__) {
37
- hook.dev = {
38
- devtools: {
39
- get: () => ({
40
- contextName: hook.context.Provider.displayName || "",
41
- value: hook.provider
42
- ? hook.provider.props.value
43
- : hook.context.default(),
44
- }),
45
- },
46
- }
47
- }
48
- if (isInit) {
49
- let n = vNode.parent
50
- while (n) {
51
- if (n.type === $CONTEXT_PROVIDER) {
52
- const provider = n as ContextProviderNode<T>
53
- const { ctx, value, dependents } = provider.props
54
- if (ctx === hook.context) {
55
- dependents.add(vNode)
56
- hook.cleanup = () => dependents.delete(vNode)
57
- hook.provider = provider
58
- return value
59
- }
60
- }
61
- n = n.parent
62
- }
63
- }
64
- if (!hook.provider) {
65
- if (__DEV__) {
66
- hook.warnIfNotFound && warnProviderNotFound(hook.context)
67
- }
68
- return hook.context.default()
69
- }
70
- return hook.provider.props.value
71
- }
72
-
73
- const contextsNotFound = new Set<Kiru.Context<any>>()
74
- function warnProviderNotFound(ctx: Kiru.Context<any>) {
75
- if (!contextsNotFound.has(ctx)) {
76
- contextsNotFound.add(ctx)
77
- console.warn("[kiru]: Unable to find context provider")
78
- }
79
- }
@@ -1,40 +0,0 @@
1
- import { sideEffectsEnabled } from "../utils/index.js"
2
- import { __DEV__ } from "../env.js"
3
- import { cleanupHook, depsRequireChange, useHook } from "./utils.js"
4
-
5
- /**
6
- * Runs a function after the component is rendered, or when a value provided in the optional [dependency
7
- * array](https://kirujs.dev/docs/hooks/dependency-arrays) has changed.
8
- *
9
- * @see https://kirujs.dev/docs/hooks/useEffect
10
- * */
11
- export function useEffect(
12
- callback: () => void | (() => void),
13
- deps?: unknown[]
14
- ): void {
15
- if (!sideEffectsEnabled()) return
16
- return useHook(
17
- "useEffect",
18
- { deps },
19
- ({ hook, isInit, isHMR, queueEffect }) => {
20
- if (__DEV__) {
21
- hook.dev = {
22
- devtools: { get: () => ({ callback, dependencies: hook.deps }) },
23
- }
24
- if (isHMR) {
25
- isInit = true
26
- }
27
- }
28
- if (isInit || depsRequireChange(deps, hook.deps)) {
29
- hook.deps = deps
30
- cleanupHook(hook)
31
- queueEffect(() => {
32
- const cleanup = callback()
33
- if (typeof cleanup === "function") {
34
- hook.cleanup = cleanup
35
- }
36
- })
37
- }
38
- }
39
- )
40
- }
@@ -1,24 +0,0 @@
1
- import { sideEffectsEnabled } from "../utils/index.js"
2
- import { node } from "../globals.js"
3
- import { useHook } from "./utils.js"
4
-
5
- /**
6
- * Wraps a function to be called within effects and other callbacks.
7
- * The function will be called with the same arguments as the original function.
8
- *
9
- * @see https://kirujs.dev/docs/hooks/useEffectEvent
10
- */
11
- export function useEffectEvent<T extends Function>(callback: T): T {
12
- if (!sideEffectsEnabled()) return callback
13
- return useHook("useEffectEvent", { callback }, ({ hook }) => {
14
- hook.callback = callback
15
- return function () {
16
- if (node.current) {
17
- throw new Error(
18
- "A function wrapped in useEffectEvent can't be called during rendering."
19
- )
20
- }
21
- return hook.callback.apply(void 0, arguments)
22
- } as any as T
23
- })
24
- }
@@ -1,42 +0,0 @@
1
- import { __DEV__ } from "../env.js"
2
- import { HookCallback, useHook } from "./utils.js"
3
-
4
- /**
5
- * Creates a unique id for the current node. This is derived based on the node's position in your application tree.
6
- * Useful for assigning predictable ids to elements.
7
- *
8
- * @see https://kirujs.dev/docs/hooks/useId
9
- */
10
- export function useId(): string {
11
- return useHook("useId", createUseIdState, useIdCallback)
12
- }
13
-
14
- type UseIdState = {
15
- id: string
16
- idx: number
17
- }
18
-
19
- const createUseIdState = (): UseIdState => ({
20
- id: "",
21
- idx: 0,
22
- })
23
-
24
- const useIdCallback: HookCallback<UseIdState> = ({ hook, isInit, vNode }) => {
25
- if (__DEV__) {
26
- hook.dev = {
27
- devtools: { get: () => ({ id: hook.id }) },
28
- }
29
- }
30
- if (isInit || vNode.index !== hook.idx) {
31
- hook.idx = vNode.index
32
- const accumulator: number[] = []
33
- let n: Kiru.VNode | null = vNode
34
- while (n) {
35
- accumulator.push(n.index)
36
- accumulator.push(n.depth)
37
- n = n.parent
38
- }
39
- hook.id = `k:${BigInt(accumulator.join("")).toString(36)}`
40
- }
41
- return hook.id
42
- }
@@ -1,43 +0,0 @@
1
- import { sideEffectsEnabled } from "../utils/index.js"
2
- import { __DEV__ } from "../env.js"
3
- import { cleanupHook, depsRequireChange, useHook } from "./utils.js"
4
-
5
- /**
6
- * Runs a function before the component is rendered, or when a value provided in the optional [dependency
7
- * array](https://kirujs.dev/docs/hooks/dependency-arrays) has changed.
8
- *
9
- * @see https://kirujs.dev/docs/hooks/useLayoutEffect
10
- * */
11
- export function useLayoutEffect(
12
- callback: () => void | (() => void),
13
- deps?: unknown[]
14
- ): void {
15
- if (!sideEffectsEnabled()) return
16
- return useHook(
17
- "useLayoutEffect",
18
- { deps },
19
- ({ hook, isInit, isHMR, queueEffect }) => {
20
- if (__DEV__) {
21
- hook.dev = {
22
- devtools: { get: () => ({ callback, dependencies: hook.deps }) },
23
- }
24
- if (isHMR) {
25
- isInit = true
26
- }
27
- }
28
- if (isInit || depsRequireChange(deps, hook.deps)) {
29
- hook.deps = deps
30
- cleanupHook(hook)
31
- queueEffect(
32
- () => {
33
- const cleanup = callback()
34
- if (typeof cleanup === "function") {
35
- hook.cleanup = cleanup
36
- }
37
- },
38
- { immediate: true }
39
- )
40
- }
41
- }
42
- )
43
- }
@@ -1,34 +0,0 @@
1
- import { sideEffectsEnabled } from "../utils/index.js"
2
- import { __DEV__ } from "../env.js"
3
- import { depsRequireChange, useHook } from "./utils.js"
4
-
5
- /**
6
- * Creates a memoized value that only changes when the [dependency
7
- * array](https://kirujs.dev/docs/hooks/dependency-arrays) has changed.
8
- *
9
- * @see https://kirujs.dev/docs/hooks/useMemo
10
- */
11
- export function useMemo<T>(factory: () => T, deps: unknown[]): T {
12
- if (!sideEffectsEnabled()) return factory()
13
- return useHook(
14
- "useMemo",
15
- { deps, value: undefined as T },
16
- ({ hook, isInit, isHMR }) => {
17
- if (__DEV__) {
18
- hook.dev = {
19
- devtools: {
20
- get: () => ({ value: hook.value, dependencies: hook.deps }),
21
- },
22
- }
23
- if (isHMR) {
24
- isInit = true
25
- }
26
- }
27
- if (isInit || depsRequireChange(deps, hook.deps)) {
28
- hook.deps = deps
29
- hook.value = factory()
30
- }
31
- return hook.value
32
- }
33
- )
34
- }