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 - Try more descriptive and contextual button labels. #33783

Closed
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 @@ -12,6 +12,7 @@ import { useDispatch, useSelect } from '@wordpress/data';
import { Placeholder, Dropdown, Button, Spinner } from '@wordpress/components';
import { serialize } from '@wordpress/blocks';
import { store as coreStore } from '@wordpress/core-data';
import { store as blockEditorStore } from '@wordpress/block-editor';

/**
* Internal dependencies
Expand All @@ -34,7 +35,16 @@ export default function TemplatePartPlaceholder( {
const { saveEntityRecord } = useDispatch( coreStore );
const [ step, setStep ] = useState( PLACEHOLDER_STEPS.initial );

const { areaIcon, areaLabel } = useSelect(
const blockNameWithArea = area
? `core/template-part/${ area }`
: 'core/template-part';

const filterPatternsFn = ( pattern ) =>
pattern?.blockTypes?.some?.(
( blockType ) => blockType === blockNameWithArea
);

const { areaIcon, areaLabel, availablePatterns } = useSelect(
( select ) => {
// FIXME: @wordpress/block-library should not depend on @wordpress/editor.
// Blocks can be loaded into a *non-post* block editor.
Expand All @@ -46,9 +56,14 @@ export default function TemplatePartPlaceholder( {
const selectedArea = find( definedAreas, { area } );
const defaultArea = find( definedAreas, { area: 'uncategorized' } );

const _availablePatterns = select( blockEditorStore )
.__experimentalGetAllowedPatterns?.()
.filter( filterPatternsFn );

return {
areaIcon: selectedArea?.icon || defaultArea?.icon,
areaLabel: selectedArea?.label || __( 'Template Part' ),
availablePatterns: _availablePatterns,
};
},
[ area ]
Expand Down Expand Up @@ -95,7 +110,7 @@ export default function TemplatePartPlaceholder( {
enableSelection
? sprintf(
// Translators: %s as template part area title ("Header", "Footer", etc.).
'Choose an existing %s or create a new one.',
'Connect this block to an existing %s or create a new one.',
areaLabel.toLowerCase()
)
: sprintf(
Expand All @@ -114,32 +129,41 @@ export default function TemplatePartPlaceholder( {
renderToggle={ ( { isOpen, onToggle } ) => (
<>
{ enableSelection && (
<Button
variant="primary"
onClick={ onToggle }
aria-expanded={ isOpen }
>
{ __( 'Choose existing' ) }
</Button>
<>
<Button
variant="primary"
onClick={ onToggle }
aria-expanded={ isOpen }
>
{ __(
// Translators: Connect this placeholder block to an existing template part entity/cpt ("Header", "Footer", etc.).
'Connect to existing'
) }
</Button>
<Button
variant={
enableSelection
? 'tertiary'
: 'primary'
}
onClick={ () =>
setStep(
PLACEHOLDER_STEPS.patterns
)
}
>
{ __(
// Translators: Create a new template part ("Header", "Footer", etc.).
'Create new'
) }
{ !! availablePatterns?.length &&
// Translators: block patterns are available for this action.
__(
' (patterns available)'
) }
</Button>
</>
) }
<Button
variant={
enableSelection
? 'tertiary'
: 'primary'
}
onClick={ () =>
setStep(
PLACEHOLDER_STEPS.patterns
)
}
>
{ sprintf(
// Translators: %s as template part area title ("Header", "Footer", etc.).
'New %s',
areaLabel.toLowerCase()
) }
</Button>
</>
) }
renderContent={ ( { onClose } ) => (
Expand All @@ -155,11 +179,11 @@ export default function TemplatePartPlaceholder( {
) }
{ step === PLACEHOLDER_STEPS.patterns && (
<PatternsSetup
area={ area }
areaLabel={ areaLabel }
areaIcon={ areaIcon }
onCreate={ onCreate }
clientId={ clientId }
filterPatternsFn={ filterPatternsFn }
resetPlaceholder={ () =>
setStep( PLACEHOLDER_STEPS.initial )
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,13 @@ import {
} from '@wordpress/components';

export default function PatternsSetup( {
area,
areaLabel,
areaIcon,
clientId,
onCreate,
resetPlaceholder,
filterPatternsFn,
} ) {
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 );
Expand All @@ -53,11 +47,7 @@ export default function PatternsSetup( {
/>
}
onBlockPatternSelect={ selectPattern }
filterPatternsFn={ ( pattern ) =>
pattern?.blockTypes?.some?.(
( blockType ) => blockType === blockNameWithArea
)
}
filterPatternsFn={ filterPatternsFn }
/>
{ isTitleStep && (
<Modal
Expand Down