Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Proposal] customElements#hasLocalName(Class) #714

Closed
thysultan opened this issue Dec 2, 2017 · 1 comment
Closed

[Proposal] customElements#hasLocalName(Class) #714

thysultan opened this issue Dec 2, 2017 · 1 comment

Comments

@thysultan
Copy link

I could not find any documentation on a method to check whether a particular class has defined its localName customElement.define(localName, Class).

customElements.hasLocalName

typeof customElements.hasLocalName(Class) === 'boolean'

This is useful to frameworks that are built to work with/on top of web components in 2 ways that tie into each other.

  1. They can validate and error if a particular class has not defined a localName before an error is thrown from instantiating the class.
  2. They can auto generate localNames at mount time(when found to be undefined)

The ability to know whether a component has defined a localName allows framework authors to create API's that can consume web component classes in a way that relegates the need to define a components localName when declaring your component to being optional.

class WebComponent extends HTMLElement {}

framework.render(
  <div>
    <WebComponent />
    <FrameworkComponent />
  </div>
);

Current Landscape.

One possible way to support this pattern is for frameworks to wrap the mount/instantiating in a try-catch, and auto-generate a localName from the classes name in the catch block when instantiating throws an error.

A second method might be to auto-generate an obscure localName regardless of if one has been defined outside of what the framework can pick up from.

Alternative API

The problems stated above could be solved in a similar fashion to customElements.hasLocalName by exposing an API that returns a components defined localName.

typeof customElements.getLocalName(Class) === 'string' || undefined
@rniwa
Copy link
Collaborator

rniwa commented Dec 3, 2017

This is a duplicate of #566.

@rniwa rniwa closed this as completed Dec 3, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants