@@ -18,7 +18,7 @@ import {
18
18
Notice ,
19
19
} from '@wordpress/components' ;
20
20
import { isBlobURL } from '@wordpress/blob' ;
21
- import { useState , useRef } from '@wordpress/element' ;
21
+ import { useState , useRef , useEffect } from '@wordpress/element' ;
22
22
import { compose } from '@wordpress/compose' ;
23
23
import { useSelect , withDispatch , withSelect } from '@wordpress/data' ;
24
24
import {
@@ -106,6 +106,13 @@ function PostFeaturedImage( {
106
106
const [ isLoading , setIsLoading ] = useState ( false ) ;
107
107
const { getSettings } = useSelect ( blockEditorStore ) ;
108
108
const { mediaSourceUrl } = getMediaDetails ( media , currentPostId ) ;
109
+ const toggleFocusTimerRef = useRef ( ) ;
110
+
111
+ useEffect ( ( ) => {
112
+ return ( ) => {
113
+ clearTimeout ( toggleFocusTimerRef . current ) ;
114
+ } ;
115
+ } , [ ] ) ;
109
116
110
117
function onDropFiles ( filesList ) {
111
118
getSettings ( ) . mediaUpload ( {
@@ -269,7 +276,12 @@ function PostFeaturedImage( {
269
276
className = "editor-post-featured-image__action"
270
277
onClick = { ( ) => {
271
278
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
+ } ) ;
273
285
} }
274
286
variant = {
275
287
isMissingMedia
@@ -294,15 +306,10 @@ function PostFeaturedImage( {
294
306
}
295
307
296
308
const applyWithSelect = withSelect ( ( select ) => {
297
- const { getMedia, getPostType } = select ( coreStore ) ;
309
+ const { getMedia, getPostType, hasFinishedResolution } =
310
+ select ( coreStore ) ;
298
311
const { getCurrentPostId, getEditedPostAttribute } = select ( editorStore ) ;
299
312
const featuredImageId = getEditedPostAttribute ( 'featured_media' ) ;
300
- const isRequestingFeaturedImageMedia =
301
- ! ! featuredImageId &&
302
- ! select ( coreStore ) . hasFinishedResolution ( 'getMedia' , [
303
- featuredImageId ,
304
- { context : 'view' } ,
305
- ] ) ;
306
313
307
314
return {
308
315
media : featuredImageId
@@ -311,7 +318,12 @@ const applyWithSelect = withSelect( ( select ) => {
311
318
currentPostId : getCurrentPostId ( ) ,
312
319
postType : getPostType ( getEditedPostAttribute ( 'type' ) ) ,
313
320
featuredImageId,
314
- isRequestingFeaturedImageMedia,
321
+ isRequestingFeaturedImageMedia :
322
+ ! ! featuredImageId &&
323
+ ! hasFinishedResolution ( 'getMedia' , [
324
+ featuredImageId ,
325
+ { context : 'view' } ,
326
+ ] ) ,
315
327
} ;
316
328
} ) ;
317
329
0 commit comments