You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
registry: Add an API to retrieve the localName given a custom element constructor #566
I've read the spec and I can't find any mention of how to get the localName of an element from the constructor. In v0 (I know, don't rely on old Blink) you could:
I can see why this is but I feel there's use cases for this. For example, when exporting a custom element constructor, consumers may need to know the name that it got registered with. In https://github.com/webcomponents/react-integration, we use the registered name in order to tell React the element it should create in the virtual DOM. Currently we have to construct the component and get the tagName from it.
A proposal for this may be to add a method to CustomElementRegistry which retrieves the name of the element when passed a constructor:
// any getLocalName(Function constructor)window.customElements.getLocalName(Ctor);
It could return null if not found, or the localName if found.
Thoughts?
Reacted by Darryl Yeo, Rhy Moore, Marvin Hagemeister, South Drifter, Owen Buckley and David Kudera
There is no way to do this. As you say, constructing the component and retrieving the .localName from it is the best way currently.
It sounds like the one use case you've provided so far is working around the fact that React cannot use normal element constructors, i.e. it insists on using createElement? Are there any other use cases that aren't working around framework-specific limitations?
Yeah, React needs the local name so it can do it within the patching algorithm.
In Skate we use Incremental DOM and though it leverages the actual DOM, we still have to tell it the localName of the element to create. This information we currently store internally and pass on.
In the same library (Skate), we also offer a way to auto-generate unique tag names. This is useful if you have two versions of the same component arriving on the same page, for whatever reason. It could be someone is slowly upgrading to the new version of your component, Webpack HMR, writing tests without ensuring the component name is unique (because you can't unregister in a teardown step), etc. While this is trivial to do at the library level - and should be done at the library level - it'd be nice for integrations if there was a standard way to retrieve the localName the constructor was registered for, that way component consumer's don't have to rely on the library the component was written in to do so. To them it's just a web component, as opposed to a Skate component.
Piggy-backing off that, a more compelling use-case may be for libraries like iDOM or React to be able to check internally that if a function is passed (React already supports this as stateless functions), to see if it's a custom element constructor (maybe Func.prototype instanceof HTMLElement or something). If so, then it could do customElements.getLocalName(Func) and then do the diffing and patching. Patching could be done using the constructor if a new element is needed, however, you probably wouldn't want to create a new element to get the localName just to diff element types. Furthermore, instanceof probably wouldn't be appropriate here because Func could be a subclass of the element being compared (and this is assuming they even use real DOM nodes - iDOM does, but I don't think React and other implementations do).
There's probably other use-cases, but those are definitely ones close to our domain that we've encountered and some we use on a daily basis.
We could add something like getName on document.customElements that gives you the name of the custom element given the constructor, which you can obtain via new.target.
My proposal would be renaming the existing get to getInterface and add getName to make the semantics clear. We could even call them findInterface and findName as well.
The use cases still aren't clear to me; they seem very speculative, relating to things that maybe future versions of some libraries might choose to do, or to things that can already be done at the library level.
I don't agree that all my use cases were speculative. If we don't have a
standardised way to retrieve a tag name for a constructor, it forces us to
store that information locally. Since part of our API is to have multiple
working versions on the page, we then force internal APIs to never change
else we break that contract. This would be a really nice feature for us to
have.
The use cases still aren't clear to me; they seem very speculative,
relating to things that maybe future versions of some libraries might
choose to do, or to things that can already be done at the library level.
I don't agree that all my use cases were speculative. If we don't have a
standardised way to retrieve a tag name for a constructor, it forces us to
store that information locally. Since part of our API is to have multiple
working versions on the page, we then force internal APIs to never change
else we break that contract. This would be a really nice feature for us to
have.
The use cases still aren't clear to me; they seem very speculative,
relating to things that maybe future versions of some libraries might
choose to do, or to things that can already be done at the library level.
I echo @domenic's sentiment. I had a chat with @treshugart offline and suggested changing Incremental DOM to allow it to be agnostic to the element name when given a constructor.
(It seems like it should be able to do a elem.constructor === Ctor check when diffing the DOM, rather than relying on some elem.tagName === getName(Ctor) logic.
Yeah, bouncing ideas off @bradleyayers helped. Though, maybe we're missing something with the theoretical implementation. I know with some built-ins this isn't reliable:
I'm doing some experimentation and if you have access to the diff algorithms, this method works. However, at the moment I'm looking for a way to support different virtual DOM libraries - sort of like a custom element / shadow DOM abstraction - and I'd like to be able to tell the virtual element functions to create an element for a given constructor. Unless all the virtual DOM libraries support doing elem.constructor === Ctor this doesn't seem possible without having a standardised way to get the name that a particular constructor was registered with. Any ideas?
One risk here for v1 is that we have customElements.get and if we're adding a new get method that returns the name given a constructor, then we'll have customElements.get and customElement.getName (or some other name) which would be quite inconsistent. Alternatively, we can make get return a name or a constructor based on argument but that seems rather odd.
We pretty regularly get requests to provide a way to get a tag name from a constructor in Lit, and I'm pretty reluctant to add anything because of how specific to Lit it would be. Any movement here?
If I'm reading the last set of comments correctly, an addition of customElements.getName() would have support from Chromium and WebKit. Someone would have to do the specification and testing work required though. (This would not return the local name, but the name of a custom element, to be clear. I doubt we would get consensus on local name given that WebKit does not support the is attribute.)
I think Gecko can also be supportive of this. (I also agree with @domenic now that overloading this with get() is weird. Not sure why I advocated for that in the past…)
Reacted by Ryosuke Niwa, Justin Fagnani and James Browning
It seems like there may be rough consensus here now on a new method customElements.getName(), am I reading the thread correctly? Do we have anyone with time to write up the spec change and make WPT tests?
changed the title [-]Is there a way to retrieve the localName from a custom element constructor?[/-][+]registry: Is there a way to retrieve the localName from a custom element constructor?[/+]on Apr 20, 2023
changed the title [-]registry: Is there a way to retrieve the localName from a custom element constructor?[/-][+]registry: Add an API to retrieve the localName given a custom element constructor[/+]on Apr 20, 2023
Activity
domenic commentedon Sep 14, 2016
There is no way to do this. As you say, constructing the component and retrieving the
.localNamefrom it is the best way currently.It sounds like the one use case you've provided so far is working around the fact that React cannot use normal element constructors, i.e. it insists on using createElement? Are there any other use cases that aren't working around framework-specific limitations?
treshugart commentedon Sep 15, 2016
Yeah, React needs the local name so it can do it within the patching algorithm.
In Skate we use Incremental DOM and though it leverages the actual DOM, we still have to tell it the
localNameof the element to create. This information we currently store internally and pass on.In the same library (Skate), we also offer a way to auto-generate unique tag names. This is useful if you have two versions of the same component arriving on the same page, for whatever reason. It could be someone is slowly upgrading to the new version of your component, Webpack HMR, writing tests without ensuring the component name is unique (because you can't unregister in a teardown step), etc. While this is trivial to do at the library level - and should be done at the library level - it'd be nice for integrations if there was a standard way to retrieve the
localNamethe constructor was registered for, that way component consumer's don't have to rely on the library the component was written in to do so. To them it's just a web component, as opposed to a Skate component.Piggy-backing off that, a more compelling use-case may be for libraries like iDOM or React to be able to check internally that if a function is passed (React already supports this as stateless functions), to see if it's a custom element constructor (maybe
Func.prototype instanceof HTMLElementor something). If so, then it could docustomElements.getLocalName(Func)and then do the diffing and patching. Patching could be done using the constructor if a new element is needed, however, you probably wouldn't want to create a new element to get thelocalNamejust to diff element types. Furthermore,instanceofprobably wouldn't be appropriate here becauseFunccould be a subclass of the element being compared (and this is assuming they even use real DOM nodes - iDOM does, but I don't think React and other implementations do).There's probably other use-cases, but those are definitely ones close to our domain that we've encountered and some we use on a daily basis.
treshugart commentedon Sep 15, 2016
I also haven't considered customised built-ins here and haven't thought about it much yet.
rniwa commentedon Sep 16, 2016
We could add something like
getNameondocument.customElementsthat gives you the name of the custom element given the constructor, which you can obtain vianew.target.treshugart commentedon Sep 16, 2016
That'd do the trick!
rniwa commentedon Sep 16, 2016
My proposal would be renaming the existing
gettogetInterfaceand addgetNameto make the semantics clear. We could even call themfindInterfaceandfindNameas well.domenic commentedon Sep 16, 2016
The use cases still aren't clear to me; they seem very speculative, relating to things that maybe future versions of some libraries might choose to do, or to things that can already be done at the library level.
treshugart commentedon Sep 16, 2016
I don't agree that all my use cases were speculative. If we don't have a
standardised way to retrieve a tag name for a constructor, it forces us to
store that information locally. Since part of our API is to have multiple
working versions on the page, we then force internal APIs to never change
else we break that contract. This would be a really nice feature for us to
have.
On Sat, 17 Sep 2016, 02:33 Domenic Denicola [email protected]
wrote:
treshugart commentedon Sep 16, 2016
And FWIW I second @rniwa's proposal, though
get*might follow existingconventions more closely
On Sat, 17 Sep 2016, 08:59 Trey Shugart [email protected] wrote:
bradleyayers commentedon Sep 19, 2016
I echo @domenic's sentiment. I had a chat with @treshugart offline and suggested changing Incremental DOM to allow it to be agnostic to the element name when given a constructor.
(It seems like it should be able to do a
elem.constructor === Ctorcheck when diffing the DOM, rather than relying on someelem.tagName === getName(Ctor)logic.treshugart commentedon Sep 19, 2016
Yeah, bouncing ideas off @bradleyayers helped. Though, maybe we're missing something with the theoretical implementation. I know with some built-ins this isn't reliable:
In my mind it should work because you can't reuse the same class for multiple components, though I feel like I'm missing something.
bradleyayers commentedon Sep 19, 2016
I don't think we need it to work with built-ins — it only needs to work with custom elements.
That being said, I'm interested in some concrete examples where builtins don't work as expected, and which environments are affected.
treshugart commentedon Oct 4, 2016
I'm doing some experimentation and if you have access to the diff algorithms, this method works. However, at the moment I'm looking for a way to support different virtual DOM libraries - sort of like a custom element / shadow DOM abstraction - and I'd like to be able to tell the virtual element functions to create an element for a given constructor. Unless all the virtual DOM libraries support doing
elem.constructor === Ctorthis doesn't seem possible without having a standardised way to get the name that a particular constructor was registered with. Any ideas?rniwa commentedon Oct 6, 2016
One risk here for v1 is that we have
customElements.getand if we're adding a newgetmethod that returns the name given a constructor, then we'll havecustomElements.getandcustomElement.getName(or some other name) which would be quite inconsistent. Alternatively, we can makegetreturn a name or a constructor based on argument but that seems rather odd.44 remaining items
mfreed7 commentedon Apr 10, 2020
I can't guarantee a schedule for getting this change implemented in Chromium, but you can count us as supportive.
justinfagnani commentedon Jul 7, 2021
We pretty regularly get requests to provide a way to get a tag name from a constructor in Lit, and I'm pretty reluctant to add anything because of how specific to Lit it would be. Any movement here?
annevk commentedon Jul 19, 2021
If I'm reading the last set of comments correctly, an addition of
customElements.getName()would have support from Chromium and WebKit. Someone would have to do the specification and testing work required though. (This would not return the local name, but the name of a custom element, to be clear. I doubt we would get consensus on local name given that WebKit does not support theisattribute.)I think Gecko can also be supportive of this. (I also agree with @domenic now that overloading this with
get()is weird. Not sure why I advocated for that in the past…)justinfagnani commentedon Sep 28, 2021
It seems like there may be rough consensus here now on a new method
customElements.getName(), am I reading the thread correctly? Do we have anyone with time to write up the spec change and make WPT tests?[-]Is there a way to retrieve the localName from a custom element constructor?[/-][+]registry: Is there a way to retrieve the localName from a custom element constructor?[/+][-]registry: Is there a way to retrieve the localName from a custom element constructor?[/-][+]registry: Add an API to retrieve the localName given a custom element constructor[/+]Add customElements.getName(constructor)
keithamus commentedon Apr 21, 2023
I threw together WPT tests (web-platform-tests/wpt#39640) and the spec (whatwg/html#9195) as well as filed implementer bugs in Chromium, Firefox and Webkit. I'm also happy to try implementing this in those browsers if there's interest from implementers.
Add customElements.getName(constructor)
Add customElements.getName(constructor)
Add customElements.getName(constructor)
Westbrook commentedon Aug 17, 2024
With
customElements.getName(constructor)shipped x-browser, looks like this is DONE!