diff --git a/packages/block-library/src/image/edit.js b/packages/block-library/src/image/edit.js index 54246499701a9c..f74530c9d54856 100644 --- a/packages/block-library/src/image/edit.js +++ b/packages/block-library/src/image/edit.js @@ -272,14 +272,10 @@ export function ImageEdit( { [ `size-${ sizeSlug }` ]: sizeSlug, } ); - // Focussing the image caption after inserting an image relies on the - // component remounting. This needs to be fixed. - const key = !! url; - return ( <> { controls } - + { url && ( { const { getMedia } = select( 'core' ); @@ -142,6 +144,16 @@ export default function Image( { .then( ( blob ) => setExternalBlob( blob ) ); }, [ id, url, isSelected, externalBlob ] ); + // Focus the caption after inserting an image from the placeholder. This is + // done to preserve the behaviour of focussing the first tabbable element + // when a block is mounted. Previously, the image block would remount when + // the placeholder is removed. Maybe this behaviour could be removed. + useEffect( () => { + if ( url && ! prevUrl && isSelected ) { + captionRef.current.focus(); + } + }, [ url, prevUrl ] ); + function onResizeStart() { toggleSelection( false ); } @@ -486,6 +498,7 @@ export default function Image( { { img } { ( ! RichText.isEmpty( caption ) || isSelected ) && (