-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
alpha.cljs
348 lines (283 loc) · 8.81 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
;; 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) 2016-2020 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-dom" :as rdom]
["react/cjs/react-jsx-runtime.production.min" :as jsx-runtime]
[goog.functions :as gf]
[rumext.util :as util]))
(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 #js {} props (when ^boolean children #js {:children children}))]
(jsx-runtime/jsx type props (unchecked-get props "key")))))
(defn forward-ref
[component]
(react/forwardRef component))
;; --- Impl
(extend-type cljs.core.UUID
INamed
(-name [this] (str this))
(-namespace [_] ""))
;; --- Main Api
(defn mount
"Add element to the DOM tree. Idempotent. Subsequent mounts will
just update element."
[element node]
(rdom/render element node)
nil)
(defn unmount
"Removes component from the DOM tree."
[node]
(rdom/unmountComponentAtNode node))
(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))
;; --- Hooks
(defn- adapt
[o]
(if (uuid? o)
(str o)
(if (instance? cljs.core/INamed o)
(name o)
o)))
;; "A convenience function that translates the list of arguments into a
;; valid js array for use in the deps list of hooks.
;; It translates INamed to Strings and uuid instances to strings for
;; correct equality check (react uses equivalent to `identical?` for
;; check the equality and uuid and INamed objects always returns false
;; to this check).
(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)))))
;; The cljs version of use-ref and use-ctx is identical to the raw (no
;; customizations/adaptations needed)
(def use-ref useRef)
(def use-ctx useContext)
(defn use-state
[initial]
(let [tmp (useState initial)
state (aget tmp 0)
set-state (aget tmp 1)]
(reify
cljs.core/IReset
(-reset! [_ new-value]
(set-state new-value))
cljs.core/ISwap
(-swap! [self f]
(set-state f))
(-swap! [self f x]
(set-state #(f % x)))
(-swap! [self f x y]
(set-state #(f % x y)))
(-swap! [self f x y more]
(set-state #(apply f % x y more)))
cljs.core/IDeref
(-deref [self] state))))
(defn use-var
"A custom hook for define mutable variables that persists
on renders (based on useRef hook)."
[initial]
(let [ref (useRef initial)]
(useMemo
#(reify
cljs.core/IReset
(-reset! [_ new-value]
(set-ref-val! ref new-value))
cljs.core/ISwap
(-swap! [self f]
(set-ref-val! ref (f (ref-val ref))))
(-swap! [self f x]
(set-ref-val! ref (f (ref-val ref) x)))
(-swap! [self f x y]
(set-ref-val! ref (f (ref-val ref) x y)))
(-swap! [self f x y more]
(set-ref-val! ref (apply f (ref-val ref) x y more)))
cljs.core/IDeref
(-deref [self] (ref-val ref)))
#js [])))
(defn use-effect
([f] (use-effect #js [] f))
([deps f]
(useEffect #(let [r (^js f)] (if (fn? r) r identity)) deps)))
(defn use-layout-effect
([f] (use-layout-effect #js [] f))
([deps f]
(useLayoutEffect #(let [r (^js f)] (if (fn? r) r identity)) 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 deref
[iref]
(let [tmp (useState 0)
state (aget tmp 0)
set-state (aget tmp 1)
key (useMemo
#(let [key (js/Symbol "rumext.alpha/deref")]
(add-watch iref key (fn [_ _ _ _] (^js set-state inc)))
key)
#js [iref])]
(useEffect #(fn [] (remove-watch iref key))
#js [iref key])
(cljs.core/deref iref)))
;; --- 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))))