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

Function useActiveScopeTracker

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

Source from the content-addressed store, hash-verified

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