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

Function useActiveScopeTracker

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

Source from the content-addressed store, hash-verified

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