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

Media Replace Flow: Add custom toggle support and fix button height #68084

Merged
merged 14 commits into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from 9 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 @@ -24,6 +24,7 @@ import {
Placeholder,
Spinner,
__experimentalDropdownContentWrapper as DropdownContentWrapper,
Button,
} from '@wordpress/components';
import { __, _x, sprintf } from '@wordpress/i18n';
import { store as noticesStore } from '@wordpress/notices';
Expand Down Expand Up @@ -369,15 +370,28 @@ function BackgroundImageControls( {
displayInPanel,
} ),
} }
name={
<InspectorImagePreviewItem
className="block-editor-global-styles-background-panel__image-preview"
imgUrl={ url }
filename={ title }
label={ imgLabel }
/>
Copy link
Member

Choose a reason for hiding this comment

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

Because name is a standard prop of MediaReplaceFlow, I think it would be safer to continue using this prop rather than rendering it directly into our renderToggle.

One risk I'm thinking about is that the MediaReplaceFlow implementation could change some day to use the name prop in another place as well.

}
variant="secondary"
renderToggle={ ( { isOpen, onToggle } ) => (
<Button
__next40pxDefaultSize
aria-expanded={ isOpen }
aria-haspopup="true"
onClick={ onToggle }
onKeyDown={ ( event ) => {
if ( event.key === 'ArrowDown' ) {
event.preventDefault();
event.target.click();
}
} }
>
<InspectorImagePreviewItem
className="block-editor-global-styles-background-panel__image-preview"
imgUrl={ url }
filename={ title }
label={ imgLabel }
/>
</Button>
) }
onError={ onUploadError }
onReset={ () => {
closeAndFocus();
Expand Down
Copy link
Member

Choose a reason for hiding this comment

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

Are changes in this file still necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, @Mamaduka. I've tried to explain it above in this comment.

Thanks a lot for reviewing the PR 🚀

Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

.components-dropdown {
display: block;
height: 36px;
}
}

Expand All @@ -44,7 +43,6 @@

.components-dropdown {
display: block;
height: 36px;
}

button.components-button {
Expand Down
33 changes: 18 additions & 15 deletions packages/block-editor/src/components/media-replace-flow/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@ import clsx from 'clsx';
/**
* WordPress dependencies
*/
import { useRef } from '@wordpress/element';
import { __, _x } from '@wordpress/i18n';
import { speak } from '@wordpress/a11y';
import {
FormFileUpload,
NavigableMenu,
MenuItem,
ToolbarButton,
Dropdown,
withFilters,
ToolbarButton,
} from '@wordpress/components';
import { useSelect, withDispatch } from '@wordpress/data';
import { DOWN } from '@wordpress/keycodes';
Expand Down Expand Up @@ -60,12 +59,12 @@ const MediaReplaceFlow = ( {
addToGallery,
handleUpload = true,
popoverProps,
renderToggle,
} ) => {
const mediaUpload = useSelect( ( select ) => {
return select( blockEditorStore ).getSettings().mediaUpload;
}, [] );
const canUpload = !! mediaUpload;
const editMediaButtonRef = useRef();
const errorNoticeID = `block-editor/media-replace-flow/error-notice/${ ++uniqueId }`;

const onUploadError = ( message ) => {
Expand Down Expand Up @@ -137,21 +136,26 @@ const MediaReplaceFlow = ( {

const gallery = multiple && onlyAllowsImages();

const defaultRenderToggle = ( { isOpen, onToggle } ) => (
<ToolbarButton
aria-expanded={ isOpen }
aria-haspopup="true"
onClick={ onToggle }
onKeyDown={ openOnArrowDown }
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if we should pass down these as default props for the media flow render toggle. This way, consumers don't have to re-implement a11y best practices.

Copy link
Member

Choose a reason for hiding this comment

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

Exactly, that is what I had in mind with this usage example. We can assume (and document) that the renderToggle function should accept and pass through button props to an eventual button element.

@yogeshbhutkar Let me know if you're unsure what to do here, I'd be happy to push up some proposed code.

Copy link
Member

Choose a reason for hiding this comment

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

@mirka, yeah, reread your example after I commented and noticed it 😅

>
{ name }
</ToolbarButton>
);

return (
<Dropdown
popoverProps={ popoverProps }
contentClassName="block-editor-media-replace-flow__options"
renderToggle={ ( { isOpen, onToggle } ) => (
<ToolbarButton
ref={ editMediaButtonRef }
aria-expanded={ isOpen }
aria-haspopup="true"
onClick={ onToggle }
onKeyDown={ openOnArrowDown }
>
{ name }
</ToolbarButton>
) }
renderToggle={
typeof renderToggle === 'function'
? renderToggle
: defaultRenderToggle
}
renderContent={ ( { onClose } ) => (
<>
<NavigableMenu className="block-editor-media-replace-flow__media-upload-menu">
Expand Down Expand Up @@ -238,7 +242,6 @@ const MediaReplaceFlow = ( {
showSuggestions={ false }
onChange={ ( { url } ) => {
onSelectURL( url );
editMediaButtonRef.current.focus();
Mamaduka marked this conversation as resolved.
Show resolved Hide resolved
} }
/>
</form>
Expand Down
Loading