aboutsummaryrefslogtreecommitdiff
path: root/src/Lib/Dom/H.ml
blob: d547a708f01a326b887822fd50d827a1c8eb060b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
(* Element creation *)

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
    else Document.createElement tag
  in
  let () =
    Js.Array.forEach
      (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 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"

let textarea = h "textarea"

let i = h "i"

let a = h "a"

let h1 = h "h1"

let h2 = h "h2"

let h3 = h "h3"