Skip to content

Block Fields: Cover block issues #74639

@talldan

Description

@talldan

This tracks some issues with the cover block related to the 'Block Fields' experiment.

A prerequisite for testing is to enable the 'Block Fields' experiment on the Gutenberg experiment page.

1. backgroundType not set correctly

After #74575, there's a small issue with the cover block. The backgroundType property is not set correctly when adding a video using the media field in the inspector.

I believe this line is the problem:

mediaType: value.backgroundType,

It should read:

backgroundType: value.mediaType, 

It's probably worth rolling this fix into other cover block fixes.

2. Overlay opacity and text color (isDark) are not set correctly when adding new images

Steps to reproduce:

  1. Add a cover block
  2. From the inspector content tab, upload an image
  3. Note that the image uploads isn't visible in the block. In the style tab the block still has 100 opacity

Cause:
I think that the issue is that the block has some internal logic for calculating the dimRatio and isDark -

const onSelectMedia = async ( newMedia ) => {
const mediaAttributes = attributesFromMedia( newMedia );
const isImage = [ newMedia?.type, newMedia?.media_type ].includes(
IMAGE_BACKGROUND_TYPE
);
const averageBackgroundColor = await getMediaColor(
isImage ? newMedia?.url : undefined
);
let newOverlayColor = overlayColor.color;
if ( ! isUserOverlayColor ) {
newOverlayColor = averageBackgroundColor;
setOverlayColor( newOverlayColor );
// Make undo revert the next setAttributes and the previous setOverlayColor.
__unstableMarkNextChangeAsNotPersistent();
}
// Only set a new dimRatio if there was no previous media selected
// to avoid resetting to 50 if it has been explicitly set to 100.
// See issue #52835 for context.
const newDimRatio =
originalUrl === undefined && dimRatio === 100 ? 50 : dimRatio;
const newIsDark = compositeIsDark(
newDimRatio,
newOverlayColor,
averageBackgroundColor
);
if ( backgroundType === IMAGE_BACKGROUND_TYPE && mediaAttributes?.id ) {
const { imageDefaultSize } = getSettings();
// Try to use the previous selected image size if it's available
// otherwise try the default image size or fallback to full size.
if (
sizeSlug &&
( newMedia?.sizes?.[ sizeSlug ] ||
newMedia?.media_details?.sizes?.[ sizeSlug ] )
) {
mediaAttributes.sizeSlug = sizeSlug;
mediaAttributes.url =
newMedia?.sizes?.[ sizeSlug ]?.url ||
newMedia?.media_details?.sizes?.[ sizeSlug ]?.source_url;
} else if (
newMedia?.sizes?.[ imageDefaultSize ] ||
newMedia?.media_details?.sizes?.[ imageDefaultSize ]
) {
mediaAttributes.sizeSlug = imageDefaultSize;
mediaAttributes.url =
newMedia?.sizes?.[ imageDefaultSize ]?.url ||
newMedia?.media_details?.sizes?.[ imageDefaultSize ]
?.source_url;
} else {
mediaAttributes.sizeSlug = DEFAULT_MEDIA_SIZE_SLUG;
}
}
setAttributes( {
...mediaAttributes,
focalPoint: undefined,
useFeaturedImage: undefined,
dimRatio: newDimRatio,
isDark: newIsDark,
isUserOverlayColor: isUserOverlayColor || false,
} );
};

This code executes whenever uploading new images from the block, but not if some external code (like the Block Fields) tries to set a new image. A fix might be to move the code into a useEffect.

Metadata

Metadata

Assignees

Labels

[Block] CoverAffects the Cover Block - used to display content laid over a background image[Status] In ProgressTracking issues with work in progress[Type] BugAn existing feature does not function as intended

Type

No fields configured for Bug.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions