Source file skyline_field_v2.ml
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
open! Core
open! Private_skyline_prelude
module Intent = struct
type t =
[ `Primary
| `Success
| `Danger
| `Warning
]
[@@deriving to_string]
let _to_other_intent : t -> Skyline_intent.t = function
| a -> (a :> Skyline_intent.t)
;;
end
module Label_position = struct
type t =
| Top
| Left
| Right
[@@deriving to_string]
end
module Label_forwarding = struct
open Js_of_ocaml
let data_exclude_from_label_forwarding =
"data-skyline-field-exclude-from-label-forwarding"
;;
let coerce_to_html_element (element : #Browser_types.event_target Js.t Js.Opt.t) =
let%bind.Option element = Js.Opt.to_option element in
let%bind.Option window = Browser_expert.Global.window () in
Browser_expert.coerce ~to_:window##._HTMLElement_t element
;;
let for_label_attr =
if not Am_running_how_js.(am_in_browser_like_api am_running_how)
then Attr.empty
else (
let selectors = Js.string [%string "[%{data_exclude_from_label_forwarding}]"] in
let handler (event : Dom_html.mouseEvent Js.t) =
let event = Browser_extra.Coercion.of_jsoo_event event in
let should_prevent_default =
match coerce_to_html_element event##.target with
| None -> false
| Some target -> Js.Opt.test (target##closest ~selectors)
in
if should_prevent_default
then
event##preventDefault;
Effect.Ignore
in
Attr.many
[
Attr.on_click handler
; Attr.on_mousedown handler
])
;;
end
module Content = struct
type t =
size:Skyline_size.t -> intent:Intent.t -> disabled:bool -> Node.t * unit Or_error.t
let make
(f : size:Skyline_size.t -> intent:Intent.t -> disabled:bool -> Node.t)
~size
~intent
~disabled
=
f ~size ~intent ~disabled, Ok ()
;;
let make' f = f
module Expert = struct
let exclude_from_label_forwarding =
Attr.create Label_forwarding.data_exclude_from_label_forwarding ""
;;
end
end
module Style = struct
let row_gap = `Px 2
let column_gap = function
| `Xs | `Sm -> `Px 4
| `Md -> `Px 6
| `Lg -> `Px 8
;;
let block_layout ~in_group =
let base_styles =
[%css
{|
display: flex;
flex-direction: column;
height: fit-content;
gap: %{row_gap#Css_gen.Length};
|}]
in
let row_styles =
if in_group then [%css {|grid-column: 1 / -1;|}] else Vdom.Attr.empty
in
Attr.many [ base_styles; row_styles ]
;;
let inline_layout ~size ~in_group =
let base_styles =
[%css
{|
display: grid;
align-items: start;
row-gap: %{row_gap#Css_gen.Length};
column-gap: %{(column_gap size)#Css_gen.Length};
|}]
in
let column_styles =
if in_group
then
[%css
{|
grid-template-columns: subgrid;
grid-column: 1 / -1;
|}]
else
[%css
{|
height: fit-content;
width: 100%;
grid-template-columns: auto 1fr;
|}]
in
Attr.many [ base_styles; column_styles ]
;;
end
module Label = struct
type t = size:Skyline_size.t -> intent:Intent.t -> disabled:bool -> Vdom.Node.t
let make f = f
module Style = struct
let text size =
let text_size =
match size with
| `Xs -> Classes.text_2xs
| `Sm -> Classes.text_xs
| `Md -> Classes.text_sm
| `Lg -> Classes.text_base
in
Attr.many [ text_size ]
;;
end
let content ?test_selector ?(attrs = []) children =
make (fun ~size ~intent:_ ~disabled:_ ->
{%html|
<div
%{Test_selector.attr_of_opt test_selector}
%{Style.text size}
*{attrs}
>
*{children}
</div>
|})
;;
end
let view'
?test_selector
?(attrs = [])
?(label_position = Label_position.Top)
?(size = `Md)
?(intent = `Primary)
?(disabled = false)
?(in_group = false)
?label
?
(contents : Content.t list)
=
let module Elements = struct
let label ?(attrs = []) () =
match label with
| Some label ->
let text_color =
match label_position with
| Top -> Classes.text_default
| Left | Right ->
if disabled then Classes.text_disabled else Classes.text_default
in
{%html|<div *{attrs} %{text_color}>%{label ~size ~intent ~disabled}</div>|}
| None -> Node.none
;;
let content_nodes_and_error =
let evaluated_contents =
List.map contents ~f:(fun content -> content ~size ~intent ~disabled)
in
let nodes, errors = List.unzip evaluated_contents in
let error = Or_error.all_unit errors in
nodes, error
;;
let ?(attrs = []) () =
let _, error = content_nodes_and_error in
match error with
| Ok () ->
(match footer with
| Some -> {%html|<div *{attrs}>%{footer ~size ~intent ~disabled}</div>|}
| None -> Node.none)
| Error error ->
let error_text = Error.to_string_hum error in
{%html|
<div
%{Footer.Style.text size}
%{Footer.Style.color ~disabled `Danger}
*{attrs}
>
%{error_text#String}
</div>
|}
;;
let contents ?(attrs = []) () =
let nodes, _ = content_nodes_and_error in
{%html|<div *{attrs} %{Classes.flex} %{Classes.flex_col}>*{nodes}</div>|}
;;
end
in
match label_position with
| Top ->
{%html|
<label
*{attrs}
%{Label_forwarding.for_label_attr}
%{Test_selector.attr_of_opt test_selector}
%{Style.block_layout ~in_group }
>
<Elements.label />
<Elements.contents />
<Elements.footer />
</label>
|}
| Left ->
{%html|
<label
*{attrs}
%{Label_forwarding.for_label_attr}
%{Test_selector.attr_of_opt test_selector}
%{Style.inline_layout ~size ~in_group }
>
<Elements.label style="grid-column: 1; align-self: center" />
<Elements.contents style="grid-column: 2" />
<Elements.footer style="grid-column: 2" />
</label>
|}
| Right ->
{%html|
<label
*{attrs}
%{Label_forwarding.for_label_attr}
%{Test_selector.attr_of_opt test_selector}
%{Style.inline_layout ~size ~in_group }
>
<Elements.contents style="grid-column: 1; width: max-content" />
<Elements.label style="grid-column: 2; align-self: center" />
<Elements.footer style="grid-column: 2" />
</label>
|}
;;
let view
?test_selector
?attrs
?label_position
?size
?intent
?disabled
?label
?
contents
=
view'
?test_selector
?attrs
?label_position
?size
?intent
?disabled
?label
?footer
contents
;;
module Grid = struct
let field
?test_selector
?attrs
?(label_position = Label_position.Left)
?(size = `Md)
?(intent = `Primary)
?(disabled = false)
?label
?
(contents : Content.t list)
: Vdom.Node.t
=
view'
?test_selector
?attrs
~label_position
~size
~intent
~disabled
~in_group:true
?label
?footer
contents
;;
let view ?test_selector ?(attrs = []) (fields : Vdom.Node.t list) =
{%html|
<fieldset
style="display: grid; grid-template-columns: auto 1fr"
*{attrs}
%{Test_selector.attr_of_opt test_selector}
>
*{fields}
</fieldset>
|}
;;
end
module For_docs = struct
let ml_filepath = [%here].pos_fname
end