Skip to content

Commit

Permalink
Editor: Update focus return handler for the Featured Image (#67236)
Browse files Browse the repository at this point in the history
Co-authored-by: Mamaduka <[email protected]>
Co-authored-by: afercia <[email protected]>
  • Loading branch information
3 people authored Nov 25, 2024
1 parent a6a04be commit 814bb4e
Showing 1 changed file with 14 additions and 16 deletions.
30 changes: 14 additions & 16 deletions packages/editor/src/components/post-featured-image/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
Notice,
} from '@wordpress/components';
import { isBlobURL } from '@wordpress/blob';
import { useState, useRef, useEffect } from '@wordpress/element';
import { useState, useRef } from '@wordpress/element';
import { compose } from '@wordpress/compose';
import { useSelect, withDispatch, withSelect } from '@wordpress/data';
import {
Expand Down Expand Up @@ -102,17 +102,10 @@ function PostFeaturedImage( {
noticeOperations,
isRequestingFeaturedImageMedia,
} ) {
const toggleRef = useRef();
const returnsFocusRef = useRef( false );
const [ isLoading, setIsLoading ] = useState( false );
const { getSettings } = useSelect( blockEditorStore );
const { mediaSourceUrl } = getMediaDetails( media, currentPostId );
const toggleFocusTimerRef = useRef();

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

function onDropFiles( filesList ) {
getSettings().mediaUpload( {
Expand Down Expand Up @@ -164,6 +157,13 @@ function PostFeaturedImage( {
);
}

function returnFocus( node ) {
if ( returnsFocusRef.current && node ) {
node.focus();
returnsFocusRef.current = false;
}
}

const isMissingMedia =
! isRequestingFeaturedImageMedia && !! featuredImageId && ! media;

Expand Down Expand Up @@ -203,7 +203,7 @@ function PostFeaturedImage( {
) : (
<Button
__next40pxDefaultSize
ref={ toggleRef }
ref={ returnFocus }
className={
! featuredImageId
? 'editor-post-featured-image__toggle'
Expand Down Expand Up @@ -276,12 +276,10 @@ function PostFeaturedImage( {
className="editor-post-featured-image__action"
onClick={ () => {
onRemoveImage();
// The toggle button is rendered conditionally, we need
// to wait it is rendered before moving focus to it.
toggleFocusTimerRef.current =
setTimeout( () => {
toggleRef.current?.focus();
} );
// Signal that the toggle button should be focused,
// when it is rendered. Can't focus it directly here
// because it's rendered conditionally.
returnsFocusRef.current = true;
} }
variant={
isMissingMedia
Expand Down

1 comment on commit 814bb4e

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in 814bb4e.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/12008359496
📝 Reported issues:

Please sign in to comment.