Skip to content

Commit

Permalink
Pattern Inserter: show insertion indicator when hovering on patterns (#…
Browse files Browse the repository at this point in the history
…47316)

* Pattern Inserter: show insertion indicator when hovering on patterns

* Try to fix e2e test

* Don't show indicator when focus

* Fix e2e test
  • Loading branch information
t-hamano authored Jan 26, 2023
1 parent 38a95a6 commit 2797c8c
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 12 deletions.
32 changes: 29 additions & 3 deletions packages/block-editor/src/components/block-patterns-list/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* WordPress dependencies
*/
import { useState } from '@wordpress/element';
import {
VisuallyHidden,
__unstableComposite as Composite,
Expand Down Expand Up @@ -28,9 +29,11 @@ function BlockPattern( {
isDraggable,
pattern,
onClick,
onHover,
composite,
showTooltip,
} ) {
const [ isDragging, setIsDragging ] = useState( false );
const { blocks, viewportWidth } = pattern;
const instanceId = useInstanceId( BlockPattern );
const descriptionId = `block-editor-block-patterns-list__item-description-${ instanceId }`;
Expand All @@ -45,8 +48,19 @@ function BlockPattern( {
<div
className="block-editor-block-patterns-list__list-item"
draggable={ draggable }
onDragStart={ onDragStart }
onDragEnd={ onDragEnd }
onDragStart={ ( event ) => {
setIsDragging( true );
if ( onDragStart ) {
onHover( null );
onDragStart( event );
}
} }
onDragEnd={ ( event ) => {
setIsDragging( false );
if ( onDragEnd ) {
onDragEnd( event );
}
} }
>
<WithToolTip
showTooltip={ showTooltip }
Expand All @@ -57,7 +71,17 @@ function BlockPattern( {
as="div"
{ ...composite }
className="block-editor-block-patterns-list__item"
onClick={ () => onClick( pattern, blocks ) }
onClick={ () => {
onClick( pattern, blocks );
onHover( null );
} }
onMouseEnter={ () => {
if ( isDragging ) {
return;
}
onHover( pattern );
} }
onMouseLeave={ () => onHover( null ) }
aria-label={ pattern.title }
aria-describedby={
pattern.description ? descriptionId : undefined
Expand Down Expand Up @@ -95,6 +119,7 @@ function BlockPatternList( {
isDraggable,
blockPatterns,
shownPatterns,
onHover,
onClickPattern,
orientation,
label = __( 'Block Patterns' ),
Expand All @@ -115,6 +140,7 @@ function BlockPatternList( {
key={ pattern.name }
pattern={ pattern }
onClick={ onClickPattern }
onHover={ onHover }
isDraggable={ isDraggable }
composite={ composite }
showTooltip={ showTitlesAsTooltip }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,20 +107,13 @@ function InserterListItem( {
onHover( null );
}
} }
onFocus={ () => {
if ( isDragging.current ) {
return;
}
onHover( item );
} }
onMouseEnter={ () => {
if ( isDragging.current ) {
return;
}
onHover( item );
} }
onMouseLeave={ () => onHover( null ) }
onBlur={ () => onHover( null ) }
{ ...props }
>
<span
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ function usePatternsCategories( rootClientId ) {
export function BlockPatternsCategoryDialog( {
rootClientId,
onInsert,
onHover,
category,
showTitlesAsTooltip,
} ) {
Expand All @@ -113,6 +114,7 @@ export function BlockPatternsCategoryDialog( {
<BlockPatternsCategoryPanel
rootClientId={ rootClientId }
onInsert={ onInsert }
onHover={ onHover }
category={ category }
showTitlesAsTooltip={ showTitlesAsTooltip }
/>
Expand All @@ -123,6 +125,7 @@ export function BlockPatternsCategoryDialog( {
export function BlockPatternsCategoryPanel( {
rootClientId,
onInsert,
onHover,
category,
showTitlesAsTooltip,
} ) {
Expand Down Expand Up @@ -156,6 +159,9 @@ export function BlockPatternsCategoryPanel( {

const currentShownPatterns = useAsyncList( currentCategoryPatterns );

// Hide block pattern preview on unmount.
useEffect( () => () => onHover( null ), [] );

if ( ! currentCategoryPatterns.length ) {
return null;
}
Expand All @@ -170,6 +176,7 @@ export function BlockPatternsCategoryPanel( {
shownPatterns={ currentShownPatterns }
blockPatterns={ currentCategoryPatterns }
onClickPattern={ onClick }
onHover={ onHover }
label={ category.label }
orientation="vertical"
category={ category.label }
Expand Down
1 change: 1 addition & 0 deletions packages/block-editor/src/components/inserter/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ function InserterMenu(
<BlockPatternsCategoryDialog
rootClientId={ destinationRootClientId }
onInsert={ onInsertPattern }
onHover={ onHover }
category={ selectedPatternCategory }
showTitlesAsTooltip
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ function InserterSearchResults( {
shownPatterns={ currentShownPatterns }
blockPatterns={ filteredBlockPatterns }
onClickPattern={ onSelectBlockPattern }
onHover={ onHover }
isDraggable={ isDraggable }
/>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,8 +347,10 @@ describe( 'Inserting blocks', () => {

it( 'shows block preview when hovering over block in inserter', async () => {
await openGlobalBlockInserter();
await page.waitForSelector( '.editor-block-list-item-paragraph' );
await page.focus( '.editor-block-list-item-paragraph' );
const paragraphButton = (
await page.$x( `//button//span[contains(text(), 'Paragraph')]` )
)[ 0 ];
await paragraphButton.hover();
const preview = await page.waitForSelector(
'.block-editor-inserter__preview',
{
Expand Down

1 comment on commit 2797c8c

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in 2797c8c.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/4014464139
📝 Reported issues:

Please sign in to comment.