( scopeRef: RefObject<Element[] | null>, restore?: boolean, contain?: boolean )
| 595 | } |
| 596 | |
| 597 | function 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 | |
| 630 | function shouldRestoreFocus(scopeRef: ScopeRef) { |
| 631 | let scope = focusScopeTree.getTreeNode(activeScope); |
no test coverage detected