Custom Elements Everywhere

Making sure frameworks and custom elements can be BFFs 🍻

What's this?

Custom Elements are the lynchpin in the Web Components specifications. They give developers the ability to define their own HTML elements. When coupled with Shadow DOM, Custom Elements should be able to work in any application. But things don't always work seamlessly.

This project runs a suite of tests against each framework to identify interoperability issues, and highlight potential fixes already implemented in other frameworks. If frameworks agree on how they will communicate with Custom Elements, it makes developers' jobs easier; they can author their elements to meet these expectations.

Custom Elements and Shadow DOM don't come with a pre-defined set of best practices. The tests in this project are a best guess as to how things should work, but they're by no means final. This project is also about driving discussion and finding consensus, so don't be afraid to open a GitHub issue to discuss places where the tests could be improved. ✌️

Library

Angular 6.0.1

Score

100%

Basic Tests

16/16

Advanced Tests

14/14

100%

Handling data

Angular's default binding syntax will always set properties on an element. This works well for rich data, like objects and arrays, and also works well for primitive values so long as the Custom Element author has mapped any exposed attributes to corresponding properties.

Angular also provides binding syntax specifically for setting an attribute, if a developer would prefer to communicate with an element that way.

Handling events

Angular components can listen to native DOM events dispatched from Custom Elements. It supports all styles of events (lowercase, camelCase, kebab-case, etc).

Related Issues

Yay! No open issues!

Library

AngularJS (1.x) 1.6.8

Score

71%

Basic Tests

16/16

Advanced Tests

0/14

71%

Handling data

AngularJS can declaratively pass data to attributes using ng-attr. This works well for primitive data (booleans, numbers, strings), but does not work for complex data like objects or arrays. Passing complex data would need to be manually wired up in JavaScript.

Handling events

AngularJS can listen to native DOM events imperatively, by selecting the element and adding an .on() event handler. Declarative event handling is not supported.

Library

DIO

Score

91%

Basic Tests

16/16

Advanced Tests

8/14

91%

Handling data

DIO uses a runtime heuristic to determine if it should pass data to Custom Elements as either properties or attributes. If a property is already defined on the element instance, DIO will use properties, otherwise it will fallback to attributes. The exception to this rule is when it tries to pass rich data, like objects or arrays. In those instances it will always use a property.

Handling events

DIO can listen to native DOM events dispatched from Custom Elements. However, it uses a heuristic to convert JSX event binding syntax into event names, and always lowercases the events. For example onFooUpdated={handleFoo} tells DIO to listen for an event called 'fooupdated'. This means DIO can support events with lowercase and kebab-case names, but not camelCase, PascalCase, or CAPScase events (e.g. 'URLchanged').

Library

Dojo 2 2.0.0-beta3.1

Score

100%

Basic Tests

16/16

Advanced Tests

14/14

100%

Handling data

Dojo 2 will pass data as attributes only when the data is a type of string, otherwise it is set as a property.

Handling events

Dojo 2 can listen to native DOM events dispatched from Custom Elements. However the event names must be prefixed with on, so a Custom Event of camelEvent would be oncamelEvent. Other than that, Dojo 2 supports all kinds of event names.

Related Issues

Yay! No open issues!

Library

hybrids ^1.1.4

Score

100%

Basic Tests

16/16

Advanced Tests

14/14

100%

Handling data

Hybrids will pass data to an element as properties, as long as the property is defined on the element's prototype. Otherwise it will fallback to passing data as attributes.

Handling events

Hybrids can listen to native DOM events dispatched from Custom Elements. It supports all styles of events (lowercase, camelCase, kebab-case, etc).

Related Issues

Yay! No open issues!

Library

hyperHTML 2.5.10

Score

100%

Basic Tests

16/16

Advanced Tests

14/14

100%

Handling data

hyperHTML will pass data to an element as properties, as long as the property is defined on the element's prototype. Otherwise it will fallback to passing data as attributes.

Handling events

hyperHTML can listen to native DOM events dispatched from Custom Elements. It supports all styles of events (lowercase, camelCase, kebab-case, etc).

Related Issues

Yay! No open issues!

Library

Polymer 2.5.0

Score

91%

Basic Tests

16/16

Advanced Tests

8/14

91%

Handling data

Polymer will always attempt to pass data to an element using properties. To explicitly set an attribute, Polymer provides additional syntax in the form of the $= annotation.

Handling events

Polymer supports listening to DOM events using the on-* attribute syntax. It does not support arbitrarily capitalized event names (camelCase, CAPSCase, PascalCase, etc.). This is because Polymer reads the event name directly from the HTML attribute, and the HTML parser will always lowercase attribute names.

You can read more about this issue and why we test it in the FAQ.

Library

Preact 8.2.7

Score

91%

Basic Tests

16/16

Advanced Tests

8/14

91%

Handling data

Preact uses a runtime heuristic to determine if it should pass data to Custom Elements as either properties or attributes. If a property is already defined on the element instance, Preact will use properties, otherwise it will fallback to attributes. The exception to this rule is when it tries to pass rich data, like objects or arrays. In those instances it will always use a property.

Handling events

Preact can listen to native DOM events dispatched from Custom Elements. However, it uses a heuristic to convert JSX event binding syntax into event names, and always lowercases the events. For example onFooUpdated={handleFoo} tells Preact to listen for an event called 'fooupdated'. This means Preact can support events with lowercase and kebab-case names, but not camelCase, PascalCase, or CAPScase events (e.g. 'URLchanged').

Library

React 16.2.0

Score

71%

Basic Tests

16/16

Advanced Tests

0/14

71%

Handling data

React passes all data to Custom Elements in the form of HTML attributes. For primitive data this is fine, but the system breaks down when passing rich data, like objects or arrays. In these instances you end up with stringified values like some-attr="[object Object]" which can't actually be used.

Handling events

Because React implements its own synthetic event system, it cannot listen for DOM events coming from Custom Elements without the use of a workaround. Developers will need to reference their Custom Elements using a ref and manually attach event listeners with addEventListener. This makes working with Custom Elements cumbersome.

Library

Skate w/ Preact 5.1.1

Score

91%

Basic Tests

16/16

Advanced Tests

8/14

91%

Handling data

Skate lets you use a variety of different rendering engines (Preact, React, lit-html). Most Skate apps these days use Preact, so Skate + Preact should pass data primarily using properties, and only fall back to attributes if a property is not defined.

Handling events

Skate's declarative event handling is defined by the rendering engine used. If you're using Skate + Preact then it will support events with lowercase and kebab-case names, but not camelCase, PascalCase, or CAPScase events (e.g. 'URLchanged').

Library

Surplus

Score

100%

Basic Tests

16/16

Advanced Tests

14/14

100%

Handling data

Surplus passes data to an element via properties unless the indicated field is known to be available only as an attribute (aria-*, some SVG attributes).

Handling events

By default, event handlers are registered in Surplus by setting the node.on... DOM properties: <div onclick={...}></div>. For custom events, which don't have such properties, Surplus uses surplus-mixin-on: <div fn={on('my-custom-event', ...)}></div>.

Related Issues

Yay! No open issues!

Library

Svelte 2.5.1

Score

88%

Basic Tests

16/16

Advanced Tests

10/14

88%

Handling data

Svelte passes all data to Custom Elements in the form of HTML attributes. For primitive data this is fine, but the system breaks down when passing rich data, like objects or arrays. In these instances you end up with stringified values like some-attr="[object Object]" which can't actually be used.

Handling events

Svelte can listen to native DOM events dispatched from Custom Elements. It supports all styles of events (lowercase, camelCase, kebab-case, etc).

Library

Vue 2.5.13

Score

100%

Basic Tests

16/16

Advanced Tests

14/14

100%

Handling data

By default, Vue passes all data to Custom Elements as attributes. However, Vue also provides syntax to instruct its bindings to use properties instead. To bind to a Custom Element property use :foo.prop="bar".

Handling events

Vue can listen to native DOM events dispatched from Custom Elements. It supports all styles of events (lowercase, camelCase, kebab-case, etc).

Related Issues

Yay! No open issues!

Frequently Asked Questions

What are these custom element things?

Custom elements are a new web standard which let developers create their own HTML Elements. Because they're based on web standards, these elements should work on any page. This means, you can write a component, like a datepicker, and share it everywhere.

To learn more, check out the custom elements primer and best practices guide.

Are you testing that libraries let you author custom elements?

No. These tests just check that a library/framework supports the usage of custom elements. Essentially we're trying to answer the question: "If you're building an app in framework X, and you'd like to include a few custom elements on the page, are you going to have a bad time?"

The tests check that the library/framework will let you do things like display a custom element, bind data to it, pass in children, and listen for events.

Why is each test counted twice?

Each test is run on a browser with native support for all of the web components standards (template, shadow DOM, custom elements) and on a browser with polyfilled support. It's possible that a test may work in the native setting and not in the polyfilled one, or vice versa. To account for this, we count both environments as unique tests.

How are the libraries scored?

Each test has an associated weight, based on how critical it is. The final tally of pass/fails is combined with these weights to create a weighted average score.

How are basic and advanced tests different?

Basic tests cover things which are fundamental to a library/framework's ability to display a custom element. For example, can it display a custom element that contains shadow DOM? Can it handle setting attributes on the custom element? Can it listen for DOM events from the element? Failing any of these tests is a pretty critical issue.

Advanced tests cover more opinionated framework features. For example, does the framework provide declarative syntax for listening to events with different casing styles (kebab-case, camelCase, etc). These are more like "nice to haves" that may improve the developer experience.

I thought the whole point of Polymer was to write custom elements. Why doesn't it get a 100%?

Polymer supports a non-standard feature called declarative event binding, which lets you use attributes to wire up event listeners. E.g. <my-element on-foo="handleFoo">. Because DOM events are just strings, there are no rules governing how they should be formatted or capitalized—"my-event" is just as reasonable as "myEvent" or "myevent". Even the web platform has a few examples of oddly cased events like DOMContentLoaded. Because Polymer's implementation of declarative event bindings relies on pulling the event name from the on-* attribute, and the HTML parser will always lowercase attribute names, it is unable to listen for events with capital letters in their names.

Since it is entirely possible to write a vanilla custom element that dispatches an event with a capital letter in its name, and because there is prior art in the platform that actually uses this technique for event names with acronyms ("DOM"), we feel it is imporant to test this.

Libraries like Preact also fail these capitalization tests, which points to a possible best practice of always making event names lowercase. This is the style most DOM events already use: mousedown, popstate, beforeunload, etc.

If a library is missing a feature, like declarative event or property bindings, does it automatically fail those tests?

Not necessarily. If a library omits a non-standard feature aimed at developer ergonomics, e.g. declarative event/property bindings, we would just omit those tests from the scoring process.

Why don't you have tests for _____ library?

We'd like to have as much test coverage as possible, but it's a fair bit of work building each test suite (especially because we are not experts in every library). If a framework or library is not represented it's just because we haven't had a chance to write tests for it yet.

I want to write some tests for _____ library. Do you accept pull requests?

Yes! In fact, >50% of the tests on this site are from external contributors.

If you'd like to contribute, please first open an issue saying you'd like to write some tests for a specific library/framework. This helps ensure that there's not more than one person writing tests for the same library.

Custom Elements Everywhere by Rob Dodson. Licensed under Apache 2.0.