-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
alpha.cljs
395 lines (324 loc) · 9.71 KB
/
alpha.cljs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
;; This Source Code Form is subject to the terms of the Mozilla Public
;; License, v. 2.0. If a copy of the MPL was not distributed with this
;; file, You can obtain one at http://mozilla.org/MPL/2.0/.
;;
;; Copyright (c) Andrey Antukh <niwi@niwi.nz>
(ns rumext.alpha
(:refer-clojure :exclude [ref deref])
(:require-macros [rumext.alpha :refer [defc fnc]])
(:require
["react" :as react]
["react/jsx-runtime" :as jsxrt]
["react-dom/client" :as rdom]
[cljs.core :as c]
[goog.functions :as gf]
[rumext.util :as util]))
(def noop (constantly nil))
(def Component react/Component)
(def Fragment react/Fragment)
(def Profiler react/Profiler)
(defn create-element
([type props] (create-element type props nil))
([type props children]
(let [props (js/Object.assign
(if (nil? children)
#js {}
(if (array? children)
#js {:children children}
#js {:children #js [children]}))
props)
key (unchecked-get props "key")]
(jsxrt/jsxs type props key))))
(defn forward-ref
[component]
(react/forwardRef component))
;; --- Impl
(extend-type cljs.core.UUID
INamed
(-name [this] (js* "\"\" + ~{}" this))
(-namespace [_] ""))
;; --- Main Api
(defn create-root
[node]
(rdom/createRoot node))
(defn hydrate-root
[node element]
(rdom/hydrateRoot node))
(defn mount
"Add element to the DOM tree. Idempotent. Subsequent mounts will
just update element."
[root element]
(.render ^js root element))
(defn unmount
"Removes component from the DOM tree."
[root]
(.unmount ^js root))
(defn portal
"Render `element` in a DOM `node` that is ouside of current DOM hierarchy."
[element node]
(rdom/createPortal element node))
(defn create-ref
[]
(react/createRef))
(defn ref-val
"Given state and ref handle, returns React component."
[ref]
(unchecked-get ref "current"))
(defn set-ref-val!
[ref val]
(unchecked-set ref "current" val)
val)
;; --- Context API
(defn create-context
([]
(react/createContext nil))
([value]
(react/createContext value)))
(defn provider
[ctx]
(unchecked-get ctx "Provider"))
;; --- Raw Hooks
(defn useRef
[initial]
(react/useRef initial))
(defn useState
[initial]
(react/useState initial))
(defn useEffect
[f deps]
(react/useEffect f deps))
(defn useMemo
[f deps]
(react/useMemo f deps))
(defn useCallback
[f deps]
(react/useCallback f deps))
(defn useLayoutEffect
[f deps]
(react/useLayoutEffect f deps))
(defn useContext
[ctx]
(react/useContext ctx))
(defn useTransition
[]
(react/useTransition))
;; --- Hooks
(defprotocol IDepsAdapter
(adapt [o] "adapt dep if proceed"))
(extend-protocol IDepsAdapter
default
(adapt [o] o)
cljs.core.UUID
(adapt [o] (.toString ^js o))
cljs.core.Keyword
(adapt [o] (.toString ^js o)))
;; "A convenience function that translates the list of arguments into a
;; valid js array for use in the deps list of hooks.
(defn deps
([] #js [])
([a] #js [(adapt a)])
([a b] #js [(adapt a) (adapt b)])
([a b c] #js [(adapt a) (adapt b) (adapt c)])
([a b c d] #js [(adapt a) (adapt b) (adapt c) (adapt d)])
([a b c d e] #js [(adapt a) (adapt b) (adapt c) (adapt d) (adapt e)])
([a b c d e f] #js [(adapt a) (adapt b) (adapt c) (adapt d) (adapt e) (adapt f)])
([a b c d e f g] #js [(adapt a) (adapt b) (adapt c) (adapt d) (adapt e) (adapt f) (adapt g)])
([a b c d e f g h] #js [(adapt a) (adapt b) (adapt c) (adapt d) (adapt e) (adapt f) (adapt g) (adapt h)])
([a b c d e f g h & rest] (into-array (map adapt (into [a b c d e f g h] rest)))))
(defn use-ref
([] (react/useRef nil))
([initial] (react/useRef initial)))
(defn use-ctx
[ctx]
(react/useContext ctx))
(defn start-transition
[f]
(react/startTransition f))
(defn use-effect
([f] (use-effect #js [] f))
([deps f]
(useEffect #(let [r (f)] (if (fn? r) r noop)) deps)))
(defn use-layout-effect
([f] (use-layout-effect #js [] f))
([deps f]
(useLayoutEffect #(let [r (f)] (if (fn? r) r noop)) deps)))
(defn use-memo
([f] (useMemo f #js []))
([deps f] (useMemo f deps)))
(defn use-callback
([f] (useCallback f #js []))
([deps f] (useCallback f deps)))
(defn use-fn
"A convenient alias to useCallback"
([f] (useCallback f #js []))
([deps f] (useCallback f deps)))
(defn use-transition
[]
(let [tmp (useTransition)
is-pending (aget tmp 0)
start-fn (aget tmp 1)]
(use-memo
#js [is-pending]
(fn []
(specify! (fn [cb-fn]
(^function start-fn cb-fn))
cljs.core/IDeref
(-deref [_] is-pending))))))
(defn use-id
[]
(react/useId))
(defn deref
[iref]
(let [state (use-ref (c/deref iref))
key (use-id)
get-state (use-fn #(unchecked-get state "current"))
subscribe (use-fn
#js [iref]
(fn [^function listener-fn]
(add-watch iref key (fn [_ _ _ newv]
(unchecked-set state "current" newv)
(listener-fn)))
#(remove-watch iref key)))]
(react/useSyncExternalStore subscribe get-state)))
(defn use-state
([] (use-state nil))
([initial]
(let [tmp (useState initial)
state (aget tmp 0)
update-fn (aget tmp 1)]
(use-memo
#js [state]
(fn []
(reify
c/IReset
(-reset! [_ value]
(^function update-fn value))
c/ISwap
(-swap! [self f]
(^function update-fn f))
(-swap! [self f x]
(^function update-fn #(f % x)))
(-swap! [self f x y]
(^function update-fn #(f % x y)))
(-swap! [self f x y more]
(^function update-fn #(apply f % x y more)))
c/IDeref
(-deref [_] state)))))))
(defn use-var
"A custom hook for define mutable variables that persists
on renders (based on useRef hook)."
([] (use-var nil))
([initial]
(let [ref (useRef initial)]
(use-memo
#js []
#(specify! (fn [val] (set-ref-val! ref val))
c/IReset
(-reset! [_ new-value]
(set-ref-val! ref new-value))
c/ISwap
(-swap!
([self f]
(set-ref-val! ref (f (ref-val ref))))
([self f x]
(set-ref-val! ref (f (ref-val ref) x)))
([self f x y]
(set-ref-val! ref (f (ref-val ref) x y)))
([self f x y more]
(set-ref-val! ref (apply f (ref-val ref) x y more))))
c/IDeref
(-deref [self] (ref-val ref)))))))
;; --- Other API
(defn element
([klass]
(create-element klass #js {}))
([klass props]
(let [props (cond
(object? props) props
(map? props) (util/map->obj props)
:else (throw (ex-info "Unexpected props" {:props props})))]
(create-element klass props))))
;; --- Higher-Order Components
(defn memo'
"A raw variant of React.memo."
[component equals?]
(react/memo component equals?))
(defn memo
([component] (react/memo component))
([component eq?]
(react/memo component #(util/props-equals? eq? %1 %2))))
(defn catch
[component {:keys [fallback on-error]}]
(let [constructor
(fn [props]
(this-as this
(unchecked-set this "state" #js {})
(.call Component this props)))
did-catch
(fn [error info]
(when (fn? on-error)
(on-error error info)))
derive-state
(fn [error]
#js {:error error})
render
(fn []
(this-as this
(let [state (unchecked-get this "state")
props (unchecked-get this "props")
error (unchecked-get state "error")]
(if error
(react/createElement fallback #js {:error error})
(react/createElement component props)))))
_ (goog/inherits constructor Component)
prototype (unchecked-get constructor "prototype")]
(unchecked-set constructor "displayName" "ErrorBoundary")
(unchecked-set constructor "getDerivedStateFromError" derive-state)
(unchecked-set prototype "componentDidCatch" did-catch)
(unchecked-set prototype "render" render)
constructor))
(def ^:private schedule
(or (and (exists? js/window) js/window.requestAnimationFrame)
#(js/setTimeout % 16)))
(defn deferred
([component] (deferred component schedule))
([component sfn]
(fnc deferred
{::wrap-props false}
[props]
(let [tmp (useState false)
^boolean render? (aget tmp 0)
^js set-render (aget tmp 1)]
(use-effect (fn [] (^js sfn #(set-render true))))
(when render? (create-element component props))))))
(defn throttle
[component ms]
(fnc throttle
{::wrap-props false}
[props]
(let [tmp (useState props)
state (aget tmp 0)
set-state (aget tmp 1)
ref (use-ref false)
render (use-memo #(gf/throttle
(fn [v]
(when-not (ref-val ref)
(^js set-state v)))
ms))]
(use-effect nil #(render props))
(use-effect (fn [] #(set-ref-val! ref "true")))
(create-element component state))))
(defn check-props
"Utility function to use with `memo'`.
Will check the `props` keys to see if they are equal.
Usage:
(mf/defc my-component
{::mf/wrap [#(mf/memo' % (checkprops [\"prop1\" \"prop2\"]))]}
[props]
)"
([props] (check-props props =))
([props eqfn?]
(fn [np op]
(every? #(eqfn? (unchecked-get np %)
(unchecked-get op %))
props))))