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

Function useActiveScopeTracker

packages/react-aria/src/focus/FocusScope.tsx:579–610  ·  view source on GitHub ↗
(
  scopeRef: RefObject<Element[] | null>,
  restore?: boolean,
  contain?: boolean
)

Source from the content-addressed store, hash-verified

577}
578
579function useActiveScopeTracker(
580 scopeRef: RefObject<Element[] | null>,
581 restore?: boolean,
582 contain?: boolean
583) {
584 // tracks the active scope, in case restore and contain are both false.
585 // if either are true, this is tracked in useRestoreFocus or useFocusContainment.
586 useLayoutEffect(() => {
587 if (restore || contain) {
588 return;
589 }
590
591 let scope = scopeRef.current;
592 const ownerDocument = getOwnerDocument(scope ? scope[0] : undefined);
593
594 let onFocus = e => {
595 let target = getEventTarget(e) as Element;
596 if (isElementInScope(target, scopeRef.current)) {
597 activeScope = scopeRef;
598 } else if (!isElementInAnyScope(target)) {
599 activeScope = null;
600 }
601 };
602
603 ownerDocument.addEventListener('focusin', onFocus, false);
604 scope?.forEach(element => element.addEventListener('focusin', onFocus, false));
605 return () => {
606 ownerDocument.removeEventListener('focusin', onFocus, false);
607 scope?.forEach(element => element.removeEventListener('focusin', onFocus, false));
608 };
609 }, [scopeRef, restore, contain]);
610}
611
612function shouldRestoreFocus(scopeRef: ScopeRef) {
613 let scope = focusScopeTree.getTreeNode(activeScope);

Callers 1

FocusScopeFunction · 0.85

Calls 3

getOwnerDocumentFunction · 0.90
addEventListenerMethod · 0.80
removeEventListenerMethod · 0.80

Tested by

no test coverage detected