Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do Custom Elements / WebComponents work with SVG?

For CustomElements I can do this:

class MyElement extends HTMLElement {}

Can I do the same for SVG Elements? like

class MyOwnRectangle extends SVGRectElement {}

If it works, can I trouble someone for a jsbin?

If not, why not?

This has some very good use cases like being able to store model data in the element itself. SVG manipulation is basically used in 2 major areas:

  1. Animations
  2. Diagrams like ER, Organisation Charts, Process flows etc.

A library like JointJS has its own abstractions in the form of classes (which extends a Backbone View) to store model data associated with the view (SVG diagrams on screen).

Like WebComponents can eventually replace jQuery Plugins, Framework X's components in a standard way, why not do the same for SVG?

Finally,

How is Polymer 0.5 able to do this?

Does Polymer.js support inner SVG elements?

How is this patch able to do this in Polymer 1.0?

https://github.com/Polymer/polymer/pull/3372

What is the alternative for now?

I guess we can extend HTMLElement. In our ShadowDOM use an <svg> element and then attach all the SVG tags we actually want to extend like <rect>, <polyline> etc! Perhaps a much cleaner way out of this?

Will this be implemented in the future?

like image 680
Gaurav Ramanan Avatar asked Nov 05 '16 08:11

Gaurav Ramanan


People also ask

Can I use custom HTML elements?

We can create custom HTML elements, described by our class, with its own methods and properties, events and so on. Once a custom element is defined, we can use it on par with built-in HTML elements. That's great, as HTML dictionary is rich, but not infinite.

What is Web Components custom elements?

Custom elements allow web developers to define new HTML tags, extend existing ones, and create reusable web components.

Are Web Components good?

The benefits of the Web Components. Web Components allow you to create reusable components based on HTML, JavaScript and CSS, separating their functionality and layout from the rest of the code, so it's a good encapsulation tool.

Will Web Components replace React?

I think the answer is "yes" - Web Components will replace UI frameworks like React.


1 Answers

According to the specification

If result’s namespace is not the HTML namespace, then throw a NotSupportedError.

All SVG elements are in the SVG namespace. So the speification precludes support.

Ways to store data in SVG include the the <metadata> tag and data- attributes.

like image 184
Robert Longson Avatar answered Oct 09 '22 19:10

Robert Longson