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

Background image support: Fix focus loss when resetting background image #55984

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 19 additions & 3 deletions packages/block-editor/src/hooks/background.js
Expand Up @@ -8,6 +8,7 @@ import classnames from 'classnames';
*/
import { isBlobURL } from '@wordpress/blob';
import { getBlockSupport } from '@wordpress/blocks';
import { focus } from '@wordpress/dom';
import {
__experimentalToolsPanelItem as ToolsPanelItem,
DropZone,
Expand All @@ -19,7 +20,7 @@ import {
__experimentalTruncate as Truncate,
} from '@wordpress/components';
import { useDispatch, useSelect } from '@wordpress/data';
import { Platform, useCallback } from '@wordpress/element';
import { Platform, useCallback, useRef } from '@wordpress/element';
import { __, sprintf } from '@wordpress/i18n';
import { store as noticesStore } from '@wordpress/notices';
import { getFilename } from '@wordpress/url';
Expand Down Expand Up @@ -150,6 +151,8 @@ function BackgroundImagePanelItem( props ) {
const { id, title, url } =
attributes.style?.background?.backgroundImage || {};

const replaceContainerRef = useRef();

const { mediaUpload } = useSelect( ( select ) => {
return {
mediaUpload: select( blockEditorStore ).getSettings().mediaUpload,
Expand Down Expand Up @@ -253,7 +256,10 @@ function BackgroundImagePanelItem( props ) {
resetAllFilter={ resetAllFilter }
panelId={ clientId }
>
<div className="block-editor-hooks__background__inspector-media-replace-container">
<div
className="block-editor-hooks__background__inspector-media-replace-container"
ref={ replaceContainerRef }
>
<MediaReplaceFlow
mediaId={ id }
mediaURL={ url }
Expand All @@ -271,7 +277,17 @@ function BackgroundImagePanelItem( props ) {
>
{ hasValue && (
<MenuItem
onClick={ () => resetBackgroundImage( props ) }
onClick={ () => {
const [ toggleButton ] = focus.tabbable.find(
Copy link
Member

Choose a reason for hiding this comment

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

TIL!

replaceContainerRef.current
);
// Focus the toggle button and close the dropdown menu.
// This ensures similar behaviour as to selecting an image, where the dropdown is
// closed and focus is redirected to the dropdown toggle button.
toggleButton?.focus();
Copy link
Member

Choose a reason for hiding this comment

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

I asked ChatGPT if there were any accessibility concerns when programmatically triggering the HTMLElement click() method:

  • Ensure that the programmatically triggered click is consistent with keyboard navigation.
  • Make sure that the element receiving the click event also receives focus
  • ARIA Roles and States: Ensure that the HTML elements have appropriate ARIA roles and states if necessary. (the aria-expanded has the right value)

😄

toggleButton?.click();
resetBackgroundImage( props );
} }
>
{ __( 'Reset ' ) }
</MenuItem>
Expand Down