diff options
author | Joris | 2020-02-13 18:54:18 +0100 |
---|---|---|
committer | Joris | 2020-02-15 10:59:25 +0100 |
commit | 7a01b001ccc4a7bda3da92486903540e3f9754fd (patch) | |
tree | fbe269faa319fd7f21049612f8a62c0c169f369e /src/domUtils.ml | |
parent | 8328c11ac6706e3adf9b09877c02897132b8927a (diff) |
Set up bucklescript
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] |