Skip to content

Commit 1fc531f

Browse files
committed
Address more CR feedback.
1 parent 3f3ac4f commit 1fc531f

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

packages/editor/src/components/post-featured-image/index.js

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
Notice,
1919
} from '@wordpress/components';
2020
import { isBlobURL } from '@wordpress/blob';
21-
import { useState, useRef } from '@wordpress/element';
21+
import { useState, useRef, useEffect } from '@wordpress/element';
2222
import { compose } from '@wordpress/compose';
2323
import { useSelect, withDispatch, withSelect } from '@wordpress/data';
2424
import {
@@ -106,6 +106,13 @@ function PostFeaturedImage( {
106106
const [ isLoading, setIsLoading ] = useState( false );
107107
const { getSettings } = useSelect( blockEditorStore );
108108
const { mediaSourceUrl } = getMediaDetails( media, currentPostId );
109+
const toggleFocusTimerRef = useRef();
110+
111+
useEffect( () => {
112+
return () => {
113+
clearTimeout( toggleFocusTimerRef.current );
114+
};
115+
}, [] );
109116

110117
function onDropFiles( filesList ) {
111118
getSettings().mediaUpload( {
@@ -269,7 +276,12 @@ function PostFeaturedImage( {
269276
className="editor-post-featured-image__action"
270277
onClick={ () => {
271278
onRemoveImage();
272-
toggleRef.current.focus();
279+
// The toggle button is rendered conditionally, we need
280+
// to wait it is rendered before moving focus to it.
281+
toggleFocusTimerRef.current =
282+
setTimeout( () => {
283+
toggleRef.current?.focus();
284+
} );
273285
} }
274286
variant={
275287
isMissingMedia
@@ -294,15 +306,10 @@ function PostFeaturedImage( {
294306
}
295307

296308
const applyWithSelect = withSelect( ( select ) => {
297-
const { getMedia, getPostType } = select( coreStore );
309+
const { getMedia, getPostType, hasFinishedResolution } =
310+
select( coreStore );
298311
const { getCurrentPostId, getEditedPostAttribute } = select( editorStore );
299312
const featuredImageId = getEditedPostAttribute( 'featured_media' );
300-
const isRequestingFeaturedImageMedia =
301-
!! featuredImageId &&
302-
! select( coreStore ).hasFinishedResolution( 'getMedia', [
303-
featuredImageId,
304-
{ context: 'view' },
305-
] );
306313

307314
return {
308315
media: featuredImageId
@@ -311,7 +318,12 @@ const applyWithSelect = withSelect( ( select ) => {
311318
currentPostId: getCurrentPostId(),
312319
postType: getPostType( getEditedPostAttribute( 'type' ) ),
313320
featuredImageId,
314-
isRequestingFeaturedImageMedia,
321+
isRequestingFeaturedImageMedia:
322+
!! featuredImageId &&
323+
! hasFinishedResolution( 'getMedia', [
324+
featuredImageId,
325+
{ context: 'view' },
326+
] ),
315327
};
316328
} );
317329

0 commit comments

Comments
 (0)