This repository has been archived by the owner on Oct 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
impl.cljc
201 lines (182 loc) · 6.42 KB
/
impl.cljc
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
(ns herb.impl
(:require
[clojure.string :as str]
[herb.runtime :as runtime]
[clojure.set :as set]
[garden.stylesheet :refer [at-media at-keyframes at-supports]]
#?@(:cljs [[cljs.compiler :as compiler]
[debux.cs.core :as d :refer-macros [clog clogn dbg dbgn break]]
[cljs.analyzer :as ana]]
:clj [[clojure.tools.analyzer.jvm :refer [analyze]]
[debux.core :refer [dbg]]])))
#?(:cljs (def dev? ^boolean js/goog.DEBUG)
:clj (def dev? true))
(defn convert-pseudo
[pseudos]
(map
(fn [[kw p]]
[(keyword (str "&" kw)) p])
pseudos))
(defn convert-media
[media]
(map (fn [[query style]]
(at-media query [:& style]))
media))
(defn convert-supports
[supports]
(map (fn [[query style]]
(at-supports query [:& style]))
supports))
(defn convert-vendors
[vendors]
(mapv name vendors)
#_(into []
(comp
(map name)
(distinct))
vendors))
(defn resolve-style-fns
"Calls each function provided in a collection of style-fns. Input can take
multiple forms depending on how it got called from the consumer side either
using the macro directly or via extend meta data. Takes a collection of
`style-fns` and a collection `result` that is returned with the resolved style
maps."
[style-fns]
(loop [sf style-fns
result []]
(if (empty? sf)
result
(let [input (first sf)]
(cond
(fn? input)
(conj result (apply input (rest sf)))
(and (coll? input) (fn? (first input)))
(let [style-fn (first input)
style-args (rest input)]
(recur
(rest sf)
(conj result (apply style-fn style-args))))
:else (recur
(rest sf)
(into result (resolve-style-fns input))))))))
(defn process-meta-xform
"Return a transducer that pulls out a given meta type from a sequence and filter
out nil values"
[meta-type]
(comp
(map meta)
(map meta-type)
(filter identity)))
(defn extract-styles
"Extract all the `:extend` meta, ensuring what we walk the entire tree, passing
each extend vector of style-fns to `resolve-style-fns` for resolution.
Takes a collection of `style-fns` and a result collection that is returned
with the resolved styles"
[style-fns]
(loop [sf style-fns
result []]
(cond
(fn? sf)
(recur [sf] result)
(and (vector? sf) (not (empty? sf)))
(let [styles (resolve-style-fns sf)
new-meta (into [] (process-meta-xform :extend) styles)]
(recur new-meta
(into styles result)))
:else result)))
(defn extract-meta
"Takes a group of resolved styles and a meta type. Pull out each meta obj and
merge to prevent duplicates, finally convert to garden acceptable input and
return"
[styles meta-type]
(let [convert-fn (case meta-type
:media convert-media
:supports convert-supports
:prefix identity
:vendors convert-vendors
:pseudo convert-pseudo)
extracted (into [] (process-meta-xform meta-type) styles)]
(when (seq extracted)
(let [merged (case meta-type
:prefix (last extracted)
:vendors (apply concat extracted)
(apply merge {} extracted))]
(convert-fn merged)))))
(defn prepare-data
"Prepare `resolved-styles` so they can be passed to `garden.core/css` Merge
the styles to remove duplicate entries and ensuring precedence. Extract all
meta and return a final vector of styles including meta."
[resolved-styles]
{:style (apply merge {} resolved-styles)
:pseudo (extract-meta resolved-styles :pseudo)
:vendors (extract-meta resolved-styles :vendors)
:prefix (extract-meta resolved-styles :prefix)
:supports (extract-meta resolved-styles :supports)
:media (extract-meta resolved-styles :media)})
(defn sanitize
"Takes `input` and remove any non-valid characters"
[input]
(when input
(cond
(keyword? input) (sanitize (name input))
:else (str/replace (str input) #"[^A-Za-z0-9-_]" "_"))))
(defn compose-selector
[n k]
(str (sanitize n)
(when k
(str "_" (sanitize k)))))
(defn create-data-string
"Create a fully qualified name string for use in the data-herb attr"
[n k]
(let [c (str/split n #?(:cljs #"/" :clj #"\$"))
ns (apply str (interpose "." (butlast c)))
sym (last c)]
(str (symbol ns sym)
(when k (str "[" k "]")))))
(defn get-name
[style-fn ns-name style-data]
(let [name* #?(:cljs (.-name style-fn)
:clj (str/replace
(-> (analyze style-fn)
:tag
(.getName))
#"_" "-"))
anon? #?(:clj (str/includes? name* "fn--")
:cljs (empty? name*))
hash* (when anon?
#?(:cljs (.abs js/Math (hash style-data))
:clj (Math/abs (hash style-data))))
cname (cond
(and anon? (not dev?))
(str "A-" hash*)
(and dev? anon?)
#?(:cljs
(str ns-name "/" "anonymous-" hash*)
:clj
(str/replace name* #"fn--[0-9]*" (str "anonymous-" hash*)))
:else name*)]
#?(:cljs (demunge cname)
:clj cname)))
(defn with-style!
"Entry point for macros.
Takes an `opt` map as first argument, and currently only supports `:id true`
which appends an id identifier instead of a class to the DOM"
[{:keys [id? style?]} fn-name ns-name style-fn & args]
(let [resolved-styles (extract-styles (into [style-fn] args))
style-data (prepare-data resolved-styles)
{:keys [group key prefix] :as meta-data} (-> resolved-styles last meta)
name* (get-name style-fn ns-name style-data)
data-str (when dev? (if group
(create-data-string name* nil)
(create-data-string name* key)))
selector (compose-selector name* key)
identifier (if group
(sanitize name*)
selector)
style-data [(str (if id? "#" ".") selector) style-data]
result (runtime/inject-style! identifier style-data data-str)]
(if style?
(-> result
(get identifier)
:css)
selector)))