Git Product home page Git Product logo

default-passive-events's Introduction

default-passive-events Build Status Dependency Status Bundle size

Makes {passive: true} by default when EventListenerOptions are supported

50 lines snippet that enables passive event listeners by default for some events (see list below). It basically will set { passive: true } automatically every time you declare a new event listener.

Installation

yarn add default-passive-events

Usage

Simply require the package:

require('default-passive-events');

or include it locally:

<script type="text/javascript" src="node_modules/default-passive-events/dist/index.js"></script>

or from unpkg CDN:

<script type="text/javascript" src="https://unpkg.com/default-passive-events"></script>

Bundle formats

This package is distributed as multiple, different types of output bundles. The most often your bundler will properly choose the correct version by itself.

To get more information about supported bundle formats have a look at official microbundle documentation. Especially interesting is the modern format which - if used properly with your bundle system - might produce significantly smaller output code.

Examples

Those are some examples and their output:

document.addEventListener('mouseup', onMouseUp); // {passive: true, capture: false}
document.addEventListener('mouseup', onMouseUp, true); // {passive: true, capture: true}
document.addEventListener('mouseup', onMouseUp, false); // {passive: true, capture: false}
document.addEventListener('mouseup', onMouseUp, {passive: false}); // {passive: false, capture: false}
document.addEventListener('mouseup', onMouseUp, {passive: false, capture: false}); // {passive: false, capture: false}
document.addEventListener('mouseup', onMouseUp, {passive: false, capture: true}); // {passive: false, capture: true}
document.addEventListener('mouseup', onMouseUp, {passive: true, capture: false}); // {passive: true, capture: false}
document.addEventListener('mouseup', onMouseUp, {passive: true, capture: true}); // {passive: true, capture: true}

Demo

Check the demo page for a working example.

Motivation

Just to take benefit in your apps without having to edit every single event listener you already have.

Targeted events

Default-passive-events package makes following event listeners passive by default:

  • scroll
  • wheel
  • touchstart
  • touchmove
  • touchenter
  • touchend
  • touchleave
  • mouseout
  • mouseleave
  • mouseup
  • mousedown
  • mousemove
  • mouseenter
  • mousewheel
  • mouseover

Q&A

Browser rises weird error when I try to preventDefault event inside of a passive listener.

Well, that's true, partly. First of all specification says that you shouldn't ever try to preventDefault from the context of passive listener. But if that's not a possibility you should know that in the console you see only error-looking log messages, which are not actual errors (ergo: they do not break your code).

Is there a possibility to hide these messages?

Unfortunately, no. Since they are not actual errors there is no way to catch them and (more importantly) there is no way to distinguish whether you're inside of the passive listener context to know when not to call/override preventDefault method. Now, we look at the regarding issue in WHATWG repo whatwg/dom#587.

Is there a possibility to bring default addEventListener method back for chosen elements/globally (e.g. for time of running some of the code)?

Yes, original addEventListener is available under _original property of our's addEventListener's implementation (so - element.addEventListener._original). Having that in mind, you can bring it back for however you want, e.g.:

element.addEventListener = element.addEventListener._original;

Resources

Author

@zzarcon

Maintainers

@zzarcon @frsgit

default-passive-events's People

Contributors

charliegdev avatar christef avatar dependabot[bot] avatar frsgit avatar kerumen avatar mulder90 avatar zzarcon avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

default-passive-events's Issues

Remove sourceMappingURL from dist

Getting this error:

DevTools failed to load source map: Could not load content for https://unpkg.com/index.umd.js.map: HTTP error: status code 404, net::ERR_HTTP_RESPONSE_CODE_FAILURE

Cause by file in dist folder used in CDN production enviroment:

//# sourceMappingURL=index.umd.js.map

We need min.js version that can be used in production and no additional request is in file

Planned removal of crucial DOM events

Hello, lighthouse detects this library as depracated due to planned removal of some DOMevents:

DOM Mutation Events, including DOMSubtreeModified, DOMNodeInserted, DOMNodeRemoved, DOMNodeRemovedFromDocument, DOMNodeInsertedIntoDocument, and DOMCharacterDataModified are deprecated (https://w3c.github.io/uievents/#legacy-event-types) and will be removed. Please use MutationObserver instead.

The library is great, are you planning to migrate to MutationObservers instead? I think it would be worth giving it a try.

Desactivate passive mode for specific contents or specific elements.

Hello,
I'm using https://github.com/mervick/emojionearea with your plug-in but your plug-in seem not to be compatible with https://github.com/mervick/emojionearea.

EmojioneArea uses mousedown event and it require to use event.preventDefault() in not passive mode.

I would like to know if it was possible to desactivate passive mode for specific contents or specific elements ?
I would like to desactivate passive mode for ".emojionearea-editor" .

Thanks.

Issue with Select2 plugin

Hi,

I'm default-passive-events this fixed almost all my issues except one case...

I'm using Select2 plugin for my dropdown and this creating an Error (not a warning) in the browser
jQuery-2.2.0.min.js:3 Unable to preventDefault inside passive event listener invocation.
and pointing code line related to Select2 mousewheel.

Unable to preventDefault inside passive event listener invocation.

if ($.fn.mousewheel) {
      this.$results.on('mousewheel', function (e) {
        var top = self.$results.scrollTop();

        var bottom = self.$results.get(0).scrollHeight - top + e.deltaY;

        var isAtTop = e.deltaY > 0 && top - e.deltaY <= 0;
        var isAtBottom = e.deltaY < 0 && bottom <= self.$results.height();

        if (isAtTop) {
          self.$results.scrollTop(0);

          e.preventDefault();
          e.stopPropagation();
        } else if (isAtBottom) {
          self.$results.scrollTop(
            self.$results.get(0).scrollHeight - self.$results.height()
          );

          e.preventDefault();
          e.stopPropagation();
        }
      });
    }


 if (nullLowestDeltaTimeout) { clearTimeout(nullLowestDeltaTimeout); }
        nullLowestDeltaTimeout = setTimeout(nullLowestDelta, 200);

        return ($.event.dispatch || $.event.handle).apply(this, args);
    }

