Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/alpinejs/src/utils/on.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ export default function on (el, event, modifiers, callback) {

if (modifiers.includes('prevent')) handler = wrapHandler(handler, (next, e) => { e.preventDefault(); next(e) })
if (modifiers.includes('stop')) handler = wrapHandler(handler, (next, e) => { e.stopPropagation(); next(e) })
if (modifiers.includes('self')) handler = wrapHandler(handler, (next, e) => { e.target === el && next(e) })

if (modifiers.includes('away') || modifiers.includes('outside')) {
listenerTarget = document
Expand Down Expand Up @@ -65,6 +64,8 @@ export default function on (el, event, modifiers, callback) {
})
}

if (modifiers.includes('self')) handler = wrapHandler(handler, (next, e) => { e.target === el && next(e) })

// Handle :keydown and :keyup listeners.
handler = wrapHandler(handler, (next, e) => {
if (isKeyEvent(event)) {
Expand Down
21 changes: 21 additions & 0 deletions tests/cypress/integration/directives/x-on.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,27 @@ test('.self modifier',
}
)

test(
".self.once modifiers",
html`
<div x-data="{ foo: 'bar' }">
<h1 x-on:click.self.once="foo = 'baz'" id="selfTarget">
content
<button>click</button>
content
</h1>
<span x-text="foo"></span>
</div>
`,
({ get }) => {
get("span").should(haveText("bar"));
get("button").click();
get("span").should(haveText("bar"));
get("h1").click();
get("span").should(haveText("baz"));
}
);

test('.prevent modifier',
html`
<div x-data="{}">
Expand Down