-
Notifications
You must be signed in to change notification settings - Fork 386
Description
Refs:
In the light of discussing Scoped Custom Element Registries, some folks, including @caridy, @justinfagnani, and I, believe it might be interesting to design a method for multiple elements definition.
This would allow us to define a batch of components within a single method call.
customElements.define<bulk>({ // naming TBD
'x-foo': Foo,
'x-bar': Bar,
});
// equivalent to
customElements.define('x-foo', Foo);
customElements.define('x-bar', Bar);
Existing Feedback
From @justinfagnani:
This would be a welcome feature even for the global registry. We've had users hit problems when trying to define multiple elements that interact (ie. children firing events to register with parents). They end up being sensitive to the registration order. There's also a performance concern with multiple tree walks to register multiple elements. Bulk registration would help with both.
Compatibility
Unfortunately, we cannot simply modify the existing CustomElementRegistry#define()
without breaking changes. Taking this, we might need a new method, using a new name, without replacing .define()
, which remains designed for single definitions.
Today, define
casts the first argument to string and this also applies to object values:
customElements.define({
toString() {
return 'my-element';
}
},
MyElement
);
// similar to
customElements.define('my-element', MyElement);
Naming Bikeshed
Naming is hard, and this idea is not an exception.
For now I can think of .makeDefinitions
, .defineBulk
, .defineMultiple
, .defineMany
, but I'd love better suggestions out of not being a big fan of any of the current ideas.
Alternative
We could just modify .define
to not cast the first argument to string if the method is called with only a single parameter.