[d565449] | 1 | "use strict";
|
---|
| 2 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
| 3 | var react_1 = require("react");
|
---|
| 4 | var util_1 = require("./misc/util");
|
---|
| 5 | var isTouchEvent = function (ev) {
|
---|
| 6 | return 'touches' in ev;
|
---|
| 7 | };
|
---|
| 8 | var preventDefault = function (ev) {
|
---|
| 9 | if (!isTouchEvent(ev))
|
---|
| 10 | return;
|
---|
| 11 | if (ev.touches.length < 2 && ev.preventDefault) {
|
---|
| 12 | ev.preventDefault();
|
---|
| 13 | }
|
---|
| 14 | };
|
---|
| 15 | var useLongPress = function (callback, _a) {
|
---|
| 16 | var _b = _a === void 0 ? {} : _a, _c = _b.isPreventDefault, isPreventDefault = _c === void 0 ? true : _c, _d = _b.delay, delay = _d === void 0 ? 300 : _d;
|
---|
| 17 | var timeout = react_1.useRef();
|
---|
| 18 | var target = react_1.useRef();
|
---|
| 19 | var start = react_1.useCallback(function (event) {
|
---|
| 20 | // prevent ghost click on mobile devices
|
---|
| 21 | if (isPreventDefault && event.target) {
|
---|
| 22 | util_1.on(event.target, 'touchend', preventDefault, { passive: false });
|
---|
| 23 | target.current = event.target;
|
---|
| 24 | }
|
---|
| 25 | timeout.current = setTimeout(function () { return callback(event); }, delay);
|
---|
| 26 | }, [callback, delay, isPreventDefault]);
|
---|
| 27 | var clear = react_1.useCallback(function () {
|
---|
| 28 | // clearTimeout and removeEventListener
|
---|
| 29 | timeout.current && clearTimeout(timeout.current);
|
---|
| 30 | if (isPreventDefault && target.current) {
|
---|
| 31 | util_1.off(target.current, 'touchend', preventDefault);
|
---|
| 32 | }
|
---|
| 33 | }, [isPreventDefault]);
|
---|
| 34 | return {
|
---|
| 35 | onMouseDown: function (e) { return start(e); },
|
---|
| 36 | onTouchStart: function (e) { return start(e); },
|
---|
| 37 | onMouseUp: clear,
|
---|
| 38 | onMouseLeave: clear,
|
---|
| 39 | onTouchEnd: clear,
|
---|
| 40 | };
|
---|
| 41 | };
|
---|
| 42 | exports.default = useLongPress;
|
---|