Skip to content

Commit f5c51ad

Browse files
author
Bruce Hauman
committed
fixing reliance on js/React
1 parent c16d27a commit f5c51ad

File tree

7 files changed

+61
-55
lines changed

7 files changed

+61
-55
lines changed

example_src/devdemos/core.cljs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
:rigsomelight-post true}
44
devdemos.core
55
(:require
6-
[om.core :as om :include-macros true]
7-
[om.dom :as dom :include-macros true]
6+
#_[om.core :as om :include-macros true]
7+
#_[om.dom :as dom :include-macros true]
88
[om.next :as omnext :refer-macros [defui]]
99
[create-react-class :as create-react-class]
1010
[reagent.core :as reagent]
@@ -19,7 +19,8 @@
1919
[devcards.core :as dc :refer [defcard defcard-doc deftest dom-node defcard-om-next]]))
2020

2121
;; this is to support om with the latest version of React
22-
(set! (.-createClass (.-React goog.global)) create-react-class)
22+
;; TODO not needed?
23+
#_(set! (.-createClass (.-React goog.global)) create-react-class)
2324

2425
(def ^:export front-matter
2526
{:layout false
@@ -248,7 +249,7 @@
248249

249250

250251

251-
(defn om-slider [bmi-data param value min max]
252+
#_(defn om-slider [bmi-data param value min max]
252253
(sab/html
253254
[:input {:type "range" :value value :min min :max max
254255
:style {:width "100%"}
@@ -257,7 +258,7 @@
257258
(when (not= param :bmi)
258259
(om/update! bmi-data :bmi nil)))}]))
259260

260-
(defn om-bmi-component [bmi-data owner]
261+
#_(defn om-bmi-component [bmi-data owner]
261262
(let [{:keys [weight height bmi]} (calc-bmi bmi-data)
262263
[color diagnose] (cond
263264
(< bmi 18.5) ["orange" "underweight"]
@@ -279,7 +280,7 @@
279280
[:span {:style {:color color}} diagnose]
280281
(om-slider bmi-data :bmi bmi 10 50)]]))))
281282

282-
(defcard
283+
#_(defcard
283284
"# Om support
284285
285286
Here is the same calculator being rendered as an Om application.
@@ -292,7 +293,7 @@
292293
```
293294
")
294295

295-
(defcard om-support
296+
#_(defcard om-support
296297
(dc/om-root om-bmi-component)
297298
{:height 180 :weight 80} ;; initial data
298299
{:inspect-data true
@@ -500,7 +501,7 @@ as a dependency.
500501
;; conditionally start the app based on the presence of #main-app-area
501502
;; node is on the page
502503
(if-let [node (.getElementById js/document \"main-app-area\")]
503-
(js/React.render (sab/html [:div \"This is main app is ruunning.\"]) node)))
504+
(react-dom/render (sab/html [:div \"This is main app is ruunning.\"]) node)))
504505
505506
(main)
506507
```
@@ -557,5 +558,4 @@ your application's UI so you will want to make sure it isn't getting
557558
launched.
558559
559560
560-
561561
")

example_src/devdemos/maintain_state.cljs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
[sablono.core :as sab :include-macros true]
44
[goog.object :as gobj]
55
[devcards.util.utils :refer-macros [define-react-class-once]]
6-
[devcards.core])
6+
[devcards.core]
7+
[react])
78
(:require-macros
89
;; Notice that I am not including the 'devcards.core namespace
910
;; but only the macros. This helps ensure that devcards will only
@@ -36,5 +37,5 @@ This is mainly intended for verification of behavior.")
3637
The state of this counter is stored in the local state of the component.
3738
3839
To test: increment the counter then save this file and verify that the counter state hasn't changed."
39-
(js/React.createElement County))
40+
(react/createElement County))
4041

project.clj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@
5252
}}
5353
]}
5454

55-
:figwheel { :css-dirs ["resources/public/devcards/css"]
56-
:open-file-command "emacsclient"
57-
;;:nrepl-port 7888
58-
}
55+
:figwheel { :css-dirs ["resources/public/devcards/css"]
56+
:open-file-command "emacsclient"
57+
;;:nrepl-port 7888
58+
}
5959

6060
:profiles {
6161
:repl { ;:plugins [[cider/cider-nrepl "0.11.1"]]

src/devcards/core.cljs

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,10 @@
9696

9797
(defn- react-raw [raw-html-str]
9898
"A React component that renders raw html."
99-
(js/React.createElement "div"
100-
#js {:key (str (hash raw-html-str))
101-
:dangerouslySetInnerHTML
102-
#js {:__html raw-html-str}}))
99+
(react/createElement "div"
100+
#js {:key (str (hash raw-html-str))
101+
:dangerouslySetInnerHTML
102+
#js {:__html raw-html-str}}))
103103

104104
(declare get-props ref->node)
105105

@@ -123,17 +123,17 @@
123123
(get-props this :code)]])))
124124

