Skip to content

Commit 0ece831

Browse files
committed
ClojureScript compat for props
1 parent 47607f9 commit 0ece831

File tree

1 file changed

+79
-50
lines changed

1 file changed

+79
-50
lines changed

src/lambdaisland/ornament.cljc

Lines changed: 79 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
(ns lambdaisland.ornament
22
"CSS-in-clj(s)"
33
#?@
4-
(:clj
5-
[(:require
6-
[clojure.string :as str]
7-
[clojure.walk :as walk]
8-
[garden.color :as gcolor]
9-
[garden.compiler :as gc]
10-
[garden.stylesheet :as gs]
11-
[garden.types :as gt]
12-
[garden.util :as gu]
13-
[girouette.tw.color :as girouette-color]
14-
[girouette.tw.core :as girouette]
15-
[girouette.tw.default-api :as girouette-default]
16-
[girouette.tw.preflight :as girouette-preflight]
17-
[girouette.tw.typography :as girouette-typography]
18-
[girouette.version :as girouette-version]
19-
[meta-merge.core :as meta-merge])]
20-
:cljs
21-
[(:require [clojure.string :as str] [garden.util :as gu])]))
4+
(:clj
5+
[(:require
6+
[clojure.string :as str]
7+
[clojure.walk :as walk]
8+
[garden.color :as gcolor]
9+
[garden.compiler :as gc]
10+
[garden.stylesheet :as gs]
11+
[garden.types :as gt]
12+
[garden.util :as gu]
13+
[girouette.tw.color :as girouette-color]
14+
[girouette.tw.core :as girouette]
15+
[girouette.tw.default-api :as girouette-default]
16+
[girouette.tw.preflight :as girouette-preflight]
17+
[girouette.tw.typography :as girouette-typography]
18+
[girouette.version :as girouette-version]
19+
[meta-merge.core :as meta-merge])]
20+
:cljs
21+
[(:require [clojure.string :as str] [garden.util :as gu])]))
2222

2323
#?(:clj
2424
(defonce ^{:doc "Registry of styled components
@@ -210,7 +210,7 @@
210210
(-> varsym
211211
namespace
212212
(str/replace #"\." "_")
213-
(->> (str "__"))))]
213+
(str "__")))]
214214
(str prefix (munge-str (name varsym)))))
215215

216216
(defn join-vector-by [sep val]
@@ -616,6 +616,33 @@
616616
(str "\n\n"))
617617
(gc/compile-css (process-rules rules)))))))
618618

619+
#?(:clj
620+
(defn component->selector [&env s]
621+
(if (symbol? s)
622+
(let [qsym (qualify-sym &env s)]
623+
(if (contains? @registry qsym)
624+
(str "." (get-in @registry [qsym :classname]))
625+
s))
626+
s)))
627+
628+
#?(:clj
629+
(defn component->rules [&env s]
630+
(if (symbol? s)
631+
(let [qsym (qualify-sym &env s)]
632+
(if (contains? @registry qsym)
633+
(get-in @registry [qsym :rules])
634+
[s]))
635+
[s])))
636+
637+
#?(:clj
638+
(defn prop->rvalue [&env s]
639+
(if (symbol? s)
640+
(let [qsym (qualify-sym &env s)]
641+
(if (contains? @props-registry qsym)
642+
(str "var(--" (get-in @props-registry [qsym :propname]) ")")
643+
s))
644+
s)))
645+
619646
#?(:clj
620647
(defmacro defstyled [sym tagname & styles]
621648
(let [varsym (symbol (name (ns-name *ns*)) (name sym))
@@ -649,34 +676,26 @@
649676
;; build output), and when defined defstyled in cljs files there are
650677
;; no Clojure vars that we can resolve, so we need to resolve this
651678
;; ourselves via the registry.
679+
component->selector (partial component->selector &env)
680+
prop->rvalue (partial prop->rvalue &env)
681+
component->rules (partial component->rules &env)
652682
rules (eval `(do
653683
(in-ns '~(ns-name *ns*))
654684
~(walk/postwalk
655685
(fn [o]
656-
(if (vector? o)
657-
(let [component->selector
658-
(fn [s]
659-
(if (and (symbol? s)
660-
(contains? @registry (qualify-sym &env s)))
661-
`(str "." (get-in @registry ['~(qualify-sym &env s) :classname]))
662-
s))]
663-
(into [(if (set? (first o))
664-
(into #{} (map component->selector (first o)))
665-
(component->selector (first o)))]
666-
(mapcat (fn [s]
667-
(if (and (symbol? s)
668-
(contains? @registry (qualify-sym &env s)))
669-
(get-in @registry [(qualify-sym &env s) :rules])
670-
[s])))
671-
(next o)))
686+
(cond
687+
(vector? o)
688+
(into [(if (set? (first o))
689+
(into #{} (map component->selector (first o)))
690+
(component->selector (first o)))]
691+
(mapcat component->rules)
692+
(next o))
693+
(map? o)
694+
(update-vals o prop->rvalue)
695+
:else
672696
o))
673697
(vec
674-
(mapcat (fn [s]
675-
(if (and (symbol? s)
676-
(contains? @registry (qualify-sym &env s)))
677-
(get-in @registry [(qualify-sym &env s) :rules])
678-
[s]))
679-
rules)))))]
698+
(mapcat component->rules rules)))))]
680699
(register! registry
681700
varsym
682701
{:var varsym
@@ -767,13 +786,12 @@
767786
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
768787
;; Props
769788

770-
#?(:clj
771-
(defprotocol CSSProp
772-
(lvalue [p])
773-
(rvalue [p])))
789+
(defprotocol CSSProp
790+
(lvalue [p])
791+
(rvalue [p]))
774792

775-
#?(:clj
776-
(defn css-prop [prop-name default]
793+
(defn css-prop [prop-name default]
794+
#?(:clj
777795
(with-meta
778796
(reify
779797
CSSProp
@@ -788,6 +806,17 @@
788806
(valAt [this kw] (when (= :default kw) default))
789807
(valAt [this kw fallback] (if (= :default kw) default fallback))
790808
)
809+
{:type ::prop})
810+
:cljs
811+
(with-meta
812+
(reify
813+
CSSProp
814+
(lvalue [_] (str "--" (name prop-name)))
815+
(rvalue [_] (str "var(--" (name prop-name) ")"))
816+
ILookup
817+
(-valAt [this kw] (when (= :default kw) default))
818+
(-valAt [this kw fallback] (if (= :default kw) default fallback))
819+
)
791820
{:type ::prop})))
792821

793822
#?(:clj
@@ -854,9 +883,9 @@
854883
(filter (comp some? :value)))]
855884
(when (seq props)
856885
[[":where(html)" (into {}
857-
(map (juxt (comp (partial str "--") :propname)
858-
:value))
859-
props)]]))
886+
(map (juxt (comp (partial str "--") :propname)
887+
:value))
888+
props)]]))
860889
(->> @rules-registry
861890
vals
862891
(sort-by :index)

0 commit comments

Comments
 (0)