@@ -11,6 +11,7 @@ import {get} from '../constants'
1111import { ConfirmationDialog } from '../Dialog/ConfirmationDialog'
1212import { useControllableState } from '../hooks/useControllableState'
1313import { useId } from '../hooks/useId'
14+ import useSafeTimeout from '../hooks/useSafeTimeout'
1415import Spinner from '../Spinner'
1516import sx , { SxProp } from '../sx'
1617import Text from '../Text'
@@ -513,6 +514,7 @@ const SubTree: React.FC<TreeViewSubTreeProps> = ({count, state, children}) => {
513514 const ref = React . useRef < HTMLElement > ( null )
514515 const [ loadingFocused , setLoadingFocused ] = React . useState ( false )
515516 const previousState = usePreviousValue ( state )
517+ const { safeSetTimeout} = useSafeTimeout ( )
516518
517519 React . useEffect ( ( ) => {
518520 // If `state` is undefined, we're working in a synchronous context and need
@@ -549,15 +551,19 @@ const SubTree: React.FC<TreeViewSubTreeProps> = ({count, state, children}) => {
549551 const firstChild = getFirstChildElement ( parentElement )
550552
551553 if ( firstChild ) {
552- firstChild . focus ( )
554+ safeSetTimeout ( ( ) => {
555+ firstChild . focus ( )
556+ } )
553557 } else {
554- parentElement . focus ( )
558+ safeSetTimeout ( ( ) => {
559+ parentElement . focus ( )
560+ } )
555561 }
556562
557563 setLoadingFocused ( false )
558564 }
559565 }
560- } , [ loadingFocused , previousState , state , itemId , announceUpdate , ref ] )
566+ } , [ loadingFocused , previousState , state , itemId , announceUpdate , ref , safeSetTimeout ] )
561567
562568 // Track focus on the loading indicator
563569 React . useEffect ( ( ) => {
0 commit comments