Created
October 30, 2018 18:08
-
-
Save philjackson/fce0427ddc3e5dff6b8a54e6c02b8d23 to your computer and use it in GitHub Desktop.
Drag and drop with Reagent and Interact.js
This file contains hidden or 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
(defn draggable [] | |
(let [pos (atom [0 0])] | |
(reagent/create-class | |
{:render | |
#(let [[x y] @pos] | |
[:div.d {:style {:transform (str "translate(" x "px," y "px)")}} | |
"I can be dragged"]) | |
:component-did-mount | |
(fn [this] | |
(.draggable (js/interact (reagent/dom-node this)) | |
#js {:onmove (fn [ev] | |
(let [[x y] @pos | |
new-x (+ (.-dx ev) x) | |
new-y (+ (.-dy ev) y)] | |
(reset! pos [new-x new-y])))}))}))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment