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

Update URLPopover role and focus return #61313

Merged
merged 6 commits into from
Jul 4, 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
54 changes: 22 additions & 32 deletions packages/block-editor/src/components/media-placeholder/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,28 +60,37 @@ const InsertFromURLPopover = ( {
</URLPopover>
);

const URLSelectionUI = ( {
isURLInputVisible,
src,
onChangeSrc,
onSubmitSrc,
openURLInput,
closeURLInput,
} ) => {
const URLSelectionUI = ( { src, onChangeSrc, onSelectURL } ) => {
// Use internal state instead of a ref to make sure that the component
// re-renders when the popover's anchor updates.
const [ popoverAnchor, setPopoverAnchor ] = useState( null );
const [ isURLInputVisible, setIsURLInputVisible ] = useState( false );

const openURLInput = () => {
setIsURLInputVisible( true );
};
const closeURLInput = () => {
setIsURLInputVisible( false );
popoverAnchor?.focus();
};

const onSubmitSrc = ( event ) => {
event.preventDefault();
if ( src && onSelectURL ) {
onSelectURL( src );
closeURLInput();
}
};

return (
<div
className="block-editor-media-placeholder__url-input-container"
ref={ setPopoverAnchor }
>
<div className="block-editor-media-placeholder__url-input-container">
<Button
className="block-editor-media-placeholder__button"
onClick={ openURLInput }
isPressed={ isURLInputVisible }
variant="secondary"
aria-haspopup="dialog"
ref={ setPopoverAnchor }
>
{ __( 'Insert from URL' ) }
</Button>
Expand Down Expand Up @@ -138,7 +147,6 @@ export function MediaPlaceholder( {
return getSettings().mediaUpload;
}, [] );
const [ src, setSrc ] = useState( '' );
const [ isURLInputVisible, setIsURLInputVisible ] = useState( false );

useEffect( () => {
setSrc( value?.src ?? '' );
Expand All @@ -159,21 +167,6 @@ export function MediaPlaceholder( {
setSrc( event.target.value );
};

const openURLInput = () => {
setIsURLInputVisible( true );
};
const closeURLInput = () => {
setIsURLInputVisible( false );
};

const onSubmitSrc = ( event ) => {
event.preventDefault();
if ( src && onSelectURL ) {
onSelectURL( src );
closeURLInput();
}
};

const onFilesUpload = ( files ) => {
if ( ! handleUpload ) {
return onSelect( files );
Expand Down Expand Up @@ -404,12 +397,9 @@ export function MediaPlaceholder( {
return (
onSelectURL && (
<URLSelectionUI
isURLInputVisible={ isURLInputVisible }
src={ src }
onChangeSrc={ onChangeSrc }
onSubmitSrc={ onSubmitSrc }
openURLInput={ openURLInput }
closeURLInput={ closeURLInput }
onSelectURL={ onSelectURL }
/>
)
);
Expand Down
3 changes: 3 additions & 0 deletions packages/block-editor/src/components/url-popover/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ const URLPopover = forwardRef(
return (
<Popover
ref={ ref }
role="dialog"
ntsekouras marked this conversation as resolved.
Show resolved Hide resolved
aria-modal="true"
aria-label={ __( 'Edit URL' ) }
className="block-editor-url-popover"
focusOnMount={ focusOnMount }
placement={ computedPlacement }
Expand Down
28 changes: 27 additions & 1 deletion packages/block-library/src/social-link/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ import clsx from 'clsx';
/**
* WordPress dependencies
*/
import { DELETE, BACKSPACE } from '@wordpress/keycodes';
import { useDispatch } from '@wordpress/data';

import {
InspectorControls,
URLPopover,
URLInput,
useBlockProps,
store as blockEditorStore,
} from '@wordpress/block-editor';
import { useState } from '@wordpress/element';
import {
Expand All @@ -32,17 +36,24 @@ const SocialLinkURLPopover = ( {
setAttributes,
setPopover,
popoverAnchor,
clientId,
} ) => {
const { removeBlock } = useDispatch( blockEditorStore );
return (
<URLPopover
anchor={ popoverAnchor }
onClose={ () => setPopover( false ) }
aria-label={ __( 'Edit social link' ) }
onClose={ () => {
setPopover( false );
popoverAnchor?.focus();
} }
>
<form
className="block-editor-url-popover__link-editor"
onSubmit={ ( event ) => {
event.preventDefault();
setPopover( false );
popoverAnchor?.focus();
} }
>
<div className="block-editor-url-input">
Expand All @@ -56,6 +67,18 @@ const SocialLinkURLPopover = ( {
label={ __( 'Enter social link' ) }
hideLabelFromVision
disableSuggestions
onKeyDown={ ( event ) => {
if (
!! url ||
event.defaultPrevented ||
! [ BACKSPACE, DELETE ].includes(
event.keyCode
)
) {
return;
}
removeBlock( clientId );
} }
/>
</div>
<Button
Expand All @@ -73,6 +96,7 @@ const SocialLinkEdit = ( {
context,
isSelected,
setAttributes,
clientId,
} ) => {
const { url, service, label = '', rel } = attributes;
const {
Expand Down Expand Up @@ -143,6 +167,7 @@ const SocialLinkEdit = ( {
className="wp-block-social-link-anchor"
ref={ setPopoverAnchor }
onClick={ () => setPopover( true ) }
aria-haspopup="dialog"
>
<IconComponent />
<span
Expand All @@ -159,6 +184,7 @@ const SocialLinkEdit = ( {
setAttributes={ setAttributes }
setPopover={ setPopover }
popoverAnchor={ popoverAnchor }
clientId={ clientId }
/>
) }
</li>
Expand Down
Loading