-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make parts of the BlockNavigationList overridable using slots (#21948)
* First stab at editable menu items in navigator Use Slot/Fill instead of hardcoded components Disable slots/fills in global editor block navigator Grab clientId from context instead of requiring a prop Improve readability of block-navigation/list.js Use context to determine whether or not given BlockNavigation may be customized with slots Restore default value for selectBlock in navigation-structure-panel.js Remove obsolete onChange passed to NavigationStructurePanel Use constant value for BlockNavigationContext Use memo() in BlockStyles Remove performance changes from BlockStyles Generalize BlockNavigationListItem and remove the BlockNavigationListItemFill for now Rename useBlockNavigationSlots to withBlockNavigationSlots Restore RichText in the navigator Sort out BlockNavigationListItem and BlockNavigationListItemWrapper Rename BlockNavigationListItemWrapper to BlockNavigationBranch * Move BlockNavigationBranch to a different file * Refactor BlockNavigationContext to be an internal detail * Lint * Rename all occurences of withBlockNavigationSlots to __experimentalWithBlockNavigationSlots * Remove export of BlockNavigationContext * Adjust class names * Remove the fill from edit.js for now * Update class name in e2e tests * Link
- Loading branch information
Showing
14 changed files
with
202 additions
and
48 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
packages/block-editor/src/components/block-navigation/branch.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/** | ||
* 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 { __experimentalWithBlockNavigationSlots } = useContext( | ||
BlockNavigationContext | ||
); | ||
if ( ! __experimentalWithBlockNavigationSlots ) { | ||
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 ) } />; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
packages/block-editor/src/components/block-navigation/list-item.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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__list-item"> | ||
<WrapperComponent | ||
className={ classnames( | ||
'block-editor-block-navigation__list-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 } />, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.