/
mount.cljc
69 lines (54 loc) · 4.06 KB
/
mount.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
(ns app.rum.mount
#?(:clj (:require [clojure.string :as string])
:cljs (:require [rum.core :as rum]))
(:require [app.rum.component :as component]
[lib.cognitect-transit.core :as transit']))
#?(:clj (set! *warn-on-reflection* true)
:cljs (set! *warn-on-infer* true))
;;••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
(def ^:private data-js-var "appReactMountData")
;;••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
#?(:cljs (defn mount-all
"Mount all components with data from server."
[]
(let [components (some-> (aget js/window data-js-var)
(transit'/read-transit-string))]
(js-delete js/window data-js-var)
(doseq [comp-data components]
(let [instance-id (component/instance-id comp-data)]
(rum/hydrate (component/create-component comp-data)
(js/document.getElementById (name instance-id))))))))
;;••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
;; TODO: Push state.
#?(:clj (defn mount-component!
"Hiccup-style element with pre-rendered react component.
All react components are registered in registry to be added
in page HTML later."
([registry!, comp-id]
(mount-component! registry! :div comp-id nil))
([registry!, comp-id, comp-data]
(mount-component! registry! :div comp-id comp-data))
([registry!, tag, comp-id, comp-data]
(let [comp-data (-> comp-data
(component/set-component-id comp-id))]
(swap! registry! conj comp-data)
[tag
{:id (component/instance-id comp-data)}
(component/create-component comp-data)]))))
;;••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
#?(:clj (defn init-mounter
"Create `components!` and function to declare component mounting in page hiccup."
[_]
(let [components! (atom [])]
[components! (partial mount-component! components!)])))
;;••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
#?(:clj (defn react-mount-data-js
"Hiccup for JS with data for mounting react components."
[react-data]
[:script {:dangerouslySetInnerHTML
{:__html (str "window." data-js-var "=`"
(-> (transit'/write-transit-string react-data)
(string/replace "\\" "\\\\")
(string/replace "`" "\\`"))
"`;")}}]))
;;••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••