-
Notifications
You must be signed in to change notification settings - Fork 2
/
dom.cljc
172 lines (144 loc) · 4.93 KB
/
dom.cljc
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
(ns rehook.dom
(:require [clojure.spec.alpha :as s]))
(s/def ::arg
(s/or :symbol? symbol?
:map? map?))
(s/def ::args
(s/or
:two-arity (s/tuple ::arg ::arg)
:three-arity (s/tuple ::arg ::arg symbol?)))
(s/def ::defui
(s/cat :name symbol?
:args ::args
:body (s/* any?)))
(s/def ::ui
(s/cat :args ::args
:body (s/* any?)))
(s/def ::html
(s/cat :render-fn symbol?
:component any?))
(defn eval-hiccup
([$ e]
(cond
(seq? e)
(map #(apply $ %) e)
(vector? e)
(apply eval-hiccup $ e)
(or (nil? e) (string? e) (number? e))
e
:else ($ e)))
([$ e props]
($ e props))
([$ e props & children]
(apply $ e props
(->> children
(mapcat (fn [child]
(cond
(and (vector? child) (seq child))
[(apply eval-hiccup $ child)]
(seq? child)
(map #(eval-hiccup $ %) child)
:else
[(eval-hiccup $ child)])))
(filter identity)))))
(defn compile-hiccup
([$ e]
(list $ e))
([$ e props]
(list $ e props))
([$ e props child]
(list $ e props
(cond
(vector? child)
(apply compile-hiccup $ child)
(or (nil? child) (string? child) (number? child))
child
:else
`(eval-hiccup ~$ ~child))))
([$ e props child & children]
(let [children (cons child children)]
(apply list $ e props (keep (fn [e]
(cond
(vector? e)
(apply compile-hiccup $ e)
(or (nil? e) (string? e) (number? e))
e
:else
`(eval-hiccup ~$ ~e)))
children)))))
#?(:clj
(defmacro html [$ component]
(s/assert* ::html [$ component])
(if (vector? component)
`~(apply compile-hiccup $ component)
`(apply eval-hiccup ~$ ~component))))
(defn rehook-meta
[m props _]
(assoc m :react/props props))
(defn as-element
"Turns a vector of Hiccup syntax into a React element."
[ctx form]
(if-let [$ (get ctx :rehook.dom/bootstrap)]
(eval-hiccup $ form)
(if ctx
(throw (ex-info "ctx argument to as-element null. Have you passed in the ctx map?" {}))
(throw (ex-info "as-element requires :rehook.dom/bootstrap key in ctx map. Are you you using defui?"
{:keys (keys ctx) :ctx ctx})))))
#?(:clj
(defmacro defui
[name [ctx props $?] & body]
(if $?
(do (s/assert* ::defui [name [ctx props $?] body])
`(def ~name
^{:rehook/component true
:rehook/name ~(str name)}
(fn ~name [ctx# $#]
(let [~ctx (dissoc ctx# :rehook.dom/props)
~$? $#]
(fn ~(gensym name) [props#]
(let [~props (vary-meta (get ctx# :rehook.dom/props {}) rehook-meta props# ctx#)]
~@body))))))
(do (s/assert* ::defui [name [ctx props] body])
(let [$ (gensym '$)
effects (butlast body)
hiccup (last body)]
`(def ~name
^{:rehook/component true
:rehook/name ~(str name)}
(fn ~name [ctx# $#]
(let [~ctx (dissoc ctx# :rehook.dom/props)
~$ $#]
(fn ~(gensym name) [props#]
(let [~props (vary-meta (get ctx# :rehook.dom/props {}) rehook-meta props# ctx#)]
~@effects
(html ~$ ~hiccup)))))))))))
#?(:clj
(defmacro ui
[[ctx props $?] & body]
(if $?
(let [id (gensym "ui")]
(s/assert* ::ui [[ctx props $?] body])
`(with-meta
(fn ~id [ctx# $#]
(let [~ctx (dissoc ctx# :rehook.dom/props)
~$? $#]
(fn ~(gensym id) [props#]
(let [~props (vary-meta (get ctx# :rehook.dom/props {}) rehook-meta props# ctx#)]
~@body))))
{:rehook/component true
:rehook/name ~(str id)}))
(let [id (gensym "ui")
$ (gensym '$)
effects (butlast body)
hiccup (last body)]
(s/assert* ::ui [[ctx props] body])
`(with-meta
(fn ~id [ctx# $#]
(let [~ctx (dissoc ctx# :rehook.dom/props)
~$ $#]
(fn ~(gensym id) [props#]
(let [~props (vary-meta (get ctx# :rehook.dom/props {}) rehook-meta props# ctx#)]
~@effects
(html ~$ ~hiccup)))))
{:rehook/component true
:rehook/name ~(str id)})))))