Skip to content

Commit

Permalink
Compose: Fix 'useFocusOnMount' cleanup callback (WordPress#62053)
Browse files Browse the repository at this point in the history
Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
Co-authored-by: youknowriad <youknowriad@git.wordpress.org>
Co-authored-by: tyxla <tyxla@git.wordpress.org>
  • Loading branch information
4 people authored and carstingaxion committed Jun 4, 2024
1 parent 09df1b9 commit 95f0eec
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions packages/compose/src/hooks/use-focus-on-mount/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
/**
* WordPress dependencies
*/
import { useRef, useEffect, useCallback } from '@wordpress/element';
import { useRef, useEffect } from '@wordpress/element';
import { focus } from '@wordpress/dom';

/**
* Internal dependencies
*/
import useRefEffect from '../use-ref-effect';

/**
* Hook used to focus the first tabbable element on mount.
*
Expand Down Expand Up @@ -50,15 +55,7 @@ export default function useFocusOnMount( focusOnMount = 'firstElement' ) {
focusOnMountRef.current = focusOnMount;
}, [ focusOnMount ] );

useEffect( () => {
return () => {
if ( timerId.current ) {
clearTimeout( timerId.current );
}
};
}, [] );

return useCallback( ( node ) => {
return useRefEffect( ( node ) => {
if ( ! node || focusOnMountRef.current === false ) {
return;
}
Expand All @@ -80,5 +77,11 @@ export default function useFocusOnMount( focusOnMount = 'firstElement' ) {
}

setFocus( node );

return () => {
if ( timerId.current ) {
clearTimeout( timerId.current );
}
};
}, [] );
}

0 comments on commit 95f0eec

Please sign in to comment.