This occured on static list and on my Ajax list.

Anyone have a solution for this?

It's look like similar issue occurred in the past but I don't know how to fix it to make it work with Select2
#19
#5

Add some filtering [feature-request]

Can it to filter applied events somehow?

I'm get a lot of errors in angular, for example:

Unable to preventDefault inside passive event listener invocation. platform-browser.es5:2651

Force passive should not be applied to non-touch&&non-wheel events.

Cursor event not work while jquery is appending new content.

I started using that plugin recently and it's a powerful plugin.

But after the ajax request and when new content is adding to a page cursor event not work during the appending of new content, only the scroll event works not the other events.
All my page is blocked(for some seconds) until the appending process finish.

How could i do to make cursor event working while the jquery is appending new content ?

Thanks.

ERROR TypeError: Cannot set property passive of [object Object] which has only a getter

I got this error w/ angular and angular material

ERROR TypeError: Cannot set property passive of [object Object] which has only a getter
    at EventTarget.addEventListener (modules.js?hash=c08a6c1be18ea8d3c65fba22b566f1b2e49111a3:10606)
    at modules.js?hash=c08a6c1be18ea8d3c65fba22b566f1b2e49111a3:21335
    at Object.supportsPassiveEventListeners (modules.js?hash=c08a6c1be18ea8d3c65fba22b566f1b2e49111a3:114890)
    at modules.js?hash=c08a6c1be18ea8d3c65fba22b566f1b2e49111a3:117297
    at ZoneDelegate.invoke (modules.js?hash=c08a6c1be18ea8d3c65fba22b566f1b2e49111a3:19984)
    at Zone.run (modules.js?hash=c08a6c1be18ea8d3c65fba22b566f1b2e49111a3:19734)
    at NgZone.runOutsideAngular (modules.js?hash=c08a6c1be18ea8d3c65fba22b566f1b2e49111a3:29928)
    at FocusMonitor._registerGlobalListeners (modules.js?hash=c08a6c1be18ea8d3c65fba22b566f1b2e49111a3:117294)
    at FocusMonitor._incrementMonitoredElementCount (modules.js?hash=c08a6c1be18ea8d3c65fba22b566f1b2e49111a3:117478)
    at FocusMonitor.monitor (modules.js?hash=c08a6c1be18ea8d3c65fba22b566f1b2e49111a3:117190)

Error in console - Cannot read property 'capture' of null

I imported 'default-passive-events' in one of my components where I use event listener and it was fine, but now when I navigate throw site, on one of the pages I got error like
image

I tried to remove all window and document listeners, to found a problem but that didn't helped. Any ideas || suggestions?

Store link to original preventDefault method under separate property name

In some cases legacy code may create event handlers via libraries that do not support passive events option (e.g. jquery click handlers within i.e. mobile application). In order to perform transition to default passive events it might be good to save original .preventDefault method so it can be used as a safe point of passive events integration.

Do you think it is posisble to add it?

Regards!

The routine doesn't work for YouTube videos embedded in iFrames

The code doesn't work for YouTube videos embedded in an iFrame, like this:

<iframe width="420" height="315" src="https://www.youtube.com/embed/DJYpXxWqvmo" allowfullscreen></iframe>

This one iframe generates four instances of this console error:

[Violation] Added non-passive event listener to a scroll-blocking 'touchstart' event. Consider marking event handler as 'passive' to make the page more responsive. See https://www.chromestatus.com/feature/5745543795965952

Can this please be fixed? Many thanks!

Broken `removeEventListener`

listener = prepareSafeListener(listener, options.passive);

import 'default-passive-events'
const listener = ()=> console.log('This listener is removed and should not be called')
window.addEventListener('mousemove', listener)
window.removeEventListener('mousemove', listener)

404 page demo.html

Please fix the example url at Check the demo.html for a working example.

About scroll event

Hello ๐Ÿ‘‹

Interesting utility, I'm keen to give it a spin.

There is something I would like to share about scroll event. I've read some docs recently and I've noticed this:

You don't need to worry about the value of passive for the basic scroll event. Since it can't be canceled, event listeners can't block page rendering anyway.

from here

and another note from here

Note: The basic scroll event cannot be canceled, so it does not need to be set passive.

What are your thoughts on this matter?

Doesn't work, even the example page

screenshot 18

I was curious when it didn't work in my app. Not sure if it matters, but I installed default-passive-events via NPM. I'm building a React application in Windows 10 using Chrome Version 70.0.3538.110 (Official Build) (64-bit). Maybe it's because it's React, but that's why I wanted to try your package, anyway I went to your example and ran an audit and either you're not using it on your own example or it just doesn't work. Thought I'd share with you.

Unable to preventDefault inside passive event listener

i use default-passive-events,control will say "Unable to preventDefault inside passive event listener"
when i uninstall this,it will warn "Added non-passive event listener to a scroll-blocking 'mousewheel' eve"

how can i solve this problem

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.