Skip to content

Commit dac82f4

Browse files
committed
Better (and better documented) Tailwind v3 support
1 parent 8e4e1a1 commit dac82f4

File tree

5 files changed

+59
-15
lines changed

5 files changed

+59
-15
lines changed

CHANGELOG.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22

33
## Added
44

5-
## Fixed
6-
7-
## Changed
5+
- Added a `:tw-version` flag for the preflight, similar to `set-tokens!`
6+
- Document how to opt-in to Tailwind v3
87

98
# 0.8.84 (2023-02-28 / 8d54daa)
109

@@ -67,4 +66,4 @@
6766

6867
## Added
6968

70-
- Initial implementation
69+
- Initial implementation

README.md

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ so it gets served as any other plain CSS file.
8282
8383
If you prefer to use [Girouette](https://github.com/green-coder/girouette)
8484
(Tailwind) utility classes (a.k.a. tokens), then you can do that as well, or you
85-
can mix and match.
85+
can mix and match. Note that for compatibility reasons Ornament will continue to
86+
default to Tailwind v2 components, but you can opt-in to v3, see the section
87+
below.
8688
8789
```clojure
8890
(o/defstyled freebies-link :a
@@ -113,6 +115,32 @@ to explain Ornament's philosophy on how to deal with CSS, to give you accurate
113115
expectations of how it will behave. This is especially relevant for
114116
ClojureScript projects.
115117
118+
## Choosing your Tailwind version
119+
120+
We rely on [Girouette](https://github.com/green-coder/girouette) to provide us
121+
with a re-implementation in Clojure of Tailwinds components, classes, and
122+
styles. At time of writing Girouette is compatible with either Tailwind 2.0.3,
123+
or 3.0.24.
124+
125+
To use Tailwind 3 tokens (classes), pass an extra `:tw-version 3` option to
126+
`set-tokens!`
127+
128+
```clj
129+
(o/set-tokens! {:tw-version 3})
130+
```
131+
132+
See "Customizing Girouette" below for more info on `set-tokens`, or check the
133+
docstring.
134+
135+
The `girouette.tw.preflight` namespace provides the Tailwind preflight (reset)
136+
stylesheet for either v2 or v3. If you are using `o/defined-styles` you can also
137+
opt to have it automatically prepended. This function also accepts a similar
138+
`:tw-version` argument (`2` or `3)
139+
140+
```clj
141+
(o/defined-styles {:preflight? true :tw-version 3})
142+
```
143+
116144
## Choosing a Hiccup Implementation
117145
118146
Representing HTML using nested Clojure vectors is an approach that was
@@ -754,7 +782,8 @@ style declarations.
754782
- `:components`: sequence of Girouette components, each a map with `:id`
755783
(keyword), `:rules` (string, instaparse, can be omitted), and `:garden` (map,
756784
or function taking instaparse results and returning Garden map)
757-
785+
- `:tw-version`: which Girouette defaults to use, either based on Tailwind
786+
v2, or v3. Valid values: `2`, `3`. Defaults to v2.
758787

