Source file skyline_popover_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
open! Core
open! Private_skyline_prelude
module Position = struct
type t = Bonsai_web_toplayer.Position.t =
| Auto
| Top
| Bottom
| Left
| Right
[@@deriving sexp, equal, compare, enumerate, to_string]
end
module Alignment = struct
type t = Bonsai_web_toplayer.Alignment.t =
| Center
| Start
| End
[@@deriving sexp_of, equal, to_string]
end
module Match_anchor_side = struct
type t = Bonsai_web_toplayer.Match_anchor_side.t =
| Grow_to_match
| Match_exactly
| Shrink_to_match
[@@deriving sexp_of, equal, to_string]
end
module Controller = struct
type t =
{ autoclose : Vdom.Attr.t
; is_open : bool
; set_is_open : bool -> unit Effect.t
}
[@@deriving fields ~getters]
let component ?(close_on_click_outside = return true) ?state (local_ graph) =
Bonsai_web_toplayer.Expert.ensure_global_toplayer_root_mounted ();
let is_open, set_is_open =
match state with
| Some (visible, set_visible) -> visible, set_visible
| None -> Bonsai.state false graph
in
let autoclose =
let close =
let%arr set_is_open in
set_is_open false
in
let close_on_click_outside =
if%arr close_on_click_outside
then Bonsai_web_toplayer.Close_on_click_outside.Yes
else No
in
Bonsai_web_toplayer.Autoclose.create
~close
~close_on_click_outside
~close_on_right_click_outside:close_on_click_outside
~close_on_esc:(return true)
graph
in
let%arr autoclose and is_open and set_is_open in
{ autoclose :> Vdom.Attr.t; is_open; set_is_open }
;;
end
let popover_attrs ~match_anchor_side_length =
[ (match match_anchor_side_length with
| None ->
[%css
{|
display: flex;
flex-direction: column;
width: max-content;
max-width: max(300px, calc(100vw - 8px));
|}]
| Some (_ : Match_anchor_side.t) ->
[%css
{|
display: grid;
grid-auto-flow: row;
|}])
;
[%css
{|
padding: 0;
border: none;
background: none;
|}]
; Classes.data_skyline_component "popover"
]
;;
let attr
?(position = Position.Auto)
?(alignment = Alignment.Center)
?match_anchor_side_length
?(focus_on_show = true)
~controller:
({ autoclose; is_open; set_is_open = (_ : bool -> unit Effect.t) } : Controller.t)
contents
=
match is_open with
| false -> Vdom.Attr.empty
| true ->
let focus_attrs =
if focus_on_show then [ Bonsai_web_toplayer.Expert.focus_popover_on_open ] else []
in
Bonsai_web_toplayer.vdom_popover
~popover_attrs:(focus_attrs @ (autoclose :: popover_attrs ~match_anchor_side_length))
~position
~alignment
?match_anchor_side_length
~overflow_auto_wrapper:false
contents
;;
type t =
{ anchor : Vdom.Attr.t
; is_open : bool
; set_is_open : bool -> unit Effect.t
}
let component'
?(position = return Position.Auto)
?(alignment = return Alignment.Center)
?match_anchor_side_length
?(focus_on_show = return true)
?close_on_click_outside
?state
contents
(local_ graph)
=
let controller = Controller.component ?close_on_click_outside ?state graph in
let%sub { Controller.is_open; set_is_open; autoclose = _ } = controller in
let contents =
match%sub is_open with
| true ->
let hide =
let%arr set_is_open in
set_is_open false
in
contents ~hide graph
| false -> return Vdom.Node.none
in
let%arr controller
and contents
and position
and alignment
and match_anchor_side_length = Bonsai.transpose_opt match_anchor_side_length
and focus_on_show in
let anchor =
attr
~position
~alignment
?match_anchor_side_length
~focus_on_show
~controller
contents
in
{ anchor
; is_open = Controller.is_open controller
; set_is_open = Controller.set_is_open controller
}
;;
let component
?position
?alignment
?match_anchor_side_length
?focus_on_show
?close_on_click_outside
?state
contents
(local_ graph)
=
component'
?position
?alignment
?match_anchor_side_length
?focus_on_show
?close_on_click_outside
?state
(fun ~hide graph ->
let%arr content = contents ~hide graph in
[%html
{|
<Skyline_card_v2.view
~elevation:%{Two}
~size:%{`Xs}
style="min-width: 0"
>
<Skyline_card_v2.Section.text> %{content} </>
</>
|}])
graph
;;
module For_docs = struct
let ml_filepath = [%here].pos_fname
end