-
Notifications
You must be signed in to change notification settings - Fork 51
/
core.cljs
136 lines (101 loc) · 3.13 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
(ns helix.core
(:refer-clojure :exclude [type])
(:require [goog.object :as gobj]
[helix.impl.props :as impl.props]
["./impl/class.js" :as helix.class]
[cljs-bean.core :as bean]
[ueaq.core :as ueaq]
["react" :as react]
["react-dom/server" :as rds])
(: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)
(defn $$
"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\" ))
```"
[type & args]
(let [?p (first args)
?c (rest args)
native? (string? type)]
(if (map? ?p)
(apply create-element
type
(if native?
(impl.props/-native-props ?p)
(impl.props/-props ?p))
?c)
(apply create-element
type
nil
args))))
(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]
(if (map? (first args))
(apply create-element type (ueaq/ueaq (first args)) (rest args))
(apply create-element type nil args)))
(specify! IExtractType
(-type [_] type))))
(defn- extract-cljs-props
[o]
(bean/bean 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)))