Skip to content

Permit retrieval of registered classes from window.customElements #445

@JanMiksovsky

Description

@JanMiksovsky

Following up on issue #431, I'd like to propose that window.customElements allow a dev to retrieve information about the custom elements which have been registered. It sounds like @domenic was supportive of this idea, so as a strawman, I propose adding the following:

  • window.customElements.get(name). Returns the class which was previously registered with define(name), or undefined if no element has yet been registered with that name.
  • window.customElements[@@iterator](). This returns a new Iterator object that enumerates the [name, class] pairs registered. I don't particularly care in what order the entries are returned. For consistency with Map.prototype[@@iterator](), it would be nice to return these in the order in which they were defined.

This, at least would meet the needs I've encountered, chiefly being able to: 1) easily verify that a particular custom element has in fact been registered, 2) conditionally define an element if it hasn't already been registered. E.g.:

if (!window.customElements.get('my-element')) {
  window.customElements.define('my-element', MyElement);
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions