@@ -2,6 +2,7 @@ import * as React from 'react';
2
2
import { useComposedRefs } from '@radix-ui/react-compose-refs' ;
3
3
import { Primitive } from '@radix-ui/react-primitive' ;
4
4
import { useCallbackRef } from '@radix-ui/react-use-callback-ref' ;
5
+ import { getDeepActiveElement } from '@radix-ui/deep-active-element'
5
6
6
7
const AUTOFOCUS_ON_MOUNT = 'focusScope.autoFocusOnMount' ;
7
8
const AUTOFOCUS_ON_UNMOUNT = 'focusScope.autoFocusOnUnmount' ;
@@ -109,7 +110,7 @@ const FocusScope = React.forwardRef<FocusScopeElement, FocusScopeProps>((props,
109
110
// back to the document.body. In this case, we move focus to the container
110
111
// to keep focus trapped correctly.
111
112
function handleMutations ( mutations : MutationRecord [ ] ) {
112
- const focusedElement = document . activeElement as HTMLElement | null ;
113
+ const focusedElement = getDeepActiveElement ( ) as HTMLElement | null ;
113
114
if ( focusedElement !== document . body ) return ;
114
115
for ( const mutation of mutations ) {
115
116
if ( mutation . removedNodes . length > 0 ) focus ( container ) ;
@@ -132,7 +133,7 @@ const FocusScope = React.forwardRef<FocusScopeElement, FocusScopeProps>((props,
132
133
React . useEffect ( ( ) => {
133
134
if ( container ) {
134
135
focusScopesStack . add ( focusScope ) ;
135
- const previouslyFocusedElement = document . activeElement as HTMLElement | null ;
136
+ const previouslyFocusedElement = getDeepActiveElement ( ) as HTMLElement | null ;
136
137
const hasFocusedCandidate = container . contains ( previouslyFocusedElement ) ;
137
138
138
139
if ( ! hasFocusedCandidate ) {
@@ -141,7 +142,7 @@ const FocusScope = React.forwardRef<FocusScopeElement, FocusScopeProps>((props,
141
142
container . dispatchEvent ( mountEvent ) ;
142
143
if ( ! mountEvent . defaultPrevented ) {
143
144
focusFirst ( removeLinks ( getTabbableCandidates ( container ) ) , { select : true } ) ;
144
- if ( document . activeElement === previouslyFocusedElement ) {
145
+ if ( getDeepActiveElement ( ) === previouslyFocusedElement ) {
145
146
focus ( container ) ;
146
147
}
147
148
}
@@ -176,7 +177,7 @@ const FocusScope = React.forwardRef<FocusScopeElement, FocusScopeProps>((props,
176
177
if ( focusScope . paused ) return ;
177
178
178
179
const isTabKey = event . key === 'Tab' && ! event . altKey && ! event . ctrlKey && ! event . metaKey ;
179
- const focusedElement = document . activeElement as HTMLElement | null ;
180
+ const focusedElement = getDeepActiveElement ( ) as HTMLElement | null ;
180
181
181
182
if ( isTabKey && focusedElement ) {
182
183
const container = event . currentTarget as HTMLElement ;
@@ -216,10 +217,10 @@ FocusScope.displayName = FOCUS_SCOPE_NAME;
216
217
* Stops when focus has actually moved.
217
218
*/
218
219
function focusFirst ( candidates : HTMLElement [ ] , { select = false } = { } ) {
219
- const previouslyFocusedElement = document . activeElement ;
220
+ const previouslyFocusedElement = getDeepActiveElement ( ) ;
220
221
for ( const candidate of candidates ) {
221
222
focus ( candidate , { select } ) ;
222
- if ( document . activeElement !== previouslyFocusedElement ) return ;
223
+ if ( getDeepActiveElement ( ) !== previouslyFocusedElement ) return ;
223
224
}
224
225
}
225
226
@@ -290,7 +291,7 @@ function isSelectableInput(element: any): element is FocusableTarget & { select:
290
291
function focus ( element ?: FocusableTarget | null , { select = false } = { } ) {
291
292
// only focus if that element is focusable
292
293
if ( element && element . focus ) {
293
- const previouslyFocusedElement = document . activeElement ;
294
+ const previouslyFocusedElement = getDeepActiveElement ( ) ;
294
295
// NOTE: we prevent scrolling on focus, to minimize jarring transitions for users
295
296
element . focus ( { preventScroll : true } ) ;
296
297
// only select if its not the same element, it supports selection and we need to select
0 commit comments