-
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
Add block navigator movers #18014
Merged
Merged
Add block navigator movers #18014
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
68 changes: 68 additions & 0 deletions
68
packages/block-editor/src/components/block-navigation/appender.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,68 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { __experimentalTreeGridCell as TreeGridCell } from '@wordpress/components'; | ||
import { useInstanceId } from '@wordpress/compose'; | ||
import { __, sprintf } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import BlockNavigationLeaf from './leaf'; | ||
import ButtonBlockAppender from '../button-block-appender'; | ||
import DescenderLines from './descender-lines'; | ||
|
||
export default function BlockNavigationAppender( { | ||
parentBlockClientId, | ||
position, | ||
level, | ||
rowCount, | ||
terminatedLevels, | ||
path, | ||
} ) { | ||
const instanceId = useInstanceId( BlockNavigationAppender ); | ||
const descriptionId = `block-navigation-appender-row__description_${ instanceId }`; | ||
|
||
const appenderPositionDescription = sprintf( | ||
/* translators: 1: The numerical position of the block that will be inserted. 2: The level of nesting for the block that will be inserted. */ | ||
__( 'Add block at position %1$d, Level %2$d' ), | ||
position, | ||
level | ||
); | ||
|
||
return ( | ||
<BlockNavigationLeaf | ||
level={ level } | ||
position={ position } | ||
rowCount={ rowCount } | ||
path={ path } | ||
> | ||
<TreeGridCell | ||
className="block-editor-block-navigation-appender__cell" | ||
colSpan="3" | ||
> | ||
{ ( props ) => ( | ||
<div className="block-editor-block-navigation-appender__container"> | ||
<DescenderLines | ||
level={ level } | ||
isLastRow={ position === rowCount } | ||
terminatedLevels={ terminatedLevels } | ||
/> | ||
<ButtonBlockAppender | ||
rootClientId={ parentBlockClientId } | ||
__experimentalSelectBlockOnInsert={ false } | ||
aria-describedby={ descriptionId } | ||
{ ...props } | ||
/> | ||
<div | ||
className="block-editor-block-navigation-appender__description" | ||
id={ descriptionId } | ||
> | ||
{ appenderPositionDescription } | ||
</div> | ||
</div> | ||
) } | ||
</TreeGridCell> | ||
</BlockNavigationLeaf> | ||
); | ||
} |
185 changes: 185 additions & 0 deletions
185
packages/block-editor/src/components/block-navigation/block-contents.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,185 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import classnames from 'classnames'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { | ||
__experimentalGetBlockLabel as getBlockLabel, | ||
getBlockType, | ||
} from '@wordpress/blocks'; | ||
import { Button, Fill, Slot, VisuallyHidden } from '@wordpress/components'; | ||
import { useInstanceId } from '@wordpress/compose'; | ||
import { | ||
Children, | ||
cloneElement, | ||
forwardRef, | ||
useContext, | ||
} from '@wordpress/element'; | ||
import { __, sprintf } from '@wordpress/i18n'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import BlockIcon from '../block-icon'; | ||
import { BlockListBlockContext } from '../block-list/block'; | ||
import { useBlockNavigationContext } from './context'; | ||
|
||
export const BlockNavigationBlockContentWrapper = forwardRef( function( | ||
{ | ||
as: WrapperComponent, | ||
className, | ||
block, | ||
isSelected, | ||
onClick, | ||
position, | ||
siblingCount, | ||
level, | ||
children, | ||
...props | ||
}, | ||
ref | ||
) { | ||
const { name, attributes } = block; | ||
const instanceId = useInstanceId( BlockNavigationBlockContentWrapper ); | ||
const descriptionId = `block-navigation-block-select-button_${ instanceId }`; | ||
const blockType = getBlockType( name ); | ||
const blockDisplayName = getBlockLabel( blockType, attributes ); | ||
const blockPositionDescription = sprintf( | ||
/* translators: 1: The numerical position of the block. 2: The total number of blocks. 3. The level of nesting for the block. */ | ||
__( 'Block %1$d of %2$d, Level %3$d' ), | ||
position, | ||
siblingCount, | ||
level | ||
); | ||
|
||
return ( | ||
<> | ||
<WrapperComponent | ||
className={ classnames( | ||
'block-editor-block-navigation-block-content-wrapper', | ||
className | ||
) } | ||
onClick={ onClick } | ||
aria-describedby={ descriptionId } | ||
ref={ ref } | ||
{ ...props } | ||
> | ||
<BlockIcon icon={ blockType.icon } showColors /> | ||
{ children ? children : blockDisplayName } | ||
{ isSelected && ( | ||
<VisuallyHidden> | ||
{ __( '(selected block)' ) } | ||
</VisuallyHidden> | ||
) } | ||
</WrapperComponent> | ||
<div | ||
className="block-editor-block-navigation-block-content-wrapper__description" | ||
id={ descriptionId } | ||
> | ||
{ blockPositionDescription } | ||
</div> | ||
</> | ||
); | ||
} ); | ||
|
||
const BlockNavigationBlockSelectButton = forwardRef( ( props, ref ) => ( | ||
<BlockNavigationBlockContentWrapper | ||
ref={ ref } | ||
as={ Button } | ||
className="block-editor-block-navigation-block-select-button" | ||
{ ...props } | ||
/> | ||
) ); | ||
|
||
const getSlotName = ( clientId ) => `BlockNavigationBlock-${ clientId }`; | ||
|
||
const BlockNavigationBlockSlot = forwardRef( | ||
( | ||
{ block, isSelected, onClick, position, siblingCount, level, ...props }, | ||
ref | ||
) => { | ||
const { clientId } = block; | ||
|
||
return ( | ||
<Slot name={ getSlotName( clientId ) }> | ||
{ ( fills ) => { | ||
if ( ! fills.length ) { | ||
return ( | ||
<BlockNavigationBlockSelectButton | ||
ref={ ref } | ||
block={ block } | ||
onClick={ onClick } | ||
isSelected={ isSelected } | ||
position={ position } | ||
siblingCount={ siblingCount } | ||
level={ level } | ||
{ ...props } | ||
/> | ||
); | ||
} | ||
|
||
return ( | ||
<BlockNavigationBlockContentWrapper as="div"> | ||
{ Children.map( fills, ( fill ) => | ||
cloneElement( fill, { | ||
...{ | ||
block, | ||
isSelected, | ||
onClick, | ||
...props, | ||
}, | ||
...fill.props, | ||
} ) | ||
) } | ||
</BlockNavigationBlockContentWrapper> | ||
); | ||
} } | ||
</Slot> | ||
); | ||
} | ||
); | ||
|
||
export const BlockNavigationBlockFill = ( props ) => { | ||
const { clientId } = useContext( BlockListBlockContext ); | ||
return <Fill { ...props } name={ getSlotName( clientId ) } />; | ||
}; | ||
|
||
const BlockNavigationBlockContents = forwardRef( | ||
( | ||
{ onClick, block, isSelected, position, siblingCount, level, ...props }, | ||
ref | ||
) => { | ||
const { | ||
__experimentalWithBlockNavigationSlots: withBlockNavigationSlots, | ||
} = useBlockNavigationContext(); | ||
|
||
return withBlockNavigationSlots ? ( | ||
<BlockNavigationBlockSlot | ||
ref={ ref } | ||
block={ block } | ||
onClick={ onClick } | ||
isSelected={ isSelected } | ||
position={ position } | ||
siblingCount={ siblingCount } | ||
level={ level } | ||
{ ...props } | ||
/> | ||
) : ( | ||
<BlockNavigationBlockSelectButton | ||
ref={ ref } | ||
block={ block } | ||
onClick={ onClick } | ||
isSelected={ isSelected } | ||
position={ position } | ||
siblingCount={ siblingCount } | ||
level={ level } | ||
{ ...props } | ||
/> | ||
); | ||
} | ||
); | ||
|
||
export default BlockNavigationBlockContents; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@adamziel This main file now contains a lot of the Wrapper and Slot/Fill logic. I ended up renaming a few things.
The main logic I changed is that the wrapper is now rendered around the slot, while in your PR the wrapper is rendered inside the fill:
https://github.com/WordPress/gutenberg/pull/22210/files#diff-2e071f8429cd2c500b830947ffde4239R226-R231
Pretty open to handling that differently, I think some aspects of this may need to be changed to handle adding the menu described in #22089.
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.
@talldan I went for the "wrapper inside of the fill" approach to make something like this possible:
With "wrapper outside of the fill" approach, these slots are only good for something like replacing the button entirely with RichText. You cannot the keep navigation item as a button and you cannot add any additional components.
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.
Hmm, this is made difficult because this PR introduces the movers which are rendered after the fill, and then the designs in #22089 shown the dropdown menu after the movers.
I think most blocks will end up having the dropdown menu, so it probably makes sense to add that as an internal part of
BlockNavigation
, and then if we need to add custom items for particular blocks we can try a slot for the menu.