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

Function useActiveScopeTracker

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

Source from the content-addressed store, hash-verified

595}
596
597function useActiveScopeTracker(
598 scopeRef: RefObject<Element[] | null>,
599 restore?: boolean,
600 contain?: boolean
601) {
602 // tracks the active scope, in case restore and contain are both false.
603 // if either are true, this is tracked in useRestoreFocus or useFocusContainment.
604 useLayoutEffect(() => {
605 if (restore || contain) {
606 return;
607 }
608
609 let scope = scopeRef.current;
610 const ownerDocument = getOwnerDocument(scope ? scope[0] : undefined);
611
612 let onFocus = e => {
613 let target = getEventTarget(e) as Element;
614 if (isElementInScope(target, scopeRef.current)) {
615 activeScope = scopeRef;
616 } else if (!isElementInAnyScope(target)) {
617 activeScope = null;
618 }
619 };
620
621 ownerDocument.addEventListener('focusin', onFocus, false);
622 scope?.forEach(element => element.addEventListener('focusin', onFocus, false));
623 return () => {
624 ownerDocument.removeEventListener('focusin', onFocus, false);
625 scope?.forEach(element => element.removeEventListener('focusin', onFocus, false));
626 };
627 }, [scopeRef, restore, contain]);
628}
629
630function shouldRestoreFocus(scopeRef: ScopeRef) {
631 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