MCPcopy Create free account
hub / github.com/adobe/react-spectrum / onPressStart

Function onPressStart

packages/react-aria/src/interactions/useLongPress.ts:76–126  ·  view source on GitHub ↗
(e)

Source from the content-addressed store, hash-verified

74 let {pressProps} = usePress({
75 isDisabled,
76 onPressStart(e) {
77 e.continuePropagation();
78 if (e.pointerType === 'mouse' || e.pointerType === 'touch') {
79 if (onLongPressStart) {
80 onLongPressStart({
81 ...e,
82 type: 'longpressstart'
83 });
84 }
85
86 timeRef.current = setTimeout(() => {
87 // Prevent other usePress handlers from also handling this event.
88 e.target.dispatchEvent(new PointerEvent('pointercancel', {bubbles: true}));
89
90 // Ensure target is focused. On touch devices, browsers typically focus on pointer up.
91 if (getOwnerDocument(e.target).activeElement !== e.target) {
92 focusWithoutScrolling(e.target as FocusableElement);
93 }
94
95 if (onLongPress) {
96 onLongPress({
97 ...e,
98 type: 'longpress'
99 });
100 }
101 timeRef.current = undefined;
102 }, threshold);
103
104 // Prevent context menu, which may be opened on long press on touch devices
105 if (e.pointerType === 'touch') {
106 let onContextMenu = e => {
107 e.preventDefault();
108 };
109
110 let ownerWindow = getOwnerWindow(e.target);
111 addGlobalListener(e.target, 'contextmenu', onContextMenu, {once: true});
112 addGlobalListener(
113 ownerWindow,
114 'pointerup',
115 () => {
116 // If no contextmenu event is fired quickly after pointerup, remove the handler
117 // so future context menu events outside a long press are not prevented.
118 setTimeout(() => {
119 removeGlobalListener(e.target, 'contextmenu', onContextMenu);
120 }, 30);
121 },
122 {once: true}
123 );
124 }
125 }
126 },
127 onPressEnd(e) {
128 if (timeRef.current) {
129 clearTimeout(timeRef.current);

Callers 1

usePressFunction · 0.70

Calls 6

getOwnerDocumentFunction · 0.90
focusWithoutScrollingFunction · 0.90
getOwnerWindowFunction · 0.90
onLongPressStartFunction · 0.85
continuePropagationMethod · 0.65
onLongPressFunction · 0.50

Tested by

no test coverage detected