-
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
Deprecate the original slot implementation and refactor usage #22027
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,16 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
|
||
import { orderBy } from 'lodash'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
|
||
import { __ } from '@wordpress/i18n'; | ||
import { Toolbar, Slot, DropdownMenu } from '@wordpress/components'; | ||
import { | ||
Toolbar, | ||
Slot, | ||
Dropdown, | ||
Button, | ||
NavigableMenu, | ||
__experimentalUseSlot as useSlot, | ||
} from '@wordpress/components'; | ||
import { DOWN } from '@wordpress/keycodes'; | ||
import { chevronDown } from '@wordpress/icons'; | ||
|
||
const POPOVER_PROPS = { | ||
|
@@ -18,6 +19,9 @@ const POPOVER_PROPS = { | |
}; | ||
|
||
const FormatToolbar = () => { | ||
const slot = useSlot( 'RichText.ToolbarControls' ); | ||
const hasFills = Boolean( slot.fills && slot.fills.length ); | ||
|
||
return ( | ||
<div className="block-editor-format-toolbar"> | ||
<Toolbar> | ||
|
@@ -26,24 +30,48 @@ const FormatToolbar = () => { | |
<Slot | ||
name={ `RichText.ToolbarControls.${ format }` } | ||
key={ format } | ||
bubblesVirtually | ||
/> | ||
) | ||
) } | ||
<Slot name="RichText.ToolbarControls"> | ||
{ ( fills ) => | ||
fills.length !== 0 && ( | ||
<DropdownMenu | ||
icon={ chevronDown } | ||
label={ __( 'More rich text controls' ) } | ||
controls={ orderBy( | ||
fills.map( ( [ { props } ] ) => props ), | ||
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. Mapping through fills and using them as "controls" is not something I was expecting. I'm not sure how we can solve this here? Why can't we just use a render Slot in renderContent of a "Dropdown" component. Anyway, @ellatrix, maybe you can help solve this. (bubbles virtually don't support children as function and can't support it) 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.
Could you elaborate? 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. Pushed a fix, let me know what you think. 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. How are they now sorted alphabetically? 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'm also not sure how the collapsed button can now know if any buttons are active. I guess this would be easier with a controls array. Another thing I'm noticing is that the buttons have too much space in between them compared to master 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. They are not sorted, I believe they are probably sorted at the "import" level. The thing is, with Slot/Fill, we're not supposed to know the shape of the children... So I'd say we were doing it wrong previously or Slot/Fill, is not the right API here. 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'll have a look at it now 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. Hm, I tried converting it to control object but that doesn't really work because we're stuck with the React component rendered in the format type edit functions. Maybe we can create a slot and fill for each button, which could be sorted by the title of the format type? 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. It still wouldn't work perfectly though, because some format types could render multiple buttons. That's more of an edge case though. |
||
'title' | ||
) } | ||
popoverProps={ POPOVER_PROPS } | ||
/> | ||
) | ||
} | ||
</Slot> | ||
{ hasFills && ( | ||
<Dropdown | ||
popoverProps={ POPOVER_PROPS } | ||
renderToggle={ ( { isOpen, onToggle } ) => { | ||
const openOnArrowDown = ( event ) => { | ||
if ( ! isOpen && event.keyCode === DOWN ) { | ||
event.preventDefault(); | ||
event.stopPropagation(); | ||
onToggle(); | ||
} | ||
}; | ||
|
||
return ( | ||
<Button | ||
icon={ chevronDown } | ||
onClick={ onToggle } | ||
onKeyDown={ openOnArrowDown } | ||
aria-haspopup="true" | ||
aria-expanded={ isOpen } | ||
label={ __( 'More rich text controls' ) } | ||
showTooltip | ||
/> | ||
); | ||
} } | ||
renderContent={ ( { onClose } ) => ( | ||
<NavigableMenu | ||
aria-label={ __( 'More rich text controls' ) } | ||
role="menu" | ||
> | ||
<Slot | ||
name="RichText.ToolbarControls" | ||
bubblesVirtually | ||
fillProps={ { onClose } } | ||
/> | ||
</NavigableMenu> | ||
) } | ||
/> | ||
) } | ||
</Toolbar> | ||
</div> | ||
); | ||
|
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.
It’s used nearly everywhere, how about
useSlot
exposeshasFills
by default?