( scopeRef: RefObject<Element[] | null>, restore?: boolean, contain?: boolean )
| 577 | } |
| 578 | |
| 579 | function 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 | |
| 612 | function shouldRestoreFocus(scopeRef: ScopeRef) { |
| 613 | let scope = focusScopeTree.getTreeNode(activeScope); |
no test coverage detected