From eee6ba722a808ac27559a7ed2e64f5e51d03d847 Mon Sep 17 00:00:00 2001 From: George Mamadashvili Date: Tue, 10 Sep 2024 12:17:04 +0400 Subject: [PATCH] Compose: Correctly call timeout cleanup --- .../src/hooks/use-focus-on-mount/index.js | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/packages/compose/src/hooks/use-focus-on-mount/index.js b/packages/compose/src/hooks/use-focus-on-mount/index.js index 77bee27772d2d0..bb02eb78e5689a 100644 --- a/packages/compose/src/hooks/use-focus-on-mount/index.js +++ b/packages/compose/src/hooks/use-focus-on-mount/index.js @@ -64,19 +64,17 @@ export default function useFocusOnMount( focusOnMount = 'firstElement' ) { return; } - if ( focusOnMountRef.current === 'firstElement' ) { - timerIdRef.current = setTimeout( () => { - const firstTabbable = focus.tabbable.find( node )[ 0 ]; - - if ( firstTabbable ) { - setFocus( firstTabbable ); - } - }, 0 ); - + if ( focusOnMountRef.current !== 'firstElement' ) { + setFocus( node ); return; } - setFocus( node ); + timerIdRef.current = setTimeout( () => { + const firstTabbable = focus.tabbable.find( node )[ 0 ]; + if ( firstTabbable ) { + setFocus( firstTabbable ); + } + }, 0 ); return () => { if ( timerIdRef.current ) {