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