Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for CSS Custom Properties in the :style property #215

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/sablono/compiler.clj
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
:else `(sablono.util/join-classes ~value)))

(defmethod compile-attr :style [_ value]
(let [value (camel-case-keys value)]
(let [value (style-attribute-camel-case-keys value)]
(if (map? value)
(to-js value)
`(sablono.interpreter/attributes ~value))))
Expand Down
8 changes: 5 additions & 3 deletions src/sablono/html.cljc
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
(ns sablono.html
(:refer-clojure :exclude [map meta time])
(:require [clojure.string :as str]
[sablono.protocol :as p])
[sablono.protocol :as p]
[sablono.util :as util])
#?(:cljs (:import goog.string.StringBuffer)))

(def MOD 65521)
Expand Down Expand Up @@ -468,8 +469,9 @@
(pos? v))
(str "px")))]
(run! (fn [[k v]]
(let [k (name k)]
(append! sb (camel->kebab-case k) ":" (coerce-value k v) ";")))
(let [k (name k)
k-ident (if (util/css-custom-property? k) k (camel->kebab-case k))]
(append! sb k-ident ":" (coerce-value k v) ";")))
styles)))

(defn render-styles! [sb styles]
Expand Down
26 changes: 21 additions & 5 deletions src/sablono/util.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
[& xs]
(str/join (map to-str xs)))

(defn css-custom-property? [k]
"Tests whether the string `k` represents a CSS custom property;
according to the standard, such properties starts with the prefix '--'."
(str/starts-with? (name k) "--"))

(defn camel-case
"Returns camel case version of the key, e.g. :http-equiv becomes :httpEquiv."
[k]
Expand All @@ -33,16 +38,27 @@
keyword)))
k))

(defn map-keys [k-fn m]
(into {}
(map (fn [[k v]] [(k-fn k) v]))
m))

(defn style-attribute-camel-case-keys
"Transform all the map keys into camel case while not transforming CSS custom properties."
[style-m]
(if (map? style-m)
(map-keys #(if (css-custom-property? %) % (camel-case %)) style-m)
style-m))

(defn camel-case-keys
"Recursively transforms all map keys into camel case."
"Recursively transforms all map keys into camel case; the `:style` key is handled as
a special case to support CSS custom properties."
[m]
(if (map? m)
(let [m (into {}
(map (fn [[k v]] [(camel-case k) v]))
m)]
(let [m (map-keys camel-case m)]
(cond-> m
(map? (:style m))
(update :style camel-case-keys)))
(update :style style-attribute-camel-case-keys)))
m))

(defn element?
Expand Down
6 changes: 5 additions & 1 deletion test/sablono/compiler_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@
:style {:background-color '(identity "black")}}
#j {:className (sablono.util/join-classes (identity "my-class"))
:style #j {:backgroundColor (identity "black")}}
{:style {:--var 1}}
#j {:style #j {:--var 1}}
{:id :XY}
#j {:id "XY"}))

Expand All @@ -74,7 +76,9 @@
{:class '(identity "my-class")
:style {:background-color '(identity "black")}}
#j {:className (sablono.util/join-classes '(identity "my-class"))
:style #j {:backgroundColor '(identity "black")}}))
:style #j {:backgroundColor '(identity "black")}}
{:style {:--var 1}}
#j {:style #j {:--var 1}}))

(deftest test-to-js
(let [v (to-js [])]
Expand Down
4 changes: 4 additions & 0 deletions test/sablono/util_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
{:httpEquiv "Expires"}
{:style {:z-index 1000}}
{:style {:zIndex 1000}}
{:style {:--var 1}}
{:style {:--var 1}}
{:on-click '(fn [e] (let [m {:a-b "c"}]))}
{:onClick '(fn [e] (let [m {:a-b "c"}]))}
{'(identity :class) "my-class"
Expand All @@ -46,6 +48,8 @@
{:httpEquiv "Expires"}
{:style {:z-index 1000}}
{:style {:zIndex 1000}}
{:style {:--var 1}}
{:style {:--var 1}}
{:on-click '(fn [e] (let [m {:a-b "c"}]))}
{:onClick '(fn [e] (let [m {:a-b "c"}]))}
{'(identity :class) "my-class"
Expand Down