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

feat: x-on .passive.false modifier #4404

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 6 additions & 2 deletions packages/alpinejs/src/utils/on.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ export default function on (el, event, modifiers, callback) {

if (modifiers.includes("dot")) event = dotSyntax(event)
if (modifiers.includes('camel')) event = camelCase(event)
if (modifiers.includes('passive')) options.passive = true
if (modifiers.includes('capture')) options.capture = true
if (modifiers.includes('window')) listenerTarget = window
if (modifiers.includes('document')) listenerTarget = document

if (modifiers.includes('passive')) {
let nextModifier = modifiers[modifiers.indexOf('passive')+1]
options.passive = nextModifier === 'false' ? false : true
}

// By wrapping the handler with debounce & throttle first, we ensure that the wrapping logic itself is not
// throttled/debounced, only the user's callback is. This way, if the user expects
// `e.preventDefault()` to happen, it'll still happen even if their callback gets throttled.
Expand Down Expand Up @@ -73,7 +77,7 @@ export default function on (el, event, modifiers, callback) {
if (isListeningForASpecificKeyThatHasntBeenPressed(e, modifiers)) {
return
}

next(e)
})
}
Expand Down
9 changes: 9 additions & 0 deletions packages/docs/src/en/directives/on.md
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,15 @@ If you are listening for touch events, it's important to add `.passive` to your

[→ Read more about passive listeners](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#improving_scrolling_performance_with_passive_listeners)

<a name="passive-false"></a>
### .passive.false

In modern browsers, `wheel` and `touchmove` event listeners are passive by default. If, for some reason, you need to call `preventDefault` on such an event, you can do so by adding `.passive.false` to your listener.

```alpine
<div @touchstart.passive.false="console.log($event.cancelable)">...</div>
```

### .capture

Add this modifier if you want to execute this listener in the event's capturing phase, e.g. before the event bubbles from the target element up the DOM.
Expand Down
17 changes: 17 additions & 0 deletions tests/cypress/integration/directives/x-on.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,23 @@ test('.passive modifier should disable e.preventDefault()',
}
)

test('.passive.false modifier should enable e.preventDefault()',
html`
<div
x-data="{ defaultPrevented: null }"
x-on:touchmove.passive.false="
$event.preventDefault();
defaultPrevented = $event.defaultPrevented;
">
<br>
</div>
`,
({ get }) => {
get('div').trigger('touchmove')
get('div').should(haveData('defaultPrevented', true))
}
)

test('.stop modifier',
html`
<div x-data="{ foo: 'bar' }">
Expand Down
Loading