// This variable is useful when disableAutoFocus is true.
// It waits for the active element to move into the component to activate.
constactivated=React.useRef(false);
constrootRef=React.useRef(null);
// @ts-expect-error TODO upstream fix
consthandleRef=useForkRef(children.ref,rootRef);
constlastKeydown=React.useRef(null);
React.useEffect(()=>{
// We might render an empty child.
if(!open||!rootRef.current){
return;
}
activated.current=!disableAutoFocus;
},[disableAutoFocus,open]);
React.useEffect(()=>{
// We might render an empty child.
if(!open||!rootRef.current){
return;
}
constdoc=ownerDocument(rootRef.current);
if(!rootRef.current.contains(doc.activeElement)){
if(!rootRef.current.hasAttribute('tabIndex')){
if(process.env.NODE_ENV!=='production'){
console.error(['MUI: The modal content node does not accept focus.','For the benefit of assistive technologies, '+'the tabIndex of the node is being set to "-1".'].join('\n'));
}
rootRef.current.setAttribute('tabIndex','-1');
}
if(activated.current){
rootRef.current.focus();
}
}
return()=>{
// restoreLastFocus()
if(!disableRestoreFocus){
// In IE11 it is possible for document.activeElement to be null resulting
// in nodeToRestore.current being null.
// Not all elements in IE11 have a focus method.
// Once IE11 support is dropped the focus() call can be unconditional.