-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
[Svelte 5] Events should be allowed to be camelCased #10044
Comments
The problem is that this would allow having 2 different listeners for the same event <button onClick={increment} onclick={decrement}>My button</button> Which seems to be something the team wants to avoid Considering that it is also no longer possible to call global functions using strings <dialog id="dialog">My dialog</dialog>
<button onclick="dialog.openModal()">Open modal</button> It seems that being a superset of HTML is no longer an objective |
If it has to be one or the other, then I think camelCased |
There was some internal discussion on this before, nobody wanted the camelCasing option (even knowing of its advantages). While HTML attributes have no case sensitivity, the DOM properties do and they are all lowercase. Components have to declare their properties (unless spread), so there either would be no option to use a different casing or there needs to be some normalization which then might mean that the property is spelled differently in declaration and usage site. @Thiagolino8 No longer supporting something like |
Moreover, |
If it has to be one or the other, then I think lowercase is certainly the best choice. |
I was reading through the specs for this stuff to see what it said, and I think sticking with lowercase is the way to go. The whatwg spec for event listeners states:
So it seems that the spec intends for event handlers to be of the form As far as I can tell, the spec says nothing about whether HTML parsers should lowercase |
This was originally posted in response to a comment that was deleted, then reposted with different content.
Where is this in the spec? (Not trying to call you out, I was just looking for it and couldn't find it 😅) |
The colon is much more comparable to camelCase than it is nocase. The colon is effectively a type of casing, as it indicates two separate words:
Both examples of camelCasing and colon:casing indicate two separate words: Using There's no doubt that camelCasing is the best choice for developer experience, as this is what JavaScript developers are used to and it's how React does it too.
Try out using: |
Nope, it's not nonsense. What's nonsense is claiming that Svelte is simply following HTML specs, and that's it, when that's simply false. Svelte is artificially restricting itself to nocase, when even HTML doesn't.
That's great that you tried it. Now you see that Svelte is in fact diverging away from how HTML works.
I'm a fan of being as strict as vanilla HTML, that's correct. It's an excellent argument to want to stick to how HTML does things as much as possible to reduce the learning curve. However, if it has to be one, then there's no doubt that camelCase would be the better choice for DX and would closer resemble the old |
As I stated earlier, HTML is case insensitive, everything gets automatically lowercased. See the "get an attribute" algorithm:
|
So why not keep it like that in Svelte too, so that both nocase and camelCase works? Not to mention take for example Svelte's decision to move away from the double mustache So why not apply this logic to events to allow camelCasing? |
Again, see my previous comment, it causes various inconsistencies. |
Thank you! That's what I've been searching for this whole time. This means that it's officially acceptable to use case-insensitive attributes, though the canonical name (at least, for events and event handlers) is lowercase. That's a bit more permissive, and gives us more room to reason. My personal opinion is that in cases like this, one way to do things is better than two (working in a codebase where both First, why do we have to chose one or the other, and why is case-insensitivity not an option?:
So if we have to choose I can see arguments for both. |
Elliot argued nicely for why we're doing it the way we're currently doing it. There's one more crucial runtime-related argument: Writing I'm therefore going to close this issue. |
Describe the problem
While it's a welcomed addition that Svelte 5 is becoming more like vanilla JS, when it comes to events, like
onclick={}
,onsubmit={}
, etc., it's weird why it doesn't allow for it to be camelCased, like so:onClick={}
andonSubmit={}
.Even vanilla HTML/JS allows you to camelCase the event name, so it doesn't make sense why Svelte 5 is restricting it to only allowed nocase. Additionally, React is the opposite, where it only allows camelCase, as functions and variables are typically camelCased in JS. If camelCased events were allowed in Svelte 5, it would be easier to do the shorthand syntax of
{onClick}
(of course it normally should be more descriptive anyway).Describe the proposed solution
I propose to allow both camelCase and nocase syntax for native events, much like how it already work.
So following would both work:
Alternatives considered
There's no alternative that I'm aware of.
Importance
would make my life easier
The text was updated successfully, but these errors were encountered: