UNPKG

@enact/ui

Version:

A collection of simplified unstyled cross-platform UI components for Enact

68 lines (65 loc) 3.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = exports.ClickAllow = void 0; var _platform = require("@enact/core/platform"); function _classCallCheck(a, n) { if (!(a instanceof n)) throw new TypeError("Cannot call a class as a function"); } function _defineProperties(e, r) { for (var t = 0; t < r.length; t++) { var o = r[t]; o.enumerable = o.enumerable || !1, o.configurable = !0, "value" in o && (o.writable = !0), Object.defineProperty(e, _toPropertyKey(o.key), o); } } function _createClass(e, r, t) { return r && _defineProperties(e.prototype, r), t && _defineProperties(e, t), Object.defineProperty(e, "prototype", { writable: !1 }), e; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == typeof i ? i : i + ""; } function _toPrimitive(t, r) { if ("object" != typeof t || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != typeof i) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } // It's possible that emitting `onTap` will cause a DOM change before the mousedown fires resulting // in multiple tap/click events for the same user action. To avoid this, we store the last touchend // target and timestamp to compare against the next mouse down. If the timestamp is different (e.g. // we're on a hybrid device that emitted a touch event but the next was a mouse event) or the target // is the same (or no previous target was set if no touch events have been emitted), we allow the // mousedown *across Touchable instances*. var _lastTouchEnd = { target: null, timeStamp: 0 }; var shouldAllowMouseDown = function shouldAllowMouseDown(ev) { return ev.timeStamp !== _lastTouchEnd.timeStamp || ev.target === _lastTouchEnd.target || _lastTouchEnd.target === null; }; var ClickAllow = exports.ClickAllow = /*#__PURE__*/function () { function ClickAllow() { _classCallCheck(this, ClickAllow); this.lastTouchEndTime = 0; this.lastMouseUpTime = 0; } return _createClass(ClickAllow, [{ key: "setLastTouchEnd", value: function setLastTouchEnd(ev) { if (ev && ev.type === 'touchend') { this.lastTouchEndTime = ev.timeStamp; _lastTouchEnd.timeStamp = ev.timeStamp; _lastTouchEnd.target = ev.target; } } }, { key: "setLastMouseUp", value: function setLastMouseUp(ev) { if (ev && ev.type === 'mouseup') { this.lastMouseUpTime = ev.timeStamp; } } }, { key: "shouldAllowMouseEvent", value: function shouldAllowMouseEvent(ev) { var timeStamp = ev.timeStamp; // iOS Safari sends both touch and mouse events (with differing timestamps) return !(_platform.platform.browserName === 'safari' && _platform.platform.type === 'mobile') && this.lastTouchEndTime !== timeStamp && shouldAllowMouseDown(ev); } }, { key: "shouldAllowTap", value: function shouldAllowTap(ev) { var type = ev.type, timeStamp = ev.timeStamp; // Allow the custom tap event for a “click” when it’s actually a click and it’s not from the // last mouseup event which would have fired the click for us return type === 'click' && this.lastMouseUpTime !== timeStamp; } }]); }(); var _default = exports["default"] = ClickAllow;