diff options
Diffstat (limited to 'src/domUtils.ml')
-rw-r--r-- | src/domUtils.ml | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/src/domUtils.ml b/src/domUtils.ml new file mode 100644 index 0000000..282ac12 --- /dev/null +++ b/src/domUtils.ml @@ -0,0 +1,37 @@ +open Webapi.Dom + +let toggleClassName (element : Dom.element) (className : string) : unit = + Element.setClassName element + (if Element.className element == className then "" else className) + +type child = TextChild of string | ElemChild of Dom.element + +let h (tag : string) (attributes : (string * string) Js.Array.t) + (children : child Js.Array.t) : Dom.element = + let element = Document.createElement tag document in + let () = + attributes + |> Js.Array.forEach (fun a -> Element.setAttribute (fst a) (snd a) element) + in + let () = + children + |> Js.Array.forEach (fun c -> + match c with + | TextChild t -> + Element.appendChild (Document.createTextNode t document) element + | ElemChild e -> Element.appendChild e element) + in + element + +external replace_child : Dom.node -> Dom.element -> Dom.element -> unit + = "replaceChild" + [@@bs.send] + +let replace (element : Dom.element) (replacement : Dom.element) : unit = + match Element.parentNode element with + | Some parent -> replace_child parent replacement element + | _ -> () + +external value : Dom.eventTarget -> string option = "value" [@@bs.get] + +external setValue : Dom.element -> string -> unit = "value" [@@bs.set] |