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

Pattern block: get align setting from children #50230

Merged
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
31 changes: 30 additions & 1 deletion packages/block-library/src/pattern/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
import { ToolbarButton } from '@wordpress/components';

const PatternEdit = ( { attributes, clientId, setAttributes } ) => {
const { forcedAlignment, slug } = attributes;
const { forcedAlignment, slug, syncStatus } = attributes;
const { selectedPattern, innerBlocks } = useSelect(
( select ) => {
return {
Expand Down Expand Up @@ -60,6 +60,35 @@ const PatternEdit = ( { attributes, clientId, setAttributes } ) => {
innerBlocks,
] );

useEffect( () => {
const alignments = [ 'wide', 'full' ];
const blocks =
syncStatus === 'synced' ? selectedPattern?.blocks : innerBlocks;
if ( ! blocks || blocks.length === 0 ) {
return;
}
// Determine the widest setting of all the contained blocks.
const widestAlignment = blocks.reduce( ( accumulator, block ) => {
const { align } = block.attributes;
return alignments.indexOf( align ) >
alignments.indexOf( accumulator )
? align
: accumulator;
}, undefined );

// Set the attribute of the Pattern block to match the widest
// alignment.
setAttributes( {
forcedAlignment: widestAlignment ?? '',
} );
}, [
innerBlocks,
selectedPattern?.blocks,
setAttributes,
syncStatus,
forcedAlignment,
] );

const blockProps = useBlockProps( {
className: forcedAlignment && `align${ forcedAlignment }`,
} );
Expand Down