Coder Social home page Coder Social logo

andarist / detect-passive-events Goto Github PK

View Code? Open in Web Editor NEW

This project forked from rafgraph/detect-passive-events

0.0 2.0 0.0 11 KB

Detect if the browser supports passive event listeners

Home Page: http://detect-it.rafrex.com#detect-passive-events

License: MIT License

JavaScript 100.00%

detect-passive-events's Introduction

Detect Passive Events

Detect if the browser supports passive event listeners.

Live detection test

Exports a reference to a singleton object (a micro state machine with an update function) with its state set to if the browser supports passive event listeners, as well as an update() function which re-runs the tests and updates the object's state.

Note that the code used in the detection is adapted from this Passive Events Explainer.

detectPassiveEvents micro state machine

const detectPassiveEvents = {
  hasSupport: boolean,

  // re-run the detection tests and update state
  update() {...},
}

Installing detect-passive-events

$ npm install detect-passive-events

Using detect-passive-events

import detectPassiveEvents from 'detect-passive-events';
// passive events are supported by the browser
if (detectPassiveEvents.hasSupport === true) {
  // set listeners like this
  document.addEventListener('scroll', handleScroll, { capture: false, passive: true });
}

// passive events are not supported by the browser
if (detectPassiveEvents.hasSupport === false) {
  // set listeners like this
  document.addEventListener('scroll', handleScroll, false);
}

// updating the state - most apps won't need to use this at all
detectPassiveEvents.update();

Note that the update() function is run once at the time of import to set the object's initial state, and generally doesn't need to be run again. If it doesn't have access to the window, then the state will be undefined (detect-passive-events will not throw an error), and you will need to call the update() function manually at a later time to update its state.

Part of the detect-it family

detect-passive-events's People

Contributors

rafgraph avatar stephenmathieson avatar

Watchers

 avatar  avatar

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.