-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Block Fields: Cover block issues #74639
Description
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:
- Add a cover block
- From the inspector content tab, upload an image
- 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 -
gutenberg/packages/block-library/src/cover/edit/index.js
Lines 194 to 261 in b06f167
| 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.