|
232 | 232 | (into [] tag) |
233 | 233 | [(cond |
234 | 234 | (= ::styled (type tag)) |
235 | | - (classname tag) |
| 235 | + (str "." (classname tag)) |
236 | 236 | (sequential? tag) |
237 | 237 | (process-rule tag) |
238 | 238 | :else |
|
361 | 361 | names." |
362 | 362 | [classes class] |
363 | 363 | (cond |
| 364 | + (nil? class) |
| 365 | + classes |
| 366 | + |
364 | 367 | (sequential? class) |
365 | 368 | (reduce add-class classes class) |
366 | 369 |
|
|
416 | 419 | (reduce merge-attrs (merge-attrs p1 p2) ps))) |
417 | 420 |
|
418 | 421 | (defn attr-add-class [attrs class] |
419 | | - (update attrs :class add-class class)) |
| 422 | + (if class |
| 423 | + (update attrs :class add-class class) |
| 424 | + attrs)) |
420 | 425 |
|
421 | 426 | (defn expand-hiccup-tag-simple |
422 | 427 | "Expand an ornament component being called directly with child elements, without |
423 | 428 | custom render function." |
424 | 429 | [tag css-class children extra-attrs] |
425 | | - (if (sequential? children) |
426 | | - (as-> children $ |
427 | | - (if (= :<> (first $)) (next $) $) |
428 | | - (if (map? (first $)) |
429 | | - (into [tag (attr-add-class |
430 | | - (merge-attrs (first $) (meta children) extra-attrs) |
431 | | - css-class)] (next $)) |
432 | | - (into [tag (attr-add-class |
433 | | - (merge-attrs (meta children) extra-attrs) |
434 | | - css-class)] |
435 | | - (if (vector? $) (list $) $)))) |
436 | | - [tag (attr-add-class extra-attrs css-class) children])) |
| 430 | + (let [[tag attrs children :as result] |
| 431 | + (if (sequential? children) |
| 432 | + (as-> children $ |
| 433 | + (if (= :<> (first $)) (next $) $) |
| 434 | + (if (map? (first $)) |
| 435 | + (into [tag (attr-add-class |
| 436 | + (merge-attrs (first $) (meta children) extra-attrs) |
| 437 | + css-class)] (next $)) |
| 438 | + (into [tag (attr-add-class |
| 439 | + (merge-attrs (meta children) extra-attrs) |
| 440 | + css-class)] |
| 441 | + (if (vector? $) (list $) $)))) |
| 442 | + [tag (attr-add-class extra-attrs css-class) children])] |
| 443 | + (if (= :<> (first children)) |
| 444 | + (recur tag nil children attrs) |
| 445 | + result))) |
437 | 446 |
|
438 | 447 | (defn expand-hiccup-tag |
439 | 448 | "Handle expanding/rendering the component to Hiccup |
|
663 | 672 | (when (symbol? tagname) |
664 | 673 | (:fn-tails (get @registry (qualify-sym &env tagname))))) |
665 | 674 |
|
| 675 | + fn-tails (when (seq fn-tails) |
| 676 | + (if (and (= 1 (count fn-tails)) |
| 677 | + (= 0 (count (ffirst fn-tails)))) |
| 678 | + `(([] ~@(rest (first fn-tails))) |
| 679 | + ([attrs#] [:<> attrs# (do ~@(rest (first fn-tails)))])) |
| 680 | + fn-tails)) |
666 | 681 | ;; For ClojureScript support (but also used in Clojure-only), add the |
667 | 682 | ;; Clojure-version of the styled component to the registry directly |
668 | 683 | ;; during macroexpansion, so that even in a ClojureScript-only world |
|
745 | 760 | rules |
746 | 761 | (cons nil rules)) |
747 | 762 | varsym (qualify-sym &env rules-name) |
748 | | - rules (eval `(do |
| 763 | + rules (process-rules |
| 764 | + (eval `(do |
749 | 765 | (in-ns '~(ns-name *ns*)) |
750 | | - ~(cons 'list rules)))] |
| 766 | + ~(cons 'list rules))))] |
751 | 767 | (register! rules-registry varsym {:rules rules}) |
752 | 768 | (when-not (:ns &env) |
753 | 769 | `(def ~rules-name ~(render-docstring docstring rules) '~rules))))) |
|
857 | 873 |
|
858 | 874 | #?(:clj |
859 | 875 | (defn import-tokens*! |
860 | | - ([prefix tokens] |
| 876 | + ([tokens {:keys [include-values? prefix] |
| 877 | + :or {include-values? true |
| 878 | + prefix ""}}] |
861 | 879 | (mapcat |
862 | 880 | identity |
863 | 881 | (for [[tname tdef] tokens] |
864 | 882 | (let [tname (str prefix tname) |
865 | 883 | {:strs [$description $value $type]} tdef |
866 | 884 | more (into {} (remove (fn [[k v]] (= (first k) \$))) tdef)] |
867 | | - (cond-> [`(defprop ~(symbol tname) ~@(when $description [$description]) ~$value)] |
| 885 | + (cond-> [`(defprop ~(symbol tname) |
| 886 | + ~@(when $description [(str $description "\n\nDefault: " $value)]) |
| 887 | + ~@(when (and $value include-values?) |
| 888 | + [$value]))] |
868 | 889 | (seq more) |
869 | 890 | (into (import-tokens*! (str tname "-") more))))))))) |
870 | 891 |
|
871 | 892 | #?(:clj |
872 | 893 | (defmacro import-tokens! |
873 | | - ([tokens] |
874 | | - `(import-tokens! "" ~tokens)) |
875 | | - ([prefix tokens] |
876 | | - `(do ~@(import-tokens*! prefix (eval tokens)))))) |
| 894 | + "Import a standard design tokens JSON file. |
| 895 | + Emits a sequence of `defprop`, i.e. it defines custom CSS properties (aka |
| 896 | + variables). See https://design-tokens.github.io/community-group/format/ |
| 897 | + - tokens: parsed JSON, we don't bundle a parser, you have to do that yourself |
| 898 | + - opts: options map, supports `:prefix` and `:include-values?`. Has to be |
| 899 | + literal (used by the macro itself) |
| 900 | + - prefix: string prefix to add to the (clojure and CSS) var names |
| 901 | + - :include-values? false: only create the Clojure vars to access the props, |
| 902 | + don't include their definitions/values in the CSS. Presumably because you are |
| 903 | + loading CSS separately that already defines these. |
| 904 | + " |
| 905 | + ([tokens & [opts]] |
| 906 | + `(do ~@(import-tokens*! (eval tokens) opts))))) |
877 | 907 |
|
878 | 908 | #?(:clj |
879 | | - (defn defined-garden [] |
| 909 | + (defn defined-garden |
| 910 | + "All CSS defined through the different Ornament facilities (defprop, defstyled, |
| 911 | + defrules), in Garden syntax. Run this through `garden.compiler/compile-css`." |
| 912 | + [] |
880 | 913 | (concat |
881 | 914 | (let [props (->> @props-registry |
882 | 915 | vals |
|
889 | 922 | (->> @rules-registry |
890 | 923 | vals |
891 | 924 | (sort-by :index) |
892 | | - (mapcat :rules) |
893 | | - (map process-rules)) |
| 925 | + (mapcat :rules)) |
894 | 926 | (->> @registry |
895 | 927 | vals |
896 | 928 | (sort-by :index) |
|
934 | 966 | (comment |
935 | 967 | (spit "/tmp/ornament.css" (defined-styles)) |
936 | 968 | ) |
| 969 | + (->> @rules-registry |
| 970 | + vals |
| 971 | + (sort-by :index) |
| 972 | + (mapcat :rules) |
| 973 | + process-rules) |
0 commit comments