diff --git a/packages/block-library/src/template-part/edit/index.js b/packages/block-library/src/template-part/edit/index.js
index 3b0bbb663ea97..4e75824229f29 100644
--- a/packages/block-library/src/template-part/edit/index.js
+++ b/packages/block-library/src/template-part/edit/index.js
@@ -32,6 +32,7 @@ import {
useAlternativeBlockPatterns,
useAlternativeTemplateParts,
useTemplatePartArea,
+ useCreateTemplatePartFromBlocks,
} from './utils/hooks';
function ReplaceButton( {
@@ -155,6 +156,11 @@ export default function TemplatePartEdit( {
hasReplacements &&
( area === 'header' || area === 'footer' );
+ const createFromBlocks = useCreateTemplatePartFromBlocks(
+ area,
+ setAttributes
+ );
+
// 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 (
@@ -189,13 +195,12 @@ export default function TemplatePartEdit( {
const partsAsPatterns = templateParts.map( ( templatePart ) =>
mapTemplatePartToBlockPattern( templatePart )
);
- const patternsAndParts = [ ...blockPatterns, ...partsAsPatterns ];
- const onTemplatePartSelect = ( { templatePart } ) => {
+ const onTemplatePartSelect = ( templatePart ) => {
setAttributes( {
slug: templatePart.slug,
theme: templatePart.theme,
- area: templatePart.area,
+ area: undefined,
} );
createSuccessNotice(
sprintf(
@@ -263,16 +268,31 @@ export default function TemplatePartEdit( {
} }
- { canReplace && patternsAndParts.length && (
-
-
-
-
-
- ) }
+ { canReplace &&
+ ( partsAsPatterns.length > 0 ||
+ blockPatterns.length > 0 ) && (
+
+
+ {
+ onTemplatePartSelect(
+ pattern.templatePart
+ );
+ } }
+ />
+ {
+ createFromBlocks(
+ blocks,
+ pattern.title
+ );
+ } }
+ />
+
+
+ ) }
{ isEntityAvailable && (