forked from roman01la/uix
-
Notifications
You must be signed in to change notification settings - Fork 21
/
core.clj
293 lines (249 loc) · 10.1 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
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
(ns uix.core
"Public API"
(:refer-clojure :exclude [fn])
(:require [clojure.core :as core]
[clojure.string :as str]
[uix.compiler.aot]
[uix.source]
[cljs.core]
[uix.linter]
[uix.dev]
[uix.lib]
[uix.hooks.alpha :as hooks]))
(def ^:private goog-debug (with-meta 'goog.DEBUG {:tag 'boolean}))
(defn- no-args-component [sym var-sym body]
`(defn ~sym []
(let [f# (core/fn [] ~@body)]
(if ~goog-debug
(binding [*current-component* ~var-sym] (f#))
(f#)))))
(defn- with-args-component [sym var-sym args body]
`(defn ~sym [props#]
(let [clj-props# (glue-args props#)
~args (cljs.core/array clj-props#)
f# (core/fn [] ~@body)]
(if ~goog-debug
(binding [*current-component* ~var-sym]
(assert (or (map? clj-props#)
(nil? clj-props#))
(str "UIx component expects a map of props, but instead got " clj-props#))
(f#))
(f#)))))
(defn- no-args-fn-component [sym var-sym body]
`(core/fn ~sym []
(let [f# (core/fn [] ~@body)]
(if ~goog-debug
(binding [*current-component* ~var-sym] (f#))
(f#)))))
(defn- with-args-fn-component [sym var-sym args body]
`(core/fn ~sym [props#]
(let [clj-props# (glue-args props#)
~args (cljs.core/array clj-props#)
f# (core/fn [] ~@body)]
(if ~goog-debug
(binding [*current-component* ~var-sym]
(assert (or (map? clj-props#)
(nil? clj-props#))
(str "UIx component expects a map of props, but instead got " clj-props#))
(f#))
(f#)))))
(defn parse-defui-sig [form name fdecl]
(let [[fname fdecl] (uix.lib/parse-sig name fdecl)]
(uix.lib/assert!
(= 1 (count fdecl))
(str form " doesn't support multi-arity.\n"
"If you meant to make props an optional argument, you can safely skip it and have a single-arity component.\n
It's safe to destructure the props value even if it's `nil`."))
(let [[args & fdecl] (first fdecl)]
(uix.lib/assert!
(>= 1 (count args))
(str form " is a single argument component taking a map of props, found: " args "\n"
"If you meant to retrieve `children`, they are under `:children` field in props map."))
[fname args fdecl])))
(defmacro
^{:arglists '([name doc-string? attr-map? [params*] prepost-map? body]
[name doc-string? attr-map? ([params*] prepost-map? body) + attr-map?])}
defui
"Creates UIx component. Similar to defn, but doesn't support multi arity.
A component should have a single argument of props."
[sym & fdecl]
(let [[fname args fdecl] (parse-defui-sig `defui sym fdecl)]
(uix.linter/lint! sym fdecl &form &env)
(if (uix.lib/cljs-env? &env)
(let [var-sym (-> (str (-> &env :ns :name) "/" sym) symbol (with-meta {:tag 'js}))
body (uix.dev/with-fast-refresh var-sym fdecl)]
`(do
~(if (empty? args)
(no-args-component fname var-sym body)
(with-args-component fname var-sym args body))
(set! (.-uix-component? ~var-sym) true)
(set! (.-displayName ~var-sym) ~(str var-sym))
~(uix.dev/fast-refresh-signature var-sym body)))
`(defn ~fname [& args#]
(let [~args args#]
~@fdecl)))))
(defmacro fn
"Creates anonymous UIx component. Similar to fn, but doesn't support multi arity.
A component should have a single argument of props."
[& fdecl]
(let [[sym fdecl] (if (symbol? (first fdecl))
[(first fdecl) (rest fdecl)]
[(gensym "uix-fn") fdecl])
[fname args body] (parse-defui-sig `fn sym fdecl)]
(uix.linter/lint! sym body &form &env)
(if (uix.lib/cljs-env? &env)
(let [var-sym (with-meta sym {:tag 'js})]
`(let [~var-sym ~(if (empty? args)
(no-args-fn-component fname var-sym body)
(with-args-fn-component fname var-sym args body))]
(set! (.-uix-component? ~var-sym) true)
(set! (.-displayName ~var-sym) ~(str var-sym))
~var-sym))
`(core/fn ~fname [& args#]
(let [~args args#]
~@fdecl)))))
(defmacro source
"Returns source string of UIx component"
[sym]
(uix.source/source &env sym))
(defmacro $
"Creates React element
DOM element: ($ :button#id.class {:on-click handle-click} \"click me\")
React component: ($ title-bar {:title \"Title\"})"
([tag]
(uix.linter/lint-element* &form &env)
(uix.compiler.aot/compile-element [tag] {:env &env}))
([tag props & children]
(uix.linter/lint-element* &form &env)
(uix.compiler.aot/compile-element (into [tag props] children) {:env &env})))
(defn parse-defhook-sig [sym fdecl]
(let [[fname fdecl] (uix.lib/parse-sig sym fdecl)]
(uix.lib/assert! (str/starts-with? (name fname) "use-")
(str "React Hook name should start with `use-`, found `" (name fname) "` instead."))
(uix.lib/assert!
(= 1 (count fdecl))
"uix.core/defhook should be single-arity function")
[fname fdecl]))
(defmacro
^{:arglists '([name doc-string? attr-map? [params*] prepost-map? body]
[name doc-string? attr-map? ([params*] prepost-map? body) + attr-map?])}
defhook
"Like `defn`, but creates React hook with additional validation,
the name should start with `use-`
(defhook use-in-viewport []
...)"
[sym & fdecl]
(let [[fname fdecl] (parse-defhook-sig sym fdecl)
fname (vary-meta fname assoc :uix/hook true)]
(uix.linter/lint! sym fdecl &form &env)
`(defn ~fname ~@fdecl)))
;; === Error boundary ===
(defn create-error-boundary
"Creates React's error boundary component
display-name — the name of the component to be displayed in stack trace
derive-error-state — maps error object to component's state that is used in render-fn
did-catch — 2 arg function for side effeects, logging etc.
receives the exception and additional component info as args
render-fn — takes state value returned from derive-error-state and a vector
of arguments passed into error boundary"
[{:keys [display-name derive-error-state did-catch]
:or {display-name (str (gensym "uix.error-boundary"))}}
render-fn]
^::error-boundary {:display-name display-name
:render-fn render-fn
:did-catch did-catch
:derive-error-state derive-error-state})
;; === Hooks ===
(defn use-state [value]
(hooks/use-state value))
(defn use-reducer
([f value]
(hooks/use-reducer f value))
([f value init-state]
(hooks/use-reducer f value init-state)))
(defn use-ref [value]
(hooks/use-ref value))
(defn use-context [value]
(hooks/use-context value))
(defn use-debug
([v]
(hooks/use-debug v))
([v fmt]
(hooks/use-debug v fmt)))
(defn use-deferred-value [v]
(hooks/use-deferred-value v))
(defn use-transition []
(hooks/use-transition))
(defn use-id []
(hooks/use-id))
(defn use-sync-external-store
([subscribe get-snapshot]
(hooks/use-sync-external-store subscribe get-snapshot))
([subscribe get-snapshot get-server-snapshot]
(hooks/use-sync-external-store subscribe get-snapshot get-server-snapshot)))
(defn vector->js-array [coll]
(cond
(vector? coll) `(jsfy-deps (cljs.core/array ~@coll))
(some? coll) `(jsfy-deps ~coll)
:else coll))
(defn- make-hook-with-deps [sym env form f deps]
(when (uix.lib/cljs-env? env)
(uix.linter/lint-exhaustive-deps! env form f deps))
(if deps
(if (uix.lib/cljs-env? env)
`(~sym ~f ~(vector->js-array deps))
`(~sym ~f ~deps))
`(~sym ~f)))
(defmacro use-effect
"Takes a function to be executed in an effect and optional vector of dependencies.
See: https://reactjs.org/docs/hooks-reference.html#useeffect"
([f]
(make-hook-with-deps 'uix.hooks.alpha/use-effect &env &form f nil))
([f deps]
(make-hook-with-deps 'uix.hooks.alpha/use-effect &env &form f deps)))
(defmacro use-layout-effect
"Takes a function to be executed in a layout effect and optional vector of dependencies.
See: https://reactjs.org/docs/hooks-reference.html#uselayouteffect"
([f]
(make-hook-with-deps 'uix.hooks.alpha/use-layout-effect &env &form f nil))
([f deps]
(make-hook-with-deps 'uix.hooks.alpha/use-layout-effect &env &form f deps)))
(defmacro use-insertion-effect
"Takes a function to be executed synchronously before all DOM mutations
and optional vector of dependencies. Use this to inject styles into the DOM
before reading layout in `useLayoutEffect`.
See: https://reactjs.org/docs/hooks-reference.html#useinsertioneffect"
([f]
(make-hook-with-deps 'uix.hooks.alpha/use-insertion-effect &env &form f nil))
([f deps]
(make-hook-with-deps 'uix.hooks.alpha/use-insertion-effect &env &form f deps)))
(defmacro use-memo
"Takes function f and required vector of dependencies, and returns memoized result of f.
See: https://reactjs.org/docs/hooks-reference.html#usememo"
[f deps]
(make-hook-with-deps 'uix.hooks.alpha/use-memo &env &form f deps))
(defmacro use-callback
"Takes function f and required vector of dependencies, and returns memoized f.
See: https://reactjs.org/docs/hooks-reference.html#usecallback"
[f deps]
(make-hook-with-deps 'uix.hooks.alpha/use-callback &env &form f deps))
(defmacro use-imperative-handle
"Customizes the instance value that is exposed to parent components when using ref.
See: https://reactjs.org/docs/hooks-reference.html#useimperativehandle"
([ref f]
(when (uix.lib/cljs-env? &env)
(uix.linter/lint-exhaustive-deps! &env &form f nil))
`(uix.hooks.alpha/use-imperative-handle ~ref ~f))
([ref f deps]
(when (uix.lib/cljs-env? &env)
(uix.linter/lint-exhaustive-deps! &env &form f deps))
`(uix.hooks.alpha/use-imperative-handle ~ref ~f ~(vector->js-array deps))))
(defui suspense [{:keys [children]}]
children)
(defui strict-mode [{:keys [children]}]
children)
(defui profiler [{:keys [children]}]
children)
;; SSR helpers
(def client? false) ;; no JVM front-ends
(def server? (not client?))