Skip to content

Instantly share code, notes, and snippets.

@roman01la
Created September 25, 2018 13:07
Show Gist options
  • Select an option

  • Save roman01la/98d23f56468e266d86314aadabb56f12 to your computer and use it in GitHub Desktop.

Select an option

Save roman01la/98d23f56468e266d86314aadabb56f12 to your computer and use it in GitHub Desktop.
(ns rnstyles.core
(:require [clojure.string :as cstr])
(:import (cljs.tagged_literals JSValue)))
;; Sablono's stuff
;; Converting Clojure data into ClojureScript (JS)
;; ====================================================
(defprotocol IJSValue
(to-js [x]))
(defn- to-js-map [m]
(JSValue. (into {} (map (fn [[k v]] [k (to-js v)])) m)))
(extend-protocol IJSValue
clojure.lang.Keyword
(to-js [x]
(if (qualified-keyword? x)
(str (namespace x) "/" (name x))
(name x)))
clojure.lang.PersistentArrayMap
(to-js [x]
(to-js-map x))
clojure.lang.PersistentHashMap
(to-js [x]
(to-js-map x))
clojure.lang.PersistentVector
(to-js [x]
(JSValue. (mapv to-js x)))
Object
(to-js [x]
x)
nil
(to-js [_]
nil))
;; =========================================
(defn kebab->camel [^String method-name]
(cstr/replace
method-name
#"-(\w)"
#(cstr/upper-case (second %1))))
(defn kebab-k->camel-s [k]
(-> k name kebab->camel))
(defn clj-css->rn-css [styles]
(->> styles
(map (fn [[k v]]
[(kebab-k->camel-s k) v]))
(into {})))
(defn make-styles [styles]
(->> styles
(map (fn [[k styles]]
[(name k) (clj-css->rn-css styles)]))
(into {})
to-js))
(defmacro defstyles [sym styles]
`(let [s# (.create js/StyleSheet ~(make-styles styles))]
(defn ~sym [k#]
(goog.object/get s# (name k#)))))
(comment
(macroexpand-1 '(defstyles styles {:header {:flex-direction "row"}})))
;; (let [s (.create js/StyleSheet #js {:header #js {:flexDirection "row"}})]
;; (defn styles [k] (goog.object/get s (name k))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment