Skip to content

[Pivots] customElements.get/whenDefined should not return PivotConstructor #3073

@nolanlawson

Description

@nolanlawson

Description

This is follow-up work for custom element registry pivots (#2724).

After we call lwc.createElement('x-foo'), customElements.get('x-foo') will return the PivotCtor (which is useless without the UserCtor – it gives you a "dummy" custom element). This matches LWC behavior pre-pivots.

However, this may break a third-party component using this common pattern:

if (!customElements.get('my-tag')) {
  customElements.define('my-tag', ...)
}

The above will break if LWC reserves my-tag first. So get and whenDefined should return undefined in this case, not the PivotCtor.

Note

However, it's worth acknowledging that even if we fix customElements.get and customElements.whenDefined to return undefined rather than the PivotCtor, we still can't fix the following case:

// LWC code
lwc.createElement('x-foo', ...)

// userland code
const elm = document.createElement('x-foo')
const PivotCtor = elm.constructor
new PivotCtor()

So in a sense, we can't fully hide the PivotCtor. But we can potentially hide it from customElements.get and customElements.whenDefined at least.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions