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

Function onKeyDown

packages/react-aria/src/focus/FocusScope.tsx:370–411  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

368
369 // Handle the Tab key to contain focus within the scope
370 let onKeyDown = e => {
371 if (
372 e.key !== 'Tab' ||
373 e.altKey ||
374 e.ctrlKey ||
375 e.metaKey ||
376 !shouldContainFocus(scopeRef) ||
377 e.isComposing
378 ) {
379 return;
380 }
381
382 let focusedElement = getActiveElement(ownerDocument);
383 let scope = scopeRef.current;
384 if (!scope || !isElementInScope(focusedElement, scope)) {
385 return;
386 }
387
388 let scopeRoot = getScopeRoot(scope);
389 let walker = getFocusableTreeWalker(scopeRoot, {tabbable: true}, scope);
390 if (!focusedElement) {
391 return;
392 }
393 walker.currentNode = focusedElement;
394 let nextElement = (
395 e.shiftKey ? walker.previousNode() : walker.nextNode()
396 ) as FocusableElement;
397 if (!nextElement) {
398 walker.currentNode = e.shiftKey
399 ? scope[scope.length - 1].nextElementSibling!
400 : scope[0].previousElementSibling!;
401 nextElement = (e.shiftKey ? walker.previousNode() : walker.nextNode()) as FocusableElement;
402 }
403
404 e.preventDefault();
405 if (nextElement) {
406 focusElement(nextElement, true);
407 if (nextElement instanceof getOwnerWindow(nextElement).HTMLInputElement) {
408 nextElement.select();
409 }
410 }
411 };
412
413 let onFocus: EventListener = e => {
414 // If focusing an element in a child scope of the currently active scope, the child becomes active.

Callers

nothing calls this directly

Calls 14

getActiveElementFunction · 0.90
getOwnerWindowFunction · 0.90
shouldContainFocusFunction · 0.85
isElementInScopeFunction · 0.85
getScopeRootFunction · 0.85
getFocusableTreeWalkerFunction · 0.85
focusElementFunction · 0.85
isElementInChildScopeFunction · 0.85
shouldRestoreFocusFunction · 0.85
isElementInAnyScopeFunction · 0.85
previousNodeMethod · 0.80
nextNodeMethod · 0.80

Tested by

no test coverage detected