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

Added new .self modifier for event listeners #379

Merged
merged 7 commits into from
Apr 21, 2020
Merged

Added new .self modifier for event listeners #379

merged 7 commits into from
Apr 21, 2020

Conversation

ryangjchandler
Copy link
Contributor

This PR introduces a new .self modifier with the same behaviour as Vue's implementation. This should also make it easier to prevent event's bubbled from nested elements being handled by parent elements, as in the case of #378.

When used on an element, i.e. <div @click.self="handle()">, the handle() method will only be called (or expression evaluated) if the element triggering the event matches the element that registered it. This will remove the need to add several .stop modifiers to nested elements when you don't want any event bubbling for that particular parent / component.

I'm not sure if it's worth noting in the docs / README that chaining modifiers in different orders, for example @click.self.prevent and @click.prevent.self, won't have any effect on the order of execution / evaluation since they are not being handled sequentially. The .prevent and .stop modifiers are always handled first, then the .self modifier.

P.s. there's a new test for this feature.

@ryangjchandler
Copy link
Contributor Author

ryangjchandler commented Apr 18, 2020

I realise now that the test I added doesn't test whether the handler runs when clicking the '.self' element... bozo moment 🤦‍♂️ Consider the test updated 😅

@calebporzio
Copy link
Collaborator

calebporzio commented Apr 21, 2020

Thanks @ryangjchandler, great addition!

@calebporzio calebporzio reopened this Apr 21, 2020
@calebporzio calebporzio merged commit 9a0f2de into alpinejs:master Apr 21, 2020
@bszyman
Copy link

bszyman commented Jan 10, 2024

Seconding that this is a really great addition. Thank you! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants