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

Function onFocus

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

Source from the content-addressed store, hash-verified

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

Tested by

no test coverage detected