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 placeholder - Add title step to creation flow. #33703

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
Expand Up @@ -55,8 +55,10 @@ export default function TemplatePartPlaceholder( {
);

const onCreate = useCallback(
async ( startingBlocks = [] ) => {
const title = __( 'Untitled Template Part' );
async (
startingBlocks = [],
title = __( 'Untitled Template Part' )
) => {
// If we have `area` set from block attributes, means an exposed
// block variation was inserted. So add this prop to the template
// part entity on creation. Afterwards remove `area` value from
Expand Down Expand Up @@ -154,8 +156,13 @@ export default function TemplatePartPlaceholder( {
{ step === PLACEHOLDER_STEPS.patterns && (
<PatternsSetup
area={ area }
areaLabel={ areaLabel }
areaIcon={ areaIcon }
onCreate={ onCreate }
clientId={ clientId }
resetPlaceholder={ () =>
setStep( PLACEHOLDER_STEPS.initial )
}
/>
) }
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,122 @@
* WordPress dependencies
*/
import { __experimentalBlockPatternSetup as BlockPatternSetup } from '@wordpress/block-editor';
import { useEffect } from '@wordpress/element';
import { useEffect, useState } from '@wordpress/element';
import { __, sprintf } from '@wordpress/i18n';
import {
TextControl,
Flex,
FlexItem,
Button,
Modal,
Placeholder,
} from '@wordpress/components';

export default function PatternsSetup( { area, clientId, onCreate } ) {
export default function PatternsSetup( {
area,
areaLabel,
areaIcon,
clientId,
onCreate,
resetPlaceholder,
} ) {
const blockNameWithArea = area
? `core/template-part/${ area }`
: 'core/template-part';

// Restructure onCreate to set the blocks on local state.
// Add modal to confirm title and trigger onCreate.
const [ title, setTitle ] = useState( __( 'Untitled Template Part' ) );
const [ startingBlocks, setStartingBlocks ] = useState( [] );
const [ isTitleStep, setIsTitleStep ] = useState( false );

const selectPattern = ( selectedPattern ) => {
setStartingBlocks( selectedPattern );
setIsTitleStep( true );
};

const submitForCreation = ( event ) => {
event.preventDefault();
onCreate( startingBlocks, title );
};

return (
<BlockPatternSetup
clientId={ clientId }
startBlankComponent={
<StartBlankComponent onCreate={ onCreate } />
}
onBlockPatternSelect={ onCreate }
filterPatternsFn={ ( pattern ) =>
pattern?.blockTypes?.some?.(
( blockType ) => blockType === blockNameWithArea
)
}
/>
<>
<BlockPatternSetup
clientId={ clientId }
startBlankComponent={
<StartBlankComponent
setTitleStep={ setIsTitleStep }
areaLabel={ areaLabel }
areaIcon={ areaIcon }
/>
}
onBlockPatternSelect={ selectPattern }
filterPatternsFn={ ( pattern ) =>
pattern?.blockTypes?.some?.(
( blockType ) => blockType === blockNameWithArea
)
}
/>
{ isTitleStep && (
<Modal
title={ sprintf(
'Name and create your new %s',
areaLabel.toLowerCase()
) }
closeLabel={ __( 'Cancel' ) }
onRequestClose={ resetPlaceholder }
overlayClassName="wp-block-template-part__placeholder-create-new__title-form"
>
<form onSubmit={ submitForCreation }>
<TextControl
label={ __( 'Name' ) }
value={ title }
onChange={ setTitle }
/>
<Flex
className="wp-block-template-part__placeholder-create-new__title-form-actions"
justify="flex-end"
>
<FlexItem>
<Button
variant="secondary"
onClick={ resetPlaceholder }
>
{ __( 'Cancel' ) }
</Button>
</FlexItem>
<FlexItem>
<Button
variant="primary"
type="submit"
disabled={ ! title.length }
aria-disabled={ ! title.length }
>
{ __( 'Create' ) }
</Button>
</FlexItem>
</Flex>
</form>
</Modal>
) }
</>
);
}

function StartBlankComponent( { onCreate } ) {
function StartBlankComponent( { setTitleStep, areaLabel, areaIcon } ) {
useEffect( () => {
onCreate();
setTitleStep( true );
}, [] );
return null;
return (
<Placeholder
label={ areaLabel }
icon={ areaIcon }
instructions={ sprintf(
// Translators: %s as template part area title ("Header", "Footer", "Template Part", etc.).
'Creating your new %s...',
areaLabel.toLowerCase()
) }
/>
);
}
15 changes: 15 additions & 0 deletions packages/block-library/src/template-part/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,18 @@
}
}
}

.wp-block-template-part__placeholder-create-new__title-form {
.wp-block-template-part__placeholder-create-new__title-form-actions {
padding-top: $grid-unit-15;
// Unsure why these styles need to be added since we are using the Flex component with
// flex-end setting. Created issue https://github.com/WordPress/gutenberg/issues/33735 to
// attempt document this issue.
display: flex;
justify-content: flex-end;

.components-flex-item {
margin-left: $grid-unit-15;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ describe( 'Multi-entity save flow', () => {
const saveA11ySelector =
'.edit-post-layout__toggle-entities-saved-states-panel-button';
const publishPanelSelector = '.editor-post-publish-panel';
const confirmTitleButtonSelector =
'.wp-block-template-part__placeholder-create-new__title-form .components-button.is-primary';

// Reusable assertions inside Post editor.
const assertMultiSaveEnabled = async () => {
Expand Down Expand Up @@ -104,6 +106,11 @@ describe( 'Multi-entity save flow', () => {
createNewButtonSelector
);
await createNewButton.click();
const confirmTitleButton = await page.waitForSelector(
confirmTitleButtonSelector
);
await confirmTitleButton.click();

await page.waitForSelector( activatedTemplatePartSelector );
await page.click( '.block-editor-button-block-appender' );
await page.click( '.editor-block-list-item-paragraph' );
Expand Down
6 changes: 6 additions & 0 deletions packages/e2e-tests/specs/experiments/template-part.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,8 @@ describe( 'Template Part', () => {
'//button[contains(text(), "New template part")]';
const chooseExistingButtonSelector =
'//button[contains(text(), "Choose existing")]';
const confirmTitleButtonSelector =
'.wp-block-template-part__placeholder-create-new__title-form .components-button.is-primary';

it( 'Should insert new template part on creation', async () => {
await createNewPost();
Expand All @@ -287,6 +289,10 @@ describe( 'Template Part', () => {
createNewButtonSelector
);
await createNewButton.click();
const confirmTitleButton = await page.waitForSelector(
confirmTitleButtonSelector
);
await confirmTitleButton.click();

const newTemplatePart = await page.waitForSelector(
activatedTemplatePartSelector
Expand Down