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

Function onKeyDown

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

Source from the content-addressed store, hash-verified

352
353 // Handle the Tab key to contain focus within the scope
354 let onKeyDown = e => {
355 if (
356 e.key !== 'Tab' ||
357 e.altKey ||
358 e.ctrlKey ||
359 e.metaKey ||
360 !shouldContainFocus(scopeRef) ||
361 e.isComposing
362 ) {
363 return;
364 }
365
366 let focusedElement = getActiveElement(ownerDocument);
367 let scope = scopeRef.current;
368 if (!scope || !isElementInScope(focusedElement, scope)) {
369 return;
370 }
371
372 let scopeRoot = getScopeRoot(scope);
373 let walker = getFocusableTreeWalker(scopeRoot, {tabbable: true}, scope);
374 if (!focusedElement) {
375 return;
376 }
377 walker.currentNode = focusedElement;
378 let nextElement = (
379 e.shiftKey ? walker.previousNode() : walker.nextNode()
380 ) as FocusableElement;
381 if (!nextElement) {
382 walker.currentNode = e.shiftKey
383 ? scope[scope.length - 1].nextElementSibling!
384 : scope[0].previousElementSibling!;
385 nextElement = (e.shiftKey ? walker.previousNode() : walker.nextNode()) as FocusableElement;
386 }
387
388 e.preventDefault();
389 if (nextElement) {
390 focusElement(nextElement, true);
391 if (nextElement instanceof getOwnerWindow(nextElement).HTMLInputElement) {
392 nextElement.select();
393 }
394 }
395 };
396
397 let onFocus: EventListener = e => {
398 // 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