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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
open! Core
open! Import
module Flex = struct
module Horizontal_dir = struct
type t =
| Left_to_right
| Right_to_left
[@@deriving equal, compare, sexp, enumerate]
end
module Vertical_dir = struct
type t =
| Top_to_bottom
| Bottom_to_top
[@@deriving equal, compare, sexp, enumerate]
end
module Wrap = struct
type t =
| Wrap
| Wrap_reverse
| No_wrap
[@@deriving equal, compare, sexp, enumerate]
end
module Align_content = struct
type t =
| Start
| End
| Normal
| Center
| Space_between
| Space_around
| Stretch
[@@deriving equal, compare, sexp, enumerate]
end
module Main_axis_alignment = struct
type t =
| Start
| End
| Center
| Space_between
| Space_around
| Space_evenly
[@@deriving equal, compare, sexp, enumerate]
end
module Cross_axis_alignment = struct
type t =
| Start
| End
| Center
| Stretch
| Baseline
[@@deriving equal, compare, sexp, enumerate]
end
end
let to_css_gen_wrap : Flex.Wrap.t -> _ = function
| No_wrap -> `Nowrap
| Wrap -> `Wrap
| Wrap_reverse -> `Wrap_reverse
;;
let to_css_gen_align_content : Flex.Align_content.t -> _ = function
| Start -> `Flex_start
| End -> `Flex_end
| Normal -> `Normal
| Center -> `Center
| Space_between -> `Space_between
| Space_around -> `Space_around
| Stretch -> `Stretch
;;
let to_css_gen_justify_content : Flex.Main_axis_alignment.t -> _ = function
| Start -> `Flex_start
| End -> `Flex_end
| Center -> `Center
| Space_between -> `Space_between
| Space_around -> `Space_around
| Space_evenly -> `Space_evenly
;;
let to_css_gen_align_items : Flex.Cross_axis_alignment.t -> _ = function
| Start -> `Flex_start
| End -> `Flex_end
| Center -> `Center
| Stretch -> `Stretch
| Baseline -> `Baseline
;;
let to_css_gen_horizontal_direction : Flex.Horizontal_dir.t -> _ = function
| Left_to_right -> `Row
| Right_to_left -> `Row_reverse
;;
let to_css_gen_vertical_direction : Flex.Vertical_dir.t -> _ = function
| Top_to_bottom -> `Column
| Bottom_to_top -> `Column_reverse
;;
let box
~(default_direction : 'direction option)
~(direction_to_css_gen_direction :
'direction -> [ `Column | `Column_reverse | `Default | `Row | `Row_reverse ])
?(attrs = [])
?row_gap
?column_gap
?main_axis_alignment
?cross_axis_alignment
?direction
?wrap
?align_content
children
=
let direction =
match default_direction, direction with
| _, Some direction -> direction_to_css_gen_direction direction
| Some default_direction, None -> direction_to_css_gen_direction default_direction
| _ -> `Default
in
let wrap = Option.value_map wrap ~f:to_css_gen_wrap ~default:`Default in
let align_content = Option.map ~f:to_css_gen_align_content align_content in
let justify_content = Option.map ~f:to_css_gen_justify_content main_axis_alignment in
let align_items = Option.map ~f:to_css_gen_align_items cross_axis_alignment in
let flex_options =
Css_gen.flex_container
~direction
~wrap
?align_items
?align_content
?justify_content
?row_gap
?column_gap
()
in
Vdom.Node.div ~attrs:(Vdom.Attr.style flex_options :: attrs) children
;;
let hbox ?attrs ?gap =
box
~default_direction:None
~direction_to_css_gen_direction:to_css_gen_horizontal_direction
?attrs
?column_gap:gap
?row_gap:None
?align_content:None
?wrap:None
;;
let vbox ?attrs ?gap =
box
~default_direction:(Some Flex.Vertical_dir.Top_to_bottom)
~direction_to_css_gen_direction:to_css_gen_vertical_direction
?attrs
?row_gap:gap
?column_gap:None
?align_content:None
?wrap:None
;;
let hbox_wrap =
box
~default_direction:None
~direction_to_css_gen_direction:to_css_gen_horizontal_direction
~wrap:Wrap
;;
let vbox_wrap =
box
~default_direction:(Some Flex.Vertical_dir.Top_to_bottom)
~direction_to_css_gen_direction:to_css_gen_vertical_direction
~wrap:Wrap
;;