Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[question] Why are we unable to build web components from SVG elements? #650

Closed
trusktr opened this issue Jul 22, 2017 · 4 comments
Closed

Comments

@trusktr
Copy link

trusktr commented Jul 22, 2017

Suppose I'm making a simple SVG game. I want to make custom elements with shadow roots that consist of SVG primitives (<rect>, <circle>, etc). The result would be like this:

<svg>
  <redwood-tree position="10 20"></redwood-tree>
  <player-tank position="30 50"></player-tank>
  <enemy-tank position="50 100"></enemy-tank>
  <!-- ... etc ... -->
</svg>

where each one of those elements could be defined as

customElements.define('player-tank', class extends SVGRectElement {
  constructor() {
    this.root = this.attachShadow({mode:'closed'})
    this.root.innerHTML = `<rect width="10" height="10"></rect>`
    // ... some more code to map `position` attribute to `x/y` attributes ...
  }
})

Demo (nothing happens): https://jsfiddle.net/pem90k9a (but if you try to place the player-tank element outside of an SVG tree, then it will say Uncaught TypeError: Illegal constructor anyways since we've extended SVGRectElement.

Why is SVG not component-friendly?

@trusktr
Copy link
Author

trusktr commented Jul 22, 2017

A solution is to use <div>s, and each item (player-tank, enemy-tank, etc) can be a separate <svg> tree, but anyways it would still be intuitive for it to just work (rather than resulting in nothing).

@trusktr trusktr changed the title [bug] Unable to build web components for use in an SVG context [question] Why are we unable to build web components from SVG elements? Jul 22, 2017
@annevk
Copy link
Collaborator

annevk commented Jul 22, 2017

We'd have to modify the parser even more. So we opted not to do that to reduce complexity.

@trusktr
Copy link
Author

trusktr commented Sep 4, 2017

It just needs to look at the composed tree. The rendering should be decoupled from parsing, it would be proper. Idk how complex that is, but it seems worthwhile.

@Joebeazelman
Copy link

We'd have to modify the parser even more. So we opted not to do that to reduce complexity.

Great design and development ethos! Push the complexity on the developers and end-users!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants