MCPcopy
hub / github.com/adobe/react-spectrum / usePress

Function usePress

packages/react-aria/src/interactions/usePress.ts:193–1033  ·  view source on GitHub ↗
(props: PressHookProps)

Source from the content-addressed store, hash-verified

191 * of dealing with pointer and keyboard events.
192 */
193export function usePress(props: PressHookProps): PressResult {
194 let {
195 onPress,
196 onPressChange,
197 onPressStart,
198 onPressEnd,
199 onPressUp,
200 onClick,
201 isDisabled,
202 isPressed: isPressedProp,
203 preventFocusOnPress,
204 shouldCancelOnPointerExit,
205 allowTextSelectionOnPress,
206 ref: domRef,
207 ...domProps
208 } = usePressResponderContext(props);
209
210 let [isPressed, setPressed] = useState(false);
211 let ref = useRef<PressState>({
212 isPressed: false,
213 ignoreEmulatedMouseEvents: false,
214 didFirePressStart: false,
215 isTriggeringEvent: false,
216 activePointerId: null,
217 target: null,
218 isOverTarget: false,
219 pointerType: null,
220 disposables: []
221 });
222
223 let {addGlobalListener, removeAllGlobalListeners} = useGlobalListeners();
224
225 let triggerPressStart = useCallback(
226 (originalEvent: EventBase, pointerType: PointerType) => {
227 let state = ref.current;
228 if (isDisabled || state.didFirePressStart) {
229 return false;
230 }
231
232 let shouldStopPropagation = true;
233 state.isTriggeringEvent = true;
234 if (onPressStart) {
235 let event = new PressEvent('pressstart', pointerType, originalEvent);
236 onPressStart(event);
237 shouldStopPropagation = event.shouldStopPropagation;
238 }
239
240 if (onPressChange) {
241 onPressChange(true);
242 }
243
244 state.isTriggeringEvent = false;
245 state.didFirePressStart = true;
246 setPressed(true);
247 return shouldStopPropagation;
248 },
249 [isDisabled, onPressStart, onPressChange]
250 );

Callers 15

ExampleWithPressFunction · 0.90
ExampleFunction · 0.90
TestStyleChangeFunction · 0.90
PressableFunction · 0.90
PortalExampleFunction · 0.90
Pressable.tsxFile · 0.90
useLongPressFunction · 0.90
useLinkFunction · 0.90
useCalendarCellFunction · 0.90
useSelectableItemFunction · 0.90
useTableColumnResizeFunction · 0.90
useTableColumnHeaderFunction · 0.90

Calls 15

useGlobalListenersFunction · 0.90
useEffectEventFunction · 0.90
restoreTextSelectionFunction · 0.90
setEventTargetFunction · 0.90
createSyntheticEventFunction · 0.90
nodeContainsFunction · 0.90
getEventTargetFunction · 0.90
isVirtualPointerEventFunction · 0.90
disableTextSelectionFunction · 0.90
getOwnerDocumentFunction · 0.90
preventFocusFunction · 0.90
isVirtualClickFunction · 0.90

Tested by 6

ExampleWithPressFunction · 0.72
ExampleFunction · 0.72
TestStyleChangeFunction · 0.72
PressableFunction · 0.72
PortalExampleFunction · 0.72
ComponentFunction · 0.72