├── .VERSION_PREFIX
├── .dir-locals.el
├── .github
└── workflows
│ └── main.yml
├── .gitignore
├── CHANGELOG.md
├── LICENSE.txt
├── README.md
├── bb.edn
├── bin
├── kaocha
└── proj
├── deps.edn
├── dev
├── build_notebooks.clj
└── user.clj
├── notebooks
├── attributes_and_properties.clj
├── demo.clj
├── ornament_next.clj
└── template.clj
├── pom.xml
├── repl_sessions
├── cssparser.clj
└── poke.clj
├── src
├── .gitkeep
└── lambdaisland
│ ├── ornament.cljc
│ └── ornament
│ ├── clerk_util.clj
│ └── watcher.clj
├── test
├── .gitkeep
└── lambdaisland
│ └── ornament_test.cljc
└── tests.edn
/.VERSION_PREFIX:
--------------------------------------------------------------------------------
1 | 1.16
--------------------------------------------------------------------------------
/.dir-locals.el:
--------------------------------------------------------------------------------
1 | ((nil . ((cider-clojure-cli-global-options . "-A:dev:test:byo")
2 | (cider-default-cljs-repl . browser))))
3 |
--------------------------------------------------------------------------------
/.github/workflows/main.yml:
--------------------------------------------------------------------------------
1 | name: Continuous Delivery
2 |
3 | on: push
4 |
5 | jobs:
6 | Kaocha:
7 | runs-on: ${{matrix.sys.os}}
8 |
9 | strategy:
10 | matrix:
11 | sys:
12 | # - { os: macos-latest, shell: bash }
13 | - { os: ubuntu-latest, shell: bash }
14 | # - { os: windows-latest, shell: powershell }
15 |
16 | defaults:
17 | run:
18 | shell: ${{matrix.sys.shell}}
19 |
20 | steps:
21 | - uses: actions/checkout@v2
22 | with:
23 | fetch_depth: 0
24 |
25 | - name: 🔧 Install java
26 | uses: actions/setup-java@v1
27 | with:
28 | java-version: '11.0.7'
29 |
30 | - name: 🔧 Install clojure
31 | uses: DeLaGuardo/setup-clojure@master
32 | with:
33 | cli: '1.10.3.943'
34 |
35 | - name: 🗝 maven cache
36 | uses: actions/cache@v2
37 | with:
38 | path: |
39 | ~/.m2
40 | ~/.gitlibs
41 | key: ${{ runner.os }}-maven-${{ github.sha }}
42 | restore-keys: |
43 | ${{ runner.os }}-maven-
44 |
45 | - name: 🧪 Run tests
46 | run: bin/kaocha clj
47 |
48 |
49 | Clerk-build:
50 | runs-on: ubuntu-latest
51 | steps:
52 | - uses: actions/checkout@v2
53 | with:
54 | fetch_depth: 0
55 |
56 | - name: 🔧 Install java
57 | uses: actions/setup-java@v1
58 | with:
59 | java-version: '11.0.7'
60 |
61 | - name: 🔧 Install clojure
62 | uses: DeLaGuardo/setup-clojure@master
63 | with:
64 | cli: '1.10.3.943'
65 |
66 | - name: 🗝 maven cache
67 | uses: actions/cache@v2
68 | with:
69 | path: |
70 | ~/.m2
71 | ~/.gitlibs
72 | key: ${{ runner.os }}-maven-${{ github.sha }}
73 | restore-keys: |
74 | ${{ runner.os }}-maven-
75 |
76 | - name: 🗝 Clerk Cache
77 | uses: actions/cache@v2
78 | with:
79 | path: .clerk
80 | key: ${{ runner.os }}-clerk-cache
81 |
82 | - name: 🏗 Build Clerk Static App with default Notebooks
83 | run: clojure -A:dev:test:byo -M -m build-notebooks '${{ github.sha }}'
84 |
85 | - name: 🔐 Google Auth
86 | uses: google-github-actions/auth@v0
87 | with:
88 | credentials_json: ${{ secrets.GCLOUD_SERVICE_KEY_JSON }}
89 |
90 | - name: 🔧 Setup Google Cloud SDK
91 | uses: google-github-actions/setup-gcloud@v0.3.0
92 |
93 | - name: 📠 Copy static build to bucket under SHA
94 | run: |
95 | gsutil cp -r public/build gs://lambdaisland-notebooks/ornament/sha/${{ github.sha }}
96 | gsutil cp -r public/build gs://lambdaisland-notebooks/ornament/branch/${{ github.ref_name }}
97 |
98 | - name: ✅ Add success status to report with link to snapshot
99 | uses: Sibz/github-status-action@v1
100 | with:
101 | authToken: ${{secrets.GITHUB_TOKEN}}
102 | context: 'Browse Clerk Notebooks'
103 | description: 'Ready'
104 | state: 'success'
105 | sha: ${{github.event.pull_request.head.sha || github.sha}}
106 | target_url: https://notebooks.lambdaisland.com/ornament/sha/${{ github.sha }}
107 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | .cpcache
2 | .nrepl-port
3 | target
4 | repl
5 | scratch.clj
6 | .shadow-cljs
7 | target
8 | yarn.lock
9 | node_modules/
10 | .DS_Store
11 | resources/public/ui
12 | .store
13 | out
14 | .#*
15 | package.json
16 | package-lock.json
17 | .clerk
18 | public
19 | deps.local.edn
20 |
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | # Unreleased
2 |
3 | ## Added
4 |
5 | ## Fixed
6 |
7 | ## Changed
8 |
9 | # 1.16.141 (2025-04-29 / 8c00784)
10 |
11 | ## Changed
12 |
13 | - Only include compiled CSS in cljs docstrings when the cljs optimization level
14 | is `:none`
15 |
16 | # 1.15.138 (2025-04-24 / 8299d3c)
17 |
18 | ## Fixed
19 |
20 | - Add a require-macros so defstyled can be referred from cljs directly
21 |
22 | # 1.14.134 (2025-04-24 / dadcb61)
23 |
24 | ## Fixed
25 |
26 | - Deal with more edge cases when referencing tokens inside style rules
27 |
28 | # 1.13.130 (2025-04-16 / 83c295f)
29 |
30 | ## Changed
31 |
32 | - [BREAKING] When setting a custom `:ornament/prefix` on the namespace, the
33 | separator `__` is no longer implied, to get the same result add `__` to the
34 | end of your prefix string.
35 |
36 | ## Added
37 |
38 | - Support docstrings, they come after the tagname, before any styles or tokens
39 | - If there's only a zero-arg render function (fn-tail), also emit a one-arg
40 | version that takes HTML attributes to be merged in.
41 | - Add `defrules`, for general garden CSS rules
42 | - Add `defprop`, for CSS custom properties (aka variables)
43 | - Add `defutil`, for standalone utility classes
44 | - Add `import-tokens!`, for importing W3C design token JSON files as properties (as per `defprop`)
45 | - Allow setting metadata on a child list, useful for reagent/react keys
46 |
47 | ## Fixed
48 |
49 | - Fix `defined-garden`
50 | - Use of `defrules` in pure-cljs namespaces
51 | - Fix implementation of ILookup on cljs
52 |
53 | # 1.12.107 (2023-09-27 / 2444e34)
54 |
55 | ## Fixed
56 |
57 | - Fix component resolution inside a set (in a rule of another component) (see tests for example)
58 |
59 | # 1.11.101 (2023-09-13 / 213279d)
60 |
61 | ## Fixed
62 |
63 | - Allow reusing the styles of one component directly inside another (see tests for example)
64 |
65 | # 1.10.94 (2023-08-30 / d1e1c3b)
66 |
67 | ## Fixed
68 |
69 | - Support using `defstyled` components as reagent form-2 components
70 |
71 | # 0.9.87 (2023-04-15 / dac82f4)
72 |
73 | ## Added
74 |
75 | - Added a `:tw-version` flag for the preflight, similar to `set-tokens!`
76 | - Document how to opt-in to Tailwind v3
77 |
78 | # 0.8.84 (2023-02-28 / 8d54daa)
79 |
80 | ## Added
81 |
82 | - Implement inheritance for fn-tails
83 |
84 | # 0.7.77 (2022-11-25 / a1f8d65)
85 |
86 | ## Added
87 |
88 | - Add Clerk garden setup
89 |
90 | ## Fixed
91 |
92 | - improved way to handle girouette v2 and v3 tokens
93 |
94 | # 0.6.69 (2022-10-11 / a629407)
95 |
96 | ## Fixed
97 |
98 | - Fixed an issue withe direct invocation of components with a render function (tail-fn)
99 |
100 | # 0.5.65 (2022-09-20 / 94cbebe)
101 |
102 | ## Added
103 |
104 | - Support attributes when using a top-level fragment in a rendering function
105 |
106 | # 0.4.34 (2022-01-25 / df056c8)
107 |
108 | ## Fixed
109 |
110 | - Fix cljdoc build
111 |
112 | # 0.3.30 (2022-01-25 / d37c5e4)
113 |
114 | ## Fixed
115 |
116 | - Improve ClojureScript support, in particular referencing components in other components style rules
117 | - Support vectors with multiple selectors, plus alternative syntax with sets
118 |
119 | # 0.2.19 (2021-11-29 / 6c8e226)
120 |
121 | ## Fixed
122 |
123 | - Fix issue where girouette tokens were not being applied to child elements. [See Github Issue](https://github.com/lambdaisland/ornament/issues/5)
124 |
125 | ## Changed
126 |
127 | - Bump Girouette to 0.0.6
128 |
129 | # 0.1.12 (2021-10-25 / d0a739b)
130 |
131 | ## Changed
132 |
133 | - Bump Girouette to 0.0.5
134 |
135 | # 0.0.7 (2021-10-01 / 52aa304)
136 |
137 | ## Added
138 |
139 | - Initial implementation
140 |
--------------------------------------------------------------------------------
/LICENSE.txt:
--------------------------------------------------------------------------------
1 | Mozilla Public License Version 2.0
2 | ==================================
3 |
4 | 1. Definitions
5 | --------------
6 |
7 | 1.1. "Contributor"
8 | means each individual or legal entity that creates, contributes to
9 | the creation of, or owns Covered Software.
10 |
11 | 1.2. "Contributor Version"
12 | means the combination of the Contributions of others (if any) used
13 | by a Contributor and that particular Contributor's Contribution.
14 |
15 | 1.3. "Contribution"
16 | means Covered Software of a particular Contributor.
17 |
18 | 1.4. "Covered Software"
19 | means Source Code Form to which the initial Contributor has attached
20 | the notice in Exhibit A, the Executable Form of such Source Code
21 | Form, and Modifications of such Source Code Form, in each case
22 | including portions thereof.
23 |
24 | 1.5. "Incompatible With Secondary Licenses"
25 | means
26 |
27 | (a) that the initial Contributor has attached the notice described
28 | in Exhibit B to the Covered Software; or
29 |
30 | (b) that the Covered Software was made available under the terms of
31 | version 1.1 or earlier of the License, but not also under the
32 | terms of a Secondary License.
33 |
34 | 1.6. "Executable Form"
35 | means any form of the work other than Source Code Form.
36 |
37 | 1.7. "Larger Work"
38 | means a work that combines Covered Software with other material, in
39 | a separate file or files, that is not Covered Software.
40 |
41 | 1.8. "License"
42 | means this document.
43 |
44 | 1.9. "Licensable"
45 | means having the right to grant, to the maximum extent possible,
46 | whether at the time of the initial grant or subsequently, any and
47 | all of the rights conveyed by this License.
48 |
49 | 1.10. "Modifications"
50 | means any of the following:
51 |
52 | (a) any file in Source Code Form that results from an addition to,
53 | deletion from, or modification of the contents of Covered
54 | Software; or
55 |
56 | (b) any new file in Source Code Form that contains any Covered
57 | Software.
58 |
59 | 1.11. "Patent Claims" of a Contributor
60 | means any patent claim(s), including without limitation, method,
61 | process, and apparatus claims, in any patent Licensable by such
62 | Contributor that would be infringed, but for the grant of the
63 | License, by the making, using, selling, offering for sale, having
64 | made, import, or transfer of either its Contributions or its
65 | Contributor Version.
66 |
67 | 1.12. "Secondary License"
68 | means either the GNU General Public License, Version 2.0, the GNU
69 | Lesser General Public License, Version 2.1, the GNU Affero General
70 | Public License, Version 3.0, or any later versions of those
71 | licenses.
72 |
73 | 1.13. "Source Code Form"
74 | means the form of the work preferred for making modifications.
75 |
76 | 1.14. "You" (or "Your")
77 | means an individual or a legal entity exercising rights under this
78 | License. For legal entities, "You" includes any entity that
79 | controls, is controlled by, or is under common control with You. For
80 | purposes of this definition, "control" means (a) the power, direct
81 | or indirect, to cause the direction or management of such entity,
82 | whether by contract or otherwise, or (b) ownership of more than
83 | fifty percent (50%) of the outstanding shares or beneficial
84 | ownership of such entity.
85 |
86 | 2. License Grants and Conditions
87 | --------------------------------
88 |
89 | 2.1. Grants
90 |
91 | Each Contributor hereby grants You a world-wide, royalty-free,
92 | non-exclusive license:
93 |
94 | (a) under intellectual property rights (other than patent or trademark)
95 | Licensable by such Contributor to use, reproduce, make available,
96 | modify, display, perform, distribute, and otherwise exploit its
97 | Contributions, either on an unmodified basis, with Modifications, or
98 | as part of a Larger Work; and
99 |
100 | (b) under Patent Claims of such Contributor to make, use, sell, offer
101 | for sale, have made, import, and otherwise transfer either its
102 | Contributions or its Contributor Version.
103 |
104 | 2.2. Effective Date
105 |
106 | The licenses granted in Section 2.1 with respect to any Contribution
107 | become effective for each Contribution on the date the Contributor first
108 | distributes such Contribution.
109 |
110 | 2.3. Limitations on Grant Scope
111 |
112 | The licenses granted in this Section 2 are the only rights granted under
113 | this License. No additional rights or licenses will be implied from the
114 | distribution or licensing of Covered Software under this License.
115 | Notwithstanding Section 2.1(b) above, no patent license is granted by a
116 | Contributor:
117 |
118 | (a) for any code that a Contributor has removed from Covered Software;
119 | or
120 |
121 | (b) for infringements caused by: (i) Your and any other third party's
122 | modifications of Covered Software, or (ii) the combination of its
123 | Contributions with other software (except as part of its Contributor
124 | Version); or
125 |
126 | (c) under Patent Claims infringed by Covered Software in the absence of
127 | its Contributions.
128 |
129 | This License does not grant any rights in the trademarks, service marks,
130 | or logos of any Contributor (except as may be necessary to comply with
131 | the notice requirements in Section 3.4).
132 |
133 | 2.4. Subsequent Licenses
134 |
135 | No Contributor makes additional grants as a result of Your choice to
136 | distribute the Covered Software under a subsequent version of this
137 | License (see Section 10.2) or under the terms of a Secondary License (if
138 | permitted under the terms of Section 3.3).
139 |
140 | 2.5. Representation
141 |
142 | Each Contributor represents that the Contributor believes its
143 | Contributions are its original creation(s) or it has sufficient rights
144 | to grant the rights to its Contributions conveyed by this License.
145 |
146 | 2.6. Fair Use
147 |
148 | This License is not intended to limit any rights You have under
149 | applicable copyright doctrines of fair use, fair dealing, or other
150 | equivalents.
151 |
152 | 2.7. Conditions
153 |
154 | Sections 3.1, 3.2, 3.3, and 3.4 are conditions of the licenses granted
155 | in Section 2.1.
156 |
157 | 3. Responsibilities
158 | -------------------
159 |
160 | 3.1. Distribution of Source Form
161 |
162 | All distribution of Covered Software in Source Code Form, including any
163 | Modifications that You create or to which You contribute, must be under
164 | the terms of this License. You must inform recipients that the Source
165 | Code Form of the Covered Software is governed by the terms of this
166 | License, and how they can obtain a copy of this License. You may not
167 | attempt to alter or restrict the recipients' rights in the Source Code
168 | Form.
169 |
170 | 3.2. Distribution of Executable Form
171 |
172 | If You distribute Covered Software in Executable Form then:
173 |
174 | (a) such Covered Software must also be made available in Source Code
175 | Form, as described in Section 3.1, and You must inform recipients of
176 | the Executable Form how they can obtain a copy of such Source Code
177 | Form by reasonable means in a timely manner, at a charge no more
178 | than the cost of distribution to the recipient; and
179 |
180 | (b) You may distribute such Executable Form under the terms of this
181 | License, or sublicense it under different terms, provided that the
182 | license for the Executable Form does not attempt to limit or alter
183 | the recipients' rights in the Source Code Form under this License.
184 |
185 | 3.3. Distribution of a Larger Work
186 |
187 | You may create and distribute a Larger Work under terms of Your choice,
188 | provided that You also comply with the requirements of this License for
189 | the Covered Software. If the Larger Work is a combination of Covered
190 | Software with a work governed by one or more Secondary Licenses, and the
191 | Covered Software is not Incompatible With Secondary Licenses, this
192 | License permits You to additionally distribute such Covered Software
193 | under the terms of such Secondary License(s), so that the recipient of
194 | the Larger Work may, at their option, further distribute the Covered
195 | Software under the terms of either this License or such Secondary
196 | License(s).
197 |
198 | 3.4. Notices
199 |
200 | You may not remove or alter the substance of any license notices
201 | (including copyright notices, patent notices, disclaimers of warranty,
202 | or limitations of liability) contained within the Source Code Form of
203 | the Covered Software, except that You may alter any license notices to
204 | the extent required to remedy known factual inaccuracies.
205 |
206 | 3.5. Application of Additional Terms
207 |
208 | You may choose to offer, and to charge a fee for, warranty, support,
209 | indemnity or liability obligations to one or more recipients of Covered
210 | Software. However, You may do so only on Your own behalf, and not on
211 | behalf of any Contributor. You must make it absolutely clear that any
212 | such warranty, support, indemnity, or liability obligation is offered by
213 | You alone, and You hereby agree to indemnify every Contributor for any
214 | liability incurred by such Contributor as a result of warranty, support,
215 | indemnity or liability terms You offer. You may include additional
216 | disclaimers of warranty and limitations of liability specific to any
217 | jurisdiction.
218 |
219 | 4. Inability to Comply Due to Statute or Regulation
220 | ---------------------------------------------------
221 |
222 | If it is impossible for You to comply with any of the terms of this
223 | License with respect to some or all of the Covered Software due to
224 | statute, judicial order, or regulation then You must: (a) comply with
225 | the terms of this License to the maximum extent possible; and (b)
226 | describe the limitations and the code they affect. Such description must
227 | be placed in a text file included with all distributions of the Covered
228 | Software under this License. Except to the extent prohibited by statute
229 | or regulation, such description must be sufficiently detailed for a
230 | recipient of ordinary skill to be able to understand it.
231 |
232 | 5. Termination
233 | --------------
234 |
235 | 5.1. The rights granted under this License will terminate automatically
236 | if You fail to comply with any of its terms. However, if You become
237 | compliant, then the rights granted under this License from a particular
238 | Contributor are reinstated (a) provisionally, unless and until such
239 | Contributor explicitly and finally terminates Your grants, and (b) on an
240 | ongoing basis, if such Contributor fails to notify You of the
241 | non-compliance by some reasonable means prior to 60 days after You have
242 | come back into compliance. Moreover, Your grants from a particular
243 | Contributor are reinstated on an ongoing basis if such Contributor
244 | notifies You of the non-compliance by some reasonable means, this is the
245 | first time You have received notice of non-compliance with this License
246 | from such Contributor, and You become compliant prior to 30 days after
247 | Your receipt of the notice.
248 |
249 | 5.2. If You initiate litigation against any entity by asserting a patent
250 | infringement claim (excluding declaratory judgment actions,
251 | counter-claims, and cross-claims) alleging that a Contributor Version
252 | directly or indirectly infringes any patent, then the rights granted to
253 | You by any and all Contributors for the Covered Software under Section
254 | 2.1 of this License shall terminate.
255 |
256 | 5.3. In the event of termination under Sections 5.1 or 5.2 above, all
257 | end user license agreements (excluding distributors and resellers) which
258 | have been validly granted by You or Your distributors under this License
259 | prior to termination shall survive termination.
260 |
261 | ************************************************************************
262 | * *
263 | * 6. Disclaimer of Warranty *
264 | * ------------------------- *
265 | * *
266 | * Covered Software is provided under this License on an "as is" *
267 | * basis, without warranty of any kind, either expressed, implied, or *
268 | * statutory, including, without limitation, warranties that the *
269 | * Covered Software is free of defects, merchantable, fit for a *
270 | * particular purpose or non-infringing. The entire risk as to the *
271 | * quality and performance of the Covered Software is with You. *
272 | * Should any Covered Software prove defective in any respect, You *
273 | * (not any Contributor) assume the cost of any necessary servicing, *
274 | * repair, or correction. This disclaimer of warranty constitutes an *
275 | * essential part of this License. No use of any Covered Software is *
276 | * authorized under this License except under this disclaimer. *
277 | * *
278 | ************************************************************************
279 |
280 | ************************************************************************
281 | * *
282 | * 7. Limitation of Liability *
283 | * -------------------------- *
284 | * *
285 | * Under no circumstances and under no legal theory, whether tort *
286 | * (including negligence), contract, or otherwise, shall any *
287 | * Contributor, or anyone who distributes Covered Software as *
288 | * permitted above, be liable to You for any direct, indirect, *
289 | * special, incidental, or consequential damages of any character *
290 | * including, without limitation, damages for lost profits, loss of *
291 | * goodwill, work stoppage, computer failure or malfunction, or any *
292 | * and all other commercial damages or losses, even if such party *
293 | * shall have been informed of the possibility of such damages. This *
294 | * limitation of liability shall not apply to liability for death or *
295 | * personal injury resulting from such party's negligence to the *
296 | * extent applicable law prohibits such limitation. Some *
297 | * jurisdictions do not allow the exclusion or limitation of *
298 | * incidental or consequential damages, so this exclusion and *
299 | * limitation may not apply to You. *
300 | * *
301 | ************************************************************************
302 |
303 | 8. Litigation
304 | -------------
305 |
306 | Any litigation relating to this License may be brought only in the
307 | courts of a jurisdiction where the defendant maintains its principal
308 | place of business and such litigation shall be governed by laws of that
309 | jurisdiction, without reference to its conflict-of-law provisions.
310 | Nothing in this Section shall prevent a party's ability to bring
311 | cross-claims or counter-claims.
312 |
313 | 9. Miscellaneous
314 | ----------------
315 |
316 | This License represents the complete agreement concerning the subject
317 | matter hereof. If any provision of this License is held to be
318 | unenforceable, such provision shall be reformed only to the extent
319 | necessary to make it enforceable. Any law or regulation which provides
320 | that the language of a contract shall be construed against the drafter
321 | shall not be used to construe this License against a Contributor.
322 |
323 | 10. Versions of the License
324 | ---------------------------
325 |
326 | 10.1. New Versions
327 |
328 | Mozilla Foundation is the license steward. Except as provided in Section
329 | 10.3, no one other than the license steward has the right to modify or
330 | publish new versions of this License. Each version will be given a
331 | distinguishing version number.
332 |
333 | 10.2. Effect of New Versions
334 |
335 | You may distribute the Covered Software under the terms of the version
336 | of the License under which You originally received the Covered Software,
337 | or under the terms of any subsequent version published by the license
338 | steward.
339 |
340 | 10.3. Modified Versions
341 |
342 | If you create software not governed by this License, and you want to
343 | create a new license for such software, you may create and use a
344 | modified version of this License if you rename the license and remove
345 | any references to the name of the license steward (except to note that
346 | such modified license differs from this License).
347 |
348 | 10.4. Distributing Source Code Form that is Incompatible With Secondary
349 | Licenses
350 |
351 | If You choose to distribute Source Code Form that is Incompatible With
352 | Secondary Licenses under the terms of this version of the License, the
353 | notice described in Exhibit B of this License must be attached.
354 |
355 | Exhibit A - Source Code Form License Notice
356 | -------------------------------------------
357 |
358 | This Source Code Form is subject to the terms of the Mozilla Public
359 | License, v. 2.0. If a copy of the MPL was not distributed with this
360 | file, You can obtain one at http://mozilla.org/MPL/2.0/.
361 |
362 | If it is not possible or desirable to put the notice in a particular
363 | file, then You may include the notice in a location (such as a LICENSE
364 | file in a relevant directory) where a recipient would be likely to look
365 | for such a notice.
366 |
367 | You may add additional accurate notices of copyright ownership.
368 |
369 | Exhibit B - "Incompatible With Secondary Licenses" Notice
370 | ---------------------------------------------------------
371 |
372 | This Source Code Form is "Incompatible With Secondary Licenses", as
373 | defined by the Mozilla Public License, v. 2.0.
374 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Ornament
2 |
3 |
4 | [](https://cljdoc.org/d/com.lambdaisland/ornament) [](https://clojars.org/com.lambdaisland/ornament)
5 |
6 |
7 | CSS-in-Clj(s)
8 |
9 | ## Features
10 |
11 | - Define styled components
12 | - Use Garden syntax for CSS, or Girouette (Tailwind-style) utility class names
13 | - Have styling live close to, but separate from DOM structure
14 | - Compile to plain CSS files as a compile step
15 | - Use components in any Hiccup implementation, frontend or backend
16 |
17 |
18 | ## Installation
19 |
20 | To use the latest release, add the following to your `deps.edn` ([Clojure CLI](https://clojure.org/guides/deps_and_cli))
21 |
22 | ```
23 | com.lambdaisland/ornament {:mvn/version "1.16.141"}
24 | ```
25 |
26 | or add the following to your `project.clj` ([Leiningen](https://leiningen.org/))
27 |
28 | ```
29 | [com.lambdaisland/ornament "1.16.141"]
30 | ```
31 |
32 |
33 | ## Introduction
34 |
35 | Ornament is the culmination of many discussions and explorations with the aim to
36 | find the sweet spot in how to handle styling in large Clojure or ClojureScript
37 | web projects. It takes ideas from CSS-in-JS approaches, and utility-class
38 | libraries, while (in our opinion) improving on both.
39 |
40 | At the heart of ornament is the `defstyled` macro, which defines a "styled
41 | component". It combines a name, a HTML tag, and styling rules.
42 |
43 | ```clojure
44 | (require '[lambdaisland.ornament :as o])
45 |
46 | (o/defstyled freebies-link :a
47 | {:font-size "1rem"
48 | :color "#cff9cf"
49 | :text-decoration "underline"})
50 | ```
51 |
52 | This does two things, first of all it creates a Hiccup component, which combines
53 | the tag (`:a` in this case), with a class name based on the component name. (See
54 | the section on "Choosing a Hiccup Implementation" for more info on where this
55 | syntax is supported.)
56 |
57 | ```clojure
58 | ;; Hiccup
59 | [freebies-link {:href "/episodes/interceptors-concepts"} "Freebies"]
60 | ```
61 |
62 | Which renders as:
63 |
64 | ```html
65 | Freebies
66 | ```
67 |
68 | The styling information is rendered during a build step to CSS, and written out,
69 | so it gets served as any other plain CSS file.
70 |
71 | ```clojure
72 | (spit "resource/public/ornament.css" (o/defined-styles))
73 | ```
74 |
75 | ```css
76 | .lambdaisland_episodes__freebies_link {
77 | font-size: 1rem;
78 | color: #cff9cf;
79 | text-decoration: underline;
80 | }
81 | ```
82 |
83 | If you prefer to use [Girouette](https://github.com/green-coder/girouette)
84 | (Tailwind) utility classes (a.k.a. tokens), then you can do that as well, or you
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.
88 |
89 | ```clojure
90 | (o/defstyled freebies-link :a
91 | :text-base
92 | :text-green-500
93 | :underline)
94 | ```
95 |
96 | Finally you can add one or more function bodies to your component, which acts as
97 | a render function, determining how the children of the component will render.
98 | Note that this render function only determines the "inside" of the component, it
99 | will still get wrapped with the tag and class name passed to `defstyled`.
100 |
101 | ```clojure
102 | (o/defstyled page-grid :div
103 | :relative :h-full :md:flex
104 | [:>.content :flex-1 :p-2 :md:p-10 :bg-gray-100]
105 | ([sidebar content]
106 | [:<>
107 | sidebar
108 | [:div.content
109 | content]]))
110 | ```
111 |
112 | Hopefully these examples have sufficiently whetted your appetite. We'll explain
113 | the syntax and features of `defstyled` in detail down below. But first we need
114 | to explain Ornament's philosophy on how to deal with CSS, to give you accurate
115 | expectations of how it will behave. This is especially relevant for
116 | ClojureScript projects.
117 |
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 |
144 | ## Choosing a Hiccup Implementation
145 |
146 | Representing HTML using nested Clojure vectors is an approach that was
147 | popularized by the [Hiccup](https://github.com/weavejester/hiccup) library, and
148 | so this is referred to as Hiccup-syntax. Since then many other libaries have
149 | implemented this same syntax, sometimes with small changes or extensions.
150 |
151 | A particular extension popularized by
152 | [Reagent](https://github.com/reagent-project/reagent) is the use of functions as
153 | components. You define functions which return hiccup, and use those functions in
154 | your hiccup where normally you would have a keyword denoting a HTML element tag.
155 | In essence you're no longer calling the function yourself (with parentheses) but
156 | telling Reagent to call it while rendering.
157 |
158 | ```clj
159 | (defn my-component [arg]
160 | [:p "in my component:" arg])
161 |
162 | (reagent-dom/render [my-component "hello"] (js/document.getElementById "app"))
163 | ```
164 |
165 | This has significance in React-apps because it allows React/Reagent to manage
166 | the component, but we like it in general to make it clear that something is
167 | conceptually a hiccup component.
168 |
169 | Ornament components are basically the same, they are (or behave like) functions
170 | which return Hiccup. If the implementation you are using supports putting them
171 | in square brackets then you can go with that, or you can just call them yourself
172 | as a function.
173 |
174 | ```clj
175 | (o/defstyled my-component :p
176 | :m-3)
177 |
178 | ;; option 1
179 | [my-component "hello"]
180 |
181 | ;; option 2
182 | (my-component "hello")
183 | ```
184 |
185 | How do different Hiccup implementations differ?
186 |
187 | - Clojure vs ClojureScript (or both with cljc)
188 | - Support functions as components
189 | - Automatically HTML-escape text (the ones that support this all have different ways of injecting "raw" html strings)
190 | - Support for `:<>` (fragments)
191 | - Have the style property be a map (instead of just a string)
192 | - Pre-compile via macros for better performance
193 |
194 | Some that we know of:
195 |
196 | Clojure:
197 |
198 | - Hiccup 1, beware: does not auto-escape text and so is sensitive to CSRF attacks
199 | - Hiccup 2, fixes the security issue and is generally the better option, despite officially being in alpha
200 | - Enlive (See `net.cgrand.enlive-html/html`)
201 | - [lambdaisland.hiccup](https://github.com/lambdaisland/hiccup) is our take on
202 | this, it is based on Enlive, but adds functions-as-component, fragments,
203 | styles-as-maps, and a convenient syntax for raw html strings
204 | - Hicada (cljc, macro-based)
205 |
206 | ClojureScript
207 |
208 | - Reagent and other React wrappers
209 | - Sablono
210 | - Hicada
211 |
212 | ## Ornament CSS Compilation
213 |
214 | Ornament is written in CLJC, meaning you can call `defstyled` in ClojureScript
215 | exactly the same way as in Clojure. But, there's one important distinction, we
216 | do not add any styling information (be it Garden, Girouette, or CSS), to your
217 | ClojureScript build.
218 |
219 | This is an important design decision, and it's worth elaborating a bit. We
220 | understand the appeal of something like CSS-in-JS from a _syntax and programmer
221 | convenience_ point of view, and we try to offer the same kind of convenience.
222 | However, we question that it is sensible to deal with CSS generation in
223 | JavaScript. We think it's vastly superior to generate CSS once at build/deploy
224 | time, and to then deal with it *as CSS*.
225 |
226 | This pays dividends, we don't need to add all the machinery of Garden and
227 | Girouette to the frontend build, neither do we need all the styling definitions,
228 | so Ornament will have minimal impact on your bundle size. And your CSS can be
229 | served — and cached — as a simple static CSS file.
230 |
231 | What you do get from using `defstyled` in ClojureScript is a component
232 | (function) which you can use in Hiccup (e.g. Reagent) to render HTML. The
233 | component knows about the HTML tag to use, the CSS class name to add, and if you
234 | added a render function, it knows about that as well.
235 |
236 | So where does the styling go? We add that to a registry *during macroexpansion*.
237 | In other words: in Clojure. Once all `defstyled` invocations have been compiled,
238 | you can grab the full CSS with `(o/defined-styles)`, and spit it to a file.
239 |
240 | Before you grab the output of `(o/defined-styles)` you need to make sure all
241 | your styles have in fact been defined. For Clojure projects this merely means
242 | requiring all namespaces. If you have some kind of main application entry point
243 | that loads all your components/views, then load that, and capture the styles
244 | once it has finished.
245 |
246 | ### ClojureScript
247 |
248 | For ClojureScript you have two options, either you define all you components in
249 | `cljc` files, and use the same approach as in Clojure. The alternative is to
250 | first run your ClojureScript build, and then in the same process write out the
251 | styles. You can for instance write your own script that invokes the
252 | ClojureScript build API, then follows it up by writing out the styles, or you
253 | can use something like Shadow-cljs build hooks.
254 |
255 | #### Limitations
256 |
257 | Keep in mind that the style (Garden/CSS) section of a component is only ever
258 | processed in Clojure, even when used in ClojureScript files. This means that it
259 | is not possible to reference ClojureScript variables or functions.
260 |
261 | ```clojure
262 | (def sizes {:s "0.5rem" :m "1.rem" :l "2rem"})
263 |
264 | (o/defstyled foo :div
265 | {:padding (:m sizes)})
266 | ```
267 |
268 | This will work in Clojure, but not ClojureScript. Referencing
269 | variables/functions inside the component body is not a problem.
270 |
271 | ```clojure
272 | (def divider [:hr])
273 |
274 | (o/defstyled dividers :div
275 | {:padding "1rem"}
276 | ([& children]
277 | (into [:<>]
278 | (interpose divider)
279 | children)))
280 | ```
281 |
282 | This works in both Clojure and ClojureScript.
283 |
284 | Note that it *is* possible to reference previously defined `defstyled`
285 | components in the style rules section, even in ClojureScript, see the section
286 | "Referencing other components in Rules" below.
287 |
288 | ```clojure
289 | (o/defstyled referenced :div
290 | {:color :blue})
291 |
292 | (o/defstyled referer :p
293 | [referenced {:color :red}] ;; use as classname
294 | [:.foo referenced]) ;; use as style rule
295 | ```
296 |
297 | #### Computing values (referencing vars) inside style rules in cljc files
298 |
299 | Style rules are processed during macroexpansion, which happens in Clojure, even
300 | when compiling ClojureScript. This means that any code inside style rules needs
301 | to be able to evaluate in Clojure by the time ClojureScript starts compiling the
302 | `defstyled` form.
303 |
304 | Consider this namespace
305 |
306 | ```clj
307 | ;; components.cljc
308 | (ns my.components
309 | (:require [lambdaisland.ornament :as o]))
310 |
311 | (def my-tokens {:main-color "green"})
312 |
313 | (o/defstyled with-code :div
314 | {:background-color (-> my-tokens :main-color)})
315 | ```
316 |
317 | In Clojure this works as you would expect but when compiling this as a
318 | ClojureScript file it fails. The failure is due to the file never being loaded
319 | as a Clojure namespace, so the Clojure var `#'my.components/my-tokens` doesn't
320 | exist in the Clojure environment which is where macro expansion takes place.
321 |
322 | To fix this you can use the `:require-macros` directive which instructs the
323 | ClojureScript compiler to load a given Clojure namespace (in this case the
324 | current namespace) before continuing the compilation of the current namespace.
325 |
326 | ```clj
327 | (ns my.components
328 | (:require [lambdaisland.ornament :as o])
329 | #?(:cljs (:require-macros my.components)))
330 |
331 | #?(:clj
332 | (def my-tokens {:main-color "green"}))
333 |
334 | (o/defstyled with-code :div
335 | {:background-color (-> my-tokens :main-color)})
336 | ```
337 |
338 | This addresses the problem by instructing the ClojureScript compiler to first
339 | load `my.components` as a clj namespace thereby creating the `#'my-tokens`
340 | Clojure var. The compiler then continues with the cljs compilation and when it
341 | gets to expansion of the `defstyled` macro form (specifically processing of the
342 | style rule `{:background-color ...}`) the `#'my-tokens` var is available in the
343 | clj environment and evaluated for it's value which is then included in the
344 | Ornament style registry.
345 |
346 | Wrapping `my-tokens` in `#?(:clj ...)` is not strictly necessary, but it helps
347 | to emphasize the point that this definition is only ever used on the Clojure
348 | side, you don't need it in your compiled ClojureScript.
349 |
350 | ##### Important note about CLJC files and clojure.core symbols
351 |
352 | If you do __any__ computation in your style rules it is recommended that you
353 | require the file as clj by utilising the `:require-macros` directive to self
354 | require the namespace.
355 |
356 | ```clj
357 | (ns my.components
358 | (:require [lambdaisland.ornament :as o])
359 | #?(:cljs (:require-macros my.components)))
360 |
361 | (o/defstyled with-code :div
362 | {:background-color (str "red")})
363 | ```
364 |
365 | The need for this is a subtle consequence of the same interaction between
366 | Clojure and the ClojureScript compiler outlined in the previous section. A
367 | similar issue will manifest if you try to use `clojure.core` symbols in your
368 | style rules without requiring the clj namespace. This is surprising at first but
369 | makes sense after considering that it is the `ns` form that auto refers all
370 | `clojure.core` symbols into the current namespace. It follows that if the `ns`
371 | form is not evaluated, because we do not require the cljc file as a clj file,
372 | then no symbols mapping to the `clojure.core` symbols will have created in the
373 | current namespace. The `clojure.core` symbols are however interned, and as such
374 | fully qualifying the namespace will work but that is not an ergonomic solution.
375 |
376 | ```clj
377 | (ns my.components
378 | (:require [lambdaisland.ornament :as o]))
379 |
380 | ;; Does not work
381 | (o/defstyled with-code :div
382 | {:background-color (str "red")})
383 |
384 | ;; Does work but not recommended
385 | (o/defstyled with-code :div
386 | {:background-color (clojure.core/str "red")})
387 | ```
388 |
389 | #### Shadow-cljs build hook example
390 |
391 | This is enough to get recompilation of your styles to CSS, which shadow-cljs
392 | will then hot-reload.
393 |
394 | ```clojure
395 | ;; Easiest to just make this a clj file.
396 | (ns my.hooks
397 | (:require [lambdaisland.ornament :as o]
398 | [garden.compiler :as gc]
399 | [girouette.tw.preflight :as girouette-preflight]))
400 |
401 | ;; Optional, but it's common to still have some style rules that are not
402 | ;; component-specific, so you can use Garden directly for that
403 | (def global-styles
404 | [[:html {:font-size "14pt"}]])
405 |
406 | (defn write-styles-hook
407 | {:shadow.build/stage :flush}
408 | [build-state & args]
409 | ;; In case your global-styles is in a separate clj file you will have to
410 | ;; reload it yourself, shadow only reloads/recompiles cljs/cljc files
411 | #_(require my.styles :reload)
412 | ;; Just writing out the CSS is enough, shadow will pick it up (make sure you
413 | ;; have a )
414 | (spit "resources/public/styles.css"
415 | (str
416 | ;; `defined-styles` takes a :preflight? flag, but we like to have some
417 | ;; style rules between the preflight and the components. This whole bit
418 | ;; is optional.
419 | (gc/compile-css (concat
420 | girouette-preflight/preflight-v2_0_3
421 | styles/global-styles))
422 | "\n"
423 | (o/defined-styles)))
424 | build-state)
425 | ```
426 |
427 | ```clojure
428 | ;; shadow-cljs.edn
429 | {,,,
430 |
431 | ;; For best results, otherwise you will find that some styles are missing after
432 | ;; restarting the shadow process
433 | :cache-blockers #{lambdaisland.ornament}
434 |
435 | :builds
436 | {:main
437 | {:target :browser
438 | ,,,
439 | :build-hooks [(my.hooks/write-styles-hook)]}}}
440 | ```
441 |
442 | ## Defstyled Component Syntax
443 |
444 | `defstyled` really does two things, the macro expands to a form like
445 |
446 | ```clojure
447 | (def footer (reify StyledComponent ...))
448 | ```
449 |
450 | This "styled component" acts as a function, which is what makes it compatible
451 | with Hiccup implementations.
452 |
453 | ```clojure
454 | (footer "hello")
455 | ;;=>
456 | [:footer {:class ["project-discovery_parts__footer"]} "hello"]
457 | ```
458 |
459 | It also implements various protocol methods. You don't typically need to call
460 | these yourself, but they can be useful for verifying how your component behaves.
461 |
462 | ```clojure
463 | (o/classname footer)
464 | ;;=> "project-discovery_parts__footer"
465 | (o/tag footer)
466 | ;;=> :footer
467 | (o/rules footer)
468 | ;;=> [{:max-width "60rem"}]
469 | (o/as-garden footer)
470 | ;;=> [".project-discovery_parts__footer" {:max-width "60rem"}]
471 | (o/css footer)
472 | ;;=> ".project-discovery_parts__footer{max-width:60rem}"
473 | ```
474 |
475 | ### Component Name
476 |
477 | The first argument to `defstyled` is the component name, this will create a var
478 | with the given name, containing the component. A function-like object that can
479 | be used to render HTML.
480 |
481 | The name will also determine the class name that will be used in the HTML and
482 | CSS. For this Ornament combines the namespace name with the component name, and
483 | munges them to be valid CSS identifiers.
484 |
485 | ```clojure
486 | (ns my.views
487 | (:require [lambdaisland.ornament :as o]))
488 |
489 | (o/defstyled footer :footer
490 | {:max-width "60rem"})
491 |
492 | (o/classname footer)
493 | ;; my_views__footer
494 | ```
495 |
496 | You can use metadata on the namespace to change the namespace prefix.
497 |
498 | ``` clojure
499 | (ns ^{:ornament/prefix "views"} com.company.project.frontend.views
500 | (:require [lambdaisland.ornament :as o]))
501 |
502 | (o/defstyled footer :footer
503 | {:max-width "60rem"})
504 |
505 | (o/classname footer)
506 | ;; views__footer
507 | ```
508 |
509 | Using fully qualified var names as class names provides the unexpected benefit
510 | that it becomes trivial to find the component you are looking at in your
511 | browser's inspector.
512 |
513 | When stringifying the component you also get the class name back, this allows
514 | using them to reference a certain class, for instance in Hiccup:
515 |
516 | ```clojure
517 | [:div {:class (str footer)} ...]
518 |
519 | ;; Depending on your Hiccup implementation this can also work
520 | [:div {:class [footer]} ...]
521 |
522 | (js/querySelector (str footer))
523 | ```
524 |
525 | ### HTML Tag
526 |
527 | The second argument to `defstyled` is the HTML tag. This is typically a keyword,
528 | like `:section`, `:tr`, or `:div`. This is used when rendering the component as
529 | Hiccup, and so anything that is valid in your Hiccup implementation of choice is
530 | fair game, including `:div#some-id` or `:p.a_class`. You could also use for
531 | instance a Reagent component, assuming it correctly handles receiving a
532 | properties map as its first argument. You can't use `:<>` as the tag, since we
533 | can't add a class name to a fragment.
534 |
535 | As a special case you can use another styled component as the tag.
536 |
537 | ```clojure
538 | (defstyled about-footer footer
539 | ,,,)
540 | ```
541 |
542 | This will cause the new component to "inherit" both the HTML tag used by the
543 | referenced componet, and any CSS rules it defines.
544 |
545 | ### Rules
546 |
547 | After the name and tag, `defstyled` takes one or more "rules". These can be
548 | maps, vectors, or keywords.
549 |
550 | Maps and vectors are handled by [Garden](https://github.com/noprompt/garden),
551 | and we recommend reading the Garden documentation and getting familiar with the
552 | syntax. Maps define CSS styles as demonstrated before, with vectors you can
553 | apply styles to descendant elements, or handle pseudo-elements.
554 |
555 | ```clojure
556 | (in-ns 'my-nav)
557 |
558 | (o/defstyled menu :nav
559 | {:padding "2rem"}
560 | [:a {:color "blue"}]
561 | [:&:hover {:background-color "#888"}])
562 |
563 | ;; Inspect the result
564 | (o/css menu)
565 | ```
566 |
567 | Result:
568 |
569 | ```css
570 | .my_nav__menu{padding:2rem}
571 | .my_nav__menu a{color:blue}
572 | .my_nav__menu:hover{background-color:#888}
573 | ```
574 |
575 | Keywords are handled by [Girouette](https://github.com/green-coder/girouette).
576 | Girouette uses a grammar to parse utility class names like `:text-green-500`,
577 | and converting the result to Garden syntax. Out of the box it supports all the
578 | same names that Tailwind provides, but you can define custom rules, or adjust
579 | the color palette. (See [Customizing Girouette](#customizing-girouette)).
580 |
581 | Note that you can mix and match these. You should be able to use a Girouette
582 | keyword anywhere where you would use a Garden properties map.
583 |
584 | #### Referencing other components in Rules
585 |
586 | You can use a previously defined `defstyled` component either as a selector, or
587 | as a style rule.
588 |
589 | Consider this "call to action" button.
590 |
591 | ```clojure
592 | (o/defstyled cta :button
593 | {:background-color "red"})
594 | ```
595 |
596 | You might use it as part of another component, and add additional styling for
597 | that context.
598 |
599 | ```clojure
600 | (o/defstyled buy-now-section :div
601 | [cta {:padding "2rem"}]
602 | ([]
603 | [:<>
604 | [:p "The best widgest in the world"]
605 | [cta {:value "Buy now!"}]]))
606 | ```
607 |
608 | Here `cta` is a shorthand for writing the full Ornament class name of the
609 | component. Now the `cta` button will get some extra padding in this context, in
610 | addition to its red background.
611 |
612 | You can also use `cta` as a reusable group of styles. In this case we want to
613 | style the `:a` element with the `cta` styles.
614 |
615 | ```clojure
616 | (o/defstyled pricing-link :span
617 | [:a cta]
618 | ([]
619 | [:a {:href "/pricing"} "Pricing"]))
620 | ```
621 |
622 | This kind of referencing previously defined components works both in Clojure and
623 | ClojureScript, even though in ClojureScript usage you can't normally reference
624 | vars inside your style declaration. To make these work we resolve these symbols
625 | during compilation based on Ornament's registry of components.
626 |
627 | ## Render functions
628 |
629 | After the component name, tag, and CSS rules, you can optionally put one or more
630 | render functions, consisting of an argument vector, and the function body.
631 |
632 | ```clojure
633 | (o/defstyled with-body :p
634 | :px-5 :py-3 :rounded-xl
635 | {:color "azure"}
636 | ([& children]
637 | (into [:strong] children)))
638 |
639 | [with-body "hello"]
640 | ;;=>
641 | "
"
272 |
273 | ;; we're getting inconsistent but equivalent rendering here between clj and
274 | ;; cljs. Not ideal, but not a big deal either. Working around with reader
275 | ;; conditionals.
276 | ;; FIXME: write this in a more robust way, maintaining this is becoming a PITA
277 | [attrs-in-fragment "hello"]
278 | "