13
13
import {
14
14
createShadowTreeWalker ,
15
15
getActiveElement ,
16
+ getEventTarget ,
16
17
getOwnerDocument ,
17
18
isAndroid ,
18
19
isChrome ,
@@ -342,13 +343,13 @@ function useFocusContainment(scopeRef: RefObject<Element[] | null>, contain?: bo
342
343
}
343
344
} ;
344
345
345
- let onFocus = ( e ) => {
346
+ let onFocus : EventListener = ( e ) => {
346
347
// If focusing an element in a child scope of the currently active scope, the child becomes active.
347
348
// 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 ) ) {
349
350
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 ) ) {
352
353
// If a focus event occurs outside the active scope (e.g. user tabs from browser location bar),
353
354
// restore focus to the previously focused node or the first tabbable element in the active scope.
354
355
if ( focusedNode . current ) {
@@ -357,11 +358,11 @@ function useFocusContainment(scopeRef: RefObject<Element[] | null>, contain?: bo
357
358
focusFirstInScope ( activeScope . current ) ;
358
359
}
359
360
} else if ( shouldContainFocus ( scopeRef ) ) {
360
- focusedNode . current = e . target ;
361
+ focusedNode . current = getEventTarget ( e ) as FocusableElement ;
361
362
}
362
363
} ;
363
364
364
- let onBlur = ( e ) => {
365
+ let onBlur : EventListener = ( e ) => {
365
366
// Firefox doesn't shift focus back to the Dialog properly without this
366
367
if ( raf . current ) {
367
368
cancelAnimationFrame ( raf . current ) ;
@@ -377,8 +378,9 @@ function useFocusContainment(scopeRef: RefObject<Element[] | null>, contain?: bo
377
378
let activeElement = getActiveElement ( ownerDocument ) ;
378
379
if ( ! shouldSkipFocusRestore && activeElement && shouldContainFocus ( scopeRef ) && ! isElementInChildScope ( activeElement , scopeRef ) ) {
379
380
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 ;
382
384
focusedNode . current ?. focus ( ) ;
383
385
} else if ( activeScope . current ) {
384
386
focusFirstInScope ( activeScope . current ) ;
@@ -521,7 +523,7 @@ function useActiveScopeTracker(scopeRef: RefObject<Element[] | null>, restore?:
521
523
const ownerDocument = getOwnerDocument ( scope ? scope [ 0 ] : undefined ) ;
522
524
523
525
let onFocus = ( e ) => {
524
- let target = e . target as Element ;
526
+ let target = getEventTarget ( e ) as Element ;
525
527
if ( isElementInScope ( target , scopeRef . current ) ) {
526
528
activeScope = scopeRef ;
527
529
} else if ( ! isElementInAnyScope ( target ) ) {
@@ -735,7 +737,7 @@ function restoreFocusToElement(node: FocusableElement) {
735
737
* Create a [TreeWalker]{@link https://developer.mozilla.org/en-US/docs/Web/API/TreeWalker}
736
738
* that matches all focusable/tabbable elements.
737
739
*/
738
- export function getFocusableTreeWalker ( root : Element , opts ?: FocusManagerOptions , scope ?: Element [ ] ) : ShadowTreeWalker {
740
+ export function getFocusableTreeWalker ( root : Element , opts ?: FocusManagerOptions , scope ?: Element [ ] ) : ShadowTreeWalker | TreeWalker {
739
741
let filter = opts ?. tabbable ? isTabbable : isFocusable ;
740
742
741
743
// Ensure that root is an Element or fall back appropriately
@@ -863,7 +865,7 @@ export function createFocusManager(ref: RefObject<Element | null>, defaultOption
863
865
} ;
864
866
}
865
867
866
- function last ( walker : ShadowTreeWalker ) {
868
+ function last ( walker : ShadowTreeWalker | TreeWalker ) {
867
869
let next : FocusableElement | undefined = undefined ;
868
870
let last : FocusableElement ;
869
871
do {
0 commit comments