Tailwind classes in OCaml
1type opacity = [ `Alpha of int | `Fraction of int * int ]
2
3type variant = [ `V50 | `V100 | `V200 | `V300 | `V400 | `V500
4 | `V600 | `V700 | `V800 | `V900 | `V950 ]
5
6type base = [ `Slate | `Gray | `Zinc | `Neutral | `Stone
7 | `Red | `Orange | `Amber | `Yellow | `Lime | `Green
8 | `Emerald | `Teal | `Cyan | `Sky | `Blue | `Indigo
9 | `Violet | `Purple | `Fuchsia | `Pink | `Rose
10 | `Black | `White | `Transparent | `Current | `Inherit ]
11
12type t = {
13 base: base;
14 variant: variant option;
15 opacity: opacity option;
16}
17
18let make base ?variant ?opacity () = { base; variant; opacity }
19
20let base_to_string = function
21 | `Slate -> "slate"
22 | `Gray -> "gray"
23 | `Zinc -> "zinc"
24 | `Neutral -> "neutral"
25 | `Stone -> "stone"
26 | `Red -> "red"
27 | `Orange -> "orange"
28 | `Amber -> "amber"
29 | `Yellow -> "yellow"
30 | `Lime -> "lime"
31 | `Green -> "green"
32 | `Emerald -> "emerald"
33 | `Teal -> "teal"
34 | `Cyan -> "cyan"
35 | `Sky -> "sky"
36 | `Blue -> "blue"
37 | `Indigo -> "indigo"
38 | `Violet -> "violet"
39 | `Purple -> "purple"
40 | `Fuchsia -> "fuchsia"
41 | `Pink -> "pink"
42 | `Rose -> "rose"
43 | `Black -> "black"
44 | `White -> "white"
45 | `Transparent -> "transparent"
46 | `Current -> "current"
47 | `Inherit -> "inherit"
48
49let variant_to_string = function
50 | `V50 -> "50"
51 | `V100 -> "100"
52 | `V200 -> "200"
53 | `V300 -> "300"
54 | `V400 -> "400"
55 | `V500 -> "500"
56 | `V600 -> "600"
57 | `V700 -> "700"
58 | `V800 -> "800"
59 | `V900 -> "900"
60 | `V950 -> "950"
61
62let opacity_to_string = function
63 | `Alpha n -> string_of_int n
64 | `Fraction (n, d) -> Printf.sprintf "%d/%d" n d
65
66let color_to_string prefix color =
67 let base_str = base_to_string color.base in
68 let color_part = match color.variant with
69 | None -> base_str
70 | Some v -> Printf.sprintf "%s-%s" base_str (variant_to_string v)
71 in
72 let class_name = match color.opacity with
73 | None -> Printf.sprintf "%s-%s" prefix color_part
74 | Some op -> Printf.sprintf "%s-%s/%s" prefix color_part (opacity_to_string op)
75 in
76 Css.make class_name
77
78let text t = color_to_string "text" t
79let bg t = color_to_string "bg" t
80let border t = color_to_string "border" t
81let ring t = color_to_string "ring" t
82let outline t = color_to_string "outline" t
83let fill t = color_to_string "fill" t
84let stroke t = color_to_string "stroke" t
85
86let black = make `Black ()
87let white = make `White ()
88let transparent = make `Transparent ()
89let current = make `Current ()