UNPKG

react-page-click

Version:

React component-wrapper to detect page events (mousedown or touchstart/touchend) outside of wrapped element.

187 lines (142 loc) 7.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ReactPageClick = void 0; var _react = _interopRequireDefault(require("react")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } function _typeof(obj) { if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } function _possibleConstructorReturn(self, call) { if (call && (_typeof(call) === "object" || typeof call === "function")) { return call; } return _assertThisInitialized(self); } function _getPrototypeOf(o) { _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { return o.__proto__ || Object.getPrototypeOf(o); }; return _getPrototypeOf(o); } function _assertThisInitialized(self) { if (self === void 0) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function"); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, writable: true, configurable: true } }); if (superClass) _setPrototypeOf(subClass, superClass); } function _setPrototypeOf(o, p) { _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) { o.__proto__ = p; return o; }; return _setPrototypeOf(o, p); } function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } var MAX_MOVE = 20; var extractCoordinates = function extractCoordinates(_ref) { var changedTouches = _ref.changedTouches; return { x: changedTouches[0].screenX, y: changedTouches[0].screenY }; }; var ReactPageClick = /*#__PURE__*/ function (_React$PureComponent) { _inherits(ReactPageClick, _React$PureComponent); function ReactPageClick() { var _getPrototypeOf2; var _this; _classCallCheck(this, ReactPageClick); for (var _len = arguments.length, _args = new Array(_len), _key = 0; _key < _len; _key++) { _args[_key] = arguments[_key]; } _this = _possibleConstructorReturn(this, (_getPrototypeOf2 = _getPrototypeOf(ReactPageClick)).call.apply(_getPrototypeOf2, [this].concat(_args))); _defineProperty(_assertThisInitialized(_this), "insideClick", false); _defineProperty(_assertThisInitialized(_this), "touchStart", null); _defineProperty(_assertThisInitialized(_this), "onDocumentMouseDown", function () { if (_this.insideClick) { return; } var notify = _this.props.notify; notify.apply(void 0, arguments); }); _defineProperty(_assertThisInitialized(_this), "onDocumentMouseUp", function () { _this.insideClick = false; }); _defineProperty(_assertThisInitialized(_this), "onDocumentTouchEnd", function (event) { // on mobile safari click events are not bubbled up to the document unless the target has the // css `cursor: pointer;` http://www.quirksmode.org/blog/archives/2010/10/click_event_del_1.html // so try and work out if we should call the notify prop var _this$props = _this.props, notifyOnTouchEnd = _this$props.notifyOnTouchEnd, notify = _this$props.notify; if (notifyOnTouchEnd && _this.touchStart && !_this.insideClick) { var _extractCoordinates = extractCoordinates(event), x = _extractCoordinates.x, y = _extractCoordinates.y; var dx = Math.abs(x - _this.touchStart.x); var dy = Math.abs(y - _this.touchStart.y); if (dx < MAX_MOVE && dy < MAX_MOVE) { for (var _len2 = arguments.length, args = new Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) { args[_key2 - 1] = arguments[_key2]; } notify.apply(void 0, [event].concat(args)); } } _this.touchStart = null; _this.insideClick = false; }); _defineProperty(_assertThisInitialized(_this), "onDocumentTouchStart", function (event) { if (_this.insideClick) { return; } var _this$props2 = _this.props, notifyOnTouchEnd = _this$props2.notifyOnTouchEnd, notify = _this$props2.notify; if (notifyOnTouchEnd) { _this.touchStart = extractCoordinates(event); } else { for (var _len3 = arguments.length, args = new Array(_len3 > 1 ? _len3 - 1 : 0), _key3 = 1; _key3 < _len3; _key3++) { args[_key3 - 1] = arguments[_key3]; } notify.apply(void 0, [event].concat(args)); } }); _defineProperty(_assertThisInitialized(_this), "onMouseDown", function () { _this.insideClick = true; var onMouseDown = _this.props.onMouseDown; if (onMouseDown) { onMouseDown.apply(void 0, arguments); } }); _defineProperty(_assertThisInitialized(_this), "onTouchStart", function () { _this.insideClick = true; var onTouchStart = _this.props.onTouchStart; if (onTouchStart) { onTouchStart.apply(void 0, arguments); } }); return _this; } _createClass(ReactPageClick, [{ key: "componentDidMount", value: function componentDidMount() { global.window.addEventListener('mousedown', this.onDocumentMouseDown, false); global.window.addEventListener('mouseup', this.onDocumentMouseUp, false); global.window.addEventListener('touchstart', this.onDocumentTouchStart, false); global.window.addEventListener('touchend', this.onDocumentTouchEnd, false); } }, { key: "componentWillUnmount", value: function componentWillUnmount() { global.window.removeEventListener('mousedown', this.onDocumentMouseDown, false); global.window.removeEventListener('mouseup', this.onDocumentMouseUp, false); global.window.removeEventListener('touchstart', this.onDocumentTouchStart, false); global.window.removeEventListener('touchend', this.onDocumentTouchEnd, false); } }, { key: "render", value: function render() { var _this$props3 = this.props, children = _this$props3.children, outsideOnly = _this$props3.outsideOnly; var props = outsideOnly ? { onMouseDown: this.onMouseDown, onTouchStart: this.onTouchStart } : {}; return _react["default"].cloneElement(_react["default"].Children.only(children), props); } }]); return ReactPageClick; }(_react["default"].PureComponent); exports.ReactPageClick = ReactPageClick; _defineProperty(ReactPageClick, "defaultProps", { onMouseDown: undefined, onTouchStart: undefined, outsideOnly: true, notifyOnTouchEnd: false });