question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add non-passive event modifier

See original GitHub issue

What problem does this feature solve?

An event modifier to support passive events was added in #5132.

In the last couple of years, browsers have adopted the passive behavior by default for touchstart and touchmove events (reference). Thus, to be able to cancel one of these events by calling e.preventDefault(), you need to explicitly pass { passive: false } when adding the event listener.

With the current API this is impossible to achieve in a Vue template (as far as I can tell). You must manually add and remove the event listener in a component hook like so:

this.$refs.someElement.addEventListener('touchstart', this.start, { passive: false });
this.$refs.someElement.addEventListener('touchmove', this.move, { passive: false });

// later
this.$refs.someElement.removeEventListener('touchstart', this.start);
this.$refs.someElement.removeEventListener('touchmove', this.move);

What does the proposed API look like?

An event modifier that does the opposite of the passive event modifier, specifying the option as false instead of true.

Unsure of the naming - perhaps nonPassive, active, assertive, intentional.

<div
  @touchstart.active="start"
  @touchmove.active="move"
></div>

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:9
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
kleinfreundcommented, Nov 10, 2019

In vue/src/core/vdom/helpers/update-listeners.js:22, normalizeEvent always returns an object with a set passive property.

I’m not familiar with Vue’s source code, but does that mean that when the .passive modifier is not present, each event handler is always declared as active? This seems wrong in the light of browser’s changing the default for certain event types to be passive by default (see Improving scrolling performance with passive listeners). If .passive is not present, I don’t expect event handlers to be marked as not passive.

With regards to the feature request itself, I think “active” would be a natural choice here.

0reactions
WORMSScommented, Nov 24, 2022

the divRef would be what links the elements, the events would be added/removed as soon as the elements are added/removed from the dom. (it will internally add a watcher on divRef and when it changes, does what it needs to to add the event.

<template>
  <div
    ref="divRef"
    v-if="condition"
    @click="yourevent"
 />
<script setup>
  import { unref } from 'vue';
  import { useEventListener } from '@vueuse/core';
  const divRef = ref(null);
  const eventCallback = (evt) => {
      on_toggle_photo_checkbox(unref(img), true, evt, unref(imgTypePos) + unref(start_list_idx_in_vw));
  };
  useEventListener(divRef, 'touchstart', eventCallback, { passive: true });
</script>

again, I’ve had to guesstimate some of this, so just assumed most of it are refs/computed, but unref doesn’t care if it’s a value/ref/computed, it will get the value regardless.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Events / Event modifiers • Svelte Tutorial
DOM event handlers can have modifiers that alter their behaviour. ... add it automatically where it's safe to do so); nonpassive — explicitly...
Read more >
Consider marking event handler as 'passive' to make the page ...
Consider marking event handler as 'passive' to make the page more responsive. So I tried to add 'passive' to the listener like so....
Read more >
7 Event Modifiers in Svelte You Must Know
7 Event Modifiers in Svelte You Must Know · 1. preventDefault · 2. stopPropagation · 3. passive · 4. nonpassive · 5. once...
Read more >
Event Handling - Vue.js
The .passive modifier is typically used with touch event listeners for improving performance on mobile devices.
Read more >
Event Modifiers | Vue.js: Understanding its Tools and Ecosystem
.prevent .capture .self .once .passive. You can also chain multiple modifiers ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found