Skip to content

Commit

Permalink
Compose: Correctly call timeout cleanup (WordPress#65184)
Browse files Browse the repository at this point in the history
Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
Co-authored-by: tyxla <tyxla@git.wordpress.org>
  • Loading branch information
3 people committed Sep 10, 2024
1 parent 57e0b9c commit a925dcd
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions packages/compose/src/hooks/use-focus-on-mount/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) {
Expand Down

0 comments on commit a925dcd

Please sign in to comment.