Skip to content

Commit c2209f5

Browse files
committed
handle consuming/inserting collection of static css strings
1 parent ff9f3cb commit c2209f5

File tree

4 files changed

+90
-36
lines changed

4 files changed

+90
-36
lines changed

example/src/example/core.cljs

Lines changed: 66 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
(ns example.core
22
(:require [rum.core :as rum]
3-
[cljss.core :as css :refer-macros [inject-global defstyles]]
3+
[cljss.core :as css :refer-macros [defstyles defkeyframes]]
4+
[cljss.rum :refer-macros [defstyled]]
45
[devcards.core :refer-macros [defcard start-devcard-ui!]]
56
[sablono.core :refer [html]]))
67

@@ -12,15 +13,15 @@
1213

1314
;; design system
1415
(def colors
15-
{:blue "#298FCA"
16-
:green "#7BC86C"
16+
{:blue "#298FCA"
17+
:green "#7BC86C"
1718
:orange "#FFB968"
18-
:red "#EF7564"
19+
:red "#EF7564"
1920
:yellow "#F5DD29"})
2021

2122
(defstyles text-styles [size]
2223
{:font-family "Helvetica Neue"
23-
:font-size size})
24+
:font-size size})
2425

2526
(rum/defc Text
2627
[{:keys [size]}
@@ -43,40 +44,45 @@
4344

4445
(def button->color
4546
{:warning (:orange colors)
46-
:error (:red colors)
47-
:ok (:green colors)})
47+
:error (:red colors)
48+
:ok (:green colors)})
4849

4950
(rum/defc Button
5051
[{:keys [kind
5152
on-click]}
5253
child]
5354
[:button
5455
{:on-click on-click
55-
:css {:background (get button->color kind)
56-
:border 0
57-
:border-radius "5px"
58-
:padding "8px 24px"
59-
:font-size "14px"
60-
:color "#fff"
61-
:&:hover {:color "black"
62-
:cursor "pointer"}
63-
:&:focus {:outline "none"}}}
56+
:css {:background (get button->color kind)
57+
:border 0
58+
:border-radius "5px"
59+
:padding "8px 24px"
60+
:font-size "14px"
61+
:color "#fff"
62+
:&:hover {:color "black"
63+
:cursor "pointer"}
64+
:&:focus {:outline "none"}}}
6465
child])
6566

6667
;; cards
6768
(defcard Colors
6869
"Base colors"
6970
(fn [state _]
7071
(html
71-
[:div {:css {:display "flex"
72+
[:div {:css {:display "flex"
7273
:justify-content "space-between"}}
74+
7375
(for [[_ color] (:colors @state)]
7476
[:div
75-
{:css {:background color
76-
:width "100px"
77-
:height "100px"
77+
{:css {:background color
78+
:width "100px"
79+
:height "100px"
7880
:border-radius "5px"
79-
:padding "8px"}}
81+
:padding "8px"
82+
:&:hover {:border "1px solid blue"}
83+
::css/media {[[:max-width "740px"]]
84+
{:width "64px"
85+
:height "64px"}}}}
8086
color])]))
8187
{:colors colors})
8288

@@ -94,6 +100,45 @@
94100
(Button {:kind :error} "Error")
95101
(Button {:kind :ok} "OK")]))
96102

103+
(def spinner-sizes
104+
{"m" "16px"
105+
"l" "32px"
106+
"xl" "64px"})
107+
108+
(defkeyframes spin []
109+
{:from {:transform "rotate(0deg)"}
110+
:to {:transform "rotate(360deg)"}})
111+
112+
(rum/defc spinner
113+
[{:keys [size color]}]
114+
[:div {:css {:width (spinner-sizes size)
115+
:height (spinner-sizes size)
116+
:border-radius "50%"
117+
:border-top (str "2px solid " color)
118+
:border-left "2px solid rgba(0,0,255,0.3)"
119+
:border-bottom "2px solid rgba(0,0,255,0.3)"
120+
:border-right "2px solid rgba(0,0,255,0.3)"
121+
:animation (str (spin) " 1500ms linear infinite")}}])
122+
123+
(defcard Spinner
124+
(html
125+
[:div
126+
(spinner {:size "m" :color "blue"})
127+
(spinner {:size "l" :color "blue"})
128+
(spinner {:size "xl" :color "blue"})]))
129+
130+
(defstyled text-field :textarea
131+
{:width :width
132+
:height :height
133+
:border-radius "5px"
134+
:border "1px solid #ccc"
135+
:&:hover {:background-color "#eee"}
136+
:&:focus {:outline "none"
137+
:border "1px solid blue"}})
138+
139+
(defcard TextField
140+
(text-field {:width "300px"
141+
:height "140px"}))
97142

98143
(defn mount []
99144
(css/remove-styles!))

project.clj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,6 @@
5454
:devcards true
5555
:optimizations :advanced
5656
:closure-defines {"goog.DEBUG" false}
57+
:pseudo-names true
5758
:verbose true
5859
:pretty-print false}}]})

src/cljss/builder.clj

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,20 @@
2424
(into vals)
2525
(concat mvals)
2626
(into []))
27-
static (apply str static (map first pstyles))
28-
static (str static mstatic)]
27+
static (into [static] (map first pstyles))
28+
static (if mstatic
29+
(conj static mstatic)
30+
static)]
2931
[cls static vals]))
3032

3133
(comment
3234
(build-styles
3335
"hello"
34-
{:cljss.core/media {{:max-width 'sa :min-width 'l} {:font-size 'a
35-
:&:hover {:margin 'b}}}}))
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"}}}))

src/cljss/core.cljs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(ns cljss.core
22
(:require [cljss.sheet :refer [create-sheet insert! filled? flush!]]
3-
[cljss.utils :refer [build-css]]
3+
[cljss.utils :refer [build-css dev?]]
44
[clojure.string :as cstr]))
55

66
(def ^:private sheets (atom (list (create-sheet))))
@@ -20,12 +20,12 @@
2020
(swap! sheets conj (create-sheet))
2121
(css cls static vars))
2222
(do
23-
(loop [[s & static] static]
24-
(if (seq static)
25-
(do
26-
(insert! sheet s cls)
27-
(recur static))
28-
(insert! sheet s cls)))
23+
(loop [[s & static] static
24+
idx 0]
25+
(let [cls (str cls "-" idx)]
26+
(insert! sheet s cls)
27+
(when-not (empty? static)
28+
(recur static (inc idx)))))
2929
(if (pos? (count vars))
3030
(let [var-cls (str "vars-" (hash vars))]
3131
(insert! sheet #(build-css var-cls vars) var-cls)
@@ -109,13 +109,13 @@
109109

110110
(defn -styled [cls static vars attrs create-element]
111111
(let [clsn (str cls "-" (gensym))
112-
static (if ^boolean goog.DEBUG
113-
(cstr/replace static cls clsn)
112+
static (if ^boolean dev?
113+
(map #(cstr/replace % cls clsn) static)
114114
static)
115-
vars (if ^boolean goog.DEBUG
115+
vars (if ^boolean dev?
116116
(->> vars (map (fn [[k v]] [(cstr/replace k cls clsn) v])))
117117
vars)
118-
cls (if ^boolean goog.DEBUG clsn cls)]
118+
cls (if ^boolean dev? clsn cls)]
119119
(fn [props & children]
120120
(let [[props children] (if (map? props)
121121
(array props children)

0 commit comments

Comments
 (0)