Shepmaster
Shepmaster

Reputation: 430466

Why does Elm.embed only accept divs?

If you attempt to use Elm.embed with an element other than a div, you get this error:

Elm.node must be given a DIV, not a BODY.

I can see that this fairly explicitly in the source. I'd like to understand why this restriction exists. Why am I unable to embed Elm inside of my body tag, for example?

Upvotes: 3

Views: 334

Answers (1)

robertjlooby
robertjlooby

Reputation: 7220

When you use Elm.embed you're saying "I'm giving full control over the contents of this element to this Elm module". Trying to embed it into the body would be the same as using Elm.fullscreen (one of the other ways to run an Elm app).

As for why you couldn't embed in any other tag (say a <span> or <p> tag), my guess is that it is just to be semantically correct and avoid any weird behavior. A <div> is just a block element that "defines a section in a document". A <span> tag has similar semantics ("defines a section in a document"), but is not a block element (what would it mean to display this Elm module as an inline element?). A <p> tag is a block element, but is meant to hold a paragraph of text. What would even happen if you tried to embed in a <canvas> or <iframe> element? You can avoid all that by just ensuring that the element the app is embedded in is a div.

Upvotes: 2

Related Questions