Skip to content

Commit

Permalink
BlockPatternsList: use the Async component (WordPress#66744)
Browse files Browse the repository at this point in the history
Co-authored-by: ellatrix <[email protected]>
Co-authored-by: mcsf <[email protected]>
  • Loading branch information
3 people authored Nov 5, 2024
1 parent c41b219 commit f1fe404
Show file tree
Hide file tree
Showing 22 changed files with 38 additions and 95 deletions.
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/block-editor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"@wordpress/keycodes": "*",
"@wordpress/notices": "*",
"@wordpress/preferences": "*",
"@wordpress/priority-queue": "*",
"@wordpress/private-apis": "*",
"@wordpress/rich-text": "*",
"@wordpress/style-engine": "*",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import { BlockPatternsList } from '@wordpress/block-editor';
const MyBlockPatternsList = () => (
<BlockPatternsList
blockPatterns={ shownBlockPatterns }
shownPatterns={ shownBlockPatterns }
onClickPattern={ onSelectBlockPattern }
/>
);
Expand All @@ -33,13 +32,6 @@ An array of block patterns that can be shown in the block patterns list.
- Type: `Array`
- Required: Yes

#### shownPatterns

An array of shown block patterns objects.

- Type: `Array`
- Required: Yes

#### onClickPattern

The performed event after a click on a block pattern. In most cases, the pattern is inserted in the block editor.
Expand Down
50 changes: 23 additions & 27 deletions packages/block-editor/src/components/block-patterns-list/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,14 @@ function BlockPattern( {
} }
onMouseLeave={ () => onHover?.( null ) }
>
<BlockPreview
blocks={ blocks }
viewportWidth={ viewportWidth }
/>
<BlockPreview.Async
placeholder={ <BlockPatternPlaceholder /> }
>
<BlockPreview
blocks={ blocks }
viewportWidth={ viewportWidth }
/>
</BlockPreview.Async>

{ showTitle && (
<HStack
Expand Down Expand Up @@ -187,7 +191,6 @@ function BlockPatternsList(
{
isDraggable,
blockPatterns,
shownPatterns,
onHover,
onClickPattern,
orientation,
Expand All @@ -205,11 +208,9 @@ function BlockPatternsList(
// Reset the active composite item whenever the available patterns change,
// to make sure that Composite widget can receive focus correctly when its
// composite items change. The first composite item will receive focus.
const firstCompositeItemId = blockPatterns.find( ( pattern ) =>
shownPatterns.includes( pattern )
)?.name;
const firstCompositeItemId = blockPatterns[ 0 ]?.name;
setActiveCompositeId( firstCompositeItemId );
}, [ shownPatterns, blockPatterns ] );
}, [ blockPatterns ] );

return (
<Composite
Expand All @@ -221,24 +222,19 @@ function BlockPatternsList(
aria-label={ label }
ref={ ref }
>
{ blockPatterns.map( ( pattern ) => {
const isShown = shownPatterns.includes( pattern );
return isShown ? (
<BlockPattern
key={ pattern.name }
id={ pattern.name }
pattern={ pattern }
onClick={ onClickPattern }
onHover={ onHover }
isDraggable={ isDraggable }
showTitle={ showTitle }
showTooltip={ showTitlesAsTooltip }
category={ category }
/>
) : (
<BlockPatternPlaceholder key={ pattern.name } />
);
} ) }
{ blockPatterns.map( ( pattern ) => (
<BlockPattern
key={ pattern.name }
id={ pattern.name }
pattern={ pattern }
onClick={ onClickPattern }
onHover={ onHover }
isDraggable={ isDraggable }
showTitle={ showTitle }
showTooltip={ showTitlesAsTooltip }
category={ category }
/>
) ) }
{ pagingProps && <BlockPatternsPaging { ...pagingProps } /> }
</Composite>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@
*/
import blockLibraryStyles from '!!raw-loader!../../../../../block-library/build-style/style.css';

/**
* WordPress dependencies
*/
import { useAsyncList } from '@wordpress/compose';

/**
* Internal dependencies
*/
Expand All @@ -26,13 +21,9 @@ export default {

export const Default = {
render: function Template( props ) {
const shownPatterns = useAsyncList( props.blockPatterns );
return (
<ExperimentalBlockEditorProvider settings={ blockEditorSettings }>
<BlockPatternsList
shownPatterns={ shownPatterns }
{ ...props }
/>
<BlockPatternsList { ...props } />
</ExperimentalBlockEditorProvider>
);
},
Expand Down
7 changes: 6 additions & 1 deletion packages/block-editor/src/components/block-preview/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import AutoHeightBlockPreview from './auto';
import EditorStyles from '../editor-styles';
import { store as blockEditorStore } from '../../store';
import { BlockListItems } from '../block-list';
import { Async } from './async';

const EMPTY_ADDITIONAL_STYLES = [];

Expand Down Expand Up @@ -86,6 +87,10 @@ export function BlockPreview( {
);
}

const MemoizedBlockPreview = memo( BlockPreview );

MemoizedBlockPreview.Async = Async;

/**
* BlockPreview renders a preview of a block or array of blocks.
*
Expand All @@ -97,7 +102,7 @@ export function BlockPreview( {
*
* @return {Component} The component to be rendered.
*/
export default memo( BlockPreview );
export default MemoizedBlockPreview;

/**
* This hook is used to lightly mark an element as a block preview wrapper
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
import { __ } from '@wordpress/i18n';
import { cloneBlock } from '@wordpress/blocks';
import { useMemo } from '@wordpress/element';
import { useAsyncList } from '@wordpress/compose';
import { useSelect, useDispatch } from '@wordpress/data';

/**
Expand Down Expand Up @@ -81,10 +80,6 @@ export default function ChangeDesign( { clientId } ) {
.slice( 0, MAX_PATTERNS_TO_SHOW );
}, [ categories, currentPatternName, patterns ] );

const currentShownPatterns = useAsyncList(
sameCategoryPatternsWithSingleWrapper
);

if ( sameCategoryPatternsWithSingleWrapper.length < 2 ) {
return null;
}
Expand Down Expand Up @@ -121,7 +116,6 @@ export default function ChangeDesign( { clientId } ) {
paddingSize="none"
>
<BlockPatternsList
shownPatterns={ currentShownPatterns }
blockPatterns={ sameCategoryPatternsWithSingleWrapper }
onClickPattern={ onClickPattern }
showTitle={ false }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,6 @@ function PatternList( {
{ hasItems && (
<>
<BlockPatternsList
shownPatterns={
pagingProps.categoryPatternsAsyncList
}
blockPatterns={ pagingProps.categoryPatterns }
onClickPattern={ onClickPattern }
isDraggable={ false }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,6 @@ export function PatternCategoryPreviews( {
) }
<BlockPatternsList
ref={ scrollContainerRef }
shownPatterns={ pagingProps.categoryPatternsAsyncList }
blockPatterns={ pagingProps.categoryPatterns }
onClickPattern={ onClickPattern }
onHover={ onHover }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
* WordPress dependencies
*/
import { useMemo, useState, useEffect } from '@wordpress/element';
import { useAsyncList, usePrevious } from '@wordpress/compose';
import { usePrevious } from '@wordpress/compose';
import { getScrollContainer } from '@wordpress/dom';

const PAGE_SIZE = 20;
const INITIAL_INSERTER_RESULTS = 5;

/**
* Supplies values needed to page the patterns list client side.
Expand Down Expand Up @@ -42,9 +41,6 @@ export default function usePatternsPaging(
pageIndex * PAGE_SIZE + PAGE_SIZE
);
}, [ pageIndex, currentCategoryPatterns ] );
const categoryPatternsAsyncList = useAsyncList( categoryPatterns, {
step: INITIAL_INSERTER_RESULTS,
} );
const numPages = Math.ceil( currentCategoryPatterns.length / PAGE_SIZE );
const changePage = ( page ) => {
const scrollContainer = getScrollContainer(
Expand All @@ -68,7 +64,6 @@ export default function usePatternsPaging(
return {
totalItems,
categoryPatterns,
categoryPatternsAsyncList,
numPages,
changePage,
currentPage,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,6 @@ function InserterSearchResults( {
const currentShownBlockTypes = useAsyncList( filteredBlockTypes, {
step: INITIAL_INSERTER_RESULTS,
} );
const currentShownPatterns = useAsyncList(
currentShownBlockTypes.length === filteredBlockTypes.length
? filteredBlockPatterns
: EMPTY_ARRAY
);

const hasItems =
filteredBlockTypes.length > 0 || filteredBlockPatterns.length > 0;
Expand All @@ -190,7 +185,6 @@ function InserterSearchResults( {
>
<div className="block-editor-inserter__quick-inserter-patterns">
<BlockPatternsList
shownPatterns={ currentShownPatterns }
blockPatterns={ filteredBlockPatterns }
onClickPattern={ onClickPattern }
onHover={ onHoverPattern }
Expand Down
1 change: 1 addition & 0 deletions packages/block-editor/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
{ "path": "../dom" },
{ "path": "../element" },
{ "path": "../escape-html" },
{ "path": "../priority-queue" },
{ "path": "../private-apis" },
{ "path": "../hooks" },
{ "path": "../html-entities" },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import { useState, useMemo } from '@wordpress/element';
import { useDispatch } from '@wordpress/data';
import { Modal, SearchControl } from '@wordpress/components';
import { useAsyncList } from '@wordpress/compose';
import {
BlockContextProvider,
store as blockEditorStore,
Expand Down Expand Up @@ -55,7 +54,6 @@ export default function PatternSelectionModal( {
const filteredBlockPatterns = useMemo( () => {
return searchPatterns( blockPatterns, searchValue );
}, [ blockPatterns, searchValue ] );
const shownBlockPatterns = useAsyncList( filteredBlockPatterns );

return (
<Modal
Expand All @@ -77,7 +75,6 @@ export default function PatternSelectionModal( {
<BlockContextProvider value={ blockPreviewContext }>
<BlockPatternsList
blockPatterns={ filteredBlockPatterns }
shownPatterns={ shownBlockPatterns }
onClickPattern={ onBlockPatternSelect }
/>
</BlockContextProvider>
Expand Down
3 changes: 0 additions & 3 deletions packages/block-library/src/template-part/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {
MenuItem,
ToolbarButton,
} from '@wordpress/components';
import { useAsyncList } from '@wordpress/compose';
import { __, sprintf } from '@wordpress/i18n';
import { store as coreStore } from '@wordpress/core-data';
import { useState } from '@wordpress/element';
Expand Down Expand Up @@ -85,7 +84,6 @@ function TemplatesList( { area, clientId, isEntityAvailable, onSelect } ) {
isEntityAvailable &&
!! blockPatterns.length &&
( area === 'header' || area === 'footer' );
const shownTemplates = useAsyncList( blockPatterns );

if ( ! canReplace ) {
return null;
Expand All @@ -96,7 +94,6 @@ function TemplatesList( { area, clientId, isEntityAvailable, onSelect } ) {
<BlockPatternsList
label={ __( 'Templates' ) }
blockPatterns={ blockPatterns }
shownPatterns={ shownTemplates }
onClickPattern={ onSelect }
showTitle={ false }
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { useMemo, useState } from '@wordpress/element';
import { __, sprintf } from '@wordpress/i18n';
import { store as noticesStore } from '@wordpress/notices';
import { useDispatch } from '@wordpress/data';
import { useAsyncList } from '@wordpress/compose';
import { __experimentalBlockPatternsList as BlockPatternsList } from '@wordpress/block-editor';
import {
SearchControl,
Expand Down Expand Up @@ -43,7 +42,6 @@ export default function TemplatePartSelectionModal( {

return searchPatterns( partsAsPatterns, searchValue );
}, [ templateParts, searchValue ] );
const shownTemplateParts = useAsyncList( filteredTemplateParts );
const blockPatterns = useAlternativeBlockPatterns( area, clientId );
const filteredBlockPatterns = useMemo( () => {
return searchPatterns( blockPatterns, searchValue );
Expand Down Expand Up @@ -89,7 +87,6 @@ export default function TemplatePartSelectionModal( {
<h2>{ __( 'Existing template parts' ) }</h2>
<BlockPatternsList
blockPatterns={ filteredTemplateParts }
shownPatterns={ shownTemplateParts }
onClickPattern={ ( pattern ) => {
onTemplatePartSelect( pattern.templatePart );
} }
Expand Down
5 changes: 2 additions & 3 deletions packages/edit-site/src/components/page-patterns/fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import { decodeEntities } from '@wordpress/html-entities';
/**
* Internal dependencies
*/
import { Async } from '../async';
import {
PATTERN_TYPES,
TEMPLATE_PART_POST_TYPE,
Expand Down Expand Up @@ -88,12 +87,12 @@ function PreviewField( { item } ) {
{ isEmpty && isTemplatePart && __( 'Empty template part' ) }
{ isEmpty && ! isTemplatePart && __( 'Empty pattern' ) }
{ ! isEmpty && (
<Async>
<BlockPreview.Async>
<BlockPreview
blocks={ blocks }
viewportWidth={ item.viewportWidth }
/>
</Async>
</BlockPreview.Async>
) }
</PreviewWrapper>
{ !! description && (
Expand Down
5 changes: 2 additions & 3 deletions packages/edit-site/src/components/page-templates/fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import { EditorProvider } from '@wordpress/editor';
/**
* Internal dependencies
*/
import { Async } from '../async';
import { default as Link, useLink } from '../routes/link';
import { useAddedBy } from './hooks';

Expand Down Expand Up @@ -63,9 +62,9 @@ function PreviewField( { item } ) {
>
{ isEmpty && __( 'Empty template' ) }
{ ! isEmpty && (
<Async>
<BlockPreview.Async>
<BlockPreview blocks={ blocks } />
</Async>
</BlockPreview.Async>
) }
</button>
</div>
Expand Down
Loading

0 comments on commit f1fe404

Please sign in to comment.