UNPKG

4.02 kBJavaScriptView Raw
1var $0294ea432cd92340$exports = require("./usePress.main.js");
2var $5sxTM$reactariautils = require("@react-aria/utils");
3var $5sxTM$react = require("react");
4
5
6function $parcel$export(e, n, v, s) {
7 Object.defineProperty(e, n, {get: v, set: s, enumerable: true, configurable: true});
8}
9
10$parcel$export(module.exports, "useLongPress", () => $3cd7b5d0eebf0ca9$export$c24ed0104d07eab9);
11/*
12 * Copyright 2020 Adobe. All rights reserved.
13 * This file is licensed to you under the Apache License, Version 2.0 (the "License");
14 * you may not use this file except in compliance with the License. You may obtain a copy
15 * of the License at http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing, software distributed under
18 * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
19 * OF ANY KIND, either express or implied. See the License for the specific language
20 * governing permissions and limitations under the License.
21 */
22
23
24const $3cd7b5d0eebf0ca9$var$DEFAULT_THRESHOLD = 500;
25function $3cd7b5d0eebf0ca9$export$c24ed0104d07eab9(props) {
26 let { isDisabled: isDisabled, onLongPressStart: onLongPressStart, onLongPressEnd: onLongPressEnd, onLongPress: onLongPress, threshold: threshold = $3cd7b5d0eebf0ca9$var$DEFAULT_THRESHOLD, accessibilityDescription: accessibilityDescription } = props;
27 const timeRef = (0, $5sxTM$react.useRef)(undefined);
28 let { addGlobalListener: addGlobalListener, removeGlobalListener: removeGlobalListener } = (0, $5sxTM$reactariautils.useGlobalListeners)();
29 let { pressProps: pressProps } = (0, $0294ea432cd92340$exports.usePress)({
30 isDisabled: isDisabled,
31 onPressStart (e) {
32 e.continuePropagation();
33 if (e.pointerType === 'mouse' || e.pointerType === 'touch') {
34 if (onLongPressStart) onLongPressStart({
35 ...e,
36 type: 'longpressstart'
37 });
38 timeRef.current = setTimeout(()=>{
39 // Prevent other usePress handlers from also handling this event.
40 e.target.dispatchEvent(new PointerEvent('pointercancel', {
41 bubbles: true
42 }));
43 if (onLongPress) onLongPress({
44 ...e,
45 type: 'longpress'
46 });
47 timeRef.current = undefined;
48 }, threshold);
49 // Prevent context menu, which may be opened on long press on touch devices
50 if (e.pointerType === 'touch') {
51 let onContextMenu = (e)=>{
52 e.preventDefault();
53 };
54 addGlobalListener(e.target, 'contextmenu', onContextMenu, {
55 once: true
56 });
57 addGlobalListener(window, 'pointerup', ()=>{
58 // If no contextmenu event is fired quickly after pointerup, remove the handler
59 // so future context menu events outside a long press are not prevented.
60 setTimeout(()=>{
61 removeGlobalListener(e.target, 'contextmenu', onContextMenu);
62 }, 30);
63 }, {
64 once: true
65 });
66 }
67 }
68 },
69 onPressEnd (e) {
70 if (timeRef.current) clearTimeout(timeRef.current);
71 if (onLongPressEnd && (e.pointerType === 'mouse' || e.pointerType === 'touch')) onLongPressEnd({
72 ...e,
73 type: 'longpressend'
74 });
75 }
76 });
77 let descriptionProps = (0, $5sxTM$reactariautils.useDescription)(onLongPress && !isDisabled ? accessibilityDescription : undefined);
78 return {
79 longPressProps: (0, $5sxTM$reactariautils.mergeProps)(pressProps, descriptionProps)
80 };
81}
82
83
84//# sourceMappingURL=useLongPress.main.js.map