Skip to content

Instantly share code, notes, and snippets.

@dantheobserver
Last active November 10, 2020 03:30
Show Gist options
  • Save dantheobserver/7e476da25003252f8b0d5217e9a9e63d to your computer and use it in GitHub Desktop.
Save dantheobserver/7e476da25003252f8b0d5217e9a9e63d to your computer and use it in GitHub Desktop.
Macro to hopefully make it easier managing utility css libraries like tachyons
(ns tachyons
(:require [clojure.string :as str]))
(defn styles->str [styles]
(let [ref-marker "*"
is-symbol? #(str/starts-with? % ref-marker)
class-or-sym #(cond
(is-symbol? %)
(-> %
name
(str/replace-first ref-marker "")
symbol)
(list? %) %
:else (name %))
items (map class-or-sym styles)]
`(str/join " " (list ~@items))))
#_[(styles->str '[a b c])
(styles->str '[a b *c])
(styles->str '[a b (if true a *c)])]
(defn style-bindings [styles]
(mapv #(if (vector? %)
(styles->str %)
(symbol %)) styles))
#_[(style-bindings
'[interact [grow pointer]])]
(defmacro defcomponent
[styles component]
(let [bind-forms (style-bindings styles)]
`(let [~@bind-forms]
~component)))
;; Component with backref and expressions
#_(defcomponent
[interact [grow pointer]
grow [*interact another one]
a [choice-a]
b [choice-b]
conditional [(if (= 1 1) a b)]]
[:div {:class interact}
[:div {:class grow}]
[:div {:class conditional}]])
#_(macroexpand-1
'(defcomponent
[interact [grow pointer]]
[:div {:class interact}]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment