When we need customElements.whenDefined() to access methods #1796
Replies: 2 comments 3 replies
-
This isn't always required, but if you attach your event listener too soon, it might interfere with Shoelace's ability to inject its own form control data when the
That said, it's a good practice to make sure custom element form controls are indeed registered before accepting a form submission (and verify everything on the server, of course). Aside from forms, you don't really need |
Beta Was this translation helpful? Give feedback.
-
I've been writing a dynamic form constructor this week where a form might have any number of shoelace elements (3 or 300..) its up to the user. I've found using a Even for simple forms I would spend a bit of time debugging my form only to find I was waiting the definition of an input that isn't present in my form (ie, I listed 5 field types but my form only contained 4). So now I do this: no more listing each element type
customElementsReady function:
@claviska, wondering if a utility like this would suit Web Awesome. |
Beta Was this translation helpful? Give feedback.
-
I have a question related to this thread, and the pattern I noticed in the examples for form controls:
I'm trying to understand generally where this async handling is needed. It seems to be an issue with libraries that wrap the customElements.define() statement you would otherwise use to create a custom element. Is that right? Related article: 1. Related StackOverflow:2, 3.
In the form examples, it's interesting that the form itself is not a custom element, and the handler does not seem to use any properties or methods of the custom elements. I tried removing the await piece, keeping just the call to form.addEventListener., and it seemed still to work.
It seems like an important topic for libraries that wrap web components from my novice perspective. A related question then is whether one can easily capture all custom elements for a dynamic "when ready." This Stack Overflow answer points to a mechanism in Polymer that might help, but a quick search indicates that, despite shared origins, Lit may not support the same. Would it make sense for libraries that wrap web components to provide some mechanism to get them and/or wait for them?
I imagine there is much to unpack in this but I would value any insights or clarity you all could bring.
Beta Was this translation helpful? Give feedback.
All reactions