[w3c/webcomponents] [Proposal] customElements#hasLocalName(Class) (#714)

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

```js
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.
1. 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.

```js
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.

```js
typeof customElements.getLocalName(Class) === 'string' || undefined
```

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/w3c/webcomponents/issues/714

Received on Saturday, 2 December 2017 11:58:00 UTC