/
stylesheet.cljc
79 lines (65 loc) · 2.24 KB
/
stylesheet.cljc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
(ns mranderson048.garden.v1v3v3.garden.stylesheet
"Utility functions for CSS properties, directives and functions."
(:require [mranderson048.garden.v1v3v3.garden.util :as util]
[mranderson048.garden.v1v3v3.garden.color :as color]
[mranderson048.garden.v1v3v3.garden.types :as t])
#?(:clj
(:import mranderson048.garden.v1v3v3.garden.types.CSSFunction
mranderson048.garden.v1v3v3.garden.types.CSSAtRule)))
;;;; ## Stylesheet helpers
(defn rule
"Create a rule function for the given selector. The `selector`
argument must be valid selector (ie. a keyword, string, or symbol).
Additional arguments may consist of extra selectors or
declarations.
The returned function accepts any number of arguments which represent
the rule's children.
Ex.
(let [text-field (rule \"[type=\"text\"])]
(text-field {:border [\"1px\" :solid \"black\"]}))
;; => [\"[type=\"text\"] {:boder [\"1px\" :solid \"black\"]}]"
[selector & more]
(if-not (or (keyword? selector)
(string? selector)
(symbol? selector))
(throw (ex-info
"Selector must be either a keyword, string, or symbol." {}))
(fn [& children]
(into (apply vector selector more) children))))
(defn cssfn [fn-name]
(fn [& args]
(t/CSSFunction. fn-name args)))
;;;; ## At-rules
(defn- at-rule [identifier value]
(t/CSSAtRule. identifier value))
(defn at-font-face
"Create a CSS @font-face rule."
[& font-properties]
["@font-face" font-properties])
(defn at-import
"Create a CSS @import rule."
([url]
(at-rule :import {:url url
:media-queries nil}))
([url & media-queries]
(at-rule :import {:url url
:media-queries media-queries})))
(defn at-media
"Create a CSS @media rule."
[media-queries & rules]
(at-rule :media {:media-queries media-queries
:rules rules}))
(defn at-keyframes
"Create a CSS @keyframes rule."
[identifier & frames]
(at-rule :keyframes {:identifier identifier
:frames frames}))
;;;; ## Functions
(defn rgb
"Create a color from RGB values."
[r g b]
(color/rgb [r g b]))
(defn hsl
"Create a color from HSL values."
[h s l]
(color/hsl [h s l]))