@@ -320,7 +320,7 @@ function useFocusContainment(scopeRef: RefObject<Element[]>, contain: boolean) {
320
320
}
321
321
let focusNextInChildScope = ( childScopeRef : ScopeRef ) : FocusableElement | null => {
322
322
if ( childScopeRef !== scopeRef ) {
323
- let walker = getFocusableTreeWalker ( getScopeRoot ( childScopeRef . current ) , { tabbable : true } , scope , scopeRef ) ;
323
+ let walker = getFocusableTreeWalker ( getScopeRoot ( childScopeRef . current ) , { tabbable : true } , scope ) ;
324
324
walker . currentNode = focusedElement ;
325
325
let nextElement = ( e . shiftKey ? walker . previousNode ( ) : walker . nextNode ( ) ) as FocusableElement ;
326
326
if ( nextElement ) {
@@ -568,7 +568,7 @@ function useRestoreFocus(scopeRef: RefObject<Element[]>, restoreFocus: boolean,
568
568
let onFocus = ( ) => {
569
569
// If focusing an element in a child scope of the currently active scope, the child becomes active.
570
570
// Moving out of the active scope to an ancestor is not allowed.
571
- if ( ( ! activeScope || isAncestorScope ( activeScope , scopeRef ) ) &&
571
+ if ( ( ! activeScope || isAncestorScope ( activeScope , scopeRef ) ) &&
572
572
isElementInScope ( document . activeElement , scopeRef . current )
573
573
) {
574
574
activeScope = scopeRef ;
@@ -709,7 +709,7 @@ function useRestoreFocus(scopeRef: RefObject<Element[]>, restoreFocus: boolean,
709
709
* Create a [TreeWalker]{@link https://developer.mozilla.org/en-US/docs/Web/API/TreeWalker}
710
710
* that matches all focusable/tabbable elements.
711
711
*/
712
- export function getFocusableTreeWalker ( root : Element , opts ?: FocusManagerOptions , scope ?: Element [ ] , scopeRef ?: ScopeRef ) {
712
+ export function getFocusableTreeWalker ( root : Element , opts ?: FocusManagerOptions , scope ?: Element [ ] ) {
713
713
let selector = opts ?. tabbable ? TABBABLE_ELEMENT_SELECTOR : FOCUSABLE_ELEMENT_SELECTOR ;
714
714
let walker = document . createTreeWalker (
715
715
root ,
@@ -721,6 +721,16 @@ export function getFocusableTreeWalker(root: Element, opts?: FocusManagerOptions
721
721
return NodeFilter . FILTER_REJECT ;
722
722
}
723
723
724
+ let scopeRef = null ;
725
+ if ( scope ) {
726
+ for ( let node of focusScopeTree . traverse ( ) ) {
727
+ if ( node . scopeRef . current === scope ) {
728
+ scopeRef = node . scopeRef ;
729
+ break ;
730
+ }
731
+ }
732
+ }
733
+
724
734
if ( ( node as Element ) . matches ( selector )
725
735
&& isElementVisible ( node as Element )
726
736
&& ( ( ! scope || isElementInScope ( node as Element , scope ) ) || ( scopeRef && isElementInChildScope ( node as Element , scopeRef ) ) )
0 commit comments