-
Notifications
You must be signed in to change notification settings - Fork 51
/
core.cljs
204 lines (156 loc) · 4.81 KB
/
core.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
(ns helix.core
(:refer-clojure :exclude [type])
(:require [goog.object :as gobj]
[helix.impl.props :as impl.props]
[helix.impl.classes :as helix.class]
[cljs-bean.core :as bean]
["react" :as react])
(:require-macros [helix.core]))
(when (exists? js/Symbol)
(extend-protocol IPrintWithWriter
js/Symbol
(-pr-writer [sym writer _]
(-write writer (str "\"" (.toString sym) "\"")))))
(def Fragment react/Fragment)
(def Suspense react/Suspense)
(def create-element react/createElement)
(def create-context react/createContext)
;; this is to enable calling `(.createElement (get-react))` without doing
;; a dynamic arity dispatch. See https://github.com/Lokeh/helix/issues/20
(defn ^js/React get-react [] react)
(defn $
"Create a new React element from a valid React type.
Example:
```
($ MyComponent
\"child1\"
($ \"span\"
{:style {:color \"green\"}}
\"child2\" ))
```"
[type & args]
(let [?p (first args)
?c (rest args)
native? (or (keyword? type)
(string? type)
(:native (meta type)))
type' (if (keyword? type)
(name type)
type)]
(if (map? ?p)
(apply create-element
type'
(if native?
(impl.props/-dom-props ?p)
(impl.props/-props ?p))
?c)
(apply create-element
type'
nil
args))))
(def ^:deprecated $$
"Dynamically create a new React element from a valid React type.
`$` can typically be faster, because it will statically process the arguments
at macro-time if possible.
Example:
```
($$ MyComponent
\"child1\"
($$ \"span\"
{:style {:color \"green\"}}
\"child2\" ))
```"
$)
(defprotocol IExtractType
(-type [factory] "Extracts the underlying type from the factory function."))
(defn type
[f]
(-type f))
(defn factory
"Creates a factory function for a React component"
[type]
(-> (fn factory [& args]
(apply $ type args))
(specify! IExtractType
(-type [_] type))))
(defn cljs-factory
[type]
(-> (fn factory [& args]
(if (map? (first args))
(let [props (first args)]
(apply react/createElement
type
#js {"helix/props"
(dissoc props
:key
:ref)
"key" (get props :key js/undefined)
"ref" (get props :ref js/undefined)}
(rest args)))
(apply react/createElement
type
#js {}
args)))
(specify! IExtractType
(-type [_] type))))
(defn assoc-some [m k x]
(if (some? x)
(assoc m k x)
m))
(defn extract-cljs-props
[o]
(when (and ^boolean goog/DEBUG (or (map? o) (nil? o)))
(throw (ex-info "Props received were a map. This probably means you're calling your component as a function." {:props o})))
(if-let [props (gobj/get o "helix/props")]
(assoc-some props :children (gobj/get o "children"))
(bean/bean o)))
(defn memo
"Like React.memo, but passes props as CLJS map-likes instead of JS objects."
([component] (react/memo component))
([component compare]
(react/memo
component
(fn memo-compare
[o o']
(compare
(extract-cljs-props o)
(extract-cljs-props o'))))))
;;
;; -- class components
;;
(defn create-component [spec statics]
(let [render (.-render ^js spec)
render' (fn [this]
(render
this
(extract-cljs-props (.-props ^js this))
(.-state ^js this)))]
(gobj/set spec "render" render')
(helix.class/createComponent react/Component spec statics)))
(comment
(def MyComponent
(create-component #js {:displayName "Foo"
:constructor
(fn [this]
(set! (.-state this) #js {:count 3}))
:render
(fn [this props state]
(prn props state)
($$ "div" (.-count (.-state this))))}
nil))
(js/console.log MyComponent)
(rds/renderToString ($$ MyComponent {:foo "baz"})))
;;
;; -- React Fast Refresh
;;
(defn register!
"Registers a component with the React Fresh runtime.
`type` is the component function, and `id` is the unique ID assigned to it
(e.g. component name) for cache invalidation."
[type id]
(when (exists? (.-$$Register$$ js/window))
(.$$Register$$ js/window type id)))
(defn signature! []
;; grrr `maybe` bug strikes again
(and (exists? (.-$$Signature$$ js/window))
(.$$Signature$$ js/window)))