-
Notifications
You must be signed in to change notification settings - Fork 382
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
registry: Add an API to retrieve the localName given a custom element constructor #566
Comments
There is no way to do this. As you say, constructing the component and retrieving the 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 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 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 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. |
I also haven't considered customised built-ins here and haven't thought about it much yet. |
We could add something like |
That'd do the trick! |
My proposal would be renaming the existing |
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 On Sat, 17 Sep 2016, 02:33 Domenic Denicola notifications@github.com
|
And FWIW I second @rniwa's proposal, though On Sat, 17 Sep 2016, 08:59 Trey Shugart treshugart@gmail.com wrote:
|
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 |
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. |
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. |
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 |
One risk here for v1 is that we have |
Yeah, that's probably not a good idea. I think the naming of |
I don't think we should have getName, and we should not disturb the existing method names even if we add it. |
Perhaps we need to get a tie breaker from Microsoft or Mozilla. @annevk, @travisleithead ? |
I think having |
This relates a bit to the algorithms discussed in whatwg/html#3452. Given that the browser needs them I'm even more convinced that it's reasonable to expose these. |
(Although if we want to support what the browser supports it'd have to return a list...) |
At spring virtual f2f, we discussed that:
|
FWIW, I no longer like overloading |
Perhaps an alternative design to return whatever arguments being passed to |
Per my action item, I spoke to @domenic about this issue. He and I do agree that if we only support custom elements with this API, we're increasing the number of differences between custom elements and built-in elements. We have built-in elements, customized built-in elements, and autonomous custom elements. If I'm handed a constructor, it might or might not work with this new API, depending on what it is, and that feels pretty wrong. @rniwa - I'd be curious to hear whether you disagree with this. In the past, I've seen you arguing pretty strongly for platform consistency. But here you're arguing for inconsistency between custom elements and built-in elements. If the argument is just that the proposed API is on Having said the above, I do see the obvious developer interest in this capability. We generally support this API, and want to move forward with an interoperable solution. So while we'd prefer to support all constructors, it seems that most/all use-cases center around custom element constructors, so we can go along with an API that only supports custom elements for now. Hopefully it would be defined in such a way that allows expansion later to support built-ins? |
@mfreed7 custom elements have a 1:1 mapping, builtins do not. What kind of API do you envision? |
That ship has sailed long ago when Google insisted that lifecycle callback can't be sync and that we support non-sync / upgrade for custom elements. Custom elements behave noting like builtin elements today and probably never will given the Web compat.
That's exactly what
It would definitely be more consistent to have To begin with, this will be an API on an interfaced named
Given there have been no concrete use case presented for retrieving builtin elements' name given their constructors, I don't think supporting builtin elements make sense. |
@mfreed7 how would a global API for both builtin and custom elements work for scoped registries? Or are scoped registries not something Google-at-large is advocating for? |
I agree that this isn't straightforward. For scoped registries, since they're scoped to the shadow root, I would think that whatever API we develop for builtins should "just work" there also. But I suppose that depends on the details of the API. Anyway, since we're okay moving ahead without support for builtins, we can punt these questions until later. I should point out that "Google-at-large" is a big place with differing opinions.
Ok. We are ok moving ahead without builtin support for now, given the strong developer demand for this feature. |
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. |
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 I think Gecko can also be supportive of this. (I also agree with @domenic now that overloading this with |
It seems like there may be rough consensus here now on a new method |
In CustomElements v0 you could use the `.name` field to get the defined tag name, but CustomElements v1 does not offer such a field. `localName` can be used within an instance but this prohibits use in, for example, static methods. This was discussed in the WCCG F2F, tracked in WICG/webcomponents#566. The conclusion of the F2F was to write up a spec change and WPT tests.
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. |
In CustomElements v0 you could use the `.name` field to get the defined tag name, but CustomElements v1 does not offer such a field. `localName` can be used within an instance but this prohibits use in, for example, static methods. This was discussed in the WCCG F2F, tracked in WICG/webcomponents#566. The conclusion of the F2F was to write up a spec change and WPT tests.
In CustomElements v0 you could use the `.name` field to get the defined tag name, but CustomElements v1 does not offer such a field. `localName` can be used within an instance but this prohibits use in, for example, static methods. This was discussed in the WCCG F2F, tracked in WICG/webcomponents#566. The conclusion of the F2F was to write up a spec change and WPT tests.
In CustomElements v0 you could use the `.name` field to get the defined tag name, but CustomElements v1 does not offer such a field. `localName` can be used within an instance but this prohibits use in, for example, static methods. This was discussed in the WCCG F2F, tracked in WICG/webcomponents#566. The conclusion of the F2F was to write up a spec change and WPT tests.
With |
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:
In Chrome Canary this behaves as I'd expect:
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:It could return
null
if not found, or thelocalName
if found.Thoughts?
The text was updated successfully, but these errors were encountered: