react-event-listener
Advanced tools
Comparing version 0.5.2 to 0.5.3
@@ -47,2 +47,4 @@ 'use strict'; | ||
var _react2 = _interopRequireDefault(_react); | ||
var _propTypes = require('prop-types'); | ||
@@ -62,6 +64,2 @@ | ||
var supports = _interopRequireWildcard(_supports); | ||
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } } | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -73,3 +71,2 @@ | ||
}; | ||
/* eslint-disable prefer-spread */ | ||
@@ -82,3 +79,3 @@ function mergeDefaultEventOptions(options) { | ||
var args = [eventName, callback]; | ||
args.push(supports.passiveOption ? options : options.capture); | ||
args.push(_supports.passiveOption ? options : options.capture); | ||
return args; | ||
@@ -88,19 +85,9 @@ } | ||
function on(target, eventName, callback, options) { | ||
if (supports.addEventListener) { | ||
target.addEventListener.apply(target, getEventListenerArgs(eventName, callback, options)); | ||
} else if (supports.attachEvent) { | ||
// IE8+ Support | ||
target.attachEvent('on' + eventName, function () { | ||
callback.call(target); | ||
}); | ||
} | ||
// eslint-disable-next-line prefer-spread | ||
target.addEventListener.apply(target, getEventListenerArgs(eventName, callback, options)); | ||
} | ||
function off(target, eventName, callback, options) { | ||
if (supports.removeEventListener) { | ||
target.removeEventListener.apply(target, getEventListenerArgs(eventName, callback, options)); | ||
} else if (supports.detachEvent) { | ||
// IE8+ Support | ||
target.detachEvent('on' + eventName, callback); | ||
} | ||
// eslint-disable-next-line prefer-spread | ||
target.removeEventListener.apply(target, getEventListenerArgs(eventName, callback, options)); | ||
} | ||
@@ -141,3 +128,3 @@ | ||
function withOptions(handler, options) { | ||
process.env.NODE_ENV !== "production" ? (0, _warning2.default)(options, 'react-event-listener: Should be specified options in withOptions.') : void 0; | ||
process.env.NODE_ENV !== "production" ? (0, _warning2.default)(options, 'react-event-listener: should be specified options in withOptions.') : void 0; | ||
@@ -150,4 +137,4 @@ return { | ||
var EventListener = function (_Component) { | ||
(0, _inherits3.default)(EventListener, _Component); | ||
var EventListener = function (_React$Component) { | ||
(0, _inherits3.default)(EventListener, _React$Component); | ||
@@ -217,3 +204,3 @@ function EventListener() { | ||
return EventListener; | ||
}(_react.Component); | ||
}(_react2.default.Component); | ||
@@ -224,3 +211,3 @@ EventListener.propTypes = process.env.NODE_ENV !== "production" ? { | ||
*/ | ||
children: _propTypes2.default.element, | ||
children: _propTypes2.default.node, | ||
/** | ||
@@ -231,2 +218,3 @@ * The DOM target to listen to. | ||
} : {}; | ||
exports.default = EventListener; |
@@ -6,3 +6,3 @@ 'use strict'; | ||
}); | ||
exports.passiveOption = exports.detachEvent = exports.attachEvent = exports.removeEventListener = exports.addEventListener = undefined; | ||
exports.passiveOption = undefined; | ||
@@ -15,16 +15,6 @@ var _defineProperty = require('babel-runtime/core-js/object/define-property'); | ||
function defineProperty(o, p, attr) { | ||
return (0, _defineProperty2.default)(o, p, attr); | ||
function defineProperty(object, property, attr) { | ||
return (0, _defineProperty2.default)(object, property, attr); | ||
} | ||
// Inspired by https://github.com/facebook/fbjs/blob/master/packages/fbjs/src/core/ExecutionEnvironment.js | ||
var canUseDOM = !!(typeof window !== 'undefined' && window.document && window.document.createElement); | ||
var addEventListener = exports.addEventListener = canUseDOM && 'addEventListener' in window; | ||
var removeEventListener = exports.removeEventListener = canUseDOM && 'removeEventListener' in window; | ||
// IE8+ Support | ||
var attachEvent = exports.attachEvent = canUseDOM && 'attachEvent' in window; | ||
var detachEvent = exports.detachEvent = canUseDOM && 'detachEvent' in window; | ||
// Passive options | ||
@@ -48,3 +38,5 @@ // Inspired by https://github.com/Modernizr/Modernizr/blob/master/feature-detects/dom/passiveeventlisteners.js | ||
})); | ||
} catch (e) {} // eslint-disable-line no-empty | ||
} catch (err) { | ||
// | ||
} | ||
@@ -55,2 +47,4 @@ cache = supportsPassiveOption; | ||
}(); | ||
}(); | ||
}(); | ||
exports.default = {}; |
{ | ||
"name": "react-event-listener", | ||
"version": "0.5.2", | ||
"version": "0.5.3", | ||
"description": "A React component that allow to bind events on the global scope", | ||
@@ -11,5 +11,4 @@ "main": "lib/index.js", | ||
"test:watch": "mocha -w", | ||
"test": "npm run lint && npm run test:unit && npm run flow", | ||
"prettier": "find . -name \"*.js\" | grep -v -f .eslintignore | xargs prettier --write --single-quote --trailing-comma all --print-width 100", | ||
"flow": "flow", | ||
"test": "npm run lint && npm run test:unit", | ||
"prettier": "find . -name \"*.js\" | grep -v -f .eslintignore | xargs prettier --write", | ||
"version": "npm run build && pkgfiles" | ||
@@ -49,3 +48,2 @@ }, | ||
"eslint-plugin-babel": "^4.1.2", | ||
"eslint-plugin-flowtype": "^2.38.0", | ||
"eslint-plugin-import": "^2.7.0", | ||
@@ -56,3 +54,2 @@ "eslint-plugin-jsx-a11y": "^5.1.1", | ||
"eslint-plugin-react": "^7.4.0", | ||
"flow-bin": "^0.56.0", | ||
"jsdom": "^11.3.0", | ||
@@ -59,0 +56,0 @@ "mocha": "^4.0.0", |
108
src/index.js
@@ -1,17 +0,8 @@ | ||
// @flow | ||
/* eslint-disable prefer-spread */ | ||
import { Component } from 'react'; | ||
import type { Node } from 'react'; | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import shallowEqual from 'fbjs/lib/shallowEqual'; | ||
import warning from 'warning'; | ||
import * as supports from './supports'; | ||
import { passiveOption } from './supports'; | ||
type EventOptions = { | ||
capture: boolean, | ||
passive: boolean, | ||
}; | ||
const defaultEventOptions: EventOptions = { | ||
const defaultEventOptions = { | ||
capture: false, | ||
@@ -21,46 +12,23 @@ passive: false, | ||
function mergeDefaultEventOptions(options: Object) { | ||
function mergeDefaultEventOptions(options) { | ||
return Object.assign({}, defaultEventOptions, options); | ||
} | ||
function getEventListenerArgs( | ||
eventName: string, | ||
callback: Function, | ||
options: EventOptions, | ||
): Array<any> { | ||
function getEventListenerArgs(eventName, callback, options) { | ||
const args = [eventName, callback]; | ||
args.push(supports.passiveOption ? options : options.capture); | ||
args.push(passiveOption ? options : options.capture); | ||
return args; | ||
} | ||
function on(target: Object, eventName: string, callback: Function, options: EventOptions): void { | ||
if (supports.addEventListener) { | ||
target.addEventListener.apply(target, getEventListenerArgs(eventName, callback, options)); | ||
} else if (supports.attachEvent) { | ||
// IE8+ Support | ||
target.attachEvent(`on${eventName}`, () => { | ||
callback.call(target); | ||
}); | ||
} | ||
function on(target, eventName, callback, options) { | ||
// eslint-disable-next-line prefer-spread | ||
target.addEventListener.apply(target, getEventListenerArgs(eventName, callback, options)); | ||
} | ||
function off(target: Object, eventName: string, callback: Function, options: EventOptions): void { | ||
if (supports.removeEventListener) { | ||
target.removeEventListener.apply(target, getEventListenerArgs(eventName, callback, options)); | ||
} else if (supports.detachEvent) { | ||
// IE8+ Support | ||
target.detachEvent(`on${eventName}`, callback); | ||
} | ||
function off(target, eventName, callback, options) { | ||
// eslint-disable-next-line prefer-spread | ||
target.removeEventListener.apply(target, getEventListenerArgs(eventName, callback, options)); | ||
} | ||
type Props = { | ||
children?: Node, | ||
target?: EventTarget, | ||
[event: string]: Function, | ||
}; | ||
function forEachListener( | ||
props: Props, | ||
iteratee: (eventName: string, listener: Function, options?: EventOptions) => any, | ||
): void { | ||
function forEachListener(props, iteratee) { | ||
const { | ||
@@ -98,10 +66,4 @@ children, // eslint-disable-line no-unused-vars | ||
export function withOptions( | ||
handler: Function, | ||
options: EventOptions, | ||
): { | ||
handler: Function, | ||
options: EventOptions, | ||
} { | ||
warning(options, 'react-event-listener: Should be specified options in withOptions.'); | ||
export function withOptions(handler, options) { | ||
warning(options, 'react-event-listener: should be specified options in withOptions.'); | ||
@@ -114,43 +76,32 @@ return { | ||
class EventListener extends Component<Props> { | ||
static propTypes = { | ||
/** | ||
* You can provide a single child too. | ||
*/ | ||
children: PropTypes.element, | ||
/** | ||
* The DOM target to listen to. | ||
*/ | ||
target: PropTypes.oneOfType([PropTypes.object, PropTypes.string]).isRequired, | ||
}; | ||
componentDidMount(): void { | ||
class EventListener extends React.Component { | ||
componentDidMount() { | ||
this.addListeners(); | ||
} | ||
shouldComponentUpdate(nextProps: Props): boolean { | ||
shouldComponentUpdate(nextProps) { | ||
return !shallowEqual(this.props, nextProps); | ||
} | ||
componentWillUpdate(): void { | ||
componentWillUpdate() { | ||
this.removeListeners(); | ||
} | ||
componentDidUpdate(): void { | ||
componentDidUpdate() { | ||
this.addListeners(); | ||
} | ||
componentWillUnmount(): void { | ||
componentWillUnmount() { | ||
this.removeListeners(); | ||
} | ||
addListeners(): void { | ||
addListeners() { | ||
this.applyListeners(on); | ||
} | ||
removeListeners(): void { | ||
removeListeners() { | ||
this.applyListeners(off); | ||
} | ||
applyListeners(onOrOff: Function): void { | ||
applyListeners(onOrOff) { | ||
const { target } = this.props; | ||
@@ -174,2 +125,13 @@ | ||
EventListener.propTypes = { | ||
/** | ||
* You can provide a single child too. | ||
*/ | ||
children: PropTypes.node, | ||
/** | ||
* The DOM target to listen to. | ||
*/ | ||
target: PropTypes.oneOfType([PropTypes.object, PropTypes.string]).isRequired, | ||
}; | ||
export default EventListener; |
@@ -1,21 +0,5 @@ | ||
// @flow | ||
function defineProperty(o, p, attr) { | ||
return Object.defineProperty(o, p, attr); | ||
function defineProperty(object, property, attr) { | ||
return Object.defineProperty(object, property, attr); | ||
} | ||
// Inspired by https://github.com/facebook/fbjs/blob/master/packages/fbjs/src/core/ExecutionEnvironment.js | ||
const canUseDOM = !!( | ||
typeof window !== 'undefined' && | ||
window.document && | ||
window.document.createElement | ||
); | ||
export const addEventListener = canUseDOM && 'addEventListener' in window; | ||
export const removeEventListener = canUseDOM && 'removeEventListener' in window; | ||
// IE8+ Support | ||
export const attachEvent = canUseDOM && 'attachEvent' in window; | ||
export const detachEvent = canUseDOM && 'detachEvent' in window; | ||
// Passive options | ||
@@ -43,3 +27,5 @@ // Inspired by https://github.com/Modernizr/Modernizr/blob/master/feature-detects/dom/passiveeventlisteners.js | ||
); | ||
} catch (e) {} // eslint-disable-line no-empty | ||
} catch (err) { | ||
// | ||
} | ||
@@ -51,1 +37,3 @@ cache = supportsPassiveOption; | ||
})(); | ||
export default {}; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
29
17610
325