# default-passive-events

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

Latest version **4.0.0** (published 2025-05-24) · MIT license · 0 weekly downloads

## Install

```sh
npm install default-passive-events
pnpm add default-passive-events
yarn add default-passive-events
bun add default-passive-events
```

## Health

**Score 40/100 (D)** — status: stable.

Positive: esm support; no vulnerabilities.

Warnings: low downloads; no types.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 4.0.0 |
| Published | 2025-05-24 |
| First published | 2016-06-15 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 29.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 364 |
| Author | zzarcon |
| Maintainers | zzarcon, frsgit |
| Keywords | default passive events, passive events, auto passive events, lightweight, simple, passive event listeners |

## Links

- npm: https://www.npmjs.com/package/default-passive-events
- Repository: https://github.com/zzarcon/default-passive-events
- Homepage: https://github.com/zzarcon/default-passive-events#readme
- Issues: https://github.com/zzarcon/default-passive-events/issues
- npm.io page: https://npm.io/package/default-passive-events

## Alternatives

- [async-exit-hook](https://npm.io/package/async-exit-hook.md) — 3.7M weekly downloads
- [evnty](https://npm.io/package/evnty.md) — 7.2K weekly downloads
- [eleventy-plugin-asciidoc](https://npm.io/package/eleventy-plugin-asciidoc.md) — 3.5K weekly downloads
- [@jswork/next-get2get](https://npm.io/package/@jswork/next-get2get.md) — 945 weekly downloads
- [@dashersw/axon](https://npm.io/package/@dashersw/axon.md) — 934 weekly downloads

## Recent versions

- 4.0.0 (latest) — 2025-05-24
- 3.0.0 — 2025-05-24
- 2.0.0 — 2020-07-01
- 1.0.10 — 2018-04-11
- 1.0.9 — 2018-04-10
- 1.0.8 — 2018-04-10
- 1.0.7 — 2018-01-21
- 1.0.6 — 2018-01-16
- 1.0.5 — 2018-01-13
- 1.0.4 — 2018-01-11
- 1.0.3 — 2018-01-11
- 1.0.2 — 2017-12-11
- 1.0.1 — 2017-12-11
- 1.0.0 — 2017-10-24
- 0.1.1 — 2016-06-15

## README

# `default-passive-events` [![NPM version](https://img.shields.io/npm/v/default-passive-events)](https://www.npmjs.com/package/default-passive-events) [![License MIT](https://img.shields.io/github/license/zzarcon/default-passive-events)](https://github.com/zzarcon/default-passive-events/blob/master/LICENSE) [![Bundle size](https://img.shields.io/bundlephobia/minzip/default-passive-events)](https://bundlephobia.com/result?p=default-passive-events)

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

50 lines snippet that enables [passive event listeners](https://github.com/WICG/EventListenerOptions/blob/gh-pages/explainer.md) by default for some events ([see list below](#targeted-events)). It basically will set **{ passive: true }** automatically every time you declare a new [event listener](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener).

## Installation

```bash
yarn add default-passive-events
```

## Usage

Simply import the package:

```javascript
import 'default-passive-events';
```

or require it:

```javascript
require('default-passive-events');
```

or include it locally:

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

or from [unpkg](https://unpkg.com/#/) [CDN](https://en.wikipedia.org/wiki/Content_delivery_network):

```html
<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](https://github.com/developit/microbundle#-output-formats-). 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:

```javascript
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](https://zzarcon.github.io/default-passive-events) 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
- animationstart
- animationend
- animationiteratio
- transitionstart
- transitionend
- transitionrun
- transitioncancel

## Configuration

You can configure library to override different set of events than the default ones.
To do that set the `window.defaultPassiveEvents_supportedPassiveEvents` property like so:

```javascript
window.defaultPassiveEvents_supportedPassiveEvents = ['scroll', 'wheel'];
```

## 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.:

```javascript
element.addEventListener = element.addEventListener._original;
```

## Resources

- About passive event listeners https://medium.com/@devlucky/about-passive-event-listeners-224ff620e68c
- EventListenerOptions https://github.com/WICG/EventListenerOptions
- Explanation https://github.com/WICG/EventListenerOptions/blob/gh-pages/explainer.md
- Polyfill https://github.com/WICG/EventListenerOptions/blob/gh-pages/EventListenerOptions.polyfill.js
- Spec https://dom.spec.whatwg.org/#dictdef-eventlisteneroptions
- Chrome feature https://www.chromestatus.com/features#passive
- About scrolling performance https://plus.google.com/+RickByers/posts/cmzrtyBYPQc
- Nice Chrome blog article https://blog.chromium.org/2016/05/new-apis-to-help-developers-improve.html

## Publishing

To release, simply run `npm version <new_Version_number>` and then `pnpm publish`.

## Author

[@zzarcon](https://github.com/zzarcon)

## Maintainers

[@zzarcon](https://github.com/zzarcon)
[@frsgit](https://github.com/frsgit)

---
_Source: https://npm.io/package/default-passive-events · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
