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

Merged
merged 1 commit into from
Nov 25, 2024
Merged
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
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
Loading