-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Add new event modifier 'self' #3372
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
Comments
One thing I like about Svelte is that it's pretty close to vanilla Javascript. I disagree that shortening event modifiers is something desirable, as it's just another incline on the learning curve.
|
The shorthand syntax for event modifiers has been discussed before and decided against, we want to keep the event modifier names in line with the DOM APIs. Shorthand modifier names will not be implemented. Regarding <script context="module">
function closeMenu(e) {
if (e.target !== e.currentTarget) return;
// do stuff...
}
</script>
<div on:click|stopPropagation={doSomething}>
</div> I can see the attraction of |
I understand that you are trying to keep it close to vanilla js. But the proposal is not for replacing it, but merely to give the option of using shorthands. Adding a In addition, the abstraction added above a function calledOnClick(e) {
...do stuff...
} But in runtime the same function will be called like: function(event) {
event.preventDefault();
return calledOnClick.call(this, event);
}; Performing a function(event) {
if (event.target !== element) return;
return calledOnClick.call(this, event);
}; Which is identical in the amount of work and abstraction that the compiled does for you. Edit: removed irrelevant point. |
Regarding additional syntax, we don't like adding additional ways of doing exactly the same thing like so it is still highly unlikely to be implemented. We prefer to optimise for readability rather than writability. |
The readability part is understandable, about |
My commit was merged. |
@samuelgozi pease update docs. Thanks for the feature. |
@stalkerg my commit includes changes to the docs. Is there any specific thing I forgot? |
ah sorry, you right, I just checked 7342d48 commit and didn't find the full PR. |
Is your feature request related to a problem? Please describe.
Event modifiers are long to type, and they could use shorter names or aliases, and in addition, it would be very helpful to have an additional
self
modifier that is very common.Describe the solution you'd like
I would like to have the next aliases (inspiration from vue) for the modifiers:
stop
as an alias forstopPropagation
andprevent
as an alias forpreventDefault
.In addition, I would like to have a
self
modifier, that essentially checks that the clicked element is the one on which I added the event listener.Describe alternatives you've considered
For the
self
modifier:How important is this feature to you?
Events are at the very core of every reactive web app, I think that making it a little bit easier to use, is very important. In addition, It shouldn't be hard to do.
The text was updated successfully, but these errors were encountered: