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

Template part block: avoid parsing ALL patterns on mount #57856

Merged
merged 1 commit into from
Jan 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
104 changes: 64 additions & 40 deletions packages/block-library/src/template-part/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,43 @@ import {
useTemplatePartArea,
} from './utils/hooks';

function ReplaceButton( {
isEntityAvailable,
area,
clientId,
templatePartId,
isTemplatePartSelectionOpen,
setIsTemplatePartSelectionOpen,
} ) {
const { templateParts } = useAlternativeTemplateParts(
area,
templatePartId
);
const blockPatterns = useAlternativeBlockPatterns( area, clientId );

const hasReplacements = !! templateParts.length || !! blockPatterns.length;
const canReplace =
isEntityAvailable &&
hasReplacements &&
( area === 'header' || area === 'footer' );

if ( ! canReplace ) {
return null;
}

return (
<MenuItem
onClick={ () => {
setIsTemplatePartSelectionOpen( true );
} }
aria-expanded={ isTemplatePartSelectionOpen }
aria-haspopup="dialog"
>
{ __( 'Replace' ) }
</MenuItem>
);
}

export default function TemplatePartEdit( {
attributes,
setAttributes,
Expand Down Expand Up @@ -81,23 +118,13 @@ export default function TemplatePartEdit( {
},
[ templatePartId, attributes.area, clientId ]
);
const { templateParts } = useAlternativeTemplateParts(
area,
templatePartId
);
const blockPatterns = useAlternativeBlockPatterns( area, clientId );
const hasReplacements = !! templateParts.length || !! blockPatterns.length;

const areaObject = useTemplatePartArea( area );
const blockProps = useBlockProps();
const isPlaceholder = ! slug;
const isEntityAvailable = ! isPlaceholder && ! isMissing && isResolved;
const TagName = tagName || areaObject.tagName;

const canReplace =
isEntityAvailable &&
hasReplacements &&
( area === 'header' || area === 'footer' );

// We don't want to render a missing state if we have any inner blocks.
// A new template part is automatically created if we have any inner blocks but no entity.
if (
Expand Down Expand Up @@ -153,36 +180,33 @@ export default function TemplatePartEdit( {
/>
</TagName>
) }
{ canReplace && (
<BlockSettingsMenuControls>
{ ( { selectedClientIds } ) => {
// Only enable for single selection that matches the current block.
// Ensures menu item doesn't render multiple times.
if (
! (
selectedClientIds.length === 1 &&
clientId === selectedClientIds[ 0 ]
)
) {
return null;
}
<BlockSettingsMenuControls>
{ ( { selectedClientIds } ) => {
// Only enable for single selection that matches the current block.
// Ensures menu item doesn't render multiple times.
if (
! (
selectedClientIds.length === 1 &&
clientId === selectedClientIds[ 0 ]
)
) {
return null;
}

return (
<MenuItem
onClick={ () => {
setIsTemplatePartSelectionOpen( true );
} }
aria-expanded={
isTemplatePartSelectionOpen
}
aria-haspopup="dialog"
>
{ __( 'Replace' ) }
</MenuItem>
);
} }
</BlockSettingsMenuControls>
) }
return (
<ReplaceButton
{ ...{
isEntityAvailable,
area,
clientId,
templatePartId,
isTemplatePartSelectionOpen,
setIsTemplatePartSelectionOpen,
} }
/>
);
} }
</BlockSettingsMenuControls>
{ isEntityAvailable && (
<TemplatePartInnerBlocks
tagName={ TagName }
Expand Down