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

Function onKeyDown

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

Source from the content-addressed store, hash-verified

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