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

ImageURLInputUI: fix focus loss when settings are changed #58647

Merged
merged 3 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { useRef, useState } from '@wordpress/element';
import { useRef, useEffect, useState } from '@wordpress/element';
import { focus } from '@wordpress/dom';
import {
ToolbarButton,
Button,
Expand Down Expand Up @@ -57,6 +58,17 @@ const ImageURLInputUI = ( {
const [ urlInput, setUrlInput ] = useState( null );

const autocompleteRef = useRef( null );
const wrapperRef = useRef();

useEffect( () => {
if ( ! wrapperRef.current ) {
return;
}
const nextFocusTarget =
focus.focusable.find( wrapperRef.current )[ 0 ] ||
wrapperRef.current;
nextFocusTarget.focus();
}, [ isEditingLink, url, lightboxEnabled ] );

const startEditLink = () => {
if (
Expand Down Expand Up @@ -249,6 +261,7 @@ const ImageURLInputUI = ( {
/>
{ isOpen && (
<URLPopover
ref={ wrapperRef }
anchor={ popoverAnchor }
onFocusOutside={ onFocusOutside() }
onClose={ closeLinkUI }
Expand Down
144 changes: 75 additions & 69 deletions packages/block-editor/src/components/url-popover/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { useState } from '@wordpress/element';
import { forwardRef, useState } from '@wordpress/element';
import {
Button,
Popover,
Expand All @@ -24,83 +24,89 @@ const { __experimentalPopoverLegacyPositionToPlacement } = unlock(

const DEFAULT_PLACEMENT = 'bottom';

function URLPopover( {
additionalControls,
children,
renderSettings,
// The DEFAULT_PLACEMENT value is assigned inside the function's body
placement,
focusOnMount = 'firstElement',
// Deprecated
position,
// Rest
...popoverProps
} ) {
if ( position !== undefined ) {
deprecated( '`position` prop in wp.blockEditor.URLPopover', {
since: '6.2',
alternative: '`placement` prop',
} );
}
const URLPopover = forwardRef(
(
{
additionalControls,
children,
renderSettings,
// The DEFAULT_PLACEMENT value is assigned inside the function's body
placement,
focusOnMount = 'firstElement',
// Deprecated
position,
// Rest
...popoverProps
},
ref
) => {
if ( position !== undefined ) {
deprecated( '`position` prop in wp.blockEditor.URLPopover', {
since: '6.2',
alternative: '`placement` prop',
} );
}

// Compute popover's placement:
// - give priority to `placement` prop, if defined
// - otherwise, compute it from the legacy `position` prop (if defined)
// - finally, fallback to the DEFAULT_PLACEMENT.
let computedPlacement;
if ( placement !== undefined ) {
computedPlacement = placement;
} else if ( position !== undefined ) {
computedPlacement =
__experimentalPopoverLegacyPositionToPlacement( position );
}
computedPlacement = computedPlacement || DEFAULT_PLACEMENT;
// Compute popover's placement:
// - give priority to `placement` prop, if defined
// - otherwise, compute it from the legacy `position` prop (if defined)
// - finally, fallback to the DEFAULT_PLACEMENT.
let computedPlacement;
if ( placement !== undefined ) {
computedPlacement = placement;
} else if ( position !== undefined ) {
computedPlacement =
__experimentalPopoverLegacyPositionToPlacement( position );
}
computedPlacement = computedPlacement || DEFAULT_PLACEMENT;

const [ isSettingsExpanded, setIsSettingsExpanded ] = useState( false );
const [ isSettingsExpanded, setIsSettingsExpanded ] = useState( false );

const showSettings = !! renderSettings && isSettingsExpanded;
const showSettings = !! renderSettings && isSettingsExpanded;

const toggleSettingsVisibility = () => {
setIsSettingsExpanded( ! isSettingsExpanded );
};
const toggleSettingsVisibility = () => {
setIsSettingsExpanded( ! isSettingsExpanded );
};

return (
<Popover
className="block-editor-url-popover"
focusOnMount={ focusOnMount }
placement={ computedPlacement }
shift
variant="toolbar"
{ ...popoverProps }
>
<div className="block-editor-url-popover__input-container">
<div className="block-editor-url-popover__row">
{ children }
{ !! renderSettings && (
<Button
className="block-editor-url-popover__settings-toggle"
icon={ chevronDown }
label={ __( 'Link settings' ) }
onClick={ toggleSettingsVisibility }
aria-expanded={ isSettingsExpanded }
size="compact"
/>
return (
<Popover
ref={ ref }
className="block-editor-url-popover"
focusOnMount={ focusOnMount }
placement={ computedPlacement }
shift
variant="toolbar"
{ ...popoverProps }
>
<div className="block-editor-url-popover__input-container">
<div className="block-editor-url-popover__row">
{ children }
{ !! renderSettings && (
<Button
className="block-editor-url-popover__settings-toggle"
icon={ chevronDown }
label={ __( 'Link settings' ) }
onClick={ toggleSettingsVisibility }
aria-expanded={ isSettingsExpanded }
size="compact"
/>
) }
</div>
{ showSettings && (
<div className="block-editor-url-popover__row block-editor-url-popover__settings">
{ renderSettings() }
</div>
) }
</div>
{ showSettings && (
<div className="block-editor-url-popover__row block-editor-url-popover__settings">
{ renderSettings() }
{ additionalControls && ! showSettings && (
<div className="block-editor-url-popover__additional-controls">
{ additionalControls }
</div>
) }
</div>
{ additionalControls && ! showSettings && (
<div className="block-editor-url-popover__additional-controls">
{ additionalControls }
</div>
) }
</Popover>
);
}
</Popover>
);
}
);

URLPopover.LinkEditor = LinkEditor;

Expand Down
98 changes: 98 additions & 0 deletions test/e2e/specs/editor/blocks/image.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -742,6 +742,104 @@ test.describe( 'Image', () => {

expect( src ).toMatch( /\/wp-content\/uploads\// );
} );

test( 'should have keyboard navigable link UI popover', async ( {
editor,
page,
pageUtils,
imageBlockUtils,
} ) => {
await editor.insertBlock( { name: 'core/image' } );
const imageBlock = editor.canvas.locator(
'role=document[name="Block: Image"i]'
);
await expect( imageBlock ).toBeVisible();

await imageBlockUtils.upload(
imageBlock.locator( 'data-testid=form-file-upload-input' )
);

await page.getByLabel( 'Block tools' ).getByLabel( 'Link' ).click();

const uriInput = page.getByRole( 'combobox', {
name: 'URL',
} );

await expect( uriInput ).toBeFocused();

// Enter a link.
await uriInput.fill( 'https://example.com' );
await page.keyboard.press( 'Enter' );
await expect(
page.getByRole( 'link', {
name: 'example.com',
} )
).toBeFocused();

// Edit a link.
await pageUtils.pressKeys( 'Tab' );
await page.keyboard.press( 'Enter' );
await expect( uriInput ).toBeFocused();
await uriInput.fill( 'https://example.com/updated/' );
await page.keyboard.press( 'Enter' );
await expect(
page.getByRole( 'link', {
name: 'example.com/updated/',
} )
).toBeFocused();

// Remove a link.
await pageUtils.pressKeys( 'Tab', { times: 2 } );
await expect(
page.getByRole( 'button', { name: 'Remove link' } )
).toBeFocused();
await page.keyboard.press( 'Enter' );
await expect( uriInput ).toBeFocused();
await expect( uriInput ).toBeEmpty();

// Select "Link to image file".
await pageUtils.pressKeys( 'Tab', { times: 3 } );
await expect(
page.getByRole( 'menuitem', { name: 'Link to image file' } )
).toBeFocused();
await page.keyboard.press( 'Enter' );
await expect(
page.getByRole( 'link', {
name: 'Link to image file',
} )
).toBeFocused();

// Select "Link to attachment page".
await pageUtils.pressKeys( 'Tab' );
await page.keyboard.press( 'Enter' );
await pageUtils.pressKeys( 'Tab', { times: 4 } );
await expect(
page.getByRole( 'menuitem', { name: 'Link to attachment page' } )
).toBeFocused();
await page.keyboard.press( 'Enter' );
await expect(
page.getByRole( 'link', {
name: 'Link to attachment page',
} )
).toBeFocused();

// Select "Expand on click", then remove it.
await pageUtils.pressKeys( 'Tab' );
await page.keyboard.press( 'Enter' );
await pageUtils.pressKeys( 'Tab', { times: 5 } );
await expect(
page.getByRole( 'menuitem', { name: 'Expand on click' } )
).toBeFocused();
await page.keyboard.press( 'Enter' );
await expect(
page.getByRole( 'button', {
name: 'Disable expand on click',
} )
).toBeFocused();
await page.keyboard.press( 'Enter' );
await expect( uriInput ).toBeFocused();
await expect( uriInput ).toBeEmpty();
} );
} );

// Skipping these tests for now as we plan
Expand Down
Loading