Skip to content
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 1 commit into from
May 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,12 @@
"markdown_source": "../packages/components/src/responsive-wrapper/README.md",
"parent": "components"
},
{
"title": "RovingTabIndex",
"slug": "roving-tab-index",
"markdown_source": "../packages/components/src/roving-tab-index/README.md",
"parent": "components"
},
{
"title": "Sandbox",
"slug": "sandbox",
Expand Down Expand Up @@ -1055,6 +1061,12 @@
"markdown_source": "../packages/components/src/tooltip/README.md",
"parent": "components"
},
{
"title": "TreeGrid",
"slug": "tree-grid",
"markdown_source": "../packages/components/src/tree-grid/README.md",
"parent": "components"
},
{
"title": "TreeSelect",
"slug": "tree-select",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { useSelect, useDispatch } from '@wordpress/data';
* Internal dependencies
*/
import { isInsideRootBlock } from '../../utils/dom';
import useMovingAnimation from './moving-animation';
import useMovingAnimation from '../use-moving-animation';
import { Context, SetBlockNodes } from './root-container';
import { BlockListBlockContext } from './block';
import ELEMENTS from './block-elements';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
flex-direction: row;
border-right: $border-width solid $light-gray-500;

.block-editor-block-mover__control {
.block-editor-block-mover-button {
width: $button-size;
height: $button-size;
border-radius: $radius-round-rectangle;
Expand All @@ -22,7 +22,7 @@
display: flex;
margin-right: auto;

.block-editor-block-mover__control {
.block-editor-block-mover-button {
float: left;
}
}
Expand Down
68 changes: 68 additions & 0 deletions packages/block-editor/src/components/block-navigation/appender.js
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>
);
}
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">
Copy link
Contributor Author

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.

Copy link
Contributor

@adamziel adamziel May 11, 2020

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:

<BlockNavigationBlockFill>
	<BlockNavigationBlockSelectButton
		ref={ ref }
		block={ block }
		onClick={ onClick }
		isSelected={ isSelected }
		position={ position }
		siblingCount={ siblingCount }
		level={ level }
		{ ...props }
	/>
	<EllipsisMenu {/* some props */} />
</BlockNavigationBlockFill>

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.

Copy link
Contributor Author

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.

{ 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;
Loading