759788
```clojure
760789
(o/set-tokens! {:colors {:primary "001122"}
@@ -818,7 +847,7 @@ The end result is that we can do something like this:
818847

819848
And get a bullet list which uses bear emojis for the bullets.
820849

821-
`set-tokens` will add the new colors, fonts, and components to the defaults that
850+
`set-tokens!` will add the new colors, fonts, and components to the defaults that
822851
Girouette provides. You can change that by adding a `^:replace` tag (this uses
823852
meta-merge). e.g. `{:colors ^:replace {...}}`)
824853

bb.edn

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
{:deps
22
{lambdaisland/open-source {:git/url "https://github.com/lambdaisland/open-source"
3-
:git/sha "5ae5327ff37b45228a40a3981fa83ad151bedf3c"}}}
3+
:git/sha "99b33741ea499e1c58e5ab6c2b785ba18eca84d2"}}}

deps.edn

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@
1414

1515
:byo
1616
{:extra-deps {hawk/hawk {:mvn/version "0.2.11"}
17-
com.lambdaisland/glogi {:mvn/version "1.2.164"}
17+
com.lambdaisland/glogi {:mvn/version "1.3.169"}
1818
io.pedestal/pedestal.log {:mvn/version "0.5.10"}}}
1919

2020
:test
2121
{:extra-paths ["test"]
2222
:extra-deps {lambdaisland/kaocha {:mvn/version "RELEASE"}
2323
lambdaisland/kaocha-cljs {:mvn/version "RELEASE"}
2424
org.clojure/clojurescript {:mvn/version "RELEASE"}
25-
com.lambdaisland/glogi {:mvn/version "1.2.164"}
25+
com.lambdaisland/glogi {:mvn/version "1.3.169"}
2626
;; for lambdaisland.hiccup and lambdaisland.thicc, used in testing
2727
lambdaisland/webstuff {:git/url "https://github.com/lambdaisland/webstuff"
2828
:git/sha "9284216340a8107dea553f20bc247787f4f2efa9"

src/lambdaisland/ornament.cljc

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@
7676
:colors girouette-color/tw-v3-unified-colors-extended
7777
:fonts girouette-typography/tw-v2-font-family-map})
7878

79+
(def default-tokens default-tokens-v2)
80+
7981
(defn set-tokens!
8082
"Set \"design tokens\": colors, fonts, and components
8183
@@ -88,6 +90,8 @@
8890
`:id` (keyword), `:rules` (string, instaparse, can be omitted), and
8991
`:garden` (map, or function taking instaparse results and returning Garden
9092
map)
93+
- `:tw-version`: which Girouette defaults to use, either based on Tailwind
94+
v2, or v3. Valid values: 2, 3.
9195
9296
If `:rules` is omitted we assume this is a static token, and we'll
9397
generate a rule of the form `token-id = <'token-id'>`.
@@ -100,8 +104,11 @@
100104
multiple tokens/maps/stylesUse `[:&]` for returning multiple
101105
tokens/maps/styles.
102106
103-
By default these are added to the Girouette defaults, use meta-merge
104-
annotations (e.g. `{:colors ^:replace {...}}`) to change that behaviour."
107+
By default these are added to the Girouette defaults, which are in terms
108+
based on the Tailwind defaults. We still default to v2 (to avoid breaking
109+
changes), but you can opt-in to Tailwind v3 by adding `:tw-version 3`. Use
110+
meta-merge annotations (e.g. `{:colors ^:replace {...}}`) to change that
111+
behaviour."
105112
[{:keys [components colors fonts tw-version]
106113
:or {tw-version 2}}]
107114
(let [{:keys [components colors fonts]}
@@ -638,8 +645,15 @@
638645
(into [(str "." css-class)] (process-rules rules)))))
639646

640647
#?(:clj
641-
(defn defined-styles [& [{:keys [preflight?]
642-
:or {preflight? false}}]]
648+
(defn defined-styles
649+
"Collect all styles that have been defined, and compile them down to CSS. Use
650+
this to either spit out or inline a stylesheet with all your Ornament styles.
651+
Optionally the Tailwind preflight (reset) stylesheet can be prepended using
652+
`:preflight? true`. This defaults to Tailwind v2 (as provided by Girouette).
653+
Version 3 is available with `:tw-version 3`"
654+
[& [{:keys [preflight? tw-version]
655+
:or {preflight? false
656+
tw-version 2}}]]
643657
;; Use registry, instead of inspecting metadata, for better cljs-only
644658
;; support
645659
(let [registry-css (->> @registry
@@ -648,7 +662,9 @@
648662
(map (fn [{:keys [var tag rules classname]}]
649663
(css (styled var classname tag rules nil)))))]
650664
(cond->> registry-css
651-
preflight? (into [(gc/compile-css girouette-preflight/preflight-v2_0_3)])
665+
preflight? (into [(gc/compile-css (case tw-version
666+
2 girouette-preflight/preflight-v2_0_3
667+
3 girouette-preflight/preflight-v3_0_24))])
652668
:always (str/join "\n")))))
653669

654670
#?(:clj

0 commit comments

Comments
 (0)