forked from clj-commons/cljss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mixins.clj
115 lines (89 loc) · 1.92 KB
/
mixins.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
(ns cljss.mixins)
;;
;; Theme
;;
(def -default-theme
{:space [0 8 16 32 64]
:font-size [12 14 16 20 24 32 48 64 72]})
(def theme* (atom -default-theme))
;;
;; Utils
;;
(defn -px [val]
(str val "px"))
(defn -% [val]
(str val "%"))
;;
;; Spacing (margin, padding)
;;
(def -space-mapping
{:m [:margin]
:mt [:margin-top]
:mr [:margin-right]
:mb [:margin-bottom]
:ml [:margin-left]
:mx [:margin-right :margin-left]
:my [:margin-top :margin-bottom]
:p [:padding]
:pt [:padding-top]
:pr [:padding-right]
:pb [:padding-bottom]
:pl [:padding-left]
:px [:padding-right :padding-left]
:py [:padding-top :padding-bottom]})
(def -space-keys
(keys -space-mapping))
(defn -transform-space-attr [space [attr idx]]
(let [val (->> idx (nth space) -px)
attrs (->> (-space-mapping attr)
(map #(vector % val)))]
attrs))
(defn -transform-space [attrs]
(let [space (:space @theme*)]
(->> (select-keys attrs -space-keys)
(mapcat #(-transform-space-attr space %)))))
(comment
(-transform-space {:mx 2 :mt 1}))
;;
;; Font Size
;;
(defn -transform-font-size [attrs]
(let [fs (:font-size @theme*)
idx (:font-size attrs)
font-size (->> idx (nth fs) -px)]
[[:font-size font-size]]))
(comment
(-transform-font-size {:font-size 3}))
;;
;; Width
;;
(defn -transform-width [attrs]
(let [width (:width attrs)
width
(if (>= 1 width)
(->> width (* 100) int -%)
(-px width))]
[[:width width]]))
(comment
(-transform-width {:width 0.4}))
;;
;; Generic transformer
;;
(defn transform-attrs [attrs & fns]
(->> fns
(mapcat #(% attrs))
(into {})))
(comment
(transform-attrs
{:width 0.1
:font-size 2
:mx 4}
space
font-size
width))
;;
;; Public Mixins
;;
(def space -transform-space)
(def font-size -transform-font-size)
(def width -transform-width)