-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
106 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
;; # 🎛 Control Lab 🧑🏼🔬 | ||
(ns viewer.control-lab | ||
(:require [clojure.string :as str] | ||
[nextjournal.clerk :as clerk] | ||
[nextjournal.clerk.viewer :as viewer])) | ||
|
||
;; Experimenting with ways of making controls. We start with two | ||
;; little helper functions, one in sci and one in Clojure. | ||
|
||
(def viewer-eval-viewer | ||
{:pred viewer/viewer-eval? | ||
:transform-fn (comp viewer/mark-presented | ||
(viewer/update-val | ||
(fn [x] (cond (viewer/viewer-eval? x) x | ||
(symbol? x) (viewer/->viewer-eval x) | ||
(var? x) (recur (symbol x)) | ||
(viewer/var-from-def? x) (recur (:nextjournal.clerk/var-from-def x)))))) | ||
:render-fn 'nextjournal.clerk.render/inspect}) | ||
|
||
(clerk/add-viewers! [viewer-eval-viewer]) | ||
|
||
(viewer/->viewer-eval | ||
'(defn make-slider [opts] | ||
(fn [!state] | ||
[:input (merge {:type :range :value @!state :on-change #(reset! !state (int (.. % -target -value)))} opts)]))) | ||
|
||
(defn make-slider | ||
([] (make-slider {})) | ||
([opts] (assoc viewer-eval-viewer :render-fn (list 'make-slider opts)))) | ||
|
||
;; Let's go through the ways we can use this. | ||
|
||
;; 1️⃣ On a var coming from a def | ||
^{::clerk/sync true ::clerk/viewer (make-slider {})} | ||
(defonce !num (atom 0)) | ||
|
||
;; 2️⃣ On a sharp quoted symbol (works with a fully qualified one as well, ofc). | ||
(clerk/with-viewer (make-slider) | ||
`!num) | ||
|
||
;; 3️⃣ On a var | ||
^{::clerk/viewer (make-slider)} | ||
#'!num | ||
|
||
;; 4️⃣ On an explicit `ViewerEval` type | ||
(clerk/with-viewer (make-slider) | ||
(viewer/->viewer-eval `!num)) | ||
|
||
|
||
#_#_ ;; TODO: plain (not quoted) symbol | ||
^{::clerk/viewer (make-slider {})} | ||
!num | ||
;; TODO: reactivity with default viewer | ||
(viewer/->viewer-eval `!num) | ||
|
||
;; We can customise the slider by passing different opts (that are merged). | ||
|
||
(clerk/with-viewer (make-slider {:max 200}) | ||
`!num) | ||
|
||
;; Or use a completely custom `:render-fn`. | ||
(clerk/with-viewer (assoc viewer-eval-viewer :render-fn '(fn [x] [:h2.bg-green-500.rounded-xl.text-center @x])) | ||
`!num) | ||
|
||
@!num | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
38F71uHxJsdbeuJsMnSfkow4eTQ2 | ||
41eXh3xrWdsqXCdwuJ43u74YfK4z |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters