UNPKG

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