From 4ee0dfae75fda3a8b6347d55c728b50ce5c210d9 Mon Sep 17 00:00:00 2001 From: Joris Date: Sun, 26 Jul 2020 18:16:59 +0200 Subject: Allow to customize icons --- src/Lib/Dom/H.ml | 49 +++++++++++++++++++++---------------------------- 1 file changed, 21 insertions(+), 28 deletions(-) (limited to 'src/Lib/Dom/H.ml') diff --git a/src/Lib/Dom/H.ml b/src/Lib/Dom/H.ml index 8183a02..d547a70 100644 --- a/src/Lib/Dom/H.ml +++ b/src/Lib/Dom/H.ml @@ -1,7 +1,10 @@ (* Element creation *) -let h tag ?(attributes = [||]) ?(eventListeners = [||]) ?(children = [||]) () : - Dom.element = +type attribute = + | TextAttr of string * string + | EventAttr of string * (Dom.event -> unit) + +let h tag attributes children = let element = if tag == "svg" || tag == "path" then Document.createElementNS "http://www.w3.org/2000/svg" tag @@ -9,17 +12,19 @@ let h tag ?(attributes = [||]) ?(eventListeners = [||]) ?(children = [||]) () : in let () = Js.Array.forEach - (fun (name, value) -> Element.setAttribute element name value) + (fun attr -> + match attr with + | TextAttr (name, value) -> + Element.setAttribute element name value + + | EventAttr (name, eventListener) -> + Element.addEventListener element name eventListener) 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 + (fun child -> Element.appendChild element child) + children in element @@ -45,28 +50,16 @@ 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 input = h "input" -let min_ v = ("min", v) +let textarea = h "textarea" -let value v = ("value", v) +let i = h "i" -(* Event listeners *) +let a = h "a" -let onClick f = ("click", f) +let h1 = h "h1" -let onInput f = ("input", f) +let h2 = h "h2" -let onSubmit f = ("submit", f) +let h3 = h "h3" -- cgit v1.2.3