|
1 | 1 | (ns lambdaisland.ornament |
2 | 2 | "CSS-in-clj(s)" |
3 | 3 | #?@ |
4 | | - (:clj |
5 | | - [(:require |
6 | | - [clojure.string :as str] |
7 | | - [clojure.walk :as walk] |
8 | | - [garden.color :as gcolor] |
9 | | - [garden.compiler :as gc] |
10 | | - [garden.stylesheet :as gs] |
11 | | - [garden.types :as gt] |
12 | | - [garden.util :as gu] |
13 | | - [girouette.tw.color :as girouette-color] |
14 | | - [girouette.tw.core :as girouette] |
15 | | - [girouette.tw.default-api :as girouette-default] |
16 | | - [girouette.tw.preflight :as girouette-preflight] |
17 | | - [girouette.tw.typography :as girouette-typography] |
18 | | - [girouette.version :as girouette-version] |
19 | | - [meta-merge.core :as meta-merge])] |
20 | | - :cljs |
21 | | - [(:require [clojure.string :as str] [garden.util :as gu])])) |
| 4 | + (:clj |
| 5 | + [(:require |
| 6 | + [clojure.string :as str] |
| 7 | + [clojure.walk :as walk] |
| 8 | + [garden.color :as gcolor] |
| 9 | + [garden.compiler :as gc] |
| 10 | + [garden.stylesheet :as gs] |
| 11 | + [garden.types :as gt] |
| 12 | + [garden.util :as gu] |
| 13 | + [girouette.tw.color :as girouette-color] |
| 14 | + [girouette.tw.core :as girouette] |
| 15 | + [girouette.tw.default-api :as girouette-default] |
| 16 | + [girouette.tw.preflight :as girouette-preflight] |
| 17 | + [girouette.tw.typography :as girouette-typography] |
| 18 | + [girouette.version :as girouette-version] |
| 19 | + [meta-merge.core :as meta-merge])] |
| 20 | + :cljs |
| 21 | + [(:require [clojure.string :as str] [garden.util :as gu])])) |
22 | 22 |
|
23 | 23 | #?(:clj |
24 | 24 | (defonce ^{:doc "Registry of styled components |
|
210 | 210 | (-> varsym |
211 | 211 | namespace |
212 | 212 | (str/replace #"\." "_") |
213 | | - (->> (str "__"))))] |
| 213 | + (str "__")))] |
214 | 214 | (str prefix (munge-str (name varsym))))) |
215 | 215 |
|
216 | 216 | (defn join-vector-by [sep val] |
|
616 | 616 | (str "\n\n")) |
617 | 617 | (gc/compile-css (process-rules rules))))))) |
618 | 618 |
|
| 619 | +#?(:clj |
| 620 | + (defn component->selector [&env s] |
| 621 | + (if (symbol? s) |
| 622 | + (let [qsym (qualify-sym &env s)] |
| 623 | + (if (contains? @registry qsym) |
| 624 | + (str "." (get-in @registry [qsym :classname])) |
| 625 | + s)) |
| 626 | + s))) |
| 627 | + |
| 628 | +#?(:clj |
| 629 | + (defn component->rules [&env s] |
| 630 | + (if (symbol? s) |
| 631 | + (let [qsym (qualify-sym &env s)] |
| 632 | + (if (contains? @registry qsym) |
| 633 | + (get-in @registry [qsym :rules]) |
| 634 | + [s])) |
| 635 | + [s]))) |
| 636 | + |
| 637 | +#?(:clj |
| 638 | + (defn prop->rvalue [&env s] |
| 639 | + (if (symbol? s) |
| 640 | + (let [qsym (qualify-sym &env s)] |
| 641 | + (if (contains? @props-registry qsym) |
| 642 | + (str "var(--" (get-in @props-registry [qsym :propname]) ")") |
| 643 | + s)) |
| 644 | + s))) |
| 645 | + |
619 | 646 | #?(:clj |
620 | 647 | (defmacro defstyled [sym tagname & styles] |
621 | 648 | (let [varsym (symbol (name (ns-name *ns*)) (name sym)) |
|
649 | 676 | ;; build output), and when defined defstyled in cljs files there are |
650 | 677 | ;; no Clojure vars that we can resolve, so we need to resolve this |
651 | 678 | ;; ourselves via the registry. |
| 679 | + component->selector (partial component->selector &env) |
| 680 | + prop->rvalue (partial prop->rvalue &env) |
| 681 | + component->rules (partial component->rules &env) |
652 | 682 | rules (eval `(do |
653 | 683 | (in-ns '~(ns-name *ns*)) |
654 | 684 | ~(walk/postwalk |
655 | 685 | (fn [o] |
656 | | - (if (vector? o) |
657 | | - (let [component->selector |
658 | | - (fn [s] |
659 | | - (if (and (symbol? s) |
660 | | - (contains? @registry (qualify-sym &env s))) |
661 | | - `(str "." (get-in @registry ['~(qualify-sym &env s) :classname])) |
662 | | - s))] |
663 | | - (into [(if (set? (first o)) |
664 | | - (into #{} (map component->selector (first o))) |
665 | | - (component->selector (first o)))] |
666 | | - (mapcat (fn [s] |
667 | | - (if (and (symbol? s) |
668 | | - (contains? @registry (qualify-sym &env s))) |
669 | | - (get-in @registry [(qualify-sym &env s) :rules]) |
670 | | - [s]))) |
671 | | - (next o))) |
| 686 | + (cond |
| 687 | + (vector? o) |
| 688 | + (into [(if (set? (first o)) |
| 689 | + (into #{} (map component->selector (first o))) |
| 690 | + (component->selector (first o)))] |
| 691 | + (mapcat component->rules) |
| 692 | + (next o)) |
| 693 | + (map? o) |
| 694 | + (update-vals o prop->rvalue) |
| 695 | + :else |
672 | 696 | o)) |
673 | 697 | (vec |
674 | | - (mapcat (fn [s] |
675 | | - (if (and (symbol? s) |
676 | | - (contains? @registry (qualify-sym &env s))) |
677 | | - (get-in @registry [(qualify-sym &env s) :rules]) |
678 | | - [s])) |
679 | | - rules)))))] |
| 698 | + (mapcat component->rules rules)))))] |
680 | 699 | (register! registry |
681 | 700 | varsym |
682 | 701 | {:var varsym |
|
767 | 786 | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; |
768 | 787 | ;; Props |
769 | 788 |
|
770 | | -#?(:clj |
771 | | - (defprotocol CSSProp |
772 | | - (lvalue [p]) |
773 | | - (rvalue [p]))) |
| 789 | +(defprotocol CSSProp |
| 790 | + (lvalue [p]) |
| 791 | + (rvalue [p])) |
774 | 792 |
|
775 | | -#?(:clj |
776 | | - (defn css-prop [prop-name default] |
| 793 | +(defn css-prop [prop-name default] |
| 794 | + #?(:clj |
777 | 795 | (with-meta |
778 | 796 | (reify |
779 | 797 | CSSProp |
|
788 | 806 | (valAt [this kw] (when (= :default kw) default)) |
789 | 807 | (valAt [this kw fallback] (if (= :default kw) default fallback)) |
790 | 808 | ) |
| 809 | + {:type ::prop}) |
| 810 | + :cljs |
| 811 | + (with-meta |
| 812 | + (reify |
| 813 | + CSSProp |
| 814 | + (lvalue [_] (str "--" (name prop-name))) |
| 815 | + (rvalue [_] (str "var(--" (name prop-name) ")")) |
| 816 | + ILookup |
| 817 | + (-valAt [this kw] (when (= :default kw) default)) |
| 818 | + (-valAt [this kw fallback] (if (= :default kw) default fallback)) |
| 819 | + ) |
791 | 820 | {:type ::prop}))) |
792 | 821 |
|
793 | 822 | #?(:clj |
|
854 | 883 | (filter (comp some? :value)))] |
855 | 884 | (when (seq props) |
856 | 885 | [[":where(html)" (into {} |
857 | | - (map (juxt (comp (partial str "--") :propname) |
858 | | - :value)) |
859 | | - props)]])) |
| 886 | + (map (juxt (comp (partial str "--") :propname) |
| 887 | + :value)) |
| 888 | + props)]])) |
860 | 889 | (->> @rules-registry |
861 | 890 | vals |
862 | 891 | (sort-by :index) |
|
0 commit comments