Helpers for DOM node types as part of an Interface.
These are primitives used for svg and html.
Compare with elm/virtual-dom
Create a DOM element with a given namespace, tag, Modifiers and sub-nodes.
For example, Web.Svg defines its elements using
element : String -> List (Modifier state) -> List (DomNode state) -> DomNode state
element tag modifiers subs =
Web.Dom.elementNamespaced "http://www.w3.org/2000/svg" tag modifiers subsSet the behavior of a Web.Dom.element.
To create one, use attribute, style, listenTo.
To combine multiple, use Web.Dom.modifierBatch and Web.Dom.modifierNone
For example to get <a href="https://elm-lang.org">elm</a>
Web.Dom.element "a"
[ Web.Dom.attribute "href" "https://elm-lang.org" ]
[ Web.Dom.text "elm" ]Btw: If you can think of a nicer name for this like "customization", "characteristic" or "aspect", please open an issue!
A namespaced key-value attribute Modifier.
For example, you could define an SVG xlink href attribute as
attributeXlinkHref : String -> Modifier msg
attributeXlinkHref value =
Web.Dom.attributeNamespaced "http://www.w3.org/1999/xlink" "xlink:href" valueListen for a specific DOM event on the DomElement.
Use modifierMap to wire this to a specific event.
If you want to override the browser's default behavior for that event,
use listenToPreventingDefaultAction
Like listenTo but preventing the browser's default action.
That's for example how elm's Browser.Events.onSubmit
prevents the form from changing the page’s location:
submitListen : Web.Dom.Modifier ()
submitListen =
Web.Dom.listenToPreventingDefaultAction "submit"
|> Web.Dom.modifierMap (\_ -> ())Wire events from this Modifier to a specific event.
Web.Dom.listen "click" |> Web.Dom.modifierMap (\_ -> ButtonClicked)Combine multiple Modifiers into one.
Doing nothing as a Modifier. These two examples are equivalent:
Web.Dom.modifierBatch
[ a, Web.Dom.modifierNone, b ]and
Web.Dom.modifierBatch
(List.filterMap identity
[ a |> Just, Nothing, b |> Just ]
)Wire events from this DomNode to a specific event.
buttonUi "start"
|> Web.Dom.map (\Clicked -> StartButtonClicked)with e.g.
buttonUi : List (Web.DomNode ()) -> Web.DomNode ButtonEvent
buttonUi subs =
Web.Dom.element "button"
[ Web.Dom.listenTo "click"
|> Web.Dom.modifierMap (\_ -> Clicked)
]
[ Web.Dom.text label ]
type ButtonEvent
= Clicked
An
Interfacethat listens for a specificdocumentevent like like keypress, keydown, keyup, click, mousemove, mousedown, mouseup