Skip to content

Commit

Permalink
feature: setup devcards
Browse files Browse the repository at this point in the history
- requires shims (marked and create-react-class)
- and bhauman/devcards#156
- see https://shadow-cljs.github.io/docs/UsersGuide.html#cljsjs

closes #6
  • Loading branch information
tangjeff0 committed Apr 28, 2020
1 parent 70948af commit a2fcd63
Show file tree
Hide file tree
Showing 9 changed files with 1,964 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/out/
/resources/public/js/compiled/
/resources/public/js/
/target/
/*-init.clj
/*.log
Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
"shadow-cljs": "2.8.83"
},
"dependencies": {
"create-react-class": "^15.6.3",
"highlight.js": "9.15.10",
"marked": "^1.0.0",
"react": "16.9.0",
"react-dom": "16.9.0",
"react-highlight.js": "1.0.7"
Expand Down
3 changes: 3 additions & 0 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
[day8.re-frame/async-flow-fx "0.1.0"]
[metosin/reitit "0.4.2"]
[instaparse "1.4.10"]
[devcards "0.2.6"]
]

:plugins [
Expand All @@ -42,6 +43,8 @@

:aliases {"dev" ["with-profile" "dev" "do"
["run" "-m" "shadow.cljs.devtools.cli" "watch" "app"]]
"devcards" ["with-profile" "dev" "do"
["run" "-m" "shadow.cljs.devtools.cli" "watch" "devcards"]]
"prod" ["with-profile" "prod" "do"
["run" "-m" "shadow.cljs.devtools.cli" "release" "app"]]
"build-report" ["with-profile" "prod" "do"
Expand Down
12 changes: 12 additions & 0 deletions resources/public/cards.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset='utf-8'>
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Athens Cards</title>
</head>
<body>
<div id="app"></div>
<script src="js/devcards/main.js"></script>
</body>
</html>
14 changes: 8 additions & 6 deletions shadow-cljs.edn
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
{:lein true
:nrepl {:port 8777
;:init-ns athens.core
}
:nrepl {:port 8777}
:builds {:app {:target :browser
:output-dir "resources/public/js/compiled"
:asset-path "/js/compiled"
Expand All @@ -13,9 +11,13 @@
day8.re-frame.tracing.trace-enabled? true}}}
:devtools {:http-root "resources/public"
:http-port 3000
:repl-init-ns athens.core
;; :repl-pprint true
}}
:repl-init-ns athens.core}}
:devcards {:asset-path "js/devcards"
:modules {:main {:init-fn athens.devcards/main}}
:compiler-options {:devcards true}
:output-dir "resources/public/js/devcards"
:target :browser}

:karma-test {:target :karma
:ns-regexp "-test$"
:output-to "target/karma-test.js"}}}
53 changes: 53 additions & 0 deletions src/cljs/athens/devcards.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
(ns athens.devcards
(:require
[cljsjs.react]
[cljsjs.react.dom]
[reagent.core :as r :include-macros true]
[devcards.core :as devcards :include-macros true :refer [defcard]]))

(def bmi-data (r/atom {:height 180 :weight 80}))

(defn calc-bmi [bmi-data]
(let [{:keys [height weight bmi] :as data} bmi-data
h (/ height 100)]
(if (nil? bmi)
(assoc data :bmi (/ weight (* h h)))
(assoc data :weight (* bmi h h)))))

(defn slider [bmi-data param value min max]
[:input {:type "range" :value value :min min :max max
:style {:width "100%"}
:on-change (fn [e]
(swap! bmi-data assoc param (.. e -target -value))
(when (not= param :bmi)
(swap! bmi-data assoc :bmi nil)))}])

(defn bmi-component [bmi-data]
(let [{:keys [weight height bmi]} (calc-bmi @bmi-data)
[color diagnose] (cond
(< bmi 18.5) ["orange" "underweight"]
(< bmi 25) ["inherit" "normal"]
(< bmi 30) ["orange" "overweight"]
:else ["red" "obese"])]
[:div
[:h3 "BM calculator"]
[:div
"Height: " (int height) "cm"
[slider bmi-data :height height 100 220]]
[:div
"Weight: " (int weight) "kg"
[slider bmi-data :weight weight 30 150]]
[:div
"BMI: " (int bmi) " "
[:span {:style {:color color}} diagnose]
[slider bmi-data :bmi bmi 10 50]]]))

(defcard bmi-calculator
"*Code taken from the Reagent readme.*"
(devcards/reagent bmi-component)
bmi-data
{:inspect-data true
:frame true
:history true})

(defn ^:export main [] (devcards.core/start-devcard-ui!))
7 changes: 7 additions & 0 deletions src/cljsjs/create_react_class.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(ns cljsjs.create-react-class
(:require
["react" :as react]
["create-react-class" :as create-react-class]))

(js/goog.object.set react "createClass" create-react-class)
(js/goog.exportSymbol "createReactClass" create-react-class)
4 changes: 4 additions & 0 deletions src/cljsjs/marked.cljs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
(ns cljsjs.marked
(:require ["marked" :as marked]))

(js/goog.exportSymbol "marked" marked)
Loading

0 comments on commit a2fcd63

Please sign in to comment.