source: imaps-frontend/node_modules/react-use/esm/useLongPress.js@ d565449

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

Update repo after prototype presentation

  • Property mode set to 100644
File size: 1.5 KB
Line 
1import { useCallback, useRef } from 'react';
2import { off, on } from './misc/util';
3var isTouchEvent = function (ev) {
4 return 'touches' in ev;
5};
6var preventDefault = function (ev) {
7 if (!isTouchEvent(ev))
8 return;
9 if (ev.touches.length < 2 && ev.preventDefault) {
10 ev.preventDefault();
11 }
12};
13var useLongPress = function (callback, _a) {
14 var _b = _a === void 0 ? {} : _a, _c = _b.isPreventDefault, isPreventDefault = _c === void 0 ? true : _c, _d = _b.delay, delay = _d === void 0 ? 300 : _d;
15 var timeout = useRef();
16 var target = useRef();
17 var start = useCallback(function (event) {
18 // prevent ghost click on mobile devices
19 if (isPreventDefault && event.target) {
20 on(event.target, 'touchend', preventDefault, { passive: false });
21 target.current = event.target;
22 }
23 timeout.current = setTimeout(function () { return callback(event); }, delay);
24 }, [callback, delay, isPreventDefault]);
25 var clear = useCallback(function () {
26 // clearTimeout and removeEventListener
27 timeout.current && clearTimeout(timeout.current);
28 if (isPreventDefault && target.current) {
29 off(target.current, 'touchend', preventDefault);
30 }
31 }, [isPreventDefault]);
32 return {
33 onMouseDown: function (e) { return start(e); },
34 onTouchStart: function (e) { return start(e); },
35 onMouseUp: clear,
36 onMouseLeave: clear,
37 onTouchEnd: clear,
38 };
39};
40export default useLongPress;
Note: See TracBrowser for help on using the repository browser.