0
Fork 0
mirror of https://github.com/penpot/penpot.git synced 2025-01-28 09:30:07 -05:00
penpot/frontend/uxbox/ui/util.cljs

63 lines
1.9 KiB
Text
Raw Normal View History

2015-12-17 16:43:58 +02:00
(ns uxbox.ui.util
"A collection of sugar syntax for define react
components using rum libary."
(:refer-clojure :exclude [derive])
(:require [rum.core :as rum]))
2015-12-20 17:54:07 +02:00
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Rum Sugar
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2015-12-17 16:43:58 +02:00
(defn component
[spec]
(let [name (or (:name spec)
(str (gensym "rum-")))
mixins (or (:mixins spec)
[])
spec (merge (dissoc spec :name :mixins)
(when-let [rfn (:render spec)]
{:render (fn [state]
[(apply rfn state (:rum/props state)) state])}))
cls (rum/build-class (conj mixins spec) name)
ctr (fn self
([] (self {}))
([& props]
(let [state {:rum/props props}]
(rum/element cls state nil))))]
(with-meta ctr {:rum/class cls})))
(defn ref-value
[own ref]
(let [component (-> own :rum/react-component)
ref-node (aget (.-refs component) ref)
dom-node (.findDOMNode js/ReactDOM ref-node)]
(.-value dom-node)))
2015-12-18 11:18:00 +02:00
(defn get-ref-dom
[own ref]
(let [component (-> own :rum/react-component)
ref-node (aget (.-refs component) ref)]
(.findDOMNode js/ReactDOM ref-node)))
2015-12-17 16:43:58 +02:00
(def mount rum/mount)
2015-12-20 17:54:07 +02:00
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Color Conversion
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2015-12-21 19:16:02 +02:00
(defn hex->rgb
2015-12-20 17:54:07 +02:00
[^string data]
(some->> (re-find #"^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$" data)
(rest)
(mapv #(js/parseInt % 16))))
2015-12-21 19:16:02 +02:00
(defn rgb->hex
2015-12-20 17:54:07 +02:00
[[r g b]]
(letfn [(to-hex [c]
(let [hexdata (.toString c 16)]
(if (= (count hexdata) 1)
(str "0" hexdata)
hexdata)))]
(str "#" (to-hex r) (to-hex g) (to-hex b))))