Skip to content

registry: Add an API to retrieve the localName given a custom element constructor #566

Closed

Description

@treshugart

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:

const Ctor = document.registerElement('x-test', {
  prototype: Object.create(HTMLElement.prototype)
});

// x-test
console.log(Ctor.name);

In Chrome Canary this behaves as I'd expect:

class Ctor extends HTMLElement {}
window.customElements.define('x-test', Ctor);

// Ctor
console.log(Ctor.name);

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?

Activity

  1. domenic commented on Sep 14, 2016

    @domenic
    Collaborator

    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?

  2. treshugart commented on Sep 15, 2016

    @treshugart
    Author

    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.

  3. treshugart commented on Sep 15, 2016

    @treshugart
    Author

    I also haven't considered customised built-ins here and haven't thought about it much yet.

  4. rniwa commented on Sep 16, 2016

    @rniwa
    Collaborator

    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.

  5. treshugart commented on Sep 16, 2016

    @treshugart
    Author

    That'd do the trick!

  6. rniwa commented on Sep 16, 2016

    @rniwa
    Collaborator

    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.

  7. domenic commented on Sep 16, 2016

    @domenic
    Collaborator

    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.

  8. treshugart commented on Sep 16, 2016

    @treshugart
    Author

    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:

    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.

    —
    You are receiving this because you authored the thread.
    Reply to this email directly, view it on GitHub
    #566 (comment),
    or mute the thread
    https://github.com/notifications/unsubscribe-auth/AAIVbHhRY2qLU-gMgCDlXEdk3gdRt_yeks5qqsTNgaJpZM4J8SND
    .

  9. treshugart commented on Sep 16, 2016

    @treshugart
    Author

    And FWIW I second @rniwa's proposal, though get* might follow existing
    conventions more closely

    On Sat, 17 Sep 2016, 08:59 Trey Shugart [email protected] wrote:

    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:

    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.

    —
    You are receiving this because you authored the thread.
    Reply to this email directly, view it on GitHub
    #566 (comment),
    or mute the thread
    https://github.com/notifications/unsubscribe-auth/AAIVbHhRY2qLU-gMgCDlXEdk3gdRt_yeks5qqsTNgaJpZM4J8SND
    .

  10. bradleyayers commented on Sep 19, 2016

    @bradleyayers

    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.

  11. treshugart commented on Sep 19, 2016

    @treshugart
    Author

    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:

    // true
    document.createElement('blockquote').constructor === document.createElement('q').constructor;

    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.

  12. bradleyayers commented on Sep 19, 2016

    @bradleyayers

    I know with some built-ins this isn't reliable

    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.

  13. treshugart commented on Oct 4, 2016

    @treshugart
    Author

    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?

  14. rniwa commented on Oct 6, 2016

    @rniwa
    Collaborator

    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.

  15. 44 remaining items

  16. mfreed7 commented on Apr 10, 2020

    @mfreed7

    I should point out that "Google-at-large" is a big place with differing opinions.

    Sure, but in the end what matters is what you're planning to ship in Chrome and figuring that out internally would save us all a lot of time.

    I can't guarantee a schedule for getting this change implemented in Chromium, but you can count us as supportive.

  17. justinfagnani commented on Jul 7, 2021

    @justinfagnani
    Contributor

    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?

  18. annevk commented on Jul 19, 2021

    @annevk
    Collaborator

    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…)

  19. justinfagnani commented on Sep 28, 2021

    @justinfagnani
    Contributor

    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?

  20. 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
  21. 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
  22. keithamus commented on Apr 21, 2023

    @keithamus
    Collaborator

    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.

  23. Westbrook commented on Aug 17, 2024

    @Westbrook
    Collaborator

    With customElements.getName(constructor) shipped x-browser, looks like this is DONE!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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