How to modify Html.Node in Elm

81 Views Asked by At

How would I go about modifying an existing Html.Node in Elm?

I would like to create a function with the following signature that would add an extra attribute to the input node.

  addAttribute : Html msg -> Attribute msg -> Html msg

Given

  node = Html.div [] [Html.text "dummy"]
  attr = Html.Attributes.style "display" : "inline-block"

a call to addAttribute node attr should return

  Html.div [Html.Attributes.style "display" "inline-block"] [Html.text "dummy"]
1

There are 1 best solutions below

4
pdamoc On BEST ANSWER

Html msg nodes are opaque by design. There is no way to access their structure and add attributes or children after they have been created.

You will need to find a different approach to building the node.

What I usually do is create some kind of Cfg msg record that is passed to some function that generates the node I need. I can then pass styles or optional children through that record.