Skip to content

Commit fd66cca

Browse files
committed
add support for arbitrary nested CSS selectors via string key
1 parent c2209f5 commit fd66cca

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

example/src/example/core.cljs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,13 @@
140140
(text-field {:width "300px"
141141
:height "140px"}))
142142

143+
(defcard Selectors
144+
(html
145+
[:ul
146+
[:li {:css {"&:first-child" {:color "red"}}} "first-child"]
147+
[:li {:css {".link" {:color "green"}}}
148+
[:a.link {:href "#"} "nested element with .link class name"]]]))
149+
143150
(defn mount []
144151
(css/remove-styles!))
145152

src/cljss/builder.clj

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,33 @@
1010
(defn build-styles [cls styles]
1111
(c/reset-env! {:cls cls})
1212
(let [pseudo (filterv utils/pseudo? styles)
13+
nested (->> styles
14+
(filterv (comp not utils/pseudo?))
15+
(filterv utils/nested?))
1316
[mstatic mvals] (some-> styles :cljss.core/media build-media)
1417
styles (dissoc styles :cljss.core/media)
15-
styles (filterv (comp not utils/pseudo?) styles)
18+
styles (filterv #(and (not (utils/pseudo? %)) (not (utils/nested? %))) styles)
1619
[static vals] (c/collect-styles cls styles)
1720
pstyles (->> pseudo
1821
(reduce
1922
(fn [coll [rule styles]]
2023
(conj coll (c/collect-styles (str cls (subs (name rule) 1)) styles)))
2124
[]))
25+
nstyles (->> nested
26+
(reduce
27+
(fn [coll [rule styles]]
28+
(conj coll (c/collect-styles (str cls " " rule) styles)))
29+
[]))
2230
vals (->> pstyles
2331
(mapcat second)
2432
(into vals)
2533
(concat mvals)
2634
(into []))
35+
vals (->> nstyles
36+
(mapcat second)
37+
(into vals))
2738
static (into [static] (map first pstyles))
39+
static (into static (map first nstyles))
2840
static (if mstatic
2941
(conj static mstatic)
3042
static)]
@@ -33,11 +45,5 @@
3345
(comment
3446
(build-styles
3547
"hello"
36-
{:background 'color
37-
:width "100px"
38-
:height "100px"
39-
:border-radius "5px"
40-
:padding "8px"
41-
:cljss.core/media {[[:max-width "740px"]]
42-
{:width "64px"
43-
:height "64px"}}}))
48+
{"&:first-child" {:color "red"}
49+
"a" {:color "blue"}}))

src/cljss/utils.cljc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
(and (re-matches #"&:.*" (name rule))
88
(map? value)))
99

10+
(defn nested? [[rule value]]
11+
(and (string? rule)
12+
(map? value)))
13+
1014
(defn literal? [x]
1115
(or (string? x) (number? x)))
1216

0 commit comments

Comments
 (0)