Skip to content

Commit

Permalink
Comment Template: Improve UX of inner block selection (#38263)
Browse files Browse the repository at this point in the history
* Comment Template: Rename firstBlock to firstComment

* Comment Template: Fix jumping of elements

* Comment template: Fix flicker
  • Loading branch information
michalczaplinski authored Feb 9, 2022
1 parent d7d6da7 commit 4f72f26
Showing 1 changed file with 59 additions and 14 deletions.
73 changes: 59 additions & 14 deletions packages/block-library/src/comment-template/edit.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
/**
* WordPress dependencies
*/
import { useState, useMemo } from '@wordpress/element';
import { useState, useMemo, memo } from '@wordpress/element';
import { useSelect } from '@wordpress/data';
import { __ } from '@wordpress/i18n';
import {
BlockContextProvider,
BlockPreview,
useBlockProps,
useInnerBlocksProps,
store as blockEditorStore,
__experimentalUseBlockPreview as useBlockPreview,
} from '@wordpress/block-editor';
import { Spinner } from '@wordpress/components';
import { store as coreStore } from '@wordpress/core-data';
Expand All @@ -35,7 +35,7 @@ const TEMPLATE = [
* @param {Array} [props.comment] - A comment object.
* @param {Array} [props.activeComment] - The block that is currently active.
* @param {Array} [props.setActiveComment] - The setter for activeComment.
* @param {Array} [props.firstBlock] - First comment in the array.
* @param {Array} [props.firstComment] - First comment in the array.
* @param {Array} [props.blocks] - Array of blocks returned from
* getBlocks() in parent .
* @return {WPElement} Inner blocks of the Comment Template
Expand All @@ -44,7 +44,7 @@ function CommentTemplateInnerBlocks( {
comment,
activeComment,
setActiveComment,
firstBlock,
firstComment,
blocks,
} ) {
const { children, ...innerBlocksProps } = useInnerBlocksProps(
Expand All @@ -53,15 +53,22 @@ function CommentTemplateInnerBlocks( {
);
return (
<li { ...innerBlocksProps }>
{ comment === ( activeComment || firstBlock ) ? (
children
) : (
<BlockPreview
blocks={ blocks }
__experimentalLive
__experimentalOnClick={ () => setActiveComment( comment ) }
/>
) }
{ comment === ( activeComment || firstComment ) ? children : null }

{ /* To avoid flicker when switching active block contexts, a preview
is ALWAYS rendered and the preview for the active block is hidden.
This ensures that when switching the active block, the component is not
mounted again but rather it only toggles the `isHidden` prop.
The same strategy is used for preventing the flicker in the Post Template
block. */ }
<MemoizedCommentTemplatePreview
blocks={ blocks }
comment={ comment }
setActiveComment={ setActiveComment }
isHidden={ comment === ( activeComment || firstComment ) }
/>

{ comment?.children?.length > 0 ? (
<CommentsList
comments={ comment.children }
Expand All @@ -74,6 +81,44 @@ function CommentTemplateInnerBlocks( {
);
}

const CommentTemplatePreview = ( {
blocks,
comment,
setActiveComment,
isHidden,
} ) => {
const blockPreviewProps = useBlockPreview( {
blocks,
} );

const handleOnClick = () => {
setActiveComment( comment );
};

// We have to hide the preview block if the `comment` props points to
// the curently active block!

// Or, to put it differently, every preview block is visible unless it is the
// currently active block - in this case we render its inner blocks.
const style = {
display: isHidden ? 'none' : undefined,
};

return (
<div
{ ...blockPreviewProps }
tabIndex={ 0 }
role="button"
style={ style }
// eslint-disable-next-line jsx-a11y/no-noninteractive-element-to-interactive-role
onClick={ handleOnClick }
onKeyPress={ handleOnClick }
/>
);
};

const MemoizedCommentTemplatePreview = memo( CommentTemplatePreview );

/**
* Component that renders a list of (nested) comments. It is called recursively.
*
Expand Down Expand Up @@ -105,7 +150,7 @@ const CommentsList = ( {
activeComment={ activeComment }
setActiveComment={ setActiveComment }
blocks={ blocks }
firstBlock={ comments[ 0 ] }
firstComment={ comments[ 0 ] }
/>
</BlockContextProvider>
) ) }
Expand Down

0 comments on commit 4f72f26

Please sign in to comment.