diff options
author | Joris | 2020-03-03 10:44:35 +0100 |
---|---|---|
committer | Joris | 2020-03-03 10:44:35 +0100 |
commit | 5c636f11cdfed82634ee572645d765b704941b68 (patch) | |
tree | 51e11a0cfbbab284985e98fcb558d2975209a9b2 /src/Dom/CreateElement.ml | |
parent | a2880850a78fc36e2612215c83cbdeac0c980a5b (diff) |
Initialize views from JavaScript
Diffstat (limited to 'src/Dom/CreateElement.ml')
-rw-r--r-- | src/Dom/CreateElement.ml | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/src/Dom/CreateElement.ml b/src/Dom/CreateElement.ml new file mode 100644 index 0000000..8183a02 --- /dev/null +++ b/src/Dom/CreateElement.ml @@ -0,0 +1,72 @@ +(* Element creation *) + +let h tag ?(attributes = [||]) ?(eventListeners = [||]) ?(children = [||]) () : + Dom.element = + let element = + if tag == "svg" || tag == "path" then + Document.createElementNS "http://www.w3.org/2000/svg" tag + else Document.createElement tag + in + let () = + Js.Array.forEach + (fun (name, value) -> Element.setAttribute element name value) + attributes + in + let () = + Js.Array.forEach + (fun (name, eventListener) -> + Element.addEventListener element name eventListener) + eventListeners + in + let () = + Js.Array.forEach (fun child -> Element.appendChild element child) children + in + element + +(* Node creation *) + +let text = Document.createTextNode + +let div = h "div" + +let span = h "span" + +let header = h "header" + +let button = h "button" + +let section = h "section" + +let svg = h "svg" + +let path = h "path" + +let form = h "form" + +let label = h "label" + +let input_ = h "input" + +(* Attribute creation *) + +let id v = ("id", v) + +let className v = ("class", v) + +let viewBox v = ("viewBox", v) + +let d v = ("d", v) + +let type_ v = ("type", v) + +let min_ v = ("min", v) + +let value v = ("value", v) + +(* Event listeners *) + +let onClick f = ("click", f) + +let onInput f = ("input", f) + +let onSubmit f = ("submit", f) |