source: imaps-frontend/node_modules/react-use/lib/useLongPress.js

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 1.6 KB
Line 
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var react_1 = require("react");
4var util_1 = require("./misc/util");
5var isTouchEvent = function (ev) {
6 return 'touches' in ev;
7};
8var preventDefault = function (ev) {
9 if (!isTouchEvent(ev))
10 return;
11 if (ev.touches.length < 2 && ev.preventDefault) {
12 ev.preventDefault();
13 }
14};
15var 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};
42exports.default = useLongPress;
Note: See TracBrowser for help on using the repository browser.