default-passive-events

WebJar for default-passive-events

License

License

MIT
GroupId

GroupId

org.webjars.npm
ArtifactId

ArtifactId

default-passive-events
Last Version

Last Version

1.0.10
Release Date

Release Date

Type

Type

jar
Description

Description

default-passive-events
WebJar for default-passive-events
Project URL

Project URL

https://www.webjars.org
Source Code Management

Source Code Management

https://github.com/zzarcon/default-passive-events

Download default-passive-events

How to add to project

<!-- https://jarcasting.com/artifacts/org.webjars.npm/default-passive-events/ -->
<dependency>
    <groupId>org.webjars.npm</groupId>
    <artifactId>default-passive-events</artifactId>
    <version>1.0.10</version>
</dependency>
// https://jarcasting.com/artifacts/org.webjars.npm/default-passive-events/
implementation 'org.webjars.npm:default-passive-events:1.0.10'
// https://jarcasting.com/artifacts/org.webjars.npm/default-passive-events/
implementation ("org.webjars.npm:default-passive-events:1.0.10")
'org.webjars.npm:default-passive-events:jar:1.0.10'
<dependency org="org.webjars.npm" name="default-passive-events" rev="1.0.10">
  <artifact name="default-passive-events" type="jar" />
</dependency>
@Grapes(
@Grab(group='org.webjars.npm', module='default-passive-events', version='1.0.10')
)
libraryDependencies += "org.webjars.npm" % "default-passive-events" % "1.0.10"
[org.webjars.npm/default-passive-events "1.0.10"]

Dependencies

There are no dependencies for this project. It is a standalone project that does not depend on any other jars.

Project Modules

There are no modules declared in this project.

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

Versions

Version
1.0.10