-
Notifications
You must be signed in to change notification settings - Fork 393
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
Unnecessary event name restrictions (such as prohibiting hyphens) should be removed #1904
Comments
This is related to: #1811 |
The gist of the issue: <sample-element onselected-index-change={handleSelectedIndexChange} selected-index="1"> The question is:
|
I believe that this is component's author decision to make and author's responsibility to document how to handle events. A component that author is developing is not a core platform feature. Because of that restrictions should be reduced to minimum. |
The attribute name is not represented as a DOMString but a ASCII lowercase string, which restricts the character set that can be expressed in template. Because of this
The issue here is not on the component dispatching the event but on the consumer attempting to receive the event. For component properties, the attribute names written in kebab-cases are mapping to properties in camelCase. The question what is our stand regarding event listeners, does event listener name written in kebab-case are preserved or converted to camelCase. Regardless of the outcome of the discussion, I would suggest updating the compiler error message to provide an alternative. What do you think about:
|
I am not sure if I understand this correctly. How |
You are right those are 2 different event names. There is am ambiguity here since:
|
Naturally it would correspond to |
Stepping back, I think the high-level question is: how many expectations should LWC place on the components used in an LWC app? For now, LWC isn't really optimized for consuming components from other frameworks, but it seems like the correct long-term goal is to work with web components created by any means. With that in mind, the question for this specific issue becomes: what's the best job the LWC compiler can do to listen to events raised by any web component? Per @pmdartus, the set of event names that can be practically listened to in markup is smaller than the set of allowable event names (which can be any string). I understand that the compiler today makes a simplifying assumption that all attributes can be handled the same way, but I agree with @jarrodek that events are not the same as properties, and deserve special handling. As a starting point, following the principle of least surprise, it seems useful to say that someone should be able to listen to an event X using
I do not think it is surprising that @pmdartus raises the point of casing. Given the general case-insensitive nature of markup, as a developer I would personally not be that surprised if
|
I can buy @JanMiksovsky articulation. Just to be clear the spec is somewhat ambiguous in the type of characters allowed, ambiguous meaning pretty much every character (but some unicode sequences) are allowed, where the reality is that browsers do have some constraints (ex. I believe the japanese characters did not work on safari). I'm ok lifting this constraint for the sake of better WC interoperability and HTML declarative expressiveness, but we should add some guiding principle recommendation in the docs (like uppercasing is discouraged due to the ambiguity it represents). |
This topic was discussed again in the context of improved integration support for third party web components. There was a general consensus that the usage of imperative API methods as a workaround for event types that LWC does not have declarative support for, is sufficient for now. It was also mentioned that if we do decide to introduce support at some point, it should come in the form of a new directive. |
Description
The LWC compiler currently imposes restrictions on the names of events which can be listened to through the
on
markup syntax. The restrictions seem unnecessary, limits developer control, and will unnecessarily constrain the interoperability of LWC with custom elements created by other frameworks.Steps to Reproduce
Given a custom element
sample-element
that raises aCustomEvent
with the valid nameselected-index-change
, attempt to instantiate the component in a template with:Expected Results
The code compiles, and listens to the
selected-index-change
event.According to the MDN CustomEvent constructor docs, the
type
of an event is DOMString, i.e., a UTF-16 String. Since "selected-index-change" is such a string, the event name is legal.Actual Results
The compiler generates an error:
Version
Possible Solution
Relax artificial constraints on event names generally. To the extent possible, any valid
DOMString
should be permitted. In particular, while the native DOM events all are lowercase strings with no hyphens, a reasonable developer might decide they want to include hyphens in event names for legibility.The fact that LWC templates are created in markup may some acceptable limitations. E.g., a single space character
" "
is a valid DOMString, but would presumably be problematic to parse in markup. It seems reasonable for a markup-based language to fail to parse such event names, and require use ofaddEventListener
. Beyond that, the compiler should not impose additional artificial constraints.The text was updated successfully, but these errors were encountered: