MCPcopy Index your code
hub / github.com/adobe/react-spectrum / onKeyDown

Function onKeyDown

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

Source from the content-addressed store, hash-verified

387 let state = ref.current;
388 let pressProps: DOMAttributes = {
389 onKeyDown(e) {
390 if (
391 isValidKeyboardEvent(e.nativeEvent, e.currentTarget as Element) &&
392 nodeContains(e.currentTarget as Element, getEventTarget(e) as Element)
393 ) {
394 if (shouldPreventDefaultKeyboard(getEventTarget(e) as Element, e.key)) {
395 e.preventDefault();
396 }
397
398 // If the event is repeating, it may have started on a different element
399 // after which focus moved to the current element. Ignore these events and
400 // only handle the first key down event.
401 let shouldStopPropagation = true;
402 if (!state.isPressed && !e.repeat) {
403 state.target = e.currentTarget;
404 state.isPressed = true;
405 state.pointerType = 'keyboard';
406 shouldStopPropagation = triggerPressStart(e, 'keyboard');
407 }
408
409 // Focus may move before the key up event, so register the event on the document
410 // instead of the same element where the key down event occurred. Make it capturing so that it will trigger
411 // before stopPropagation from useKeyboard on a child element may happen and thus we can still call triggerPress for the parent element.
412 let originalTarget = e.currentTarget;
413 let pressUp = e => {
414 if (
415 isValidKeyboardEvent(e, originalTarget) &&
416 !e.repeat &&
417 nodeContains(originalTarget, getEventTarget(e) as Element) &&
418 state.target
419 ) {
420 // eslint-disable-next-line react-hooks/rules-of-hooks
421 triggerPressUpEvent(createEvent(state.target, e), 'keyboard');
422 }
423 };
424
425 addGlobalListener(
426 getOwnerDocument(e.currentTarget),
427 'keyup',
428 chain(pressUp, onKeyUp),
429 true
430 );
431
432 if (shouldStopPropagation) {
433 e.stopPropagation();
434 }
435
436 // Keep track of the keydown events that occur while the Meta (e.g. Command) key is held.
437 // macOS has a bug where keyup events are not fired while the Meta key is down.
438 // When the Meta key itself is released we will get an event for that, and we'll act as if
439 // all of these other keys were released as well.
440 // https://bugs.chromium.org/p/chromium/issues/detail?id=1393524
441 // https://bugs.webkit.org/show_bug.cgi?id=55291
442 // https://bugzilla.mozilla.org/show_bug.cgi?id=1299553
443 if (e.metaKey && isMac()) {
444 state.metaKeyEvents?.set(e.key, e.nativeEvent);
445 }
446 } else if (e.key === 'Meta') {

Callers

nothing calls this directly

Calls 7

nodeContainsFunction · 0.90
getEventTargetFunction · 0.90
getOwnerDocumentFunction · 0.90
chainFunction · 0.90
isValidKeyboardEventFunction · 0.85
setMethod · 0.45

Tested by

no test coverage detected