-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtooltip.ml
50 lines (45 loc) · 1.33 KB
/
tooltip.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
open Js_browser
open Bindings
type options = Popper.options
type Vdom.Custom.t += Popper of options
let handler ctx options =
let instance = ref None in
let disposed = ref false in
let parent = Vdom_blit.Custom.parent ctx in
let options = ref options in
Vdom_blit.Custom.after_redraw ctx (fun () ->
if not !disposed then begin
let target = Element.parent_node parent in
let inst = Popper.create target parent (Some !options) in
instance := Some inst;
Console.log console (Popper.t_to_js inst)
end);
let dispose () =
disposed := true;
Option.iter Popper.destroy !instance
in
let sync ct =
match ct with
| Popper new_options ->
if new_options <> !options then begin
options := new_options;
Option.iter
(fun inst -> Popper.set_options inst new_options)
!instance
end;
true
| _ -> false
in
let elt = Js_browser.(Document.create_text_node document "") in
Vdom_blit.Custom.make ~dispose ~sync elt
let () =
let f ctx attr =
match attr with Popper value -> Some (handler ctx value) | _ -> None
in
Vdom_blit.(register (custom f))
let tooltip ?placement content =
Vdom.elt "div"
(Vdom.custom ~key:"popper-instance"
(Popper { placement; strategy = Some Absolute })
:: content
)