summaryrefslogtreecommitdiff
path: root/lib/elements
diff options
context:
space:
mode:
authorSébastien Dailly <sebastien@dailly.me>2022-02-07 15:49:09 +0100
committerSébastien Dailly <sebastien@dailly.me>2022-02-07 16:22:43 +0100
commit6cfd03b483b5edd809b7c94d74df2eb93ffa70aa (patch)
tree2f994a1ccac72d757a7572661eadca082b71f183 /lib/elements
parent52cbf99e0db1c0fb7b44f4101c6a673d9ec1fbbe (diff)
Added popup element
Diffstat (limited to 'lib/elements')
-rwxr-xr-xlib/elements/popup.ml79
1 files changed, 79 insertions, 0 deletions
diff --git a/lib/elements/popup.ml b/lib/elements/popup.ml
new file mode 100755
index 0000000..e2e2a76
--- /dev/null
+++ b/lib/elements/popup.ml
@@ -0,0 +1,79 @@
+open Brr
+open Brr_note
+module Js = Js_of_ocaml.Js
+
+let create
+ : ?form:('a Note.signal * El.t) option -> title:Jstr.t -> 'a option Note.event
+ = fun ?(form = None) ~title ->
+
+ (* Ensure we keep a track for the signal event.
+
+ This looks a bit like the old memory magment in C, as it require to
+ destroy the logger each time the popup is removed. *)
+ let log_opt = Option.map
+ (fun (values, _) -> Note.S.log values (fun _ -> ()))
+ form in
+
+ let close_btn =
+ El.span
+ ~at:At.[class' (Jstr.v "modal-close")]
+ [ El.txt' "×"]
+
+ and submit_btn = El.input ()
+ ~at:At.[type' (Jstr.v "submit")] in
+
+ let container = match form with
+ | None -> El.div
+ | Some _ -> El.form
+
+ and body = match form with
+ | None -> El.div []
+ | Some (_, content) -> content
+
+ and footer = match form with
+ | None -> El.txt Jstr.empty
+ | Some _ ->
+
+ El.div [ submit_btn ]
+ ~at:At.[class' (Jstr.v "row")] in
+
+ (* HTML Element creation *)
+ let el = El.div
+ ~at:At.[class' (Jstr.v "modal")]
+ [ container
+ ~at:At.[class' (Jstr.v "modal-content")]
+ [ El.div
+ ~at:At.[class' (Jstr.v "modal-header")]
+ [ close_btn
+ ; El.h3
+ [ El.txt title ]]
+ ; El.div
+ ~at:At.[class' (Jstr.v "modal-body")]
+ [ body ]
+ ; El.div
+ ~at:At.[class' (Jstr.v "modal-footer")]
+ [ footer ]]] in
+
+ let () = El.append_children (Document.body G.document)
+ [ el ] in
+
+ (* Event handler *)
+ let close_event = Evr.on_el
+ Ev.click
+ (fun _ ->
+ El.remove el;
+ Option.iter Note.Logr.destroy log_opt;
+ None)
+ close_btn
+
+ and submit_event = Evr.on_el
+ Ev.click
+ (fun _ ->
+ El.remove el;
+ Option.iter Note.Logr.destroy log_opt;
+ Option.map (fun v -> Note.S.value (fst v)) form)
+ submit_btn in
+
+ Note.E.select
+ [ close_event
+ ; submit_event ]