Skip to content
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

Feature request: event handlers with only modifiers, e.g. on:click|stopPropagation #3049

Closed
ghost opened this issue Jun 19, 2019 · 13 comments
Closed

Comments

@ghost
Copy link

ghost commented Jun 19, 2019

stopPropagation can be useful on its own, as a way of catching "cancel when user clicks away" functionality and preventing it from being triggered by clicks that originate within the focused thing itself.

It would be nice to be able to use the event modifier without having to supply a "noop" handler, e.g.

<div on:click|stopPropagation>
    ...
</div>
@Conduitry
Copy link
Member

I think this would be confusing. An event directive with no handler and no modifiers means an event that's forwarded to the parent component. This would make adding modifiers to it do something completely different.

@ghost
Copy link
Author

ghost commented Jun 19, 2019

Ah yeah, forgot about that feature. Doesn't seem worth it in that case.

@ghost ghost closed this as completed Jun 19, 2019
@Rich-Harris
Copy link
Member

I have wanted this myself in the past. What if we had something like this?

<div on:click|stopPropagation={}>
  ...
</div>

It's not not confusing, but it is arguably nicer than on:click={e => e.stopPropagation()}

@Conduitry
Copy link
Member

I was also thinking about something like that. Do we want the ={} to be equivalent to some other thing in general, like ={null} or ={undefined}? Do we want event handlers in general to check (at compile time? at run time?) whether they're truthy before running? (Semi-related: #3040.) I can see the desire to stop the addition of this feature from getting out of hand, but I also think it would be weird to have a one-off, special ={} syntax just for event handlers.

@Rich-Harris
Copy link
Member

Yeah, it's definitely a straw man. I'll reopen this anyway, in case we think of something better

@Rich-Harris Rich-Harris reopened this Jun 19, 2019
@Conduitry Conduitry added the awaiting submitter needs a reproduction, or clarification label Jun 22, 2019
@antony antony added proposal and removed awaiting submitter needs a reproduction, or clarification labels Apr 9, 2020
@antony
Copy link
Member

antony commented Apr 9, 2020

Going to vote against this mostly, since on:click={e => e.stopPropagation()} isn't that difficult, and we have other stuff to fix.

If it did get turned into a feature, I'd suggest on:click|stopPropagation not ={}.

@stale
Copy link

stale bot commented Dec 27, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale-bot label Dec 27, 2021
@aradalvand
Copy link

aradalvand commented Dec 27, 2021

I think this is something most people intuitively expect Svelte to already do, so I don't think this issue should be closed.

@stale stale bot removed the stale-bot label Dec 27, 2021
@bluwy
Copy link
Member

bluwy commented Dec 28, 2021

+1 for on:click={e => e.stopPropagation()} too. I don't think the extra complexity for both compile-time and runtime is worth supporting this.

@johnnysprinkles
Copy link

Are you sure this doesn't already work, at least as of today? I'm using bare|preventDefault and |stopPropagation all over my codebase, on keydown, mousedown, copy, paste, click, etc, seems to do what one expects...

@aradalvand
Copy link

Apprently, it does work now: https://svelte.dev/repl/d3f83ef507d74e558f5a25bd8fd24e0c?version=3.46.4
I'm not sure why, maybe this feature was silently added?!

@bluwy
Copy link
Member

bluwy commented Feb 21, 2022

on:click|preventDefault works now but it also forwards the event up to parent. IIUC this issue proposes on:click|preventDefault={} to prevent forwarding.

@Rich-Harris
Copy link
Member

Since on:x and event modifiers are essentially deprecated in Svelte 5 (in favour of callback props and function composition), I'll close this

@Rich-Harris Rich-Harris closed this as not planned Won't fix, can't repro, duplicate, stale Apr 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants