Source file skyline_button_v1.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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
open! Core
open! Bonsai_web
open! Bonsai.Let_syntax
module Style = struct
let mix_background foreground =
Skyline_theme_v1.ramp foreground (Percent.of_percentage 20.)
|> Css_gen.Color.to_string_css
;;
let mix_secondary_background foreground =
Skyline_theme_v1.ramp foreground (Percent.of_percentage 10.)
|> Css_gen.Color.to_string_css
;;
let common_colors ~intent =
let foreground =
match intent with
| Some intent -> Css_gen.Color.to_string_css intent
| None -> Css_gen.Color.to_string_css Skyline_theme_v1.primary
in
let background = mix_background Skyline_theme_v1.primary in
let hover_foreground = Css_gen.Color.to_string_css Skyline_theme_v1.background in
let hover_background =
match intent with
| Some intent -> Css_gen.Color.to_string_css intent
| None -> Css_gen.Color.to_string_css Skyline_theme_v1.primary
in
~foreground, ~background, ~hover_foreground, ~hover_background
;;
let css_reset =
[%css
{|
box-sizing: border-box;
margin: 0;
padding: 0;
font-family: var(--skyline-font-sans, sans-serif);
line-height: inherit;
color: inherit;
background-color: transparent;
background-image: none;
border-width: 0;
border-style: solid;
-webkit-appearance: button;
appearance: button;
cursor: pointer;
|}]
;;
module Regular = struct
include
[%css
stylesheet
{|
.button {
height: 28px;
padding: 0 12px;
column-gap: 4px;
font-size: 0.8rem;
font-weight: 600;
text-decoration: none;
line-height: 16px;
display: flex;
align-items: center;
justify-content: center;
white-space: nowrap;
border-radius: 4px;
color: var(--foreground);
background-color: var(--background);
}
.button.dropdown {
height: 28px;
width: 28px;
flex-grow: 0;
flex-shrink: 1;
}
.button.enabled {
cursor: pointer;
}
.button.enabled:hover {
color: var(--hover-foreground);
background-color: var(--hover-background);
}
.button.secondary {
background-color: unset;
border-width: 1px;
border-color: var(--background);
}
.button.secondary:hover {
color: var(--foreground);
background-color: var(--background);
}
.disabled {
opacity: 0.6;
cursor: not-allowed;
}
|}]
let colors ~intent =
let ~foreground, ~background, ~hover_foreground, ~hover_background =
common_colors ~intent
in
Variables.set_all ~foreground ~background ~hover_foreground ~hover_background
;;
end
module Compact = struct
include
[%css
stylesheet
{|
.container {
height: 24px;
display: flex;
justify-content: space-between;
align-items: center;
flex-shrink: 0;
border-radius: 4px;
color: var(--foreground);
background: var(--background);
}
.button {
height: 24px;
min-width: 24px;
width: max-content;
padding: 0;
padding-left: 4px;
padding-right: var(--padding-right);
display: flex;
justify-content: center;
align-items: center;
flex-shrink: 0;
gap: 2px;
flex-grow: 1;
font-size: 0.8rem;
font-weight: 600;
text-decoration: none;
line-height: 24px;
border-radius: 4px;
}
.dropdown {
height: 24px;
width: 16px;
display: flex;
align-items: center;
justify-content: center;
flex-shrink: 0;
border-radius: 4px;
background: none;
}
.container.enabled:hover {
background-color: var(--secondary-hover-background);
}
.container.enabled .dropdown:hover,
.container.enabled .button:hover {
color: var(--hover-foreground);
background-color: var(--hover-background);
}
.container.disabled {
opacity: 0.6;
}
.container.disabled .button,
.container.disabled .dropdown {
cursor: not-allowed;
}
|}]
let colors ~secondary ~intent =
let ~foreground, ~background, ~hover_foreground, ~hover_background =
match secondary with
| false -> common_colors ~intent
| true ->
let foreground =
match intent with
| Some intent -> intent
| None -> Skyline_theme_v1.primary
in
let hover_background = mix_background foreground in
let foreground = Css_gen.Color.to_string_css foreground in
~foreground, ~background:"none", ~hover_foreground:foreground, ~hover_background
in
let secondary_hover_background =
if secondary
then mix_secondary_background Skyline_theme_v1.primary
else background
in
Variables.set_all
~foreground
~background
~hover_foreground
~hover_background
~secondary_hover_background
;;
end
module Link = struct
include
[%css
stylesheet
{|
.icon {
}
.link {
display: flex;
flex-direction: row;
align-items: center;
text-align: start;
text-decoration: inherit;
}
.link:has(> .icon) {
column-gap: 4px;
}
.link:hover > span {
text-decoration: underline;
}
.link.disabled {
opacity: 0.8;
cursor: not-allowed;
}
.link.disabled > span {
text-decoration: line-through;
}
|}]
end
end
let href_and_target href target =
Vdom.Attr.combine
(Vdom.Attr.href href)
(Vdom.Attr.target (Effect.Open_url_target.to_target target))
;;
let regular_impl
~attrs
~loading
~autofocus
~disabled
~secondary
~intent
~tooltip
~test_selector
~on_click
~dropdown
~icon
~label
=
let disabled = disabled || loading in
let autofocus =
if autofocus && not disabled
then Private_skyline_autofocus.focus_on_mount
else Vdom.Attr.empty
in
let intent = if loading || disabled then None else intent in
let attrs_common =
Vdom.Attr.many
[ (if disabled
then Vdom.Attr.many [ Style.Regular.disabled; Vdom.Attr.disabled ]
else Style.Regular.enabled)
; (if secondary then Style.Regular.secondary else Vdom.Attr.empty)
]
in
let button ~attrs =
let tooltip =
match tooltip with
| Some "" | None -> Vdom.Attr.empty
| Some text -> Skyline_tooltip_v1.text ~position:Top ~alignment:Center text
in
let attrs =
Style.Regular.colors ~intent
:: Style.Regular.button
:: Style.css_reset
:: tooltip
:: autofocus
:: attrs_common
:: Test_selector.attr_of_opt test_selector
:: attrs
in
let contents =
let icon =
if loading
then Skyline_loading_indicator_v1.spinner ()
else Option.value_map icon ~f:Codicons.svg ~default:Vdom.Node.none
in
[ icon; Vdom.Node.span [ Vdom.Node.text label ] ]
in
match on_click with
| Effect.Open { url; target } when not disabled ->
Vdom.Node.a ~attrs:(href_and_target url target :: attrs) contents
| _ ->
let on_click = if disabled then Effect.Ignore else on_click in
Vdom.Node.button ~attrs:(Vdom.Attr.on_click (fun _ -> on_click) :: attrs) contents
in
match dropdown with
| Some dropdown ->
let dropdown =
let on_click = if disabled then Effect.Ignore else dropdown in
Vdom.Node.button
~attrs:
[ Style.Regular.colors ~intent:None
; Style.Regular.button
; Style.css_reset
; Style.Regular.dropdown
; attrs_common
; Vdom.Attr.on_click (fun _ -> on_click)
]
[ Codicons.svg Chevron_down ]
in
Skyline_flex_v1.row
~attrs
~gap:(`Px 2)
[ button ~attrs:[ Vdom.Attr.style (Css_gen.create ~field:"flex" ~value:"1") ]
; dropdown
]
| None -> button ~attrs
;;
let compact_impl
~attrs
~loading
~autofocus
~disabled
~secondary
~intent
~tooltip
~test_selector
~on_click
~dropdown
~icon
~label
=
let disabled = disabled || loading in
let autofocus =
if autofocus && not disabled
then Private_skyline_autofocus.focus_on_mount
else Vdom.Attr.empty
in
let intent = if loading || disabled then None else intent in
let tooltip =
match tooltip with
| Some "" | None -> Vdom.Attr.empty
| Some text -> Skyline_tooltip_v1.text ~position:Top ~alignment:Center text
in
let attrs =
Style.Compact.colors
~intent
~secondary
~padding_right:(if Option.is_some dropdown then "0px" else "4px")
:: Style.Compact.container
:: (if disabled
then Vdom.Attr.many [ Style.Compact.disabled; Vdom.Attr.disabled ]
else Style.Compact.enabled)
:: tooltip
:: attrs
in
let button =
let contents =
let icon =
if loading
then Skyline_loading_indicator_v1.spinner ()
else Option.value_map icon ~f:Codicons.svg ~default:Vdom.Node.none
in
[ icon
; (if String.is_empty label
then Vdom.Node.none
else Vdom.Node.span [ Vdom.Node.text label ])
]
in
match on_click with
| Effect.Open { url; target } when not disabled ->
Vdom.Node.a
~attrs:
[ Style.Compact.button
; Style.css_reset
; href_and_target url target
; autofocus
; Test_selector.attr_of_opt test_selector
]
contents
| _ ->
let on_click = if disabled then Effect.Ignore else on_click in
Vdom.Node.button
~attrs:
[ Style.Compact.button
; Style.css_reset
; Vdom.Attr.on_click (fun _ -> on_click)
; autofocus
; Test_selector.attr_of_opt test_selector
]
contents
in
let dropdown =
match dropdown with
| Some dropdown ->
let on_click = if disabled then Effect.Ignore else dropdown in
Vdom.Node.button
~attrs:
[ Style.Compact.dropdown
; Style.css_reset
; Vdom.Attr.on_click (fun _ -> on_click)
]
[ Codicons.svg ~size:(`Px 8) Triangle_down ]
| None -> Vdom.Node.none
in
Vdom.Node.div ~attrs [ button; dropdown ]
;;
let maybe_confirm_guard ~confirm ~on_click (local_ graph) =
match%sub confirm with
| false ->
let%arr on_click in
`Idle, on_click
| true ->
let confirm = Skyline_confirm_guard_v1.component graph in
let%arr confirm and on_click in
let on_click =
match confirm.stage with
| `Idle -> confirm.trigger
| `Cooldown -> Effect.Ignore
| `Confirm ->
let%bind.Effect () = confirm.reset in
on_click
in
confirm.stage, on_click
;;
let ~disabled ~on_open graph =
match%sub menu with
| [] -> return None
| _ :: _ as ->
let anchor, toggle =
Skyline_context_menu_v1.manual_popover
~position:(return Skyline_popover_v1.Position.Bottom)
~alignment:(return Skyline_popover_v1.Alignment.End)
menu
graph
in
let%arr anchor and toggle and disabled and on_open in
let open_effect =
if disabled then Effect.Ignore else Effect.Many [ on_open; toggle ]
in
let =
Vdom.Attr.on_contextmenu (fun event ->
Js_of_ocaml.Dom.preventDefault event;
open_effect)
in
Some (Vdom.Attr.combine anchor on_contextmenu, open_effect)
;;
let common_impl
view_impl
?(confirm = Bonsai.return false)
?(loading = Bonsai.return `No)
?(dropdown = Bonsai.return [])
?(on_dropdown_open = Bonsai.return Effect.Ignore)
?(autofocus = Bonsai.return false)
?(disabled = Bonsai.return false)
?(secondary = Bonsai.return false)
~intent
~tooltip
~(test_selector : Test_selector.t option Bonsai.t)
~icon
~on_click
label
graph
=
let loading_state = Skyline_loading_state_v1.component graph in
let on_click =
let%arr on_click and loading_state in
Skyline_loading_state_v1.handle loading_state on_click
in
let%sub idle_width, track_idle_width =
match%sub confirm with
| true ->
let idle_width, set_idle_width = Bonsai.state 0 graph in
let track_idle_width_attr =
let%arr set_idle_width in
Bonsai_web_element_size_hooks.Size_tracker.on_change
(fun { border_box = { width; height = _ }; content_box = _ } ->
set_idle_width (Int.of_float width))
in
Bonsai.both idle_width track_idle_width_attr
| false -> Bonsai.return (0, Vdom.Attr.empty)
in
let%sub confirm, on_click = maybe_confirm_guard ~confirm ~on_click graph in
let =
let =
let%arr dropdown and loading_state in
List.map dropdown ~f:(fun item ->
Skyline_context_menu_v1.Item.Expert.map_actions
item
~f:(Skyline_loading_state_v1.handle loading_state))
in
dropdown_menu ~disabled ~on_open:on_dropdown_open menu graph
in
let%arr autofocus
and disabled
and loading =
let%arr loading and loading_state in
match loading with
| `No -> false
| `Yes -> true
| `While_on_click_in_flight -> Skyline_loading_state_v1.is_loading loading_state
and secondary
and confirm
and idle_width
and track_idle_width
and intent
and tooltip
and icon
and on_click
and
and label
and test_selector in
let confirm_width =
match confirm with
| `Idle -> track_idle_width
| `Cooldown | `Confirm -> Vdom.Attr.style (Css_gen.min_width (`Px idle_width))
in
view_impl
~attrs:
[ Option.value_map dropdown_menu ~f:fst ~default:Vdom.Attr.empty; confirm_width ]
~loading
~autofocus
~disabled
~secondary
~intent:
(match confirm with
| `Idle -> intent
| _ -> None)
~tooltip
~test_selector
~on_click
~dropdown:(Option.map dropdown_menu ~f:snd)
~icon:
(match confirm with
| `Idle ->
(match icon with
| Some Codicons.Blank -> None
| icon -> icon)
| `Cooldown | `Confirm -> Some Question)
~label:
(match confirm with
| `Idle -> label
| `Cooldown | `Confirm -> "Confirm")
;;
let regular
?test_selector
?confirm
?loading
?dropdown
?autofocus
?disabled
?secondary
?intent
?tooltip
?icon
~on_click
label
graph
=
common_impl
regular_impl
?confirm
?loading
?autofocus
?disabled
?secondary
~intent:(Bonsai.transpose_opt intent)
~tooltip:(Bonsai.transpose_opt tooltip)
?dropdown
~icon:(Bonsai.transpose_opt icon)
~on_click
~test_selector:(Bonsai.transpose_opt test_selector)
label
graph
;;
let regular'
?test_selector
?(autofocus = false)
?(disabled = false)
?(secondary = false)
?intent
?tooltip
?icon
~on_click
label
=
regular_impl
~attrs:[]
~loading:false
~autofocus
~disabled
~secondary
~intent
~tooltip
~on_click
~dropdown:None
~test_selector
~icon
~label
;;
let compact
?test_selector
?confirm
?loading
?dropdown
?autofocus
?disabled
?secondary
?intent
?tooltip
?icon
~on_click
label
graph
=
common_impl
compact_impl
?confirm
?loading
?autofocus
?disabled
?secondary
~intent:(Bonsai.transpose_opt intent)
~tooltip:(Bonsai.transpose_opt tooltip)
?dropdown
~test_selector:(Bonsai.transpose_opt test_selector)
~icon:(Bonsai.transpose_opt icon)
~on_click
label
graph
;;
let compact'
?test_selector
?(autofocus = false)
?(disabled = false)
?(secondary = false)
?intent
?tooltip
?icon
~on_click
label
=
compact_impl
~attrs:[]
~loading:false
~autofocus
~disabled
~secondary
~intent
~tooltip
~test_selector
~on_click
~dropdown:None
~icon
~label
;;
let icon ?test_selector ?loading ?dropdown ?disabled ?intent ?tooltip ~on_click icon graph
=
common_impl
compact_impl
~confirm:(Bonsai.return false)
?loading
~autofocus:(Bonsai.return false)
?disabled
~secondary:(Bonsai.return true)
~intent:(Bonsai.transpose_opt intent)
~tooltip:(Bonsai.transpose_opt tooltip)
?dropdown
~icon:(Bonsai.map icon ~f:Option.some)
~test_selector:(Bonsai.transpose_opt test_selector)
~on_click
(return "")
graph
;;
let icon' ?test_selector ?(disabled = false) ?intent ?tooltip ~on_click icon =
compact_impl
~attrs:[]
~loading:false
~autofocus:false
~disabled
~secondary:true
~intent
~tooltip
~test_selector
~on_click
~dropdown:None
~icon:(Some icon)
~label:""
;;
let link
?test_selector
?(disabled = false)
?(intent = Skyline_theme_v1.accent)
?(font = Skyline_text_v1.Font_family.Sans_serif)
?(size = Skyline_text_v1.Font_size.Regular)
?(style = Skyline_text_v1.Font_style.Regular)
?icon
~on_click
label
=
let attrs =
[ Style.Link.link
; Style.css_reset
; (if disabled
then Vdom.Attr.many [ Style.Link.disabled; Vdom.Attr.disabled ]
else Vdom.Attr.empty)
; Test_selector.attr_of_opt test_selector
]
in
let text = Skyline_text_v1.span ~font ~intent ~size ~style label in
let content =
match icon with
| None -> [ text ]
| Some icon ->
[ Codicons.svg icon ~extra_attrs:[ Style.Link.icon ] ~color:intent; text ]
in
match on_click with
| Effect.Open { url; target } when not disabled ->
Vdom.Node.a ~attrs:(href_and_target url target :: attrs) content
| _ ->
let on_click =
Vdom.Attr.on_click (fun _ -> if disabled then Effect.Ignore else on_click)
in
Vdom.Node.button ~attrs:(on_click :: attrs) content
;;
let with_error_common
?test_selector
?confirm
?(loading = Bonsai.return `While_on_click_in_flight)
?(dropdown = Bonsai.return [])
?on_dropdown_open
?autofocus
?disabled
?secondary
?intent
?tooltip
?icon
impl
~on_click
label
(local_ graph)
=
let error_handler = Skyline_errors_v1.component graph in
let error_notification =
let%arr toast = Skyline_toast_v1.error graph in
function%bind.Effect
| Ok _ as ok -> Effect.return ok
| Error error ->
let%map.Effect () = toast error in
Error error
in
let on_click =
let%arr error_handler and error_notification and on_click in
let%bind.Effect () = Skyline_errors_v1.clear error_handler in
on_click |> error_notification |> Skyline_errors_v1.handle error_handler
in
let dropdown =
let%arr error_handler and error_notification and dropdown in
List.map dropdown ~f:(fun item ->
Skyline_context_menu_v1.Item.Expert.map_actions item ~f:(fun effect ->
effect |> error_notification |> Skyline_errors_v1.handle error_handler))
in
let%sub label, intent, icon, tooltip =
let%arr error_handler
and label
and intent = Bonsai.transpose_opt intent
and icon = Bonsai.transpose_opt icon
and tooltip = Bonsai.transpose_opt tooltip in
match Skyline_errors_v1.errors error_handler with
| [] -> label, intent, icon, tooltip
| error :: _ ->
( [%string "Retry %{label}"]
, Some Skyline_theme_v1.error
, Some Codicons.Refresh
, Some (Error.to_string_hum error) )
in
common_impl
impl
?confirm
~loading
~dropdown
?on_dropdown_open
?autofocus
?disabled
?secondary
~intent
~tooltip
~icon
~test_selector:(Bonsai.transpose_opt test_selector)
~on_click
label
graph
;;
let with_error
?test_selector
?confirm
?loading
?dropdown
?on_dropdown_open
?autofocus
?disabled
?secondary
?intent
?tooltip
?icon
~on_click
label
(local_ graph)
=
with_error_common
?test_selector
?confirm
?loading
?dropdown
?on_dropdown_open
?autofocus
?disabled
?secondary
?intent
?tooltip
?icon
regular_impl
~on_click
label
graph
;;
let with_error_compact
?test_selector
?confirm
?loading
?dropdown
?on_dropdown_open
?autofocus
?disabled
?secondary
?intent
?tooltip
?icon
~on_click
label
(local_ graph)
=
with_error_common
?test_selector
?confirm
?loading
?dropdown
?on_dropdown_open
?autofocus
?disabled
?secondary
?intent
?tooltip
?icon
compact_impl
~on_click
label
graph
;;