Skip to content

Commit

Permalink
Export reactified components for testing
Browse files Browse the repository at this point in the history
components using hooks not working because of this bug reagent-project/reagent#548
  • Loading branch information
zampino committed Jun 29, 2023
1 parent d801870 commit a867882
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 3 deletions.
5 changes: 4 additions & 1 deletion shadow-cljs.edn
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@
:release {:output-dir "build/"}
:compiler-options {:source-map true}
:dev {:modules {:viewer {:entries [devtools]}}}
:modules {:viewer {:entries [nextjournal.clerk.sci-env
:modules {:viewer {:exports {EvalNotebook nextjournal.clerk.render.editor/EvalNotebook
TestComponent nextjournal.clerk.render/TestComponent
TestComponentHooks nextjournal.clerk.render/TestComponentHooks}
:entries [nextjournal.clerk.sci-env
nextjournal.clerk.trim-image]}}
:js-options {:output-feature-set :es8}
:build-hooks [(shadow.cljs.build-report/hook
Expand Down
25 changes: 25 additions & 0 deletions src/nextjournal/clerk/render.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -899,3 +899,28 @@
(str "?" (opts->query opts))))))

(def consume-view-context view-context/consume)

(defn test-component [{:as props :keys [title]}]
(js/console.log "test component" (str props))
(r/with-let [!state (r/atom 0)]
[:div
[:h1 title]
[:button.p-2.bg-blue-200
{:on-click #(swap! !state inc)}
(str "Click Me / Uses Reagent Atoms: " @!state)]]))

(defn test-component-hooks [{:as props :keys [title]}]
(js/console.log "test component hooks" (str props))
;; using plain unwrapped hooks for simplicity
(let [[counter set-counter] (react/useState 0)]
[:div
[:h1 title]
[:button.p-2.bg-blue-200
{:on-click #(set-counter inc)}
(str "Click Me / Uses React Hooks: " counter)]]))

(def ^:export TestComponent
(r/reactify-component test-component (r/create-compiler {:function-components true})))

(def ^:export TestComponentHooks
(r/reactify-component test-component-hooks (r/create-compiler {:function-components true})))
9 changes: 8 additions & 1 deletion src/nextjournal/clerk/render/editor.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
[nextjournal.clerk.viewer :as v]
[nextjournal.clojure-mode.extensions.eval-region :as eval-region]
[nextjournal.clojure-mode.keymap :as clojure-mode.keymap]
[reagent.core :as r]
[rewrite-clj.node :as n]
[rewrite-clj.parser :as p]
[sci.core :as sci]
Expand Down Expand Up @@ -147,7 +148,7 @@
(update doc :blocks (partial map (fn [{:as cell :keys [type text var form]}]
(cond-> cell
(= :code type)
(assoc :result
(assoc :result
{:nextjournal/value (cond->> (eval form)
var (hash-map :nextjournal.clerk/var-from-def))}))))))

Expand All @@ -158,6 +159,12 @@
(eval-blocks)
(v/with-viewer v/notebook-viewer)))

(defn eval-notebook-component [{:keys [code]}]
[eval-notebook code])

(def ^:export EvalNotebook
(r/reactify-component eval-notebook-component (r/create-compiler {:function-components true})))

(defonce bar-height 26)

(defn view [code-string]
Expand Down
2 changes: 1 addition & 1 deletion src/nextjournal/clerk/sci_env.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
[edamame.core :as edamame]
[goog.object]
[nextjournal.clerk.cherry-env :as cherry-env]
[nextjournal.clerk.parser]
[nextjournal.clerk.render :as render]
[nextjournal.clerk.render.code]
[nextjournal.clerk.render.context :as view-context]
Expand All @@ -27,6 +26,7 @@
[nextjournal.clojure-mode.commands]
[nextjournal.clojure-mode.extensions.eval-region]
[nextjournal.clojure-mode.keymap]
[reagent.core :as r]
[reagent.dom.server :as dom-server]
[reagent.ratom :as ratom]
[sci.configs.applied-science.js-interop :as sci.configs.js-interop]
Expand Down

0 comments on commit a867882

Please sign in to comment.