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

Migrating block editor BlockPatternsList component #56210

Merged
merged 5 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Block Patterns List

The `BlockPatternList` component makes a list of the different registered block patterns. It uses the `BlockPreview` component to display a preview for each block pattern.
The `BlockPatternsList` component makes a list of the different registered block patterns. It uses the `BlockPreview` component to display a preview for each block pattern.

For more infos about blocks patterns, read [this](https://make.wordpress.org/core/2020/07/16/block-patterns-in-wordpress-5-5/).

Expand All @@ -18,10 +18,10 @@ For more infos about blocks patterns, read [this](https://make.wordpress.org/cor
Renders a block patterns list.

```jsx
import { BlockPatternList } from '@wordpress/block-editor';
import { BlockPatternsList } from '@wordpress/block-editor';
andrewhayward marked this conversation as resolved.
Show resolved Hide resolved

const MyBlockPatternList = () => (
<BlockPatternList
const MyBlockPatternsList = () => (
<BlockPatternsList
blockPatterns={ shownBlockPatterns }
shownPatterns={ shownBlockPatterns }
onClickPattern={ onSelectBlockPattern }
Expand Down
80 changes: 57 additions & 23 deletions packages/block-editor/src/components/block-patterns-list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,8 @@ import classnames from 'classnames';
import { useState, forwardRef } from '@wordpress/element';
import {
VisuallyHidden,
__unstableComposite as Composite,
__unstableUseCompositeState as useCompositeState,
__unstableCompositeItem as CompositeItem,
Tooltip,
privateApis as componentsPrivateApis,
__experimentalHStack as HStack,
} from '@wordpress/components';
import { useInstanceId } from '@wordpress/compose';
Expand All @@ -22,10 +20,17 @@ import { Icon, symbol } from '@wordpress/icons';
/**
* Internal dependencies
*/
import { unlock } from '../../lock-unlock';
import BlockPreview from '../block-preview';
import InserterDraggableBlocks from '../inserter-draggable-blocks';
import BlockPatternsPaging from '../block-patterns-paging';

const {
CompositeV2: Composite,
CompositeItemV2: CompositeItem,
useCompositeStoreV2: useCompositeStore,
} = unlock( componentsPrivateApis );

const WithToolTip = ( { showTooltip, title, children } ) => {
if ( showTooltip ) {
return <Tooltip text={ title }>{ children }</Tooltip>;
Expand All @@ -34,11 +39,11 @@ const WithToolTip = ( { showTooltip, title, children } ) => {
};

function BlockPattern( {
id,
isDraggable,
pattern,
onClick,
onHover,
composite,
showTooltip,
} ) {
const [ isDragging, setIsDragging ] = useState( false );
Expand Down Expand Up @@ -75,16 +80,26 @@ function BlockPattern( {
title={ pattern.title }
>
<CompositeItem
role="option"
as="div"
{ ...composite }
className={ classnames(
'block-editor-block-patterns-list__item',
{
'block-editor-block-patterns-list__list-item-synced':
pattern.id && ! pattern.syncStatus,
}
) }
render={
<div
role="option"
aria-label={ pattern.title }
aria-describedby={
pattern.description
? descriptionId
: undefined
}
className={ classnames(
'block-editor-block-patterns-list__item',
{
'block-editor-block-patterns-list__list-item-synced':
pattern.id &&
! pattern.syncStatus,
}
) }
/>
}
id={ id }
onClick={ () => {
onClick( pattern, blocks );
onHover?.( null );
Expand All @@ -96,10 +111,6 @@ function BlockPattern( {
onHover?.( pattern );
} }
onMouseLeave={ () => onHover?.( null ) }
aria-label={ pattern.title }
aria-describedby={
pattern.description ? descriptionId : undefined
}
>
<BlockPreview
blocks={ blocks }
Expand Down Expand Up @@ -141,7 +152,7 @@ function BlockPatternPlaceholder() {
);
}

function BlockPatternList(
function BlockPatternsList(
{
isDraggable,
blockPatterns,
Expand All @@ -155,10 +166,33 @@ function BlockPatternList(
},
ref
) {
const composite = useCompositeState( { orientation } );
const compositeStore = useCompositeStore( {
orientation,
setItems: ( items ) => {
// This is necessary for if/when we filter the block patterns;
// we can potentially remove the currently active item, so we
// check to see if the active item is still present, and if not,
// reset the active item to be the first available block.

const currentIds = items.map( ( item ) => item.id );
if ( ! currentIds.includes( activeId ) ) {
// We can't rely on the order of `currentIds` here, because
// `blockPatterns` may not be in the same order the blocks were
// originally registered in. So we filter the blocks by what's
// visible, and take the first item in that list instead.
const firstPattern = blockPatterns.filter( ( pattern ) =>
currentIds.includes( pattern.name )
)[ 0 ];
compositeStore.setActiveId( firstPattern?.name );
}
},
} );
andrewhayward marked this conversation as resolved.
Show resolved Hide resolved

const activeId = compositeStore.useState( 'activeId' );

return (
<Composite
{ ...composite }
store={ compositeStore }
role="listbox"
className="block-editor-block-patterns-list"
aria-label={ label }
Expand All @@ -169,11 +203,11 @@ function BlockPatternList(
return isShown ? (
<BlockPattern
key={ pattern.name }
id={ pattern.name }
pattern={ pattern }
onClick={ onClickPattern }
onHover={ onHover }
isDraggable={ isDraggable }
composite={ composite }
showTooltip={ showTitlesAsTooltip }
/>
) : (
Expand All @@ -185,4 +219,4 @@ function BlockPatternList(
);
}

export default forwardRef( BlockPatternList );
export default forwardRef( BlockPatternsList );
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import {
* Internal dependencies
*/
import usePatternsState from '../hooks/use-patterns-state';
import BlockPatternList from '../../block-patterns-list';
import BlockPatternsList from '../../block-patterns-list';
import usePatternsPaging from '../hooks/use-patterns-paging';
import { PatternsFilter } from './patterns-filter';
import { usePatternCategories } from './use-pattern-categories';
Expand Down Expand Up @@ -155,7 +155,7 @@ export function PatternCategoryPreviews( {
</VStack>

{ currentCategoryPatterns.length > 0 && (
<BlockPatternList
<BlockPatternsList
ref={ scrollContainerRef }
shownPatterns={ pagingProps.categoryPatternsAsyncList }
blockPatterns={ pagingProps.categoryPatterns }
Expand Down
Loading