125125
(defn code-highlight [code-str lang]
126-
(js/React.createElement CodeHighlight #js {:code code-str
127-
:lang lang}))
126+
(react/createElement CodeHighlight #js {:code code-str
127+
:lang lang}))
128128

129129
(defmulti markdown-block->react :type)
130130

131131
(defmethod markdown-block->react :default [{:keys [content]}]
132132
(-> content mark/markdown-to-html react-raw))
133133

134134
(defmethod markdown-block->react :code-block [{:keys [content] :as block}]
135-
(js/React.createElement CodeHighlight #js {:code (:content block)
136-
:lang (:lang block)}))
135+
(react/createElement CodeHighlight #js {:code (:content block)
136+
:lang (:lang block)}))
137137

138138
(declare react-element?)
139139

@@ -217,7 +217,7 @@
217217

218218
(defn ref->node [this ref]
219219
(when-let [comp (gobj/get (.. this -refs) ref)]
220-
(js/ReactDOM.findDOMNode comp)))
220+
(react-dom/findDOMNode comp)))
221221

222222
(defn get-props [this k]
223223
(gobj/get (.-props this) (name k)))
@@ -239,9 +239,9 @@
239239

240240
;; this is not currently being used
241241
(defn dont-update [change-count children-thunk]
242-
(js/React.createElement DontUpdate
243-
#js { :change_count change-count
244-
:children_thunk children-thunk}))
242+
(react/createElement DontUpdate
243+
#js {:change_count change-count
244+
:children_thunk children-thunk}))
245245

246246
(defn wrangle-inital-data [this]
247247
(let [data (or (:initial-data (get-props this :card)) {})]
@@ -366,15 +366,15 @@
366366
(componentWillUnmount
367367
[this]
368368
(when-let [node (ref->node this (get-state this :unique_id))]
369-
(js/ReactDOM.unmountComponentAtNode node)))
369+
(react-dom/unmountComponentAtNode node)))
370370
(componentDidMount [this] (render-into-dom this))
371371
(render
372372
[this]
373373
(if (html-env?)
374-
(js/React.createElement "div"
374+
(react/createElement "div"
375375
#js { :className "com-rigsomelight-devcards-dom-node" :ref (get-state this :unique_id)}
376376
"Card has not mounted DOM node.")
377-
(js/React.createElement "div" nil "Card has not mounted DOM node."))))
377+
(react/createElement "div" nil "Card has not mounted DOM node."))))
378378

379379
(defn booler? [key opts]
380380
(let [x (get opts key)]
@@ -477,7 +477,7 @@
477477
(let [errors (validate-card-options card-options)]
478478
(if (not-empty errors)
479479
(render-errors card-options errors)
480-
(js/React.createElement DevcardBase #js { :card (add-environment-defaults card-options) }))))
480+
(react/createElement DevcardBase #js { :card (add-environment-defaults card-options) }))))
481481

482482
(defrecord IdentityOptions [obj]
483483
IDevcardOptions
@@ -528,9 +528,9 @@
528528
;; keep
529529
(defn- dom-node* [node-fn]
530530
(fn [data-atom owner]
531-
(js/React.createElement DomComponent
532-
#js {:node_fn node-fn
533-
:data_atom data-atom})))
531+
(react/createElement DomComponent
532+
#js {:node_fn node-fn
533+
:data_atom data-atom})))
534534

535535
;; devcard protocol that takes a devcard and returns a devcard
536536

@@ -733,8 +733,8 @@
733733

734734
;; keep
735735
(defn- hist-recorder* [data-atom]
736-
(js/React.createElement HistoryComponent
737-
#js { :data_atom data-atom :key "devcards-history-control-bar"}))
736+
(react/createElement HistoryComponent
737+
#js { :data_atom data-atom :key "devcards-history-control-bar"}))
738738

739739
;; Testing via cljs.test
740740
(comment
@@ -789,14 +789,14 @@
789789
m
790790
(sab/html
791791
[:div
792-
(js/React.createElement CodeHighlight #js {:code (utils/pprint-code expected)
793-
:lang "clojure"})
792+
(react/createElement CodeHighlight #js {:code (utils/pprint-code expected)
793+
:lang "clojure"})
794794
(when (= type :fail)
795795
(sab/html [:div {:style {:marginTop "5px"}}
796796
[:div {:style {:position "absolute" :fontSize "0.9em"}} ""]
797797
[:div {:style {:marginLeft "20px"}}
798-
(js/React.createElement CodeHighlight #js {:code (utils/pprint-code actual)
799-
:lang "clojure"})]]))])))
798+
(react/createElement CodeHighlight #js {:code (utils/pprint-code actual)
799+
:lang "clojure"})]]))])))
800800

