-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Changes from 9 commits
c55c3b6
9894d75
ca288fe
f8983b1
e2295f0
a0b5844
96a625a
e1ec285
f37e830
92855a8
dfe66b2
eee0103
e3df6a0
d2e6e3e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are changes in this file still necessary? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'; | ||
|
@@ -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 ) => { | ||
|
@@ -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 } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 @yogeshbhutkar Let me know if you're unsure what to do here, I'd be happy to push up some proposed code. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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"> | ||
|
@@ -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> | ||
|
There was a problem hiding this comment.
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 ofMediaReplaceFlow
, I think it would be safer to continue using this prop rather than rendering it directly into ourrenderToggle
.One risk I'm thinking about is that the
MediaReplaceFlow
implementation could change some day to use thename
prop in another place as well.