-
Notifications
You must be signed in to change notification settings - Fork 10
/
styles.cljs
167 lines (141 loc) · 8.06 KB
/
styles.cljs
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
(ns stylefy.impl.styles
(:require [stylefy.impl.dom :as dom]
[garden.core :refer [css]]
[clojure.string :as str]
[garden.units :as units]
[stylefy.impl.utils :as utils]
[clojure.set :as set]
[garden.color :as color]
[garden.types :as types]
[garden.compiler :as compiler]))
(def global-vendor-prefixes (atom {:stylefy.core/vendors #{}
:stylefy.core/auto-prefix #{}}))
(defn- add-global-vendors [style]
(merge style
{:stylefy.core/vendors (set/union (:stylefy.core/vendors @global-vendor-prefixes)
(:stylefy.core/vendors style))
:stylefy.core/auto-prefix (set/union (:stylefy.core/auto-prefix @global-vendor-prefixes)
(:stylefy.core/auto-prefix style))}))
(defn hash-style [style]
(when (not (empty? style))
(let [hashable-garden-units (reduce
;; Convert Garden units to CSS to make them structurally
;; hashable (different contents = different hash)
(fn [result prop-key]
(let [prop-value (prop-key style)]
(when (or (instance? types/CSSUnit prop-value)
(instance? color/CSSColor prop-value))
(assoc result prop-key (compiler/render-css prop-value)))))
{}
(keys (utils/filter-props style)))
hashable-style (merge style hashable-garden-units)
;; Hash style without its sub-styles. ::sub-styles is only a link to other styles, it
;; does not define the actual properties of this style.
hashable-style (dissoc hashable-style :stylefy.core/sub-styles)]
(str "_stylefy_" (hash hashable-style)))))
(defn- create-style! [{:keys [props hash] :as style}]
(dom/save-style! {:props props :hash hash})
;; Create sub-styles (if any)
(doseq [sub-style (vals (:stylefy.core/sub-styles props))]
(create-style! {:props sub-style :hash (hash-style sub-style)})))
(defn- prepare-style-return-value
"Given a style, hash and options, returns HTML attributes for a Hiccup component,
or nil if there are not any attributes."
[style style-hash options]
(let [with-classes (concat (:stylefy.core/with-classes style)
(:stylefy.core/with-classes options))
html-attributes (utils/filter-props options)
html-attributes-class (:class html-attributes)
html-attributes-inline-style (:style html-attributes)
final-class (str/trim
(cond
(nil? html-attributes-class)
(str/join " " (concat with-classes [style-hash]))
(string? html-attributes-class)
(str/join " " (concat [html-attributes-class] with-classes [style-hash]))
(vector? html-attributes-class)
(str/join " " (concat html-attributes-class with-classes [style-hash]))))
final-html-attributes (merge
html-attributes
(when (not (empty? final-class)) {:class final-class}))]
(assert (or (nil? html-attributes-class)
(string? html-attributes-class)
(vector? html-attributes-class))
(str "Unsupported :class type (should be nil, string or vector). Got: " (pr-str html-attributes-class)))
(assert (nil? html-attributes-inline-style)
"HTML attribute :style is not supported in options map. Instead, you should provide your style definitions as the first argument when calling use-style.")
(when (not (empty? final-html-attributes))
final-html-attributes)))
(defn- style-return-value [style style-hash options]
(let [return-map (prepare-style-return-value style style-hash options)]
(if (or (empty? style)
(dom/style-in-dom? style-hash))
return-map
;; The style definition has not been added into the DOM yet, so return the style props
;; as inline style. Inline style gets replaced soon as the style definition
;; is added into the DOM and the component re-renders itself.
;; However, if there are media queries or specific mode definitions, inline styling is probably
;; going to look wrong. Thus, hide the component completely until the DOM is ready.
(let [contains-media-queries? (some? (:stylefy.core/media style))
contains-feature-queries? (some? (:stylefy.core/supports style))
excluded-modes #{:hover}
contains-modes-not-excluded? (not (empty?
(filter (comp not excluded-modes)
(keys (:stylefy.core/mode style)))))
inline-style (-> style
(utils/filter-props)
(utils/garden-units->to-css))]
(if (or contains-media-queries?
contains-feature-queries?
contains-modes-not-excluded?)
(merge return-map {:style (merge inline-style
{:visibility "hidden"})})
(merge return-map {:style inline-style}))))))
(defn use-style! [style options]
;; Deref to make sure components re-render themselves when styles-in-use updates
;; so that we can get rid of inline styles and use only classes as soon as possible.
@dom/styles-in-use
(let [with-classes-options (:stylefy.core/with-classes options)
with-classes-style (:stylefy.core/with-classes style)]
(assert (or (nil? with-classes-options)
(and (vector? with-classes-options)
(every? string? with-classes-options)))
(str "with-classes argument inside options map must be a vector of strings, got: " (pr-str with-classes-options)))
(assert (or (nil? with-classes-style)
(and (vector? with-classes-style)
(every? string? with-classes-style)))
(str "with-classes argument inside style map must be a vector of strings, got: " (pr-str with-classes-style)))
(dom/check-stylefy-initialisation)
(let [style-with-global-vendors (when-not (empty? style) (add-global-vendors style))
style-hash (hash-style style-with-global-vendors)
already-created (dom/style-by-hash style-hash)]
(when (and (not (empty? style-with-global-vendors))
(some? style-hash)
(not already-created))
(create-style! {:props style-with-global-vendors :hash style-hash}))
(style-return-value style-with-global-vendors style-hash options))))
(defn use-sub-style! [style sub-style options]
(let [resolved-sub-style (get (:stylefy.core/sub-styles style) sub-style)]
(if resolved-sub-style
(use-style! resolved-sub-style options)
(.warn js/console (str "Sub-style " (pr-str sub-style) " not found in style: " (pr-str style))))))
(defn sub-style
[style & sub-styles]
(let [resolved-sub-style (reduce #(get-in %1 [:stylefy.core/sub-styles %2])
style
sub-styles)]
(if resolved-sub-style
resolved-sub-style
(.warn js/console (str "Sub-style " (pr-str sub-styles) " not found in style: " (pr-str style))))))
(defn prepare-styles [styles]
(let [styles (remove nil? styles)]
(doseq [style styles]
(use-style! style {})
(when-let [sub-styles (vals (:stylefy.core/sub-styles style))]
(prepare-styles sub-styles))))
(dom/update-styles-in-dom!))
(defn init-global-vendor-prefixes [options]
(let [global-vendor-prefixes-options (:global-vendor-prefixes options)]
(reset! global-vendor-prefixes
{:stylefy.core/vendors (:stylefy.core/vendors global-vendor-prefixes-options)
:stylefy.core/auto-prefix (:stylefy.core/auto-prefix global-vendor-prefixes-options)})))