hello folks
has anyone a good example to demonstrate how useful defcssfn
is?
trying to make use of it in conjunction of @font-face, building the :src
attribute, containing urls
at the moment have something like this
(defn font-url
[family weight type]
(let [file-name (str weight "." type)
url (wrap-quotes (str "/fonts/" family "/" file-name))
type (wrap-quotes type)]
(str "url(" url ") format(" type ")")))
(defn font-face
[family weight weight-n]
(let [woff2 (font-url family weight "woff2")
woff (font-url family weight "woff")]
(at-font-face {:font-family (string/capitalize family)
:font-weight weight-n
:src (comma-join [woff2 woff])})))
;; usage:
;; let [light-font-face (font-face "roboto" "light" 300)]
;; produces
;; @font-face {
;; font-family: Roboto;
;; font-weight: 300;
;; src: url("/fonts/roboto/light.woff2") format("woff2"), url("/fonts/roboto/light.woff") format("woff");
;; }
but i think this can be tidier and better. mabye with defcssfn? cc @niamu
I haven’t used defcssfn
before. I’m also in the habit of base64 encoding all my fonts and embedding them directly into the CSS file, so I generate some very different looking code than what you have typically.
I like what you have so far though. There is definitely a point that you can reach where writing macros only hinders others from seeing what is going on. Expressive code (even if it takes up a few more lines) is usually my preference. Easier to maintain as well.
Sorry that doesn’t really answer your question...
np, your comments are good
You might consider producing a map of the weights using the string as a key and the corresponding number as the value. That would eliminate the need for one argument in your font-face
function and will likely work well for most fonts.
(def weight
{“light” 300
“bold” 600})
Something like that.
right … will do that
and regarding defcssfn, is there a good example that demonstrates its usefulness?
This is just the first one that I came across, but it seems to demonstrate a few different cases for it: https://github.com/linuccio/site/blob/c174d5d13d4bd3118f11a7e967ac9c38a626c55f/src/site/core/stylesheet/prefix.clj
thanks heaps - maybe add these to the garden documentation?
maybe. Not really sure where the maintainer would like to draw the line with documentation and examples. I could see the argument being made that garden is already well documented and it seems that defcssfn has some decent documentation already and even a sample use case.
hmmm, garden already documents defcssfn well? can you link me?
i think i read them before but somehow it didn’t make “click” in my head ...
Yeah, a lot of things don’t click for me until I’ve used it or seen many more examples for myself. 🙂