-
Notifications
You must be signed in to change notification settings - Fork 51
/
core.clj
206 lines (170 loc) · 6.52 KB
/
core.clj
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
(ns helix.core
(:require [helix.impl.analyzer :as hana]
[helix.impl.props :as impl.props]
[clojure.string :as string]))
(defmacro $
"Create a new React element from a valid React type.
Will try to statically convert props to a JS object. Falls back to `$$` when
ambiguous.
Example:
```
($ MyComponent
\"child1\"
($ \"span\"
{:style {:color \"green\"}}
\"child2\" ))
```"
[type & args]
(let [inferred (hana/inferred-type &env type)
native? (or (keyword? type)
(string? type)
(= inferred 'string)
(= inferred 'cljs.core/Keyword)
(:native (meta type)))
type (if (keyword? type)
(name type)
type)]
(cond
(map? (first args)) `^js/React.Element (create-element
~type
~(if native?
`(impl.props/native-props ~(first args))
`(impl.props/props ~(first args)))
~@(rest args))
:else `^js/React.Element (create-element ~type nil ~@args))))
(defmacro <>
"Creates a new React Fragment Element"
[& children]
`^js/React.Element ($ Fragment ~@children))
(defmacro provider
"Creates a Provider for a React Context value.
Example:
(def my-context (react/createContext))
(provider {:context my-context :value my-value} child1 child2 ...childN)"
[{:keys [context value] :as props} & children]
`^js/React.Element ($ (.-Provider ~context)
;; use contains to guard against `nil`
~@(when (contains? props :value)
`({:value ~value}))
~@children))
(defmacro suspense
"Creates a React Suspense boundary."
[{:keys [fallback]} & children]
`^js/React.Element ($ Suspense
~@(when fallback
`({:fallback ~fallback}))
~@children))
(defn- fnc*
[display-name props-bindings body]
(let [ret (gensym "return_value")]
;; maybe-ref for react/forwardRef support
`(fn ^js/React.Element ~display-name
[props# maybe-ref#]
(let [~props-bindings [(extract-cljs-props props#) maybe-ref#]]
(do ~@body)))))
(defmacro defnc
"Creates a new functional React component. Used like:
(defnc component-name
\"Optional docstring\"
[props ?ref]
{,,,opts-map}
,,,body)
\"component-name\" will now be a React function component that returns a React
Element.
Your component should adhere to the following:
First parameter is 'props', a map of properties passed to the component.
Second parameter is optional and is used with `React.forwardRef`.
'opts-map' is optional and can be used to pass some configuration options to the
macro. Current options:
- ':wrap' - ordered sequence of higher-order components to wrap the component in
'body' should return a React Element."
[display-name & form-body]
(let [docstring (when (string? (first form-body))
(first form-body))
props-bindings (if (nil? docstring)
(first form-body)
(second form-body))
body (if (nil? docstring)
(rest form-body)
(rest (rest form-body)))
wrapped-name (symbol (str display-name "-helix-render"))
opts-map? (map? (first body))
opts (if opts-map?
(first body)
{})
hooks (hana/find-hooks body)
sig-sym (gensym "sig")
fully-qualified-name (str *ns* "/" display-name)]
`(do (when goog/DEBUG
(def ~sig-sym (signature!)))
(def ~wrapped-name
(-> ~(fnc* wrapped-name props-bindings
(cons `(when goog/DEBUG
(when ~sig-sym
(~sig-sym)))
(if opts-map?
(rest body)
body)))
(cond-> goog/DEBUG
(doto (goog.object/set "displayName" ~fully-qualified-name)))
~@(-> opts :wrap)))
(def ~display-name
~@(when-not (nil? docstring)
(list docstring))
~wrapped-name)
(when goog/DEBUG
(when ~sig-sym
(~sig-sym ~wrapped-name ~(string/join hooks)
nil ;; forceReset
nil)) ;; getCustomHooks
(register! ~wrapped-name ~fully-qualified-name))
~display-name)))
(defn static? [form]
(boolean (:static (meta form))))
(defn method? [form]
(and (list? form)
(simple-symbol? (first form))
(vector? (second form))))
(defn ->method [[sym-name bindings & form]]
{:assert [(simple-symbol? sym-name)]}
(list (str sym-name)
`(fn ~sym-name ~bindings
~@form)))
(defn ->value [[sym-name value]]
{:assert [(simple-symbol? sym-name)]}
(list (str sym-name) value))
(defmacro defcomponent
"Defines a React class component."
{:style/indent [1 :form [1]]}
[display-name & spec]
{:assert [(simple-symbol? display-name)
(seq (filter #(= 'render (first %)) spec))]}
(let [[docstring spec] (if (string? (first spec))
[(first spec) (rest spec)]
[nil spec])
{statics true spec false} (group-by static? spec)
js-spec `(cljs.core/js-obj ~@(->> spec
(map ->method)
(apply concat (list "displayName" (str display-name)))))
js-statics `(cljs.core/js-obj ~@(->> statics
(map #(if (method? %)
(->method %)
(->value %)))
(apply concat)))]
;; TODO handle render specially
`(def ~display-name (create-component ~js-spec ~js-statics))))
(comment
(macroexpand
'(defcomponent asdf
(foo [] "bar")
^:static (greeting "asdf")
(bar [this] asdf)
^:static (baz [] 123)))
;; => (helix.core/create-component
;; (cljs.core/js-obj
;; "foo"
;; (clojure.core/fn foo [] "bar")
;; "bar"
;; (clojure.core/fn bar [this] asdf))
;; (cljs.core/js-obj "greeting" "asdf" "baz" (clojure.core/fn baz [] 123)))
)