Tailwind classes in OCaml
1type direction = [
2 | `All
3 | `X
4 | `Y
5 | `Top
6 | `Right
7 | `Bottom
8 | `Left
9 | `Start
10 | `End
11]
12
13type t = {
14 property: string;
15 direction: string;
16 size: string;
17}
18
19let direction_to_string = function
20 | `All -> ""
21 | `X -> "x"
22 | `Y -> "y"
23 | `Top -> "t"
24 | `Right -> "r"
25 | `Bottom -> "b"
26 | `Left -> "l"
27 | `Start -> "s"
28 | `End -> "e"
29
30let make_spacing property direction size =
31 let dir_str = direction_to_string direction in
32 let size_str = Size.to_string size in
33 { property; direction = dir_str; size = size_str }
34
35let padding direction size = make_spacing "p" direction size
36
37let margin direction size = make_spacing "m" direction size
38
39let gap gap_type size =
40 let property = match gap_type with
41 | `All -> "gap"
42 | `X -> "gap-x"
43 | `Y -> "gap-y"
44 in
45 let size_str = Size.to_string size in
46 { property; direction = ""; size = size_str }
47
48let space space_type size =
49 let property = match space_type with
50 | `X -> "space-x"
51 | `Y -> "space-y"
52 in
53 let size_str = Size.to_string size in
54 { property; direction = ""; size = size_str }
55
56let to_class t =
57 let class_name =
58 if t.direction = "" then Printf.sprintf "%s-%s" t.property t.size
59 else Printf.sprintf "%s%s-%s" t.property t.direction t.size
60 in
61 Css.make class_name
62
63(* Shorthand constructors *)
64let p size = padding `All size
65let px size = padding `X size
66let py size = padding `Y size
67let pt size = padding `Top size
68let pr size = padding `Right size
69let pb size = padding `Bottom size
70let pl size = padding `Left size
71
72let m size = margin `All size
73let mx size = margin `X size
74let my size = margin `Y size
75let mt size = margin `Top size
76let mr size = margin `Right size
77let mb size = margin `Bottom size
78let ml size = margin `Left size