-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Make parts of the BlockNavigationList overridable using slots #21948
Changes from 4 commits
c6e777b
96d16ec
57f929c
edb8c67
528a06b
3840737
774ba8e
99eda63
4fcf921
a038c0a
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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Children, cloneElement, useContext } from '@wordpress/element'; | ||
import { Fill, Slot } from '@wordpress/components'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import BlockNavigationListItem from './list-item'; | ||
import { BlockNavigationContext } from './list'; | ||
import { BlockListBlockContext } from '../block-list/block'; | ||
|
||
const BlockNavigationBranch = ( { children, ...props } ) => { | ||
const { withBlockNavigationSlots } = useContext( BlockNavigationContext ); | ||
if ( ! withBlockNavigationSlots ) { | ||
return ( | ||
<li> | ||
<BlockNavigationListItem { ...props } /> | ||
{ children } | ||
</li> | ||
); | ||
} | ||
|
||
return ( | ||
<li> | ||
<BlockNavigationListItemSlot blockId={ props.block.clientId }> | ||
{ ( fills ) => { | ||
if ( ! fills.length ) { | ||
return <BlockNavigationListItem { ...props } />; | ||
} | ||
|
||
return Children.map( fills, ( fill ) => | ||
cloneElement( fill, { | ||
...props, | ||
...fill.props, | ||
} ) | ||
); | ||
} } | ||
</BlockNavigationListItemSlot> | ||
{ children } | ||
</li> | ||
); | ||
}; | ||
|
||
export default BlockNavigationBranch; | ||
|
||
const listItemSlotName = ( blockId ) => `BlockNavigationList-item-${ blockId }`; | ||
|
||
export const BlockNavigationListItemSlot = ( { blockId, ...props } ) => ( | ||
<Slot { ...props } name={ listItemSlotName( blockId ) } /> | ||
); | ||
|
||
export const BlockNavigationListItemFill = ( props ) => { | ||
const { clientId } = useContext( BlockListBlockContext ); | ||
return <Fill { ...props } name={ listItemSlotName( clientId ) } />; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import classnames from 'classnames'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Button, VisuallyHidden } from '@wordpress/components'; | ||
import { | ||
__experimentalGetBlockLabel as getBlockLabel, | ||
getBlockType, | ||
} from '@wordpress/blocks'; | ||
import { __ } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import BlockIcon from '../block-icon'; | ||
|
||
export default function BlockNavigationListItem( { | ||
block, | ||
onClick, | ||
isSelected, | ||
wrapperComponent: WrapperComponent, | ||
children, | ||
} ) { | ||
const blockType = getBlockType( block.name ); | ||
|
||
return ( | ||
<div className="block-editor-block-navigation__item"> | ||
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 just noticed some of the class names don't seem to be right after moving the code around. The root element should have the class name |
||
<WrapperComponent | ||
className={ classnames( | ||
'block-editor-block-navigation__item-button', | ||
{ | ||
'is-selected': isSelected, | ||
} | ||
) } | ||
onClick={ onClick } | ||
> | ||
<BlockIcon icon={ blockType.icon } showColors /> | ||
{ children | ||
? children | ||
: getBlockLabel( blockType, block.attributes ) } | ||
{ isSelected && ( | ||
<VisuallyHidden as="span"> | ||
{ __( '(selected block)' ) } | ||
</VisuallyHidden> | ||
) } | ||
</WrapperComponent> | ||
</div> | ||
); | ||
} | ||
|
||
BlockNavigationListItem.defaultProps = { | ||
onClick: () => {}, | ||
wrapperComponent: ( props ) => <Button { ...props } />, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,12 @@ export { default as BlockEdit, useBlockEditContext } from './block-edit'; | |
export { default as BlockFormatControls } from './block-format-controls'; | ||
export { default as BlockIcon } from './block-icon'; | ||
export { default as BlockNavigationDropdown } from './block-navigation/dropdown'; | ||
export { default as __experimentalBlockNavigationList } from './block-navigation/list'; | ||
export { | ||
default as __experimentalBlockNavigationList, | ||
BlockNavigationContext as __experimentalBlockNavigationContext, | ||
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 think this no longer needs to be exported now that it's only used internally in the BlockNavigation components |
||
} from './block-navigation/list'; | ||
export { BlockNavigationListItemFill as __experimentalBlockNavigationListItemFill } from './block-navigation/branch'; | ||
export { default as __experimentalBlockNavigationListItem } from './block-navigation/list-item'; | ||
export { default as __experimentalBlockVariationPicker } from './block-variation-picker'; | ||
export { default as BlockVerticalAlignmentToolbar } from './block-vertical-alignment-toolbar'; | ||
export { default as ButtonBlockerAppender } from './button-block-appender'; | ||
|
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.
I think it'd be good to make this
__experimentalWithBlockNavigationSlots
, as it's a publicly exported prop, and it's hard to say what the future is, this may default totrue
at some point at which point we'd want to remove it gracefully without having to support backwards compatibility.