801801
(defmethod test-render :pass [m]
802802
(render-pass-fail m))
@@ -968,9 +968,9 @@
968968
IDevcard
969969
(-devcard [this devcard-opts]
970970
(let [path (:path devcards.system/*devcard-data*)]
971-
(js/React.createElement TestDevcard
972-
#js {:test_thunks test-thunks
973-
:path path})))))
971+
(react/createElement TestDevcard
972+
#js {:test_thunks test-thunks
973+
:path path})))))
974974

975975
;; render namespace to string
976976

@@ -998,6 +998,7 @@
998998
(println "Adding base card options!" (prn-str base-card-options))
999999
(swap! dev/app-state update-in [:base-card-options] (fn [opts] (merge opts base-card-options)))))
10001000

1001+
;; deprecated
10011002
(defn ^:export render-namespace-to-string [ns-symbol]
10021003
(when-let [card (get-cards-for-ns ns-symbol)]
10031004
(merge-front-matter-options! ns-symbol)
@@ -1011,7 +1012,7 @@
10111012

10121013
(defn render-ns [ns-symbol app-state]
10131014
(when-let [card (get-cards-for-ns ns-symbol)]
1014-
(js/ReactDOM.render
1015+
(react-dom/render
10151016
(sab/html
10161017
[:div.com-rigsomelight-devcards-base.com-rigsomelight-devcards-string-render
10171018
(dev/render-cards (dev/display-cards card) app-state)])

src/devcards/system.cljs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -315,8 +315,8 @@
315315

316316
(defn renderer [state-atom]
317317
#_(prn "Rendering")
318-
(js/ReactDOM.render
319-
(js/React.createElement DevcardsRoot)
318+
(react-dom/render
319+
(react/createElement DevcardsRoot)
320320
#_(sab/html [:div
321321
(main-template state-atom)
322322
#_(edn-rend/html-edn @state-atom)])
@@ -406,8 +406,8 @@
406406
(defn start-ui-with-renderer [channel renderer]
407407
(defonce devcards-ui-setup
408408
(do
409-
(when (exists? js/React.initializeTouchEvents)
410-
(js/React.initializeTouchEvents true))
409+
(when (exists? react/initializeTouchEvents)
410+
(react/initializeTouchEvents true))
411411
(go
412412
(<! (load-data-from-channel! channel))
413413

@@ -429,8 +429,8 @@
429429
(defn start-ui [channel]
430430
(defonce devcards-ui-setup
431431
(do
432-
(when (exists? js/React.initializeTouchEvents)
433-
(js/React.initializeTouchEvents true))
432+
(when (exists? react/initializeTouchEvents)
433+
(react/initializeTouchEvents true))
434434
(render-base-if-necessary!)
435435
(go
436436
;; initial load
@@ -490,7 +490,7 @@
490490
</svg>")
491491

492492
(defn cljs-logo []
493-
(js/React.createElement "span"
493+
(react/createElement "span"
494494
#js {:key "cljs-logo"
495495
:dangerouslySetInnerHTML
496496
#js {:__html cljs-logo-svg}}))

src/devcards/util/utils.clj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,17 @@
1818
constructor-fn (if constructor-fn
1919
`(fn [props#]
2020
(cljs.core/this-as this#
21-
(.call js/React.Component this# props#)
21+
(.call js/devcards.util.utils.react_holder.Component this# props#)
2222
(.call ~constructor-fn this# props#)
2323
this#))
2424
`(fn [props#]
2525
(cljs.core/this-as this#
26-
(.call js/React.Component this# props#)
26+
(.call js/devcards.util.utils.react_holder.Component this# props#)
2727
this#)))
2828
body (->> body
2929
(remove #(= 'constructor (first %))))]
3030
`(let [ctor# ~constructor-fn]
31-
(goog.inherits ctor# js/React.Component)
31+
(goog.inherits ctor# js/devcards.util.utils.react_holder.Component)
3232
(cljs.core/specify! (.-prototype ctor#)
3333
~'Object
3434
~@body)

src/devcards/util/utils.cljs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
(ns devcards.util.utils
22
(:require
33
[goog.object :as gobj]
4-
[cljs.pprint :as pprint])
4+
[cljs.pprint :as pprint]
5+
[react])
56
(:require-macros
67
[devcards.util.utils]))
78

9+
;; this is a reference
10+
(def react-holder react)
11+
812
(defn html-env? []
913
(if-let [doc js/goog.global.document]
1014
(gobj/get doc "write")))

0 commit comments

Comments
 (0)