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

Function onClick

packages/react-aria/src/interactions/usePress.ts:456–510  ·  view source on GitHub ↗
(e)

Source from the content-addressed store, hash-verified

454 }
455 },
456 onClick(e) {
457 if (e && !nodeContains(e.currentTarget, getEventTarget(e))) {
458 return;
459 }
460
461 if (e && e.button === 0 && !state.isTriggeringEvent && !(openLink as any).isOpening) {
462 let shouldStopPropagation = true;
463 if (isDisabled) {
464 e.preventDefault();
465 }
466
467 // A click fired by the browser as a key's default behavior is indistinguishable from a
468 // virtual click, so skip the virtual handling below. It only occurs if nothing prevented
469 // the key event's default behavior, and unlike element.click() it is always trusted.
470 let isDefaultKeyboardClick =
471 e.nativeEvent.isTrusted && state.defaultKeyboardClickEvent?.defaultPrevented === false;
472
473 // If triggered from a screen reader or by using element.click(),
474 // trigger as if it were a keyboard click.
475 if (
476 !state.ignoreEmulatedMouseEvents &&
477 !isDefaultKeyboardClick &&
478 !state.isPressed &&
479 (state.pointerType === 'virtual' || isVirtualClick(e.nativeEvent))
480 ) {
481 let stopPressStart = triggerPressStart(e, 'virtual');
482 let stopPressUp = triggerPressUpEvent(e, 'virtual');
483 let stopPressEnd = triggerPressEndEvent(e, 'virtual');
484 triggerClick(e);
485 shouldStopPropagation = stopPressStart && stopPressUp && stopPressEnd;
486 } else if (state.isPressed && state.pointerType !== 'keyboard') {
487 let pointerType =
488 state.pointerType ||
489 ((e.nativeEvent as PointerEvent).pointerType as PointerType) ||
490 'virtual';
491 let stopPressUp = triggerPressUpEvent(createEvent(e.currentTarget, e), pointerType);
492 let stopPressEnd = triggerPressEndEvent(
493 createEvent(e.currentTarget, e),
494 pointerType,
495 true
496 );
497 shouldStopPropagation = stopPressUp && stopPressEnd;
498 state.isOverTarget = false;
499 triggerClick(e);
500 // oxlint-disable-next-line react/react-compiler
501 cancelEvent(e);
502 }
503
504 state.ignoreEmulatedMouseEvents = false;
505 state.defaultKeyboardClickEvent = null;
506 if (shouldStopPropagation) {
507 e.stopPropagation();
508 }
509 }
510 }
511 };
512
513 let onKeyUp = (e: KeyboardEvent) => {

Callers 1

usePressFunction · 0.70

Calls 4

nodeContainsFunction · 0.90
getEventTargetFunction · 0.90
isVirtualClickFunction · 0.90
createEventFunction · 0.85

Tested by

no test coverage detected