UNPKG

react-page-click

Version:

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

327 lines (272 loc) 13.1 kB
(function webpackUniversalModuleDefinition(root, factory) { if(typeof exports === 'object' && typeof module === 'object') module.exports = factory(require("React")); else if(typeof define === 'function' && define.amd) define(["React"], factory); else if(typeof exports === 'object') exports["ReactPageClick"] = factory(require("React")); else root["ReactPageClick"] = factory(root["React"]); })(window, function(__WEBPACK_EXTERNAL_MODULE__0__) { return /******/ (function(modules) { // webpackBootstrap /******/ // The module cache /******/ var installedModules = {}; /******/ /******/ // The require function /******/ function __webpack_require__(moduleId) { /******/ /******/ // Check if module is in cache /******/ if(installedModules[moduleId]) { /******/ return installedModules[moduleId].exports; /******/ } /******/ // Create a new module (and put it into the cache) /******/ var module = installedModules[moduleId] = { /******/ i: moduleId, /******/ l: false, /******/ exports: {} /******/ }; /******/ /******/ // Execute the module function /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); /******/ /******/ // Flag the module as loaded /******/ module.l = true; /******/ /******/ // Return the exports of the module /******/ return module.exports; /******/ } /******/ /******/ /******/ // expose the modules object (__webpack_modules__) /******/ __webpack_require__.m = modules; /******/ /******/ // expose the module cache /******/ __webpack_require__.c = installedModules; /******/ /******/ // define getter function for harmony exports /******/ __webpack_require__.d = function(exports, name, getter) { /******/ if(!__webpack_require__.o(exports, name)) { /******/ Object.defineProperty(exports, name, { enumerable: true, get: getter }); /******/ } /******/ }; /******/ /******/ // define __esModule on exports /******/ __webpack_require__.r = function(exports) { /******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) { /******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' }); /******/ } /******/ Object.defineProperty(exports, '__esModule', { value: true }); /******/ }; /******/ /******/ // create a fake namespace object /******/ // mode & 1: value is a module id, require it /******/ // mode & 2: merge all properties of value into the ns /******/ // mode & 4: return value when already ns object /******/ // mode & 8|1: behave like require /******/ __webpack_require__.t = function(value, mode) { /******/ if(mode & 1) value = __webpack_require__(value); /******/ if(mode & 8) return value; /******/ if((mode & 4) && typeof value === 'object' && value && value.__esModule) return value; /******/ var ns = Object.create(null); /******/ __webpack_require__.r(ns); /******/ Object.defineProperty(ns, 'default', { enumerable: true, value: value }); /******/ if(mode & 2 && typeof value != 'string') for(var key in value) __webpack_require__.d(ns, key, function(key) { return value[key]; }.bind(null, key)); /******/ return ns; /******/ }; /******/ /******/ // getDefaultExport function for compatibility with non-harmony modules /******/ __webpack_require__.n = function(module) { /******/ var getter = module && module.__esModule ? /******/ function getDefault() { return module['default']; } : /******/ function getModuleExports() { return module; }; /******/ __webpack_require__.d(getter, 'a', getter); /******/ return getter; /******/ }; /******/ /******/ // Object.prototype.hasOwnProperty.call /******/ __webpack_require__.o = function(object, property) { return Object.prototype.hasOwnProperty.call(object, property); }; /******/ /******/ // __webpack_public_path__ /******/ __webpack_require__.p = ""; /******/ /******/ /******/ // Load entry module and return exports /******/ return __webpack_require__(__webpack_require__.s = 1); /******/ }) /************************************************************************/ /******/ ([ /* 0 */ /***/ (function(module, exports) { module.exports = __WEBPACK_EXTERNAL_MODULE__0__; /***/ }), /* 1 */ /***/ (function(module, exports, __webpack_require__) { var _require = __webpack_require__(2), ReactPageClick = _require.ReactPageClick; ReactPageClick.ReactPageClick = ReactPageClick; module.exports = ReactPageClick; /***/ }), /* 2 */ /***/ (function(module, __webpack_exports__, __webpack_require__) { "use strict"; __webpack_require__.r(__webpack_exports__); /* WEBPACK VAR INJECTION */(function(global) {/* harmony export (binding) */ __webpack_require__.d(__webpack_exports__, "ReactPageClick", function() { return ReactPageClick; }); /* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(0); /* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_0__); 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__WEBPACK_IMPORTED_MODULE_0___default.a.cloneElement(react__WEBPACK_IMPORTED_MODULE_0___default.a.Children.only(children), props); } }]); return ReactPageClick; }(react__WEBPACK_IMPORTED_MODULE_0___default.a.PureComponent); _defineProperty(ReactPageClick, "defaultProps", { onMouseDown: undefined, onTouchStart: undefined, outsideOnly: true, notifyOnTouchEnd: false }); /* WEBPACK VAR INJECTION */}.call(this, __webpack_require__(3))) /***/ }), /* 3 */ /***/ (function(module, exports) { var g; // This works in non-strict mode g = (function() { return this; })(); try { // This works if eval is allowed (see CSP) g = g || new Function("return this")(); } catch (e) { // This works if the window reference is available if (typeof window === "object") g = window; } // g can still be undefined, but nothing to do about it... // We return undefined, instead of nothing here, so it's // easier to handle this case. if(!global) { ...} module.exports = g; /***/ }) /******/ ]); });