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

Function onFocus

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

Source from the content-addressed store, hash-verified

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.
399 // Moving out of the active scope to an ancestor is not allowed.
400 if (
401 (!activeScope || isAncestorScope(activeScope, scopeRef)) &&
402 isElementInScope(getEventTarget(e) as Element, scopeRef.current)
403 ) {
404 activeScope = scopeRef;
405 focusedNode.current = getEventTarget(e) as FocusableElement;
406 } else if (
407 shouldContainFocus(scopeRef) &&
408 !isElementInChildScope(getEventTarget(e) as Element, scopeRef)
409 ) {
410 // If a focus event occurs outside the active scope (e.g. user tabs from browser location bar),
411 // restore focus to the previously focused node or the first tabbable element in the active scope.
412 if (focusedNode.current) {
413 focusElement(focusedNode.current);
414 } else if (activeScope && activeScope.current) {
415 focusFirstInScope(activeScope.current);
416 }
417 } else if (shouldContainFocus(scopeRef)) {
418 focusedNode.current = getEventTarget(e) as FocusableElement;
419 }
420 };
421
422 let onBlur: EventListener = e => {
423 // Firefox doesn't shift focus back to the Dialog properly without this

Callers

nothing calls this directly

Calls 9

getEventTargetFunction · 0.90
getActiveElementFunction · 0.90
isAncestorScopeFunction · 0.85
isElementInScopeFunction · 0.85
shouldContainFocusFunction · 0.85
isElementInChildScopeFunction · 0.85
focusElementFunction · 0.85
focusFirstInScopeFunction · 0.85
isElementInAnyScopeFunction · 0.85

Tested by

no test coverage detected