1313import {
1414 createShadowTreeWalker ,
1515 getActiveElement ,
16+ getEventTarget ,
1617 getOwnerDocument ,
1718 isAndroid ,
1819 isChrome ,
@@ -342,13 +343,13 @@ function useFocusContainment(scopeRef: RefObject<Element[] | null>, contain?: bo
342343 }
343344 } ;
344345
345- let onFocus = ( e ) => {
346+ let onFocus : EventListener = ( e ) => {
346347 // If focusing an element in a child scope of the currently active scope, the child becomes active.
347348 // Moving out of the active scope to an ancestor is not allowed.
348- if ( ( ! activeScope || isAncestorScope ( activeScope , scopeRef ) ) && isElementInScope ( e . target , scopeRef . current ) ) {
349+ if ( ( ! activeScope || isAncestorScope ( activeScope , scopeRef ) ) && isElementInScope ( getEventTarget ( e ) as Element , scopeRef . current ) ) {
349350 activeScope = scopeRef ;
350- focusedNode . current = e . target ;
351- } else if ( shouldContainFocus ( scopeRef ) && ! isElementInChildScope ( e . target , scopeRef ) ) {
351+ focusedNode . current = getEventTarget ( e ) as FocusableElement ;
352+ } else if ( shouldContainFocus ( scopeRef ) && ! isElementInChildScope ( getEventTarget ( e ) as Element , scopeRef ) ) {
352353 // If a focus event occurs outside the active scope (e.g. user tabs from browser location bar),
353354 // restore focus to the previously focused node or the first tabbable element in the active scope.
354355 if ( focusedNode . current ) {
@@ -357,11 +358,11 @@ function useFocusContainment(scopeRef: RefObject<Element[] | null>, contain?: bo
357358 focusFirstInScope ( activeScope . current ) ;
358359 }
359360 } else if ( shouldContainFocus ( scopeRef ) ) {
360- focusedNode . current = e . target ;
361+ focusedNode . current = getEventTarget ( e ) as FocusableElement ;
361362 }
362363 } ;
363364
364- let onBlur = ( e ) => {
365+ let onBlur : EventListener = ( e ) => {
365366 // Firefox doesn't shift focus back to the Dialog properly without this
366367 if ( raf . current ) {
367368 cancelAnimationFrame ( raf . current ) ;
@@ -377,8 +378,9 @@ function useFocusContainment(scopeRef: RefObject<Element[] | null>, contain?: bo
377378 let activeElement = getActiveElement ( ownerDocument ) ;
378379 if ( ! shouldSkipFocusRestore && activeElement && shouldContainFocus ( scopeRef ) && ! isElementInChildScope ( activeElement , scopeRef ) ) {
379380 activeScope = scopeRef ;
380- if ( e . target . isConnected ) {
381- focusedNode . current = e . target ;
381+ let target = getEventTarget ( e ) as FocusableElement ;
382+ if ( target && target . isConnected ) {
383+ focusedNode . current = target ;
382384 focusedNode . current ?. focus ( ) ;
383385 } else if ( activeScope . current ) {
384386 focusFirstInScope ( activeScope . current ) ;
@@ -521,7 +523,7 @@ function useActiveScopeTracker(scopeRef: RefObject<Element[] | null>, restore?:
521523 const ownerDocument = getOwnerDocument ( scope ? scope [ 0 ] : undefined ) ;
522524
523525 let onFocus = ( e ) => {
524- let target = e . target as Element ;
526+ let target = getEventTarget ( e ) as Element ;
525527 if ( isElementInScope ( target , scopeRef . current ) ) {
526528 activeScope = scopeRef ;
527529 } else if ( ! isElementInAnyScope ( target ) ) {
@@ -735,7 +737,7 @@ function restoreFocusToElement(node: FocusableElement) {
735737 * Create a [TreeWalker]{@link https://developer.mozilla.org/en-US/docs/Web/API/TreeWalker}
736738 * that matches all focusable/tabbable elements.
737739 */
738- export function getFocusableTreeWalker ( root : Element , opts ?: FocusManagerOptions , scope ?: Element [ ] ) : ShadowTreeWalker {
740+ export function getFocusableTreeWalker ( root : Element , opts ?: FocusManagerOptions , scope ?: Element [ ] ) : ShadowTreeWalker | TreeWalker {
739741 let filter = opts ?. tabbable ? isTabbable : isFocusable ;
740742
741743 // Ensure that root is an Element or fall back appropriately
@@ -863,7 +865,7 @@ export function createFocusManager(ref: RefObject<Element | null>, defaultOption
863865 } ;
864866}
865867
866- function last ( walker : ShadowTreeWalker ) {
868+ function last ( walker : ShadowTreeWalker | TreeWalker ) {
867869 let next : FocusableElement | undefined = undefined ;
868870 let last : FocusableElement ;
869871 do {
0 commit comments