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

Cover: Always allow transform from Group block, add handling for Row and Stack variations #40212

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
79 changes: 51 additions & 28 deletions packages/block-library/src/cover/transforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,37 +64,60 @@ const transforms = {
{
type: 'block',
blocks: [ 'core/group' ],
isMatch: ( { backgroundColor, gradient, style } ) => {
/*
* Make this transformation available only if the Group has background
* or gradient set, because otherwise `Cover` block displays a Placeholder.
*
* This helps avoid arbitrary decisions about the Cover block's background
* and user confusion about the existence of previous content.
*/
return (
transform: ( attributes, innerBlocks ) => {
const {
align,
anchor,
backgroundColor,
gradient,
style,
} = attributes;

// If no background or gradient color is provided, default to 50% opacity.
// This matches the styling of a Cover block with a background image,
// in the state where a background image has been removed.
const dimRatio =
backgroundColor ||
gradient ||
style?.color?.background ||
style?.color?.gradient ||
gradient
);
},
transform: (
{ align, anchor, backgroundColor, gradient, style },
innerBlocks
) => {
return createBlock(
'core/cover',
{
align,
anchor,
overlayColor: backgroundColor,
customOverlayColor: style?.color?.background,
gradient,
customGradient: style?.color?.gradient,
style?.color?.gradient
? undefined
: 50;

// Move the background or gradient color to the parent Cover block.
const parentAttributes = {
align,
anchor,
dimRatio,
overlayColor: backgroundColor,
customOverlayColor: style?.color?.background,
gradient,
customGradient: style?.color?.gradient,
};

const attributesWithoutBackgroundColors = {
...attributes,
backgroundColor: undefined,
gradient: undefined,
style: {
...attributes?.style,
color: {
...attributes?.style?.color,
background: undefined,
gradient: undefined,
},
},
innerBlocks
);
};

// Preserve the block by nesting it within the Cover block,
// instead of converting the Group block directly to the Cover block.
return createBlock( 'core/cover', parentAttributes, [
createBlock(
'core/group',
attributesWithoutBackgroundColors,
innerBlocks
),
] );
},
},
],
Expand Down