Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Vue 3 debugging reference for reactivity issues, computed errors, watcher bugs, async failures, and SSR hydration problems.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
reference/no-passive-with-prevent.md
1---2title: Never Use .passive and .prevent Together3impact: HIGH4impactDescription: Conflicting modifiers cause .prevent to be ignored and trigger browser warnings5type: gotcha6tags: [vue3, events, modifiers, scroll, touch, performance]7---89# Never Use .passive and .prevent Together1011**Impact: HIGH** - The `.passive` modifier tells the browser you will NOT call `preventDefault()`, while `.prevent` does exactly that. Using them together causes `.prevent` to be ignored and triggers browser console warnings. This is a logical contradiction that leads to broken event handling.1213## Task Checklist1415- [ ] Never combine `.passive` and `.prevent` on the same event16- [ ] Use `.passive` for scroll/touch events where you want better performance17- [ ] Use `.prevent` when you need to stop the default browser action18- [ ] If you need conditional prevention, handle it in JavaScript without `.passive`1920**Incorrect:**21```html22<!-- WRONG: Conflicting modifiers -->23<template>24<div @scroll.passive.prevent="handleScroll">25<!-- .prevent will be IGNORED -->26<!-- Browser shows warning -->27</div>28</template>29```3031```html32<!-- WRONG: On touch events -->33<template>34<div @touchstart.passive.prevent="handleTouch">35<!-- Cannot prevent default - passive already promised not to -->36</div>37</template>38```3940```html41<!-- WRONG: On wheel events -->42<template>43<div @wheel.passive.prevent="handleWheel">44<!-- Broken: will scroll anyway despite .prevent -->45</div>46</template>47```4849**Correct:**50```html51<!-- CORRECT: Use .passive for performance (no prevention needed) -->52<template>53<div @scroll.passive="handleScroll">54<!-- Good for scroll tracking without blocking -->55</div>56</template>57```5859```html60<!-- CORRECT: Use .prevent when you need to prevent default -->61<template>62<form @submit.prevent="handleSubmit">63<!-- Correctly prevents form submission -->64</form>65</template>66```6768```html69<!-- CORRECT: For touch events where you need to prevent -->70<template>71<div @touchmove="handleTouchMove">72<!-- Handle prevention conditionally in JS -->73</div>74</template>7576<script setup>77function handleTouchMove(event) {78if (shouldPreventScroll.value) {79event.preventDefault()80}81// ... handle touch82}83</script>84```8586## Understanding .passive8788```javascript89// .passive tells the browser:90// "I promise I won't call preventDefault()"9192// This allows the browser to:93// 1. Start scrolling immediately without waiting for JS94// 2. Improve scroll performance, especially on mobile95// 3. Reduce jank and stuttering9697// Equivalent to:98element.addEventListener('scroll', handler, { passive: true })99```100101## When to Use .passive102103```html104<!-- Good use cases for .passive -->105106<!-- Scroll tracking analytics -->107<div @scroll.passive="trackScrollPosition">108109<!-- Touch gesture detection (no prevention needed) -->110<div @touchmove.passive="detectGesture">111112<!-- Wheel event monitoring -->113<div @wheel.passive="monitorWheel">114```115116## When to Use .prevent (Without .passive)117118```html119<!-- Good use cases for .prevent -->120121<!-- Form submission -->122<form @submit.prevent="handleSubmit">123124<!-- Link clicks with custom navigation -->125<a @click.prevent="navigate">126127<!-- Preventing context menu -->128<div @contextmenu.prevent="showCustomMenu">129```130131## Browser Warning132133When you combine `.passive` and `.prevent`, the browser console shows:134```135[Intervention] Unable to preventDefault inside passive event listener136due to target being treated as passive.137```138139## Reference140- [Vue.js Event Handling - Event Modifiers](https://vuejs.org/guide/essentials/event-handling.html#event-modifiers)141- [MDN - Improving scroll performance with passive listeners](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener#improving_scrolling_performance_with_passive_listeners)142