Skip to content

Commit

Permalink
Detect passive event support and try to use it only if its available
Browse files Browse the repository at this point in the history
  • Loading branch information
Andarist committed Oct 30, 2017
1 parent c123470 commit 1f625b3
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 5 deletions.
36 changes: 34 additions & 2 deletions dist/react-onclickoutside.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,29 @@ function clickedScrollbar(evt) {
return document.documentElement.clientWidth <= evt.clientX || document.documentElement.clientHeight <= evt.clientY;
}

// ideally will get replaced with external dep
// when rafrex/detect-passive-events#4 and rafrex/detect-passive-events#5 get merged in
var testPassiveEventSupport = function testPassiveEventSupport() {
if (typeof window === 'undefined' || typeof window.addEventListener !== 'function') {
return;
}

var passive = false;

var options = Object.defineProperty({}, 'passive', {
get: function get() {
passive = true;
}
});

var noop = function noop() {};

window.addEventListener('testPassiveEventSupport', noop, options);
window.removeEventListener('testPassiveEventSupport', noop, options);

return passive;
};

function autoInc() {
var seed = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 0;

Expand Down Expand Up @@ -113,6 +136,8 @@ var possibleConstructorReturn = function (self, call) {
return call && (typeof call === "object" || typeof call === "function") ? call : self;
};

var passiveEventSupport = void 0;

var handlersMap = {};
var enabledInstances = {};

Expand Down Expand Up @@ -159,7 +184,14 @@ function onClickOutsideHOC(WrappedComponent, config) {
};

_this.enableOnClickOutside = function () {
if (typeof document === 'undefined' || enabledInstances[_this._uid]) return;
if (typeof document === 'undefined' || enabledInstances[_this._uid]) {
return;
}

if (typeof passiveEventSupport === 'undefined') {
passiveEventSupport = testPassiveEventSupport();
}

enabledInstances[_this._uid] = true;

var events = _this.props.eventTypes;
Expand Down Expand Up @@ -194,7 +226,7 @@ function onClickOutsideHOC(WrappedComponent, config) {
var handlerOptions = null;
var isTouchEvent = touchEvents.indexOf(eventName) !== -1;

if (isTouchEvent) {
if (isTouchEvent && passiveEventSupport) {
handlerOptions = { passive: !_this.props.preventDefault };
}

Expand Down
2 changes: 1 addition & 1 deletion dist/react-onclickoutside.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions src/detect-passive-events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// ideally will get replaced with external dep
// when rafrex/detect-passive-events#4 and rafrex/detect-passive-events#5 get merged in
export const testPassiveEventSupport = () => {
if (typeof window === 'undefined' || typeof window.addEventListener !== 'function') {
return;
}

let passive = false;

const options = Object.defineProperty({}, 'passive', {
get() {
passive = true;
},
});

const noop = () => {};

window.addEventListener('testPassiveEventSupport', noop, options);
window.removeEventListener('testPassiveEventSupport', noop, options);

return passive;
};
14 changes: 12 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { createElement, Component } from 'react';
import { findDOMNode } from 'react-dom';
import * as DOMHelpers from './dom-helpers';
import { testPassiveEventSupport } from './detect-passive-events';
import uid from './uid';

let passiveEventSupport;

const handlersMap = {};
const enabledInstances = {};

Expand Down Expand Up @@ -112,7 +115,14 @@ export default function onClickOutsideHOC(WrappedComponent, config) {
* for clicks and touches outside of this element.
*/
enableOnClickOutside = () => {
if (typeof document === 'undefined' || enabledInstances[this._uid]) return;
if (typeof document === 'undefined' || enabledInstances[this._uid]) {
return;
}

if (typeof passiveEventSupport === 'undefined') {
passiveEventSupport = testPassiveEventSupport();
}

enabledInstances[this._uid] = true;

let events = this.props.eventTypes;
Expand Down Expand Up @@ -147,7 +157,7 @@ export default function onClickOutsideHOC(WrappedComponent, config) {
let handlerOptions = null;
const isTouchEvent = touchEvents.indexOf(eventName) !== -1;

if (isTouchEvent) {
if (isTouchEvent && passiveEventSupport) {
handlerOptions = { passive: !this.props.preventDefault };
}

Expand Down

0 comments on commit 1f625b3

Please sign in to comment.