Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Editor: Update focus return handler for the Featured Image #67236

Draft
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 15 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 focusToggleRef = 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 @@ -203,7 +196,15 @@ function PostFeaturedImage( {
) : (
<Button
__next40pxDefaultSize
ref={ toggleRef }
ref={ ( node ) => {
if (
focusToggleRef.current &&
node
) {
node.focus();
focusToggleRef.current = false;
}
} }
className={
! featuredImageId
? 'editor-post-featured-image__toggle'
Expand Down Expand Up @@ -276,12 +277,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.
focusToggleRef.current = true;
} }
variant={
isMissingMedia
Expand Down
Loading