@@ -7,6 +7,7 @@ import ownerDocument from '@mui/utils/ownerDocument';
77import getReactElementRef from '@mui/utils/getReactElementRef' ;
88import exactProp from '@mui/utils/exactProp' ;
99import elementAcceptingRef from '@mui/utils/elementAcceptingRef' ;
10+ import getActiveElement from '../utils/getActiveElement' ;
1011import { FocusTrapProps } from './FocusTrap.types' ;
1112
1213// Inspired by https://github.com/focus-trap/tabbable
@@ -162,8 +163,9 @@ function FocusTrap(props: FocusTrapProps): React.JSX.Element {
162163 }
163164
164165 const doc = ownerDocument ( rootRef . current ) ;
166+ const activeElement = getActiveElement ( doc ) ;
165167
166- if ( ! rootRef . current . contains ( doc . activeElement ) ) {
168+ if ( ! rootRef . current . contains ( activeElement ) ) {
167169 if ( ! rootRef . current . hasAttribute ( 'tabIndex' ) ) {
168170 if ( process . env . NODE_ENV !== 'production' ) {
169171 console . error (
@@ -209,6 +211,7 @@ function FocusTrap(props: FocusTrapProps): React.JSX.Element {
209211 }
210212
211213 const doc = ownerDocument ( rootRef . current ) ;
214+ const activeElement = getActiveElement ( doc ) ;
212215
213216 const loopFocus = ( nativeEvent : KeyboardEvent ) => {
214217 lastKeydown . current = nativeEvent ;
@@ -218,8 +221,8 @@ function FocusTrap(props: FocusTrapProps): React.JSX.Element {
218221 }
219222
220223 // Make sure the next tab starts from the right place.
221- // doc. activeElement refers to the origin.
222- if ( doc . activeElement === rootRef . current && nativeEvent . shiftKey ) {
224+ // activeElement refers to the origin.
225+ if ( activeElement === rootRef . current && nativeEvent . shiftKey ) {
223226 // We need to ignore the next contain as
224227 // it will try to move the focus back to the rootRef element.
225228 ignoreNextEnforceFocus . current = true ;
@@ -238,27 +241,29 @@ function FocusTrap(props: FocusTrapProps): React.JSX.Element {
238241 return ;
239242 }
240243
244+ const activeEl = getActiveElement ( doc ) ;
245+
241246 if ( ! doc . hasFocus ( ) || ! isEnabled ( ) || ignoreNextEnforceFocus . current ) {
242247 ignoreNextEnforceFocus . current = false ;
243248 return ;
244249 }
245250
246251 // The focus is already inside
247- if ( rootElement . contains ( doc . activeElement ) ) {
252+ if ( rootElement . contains ( activeEl ) ) {
248253 return ;
249254 }
250255
251256 // The disableEnforceFocus is set and the focus is outside of the focus trap (and sentinel nodes)
252257 if (
253258 disableEnforceFocus &&
254- doc . activeElement !== sentinelStart . current &&
255- doc . activeElement !== sentinelEnd . current
259+ activeEl !== sentinelStart . current &&
260+ activeEl !== sentinelEnd . current
256261 ) {
257262 return ;
258263 }
259264
260265 // if the focus event is not coming from inside the children's react tree, reset the refs
261- if ( doc . activeElement !== reactFocusEventTarget . current ) {
266+ if ( activeEl !== reactFocusEventTarget . current ) {
262267 reactFocusEventTarget . current = null ;
263268 } else if ( reactFocusEventTarget . current !== null ) {
264269 return ;
@@ -269,10 +274,7 @@ function FocusTrap(props: FocusTrapProps): React.JSX.Element {
269274 }
270275
271276 let tabbable : ReadonlyArray < HTMLElement > = [ ] ;
272- if (
273- doc . activeElement === sentinelStart . current ||
274- doc . activeElement === sentinelEnd . current
275- ) {
277+ if ( activeEl === sentinelStart . current || activeEl === sentinelEnd . current ) {
276278 tabbable = getTabbable ( rootRef . current ! ) ;
277279 }
278280
@@ -309,7 +311,8 @@ function FocusTrap(props: FocusTrapProps): React.JSX.Element {
309311 // The whatwg spec defines how the browser should behave but does not explicitly mention any events:
310312 // https://html.spec.whatwg.org/multipage/interaction.html#focus-fixup-rule.
311313 const interval = setInterval ( ( ) => {
312- if ( doc . activeElement && doc . activeElement . tagName === 'BODY' ) {
314+ const activeEl = getActiveElement ( doc ) ;
315+ if ( activeEl && activeEl . tagName === 'BODY' ) {
313316 contain ( ) ;
314317 }
315318 } , 50 ) ;
0 commit comments