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

Set custom color when applying initial background image #54054

Merged
merged 49 commits into from Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
bde600e
set overlay from image background average color and refactor code to …
draganescu Sep 14, 2023
1824011
don't reset the maually set overlay, stop redefining utility function…
draganescu Sep 15, 2023
52b52d3
update test with the new default color being white
draganescu Sep 18, 2023
d89064a
add the userOverlayColor attribute to keep track of system vs user co…
draganescu Sep 19, 2023
4ebf178
Remove default value to fix integration tests
ajlende Sep 19, 2023
e27c1c8
Update JSDoc comments
ajlende Sep 19, 2023
4cc3ab2
Consolidate all color functions into color-utils.js
ajlende Sep 19, 2023
a0344e0
Memoize the average color calculation with memize
ajlende Sep 19, 2023
3015555
Move await before setAttributes calls so they won't be spread out
ajlende Sep 19, 2023
fb777db
Consolidate setAttributes calls
ajlende Sep 19, 2023
5ccf7b8
Refactor out setIsDark
ajlende Sep 19, 2023
411c0c7
Refactor attributesFromMedia to return the attributes rather than set…
ajlende Sep 19, 2023
169a512
Refactor out setOverlayFromAverageColor to merge more setAttribute calls
ajlende Sep 19, 2023
b9469ad
Refactor compositeIsDark to return early if second params are the same
ajlende Sep 19, 2023
6f7f55d
Rename next* to new* for consistency with other existing functions
ajlende Sep 19, 2023
89cd3ba
Move useEffect up near related code
ajlende Sep 19, 2023
3b47976
Fix comment
ajlende Sep 19, 2023
73d0a07
Add check for fully opaque overlay colors
ajlende Sep 19, 2023
ae9090b
Fix clearMedia from not resetting overlayColor when not manually spec…
ajlende Sep 19, 2023
f13dad1
Rename getAverageBackgroundColor to getMediaColor
ajlende Sep 19, 2023
15a3a35
Fix use newDimRatio on media select
ajlende Sep 19, 2023
e7466a4
Fix overlay color not resetting when clearing featured image
ajlende Sep 19, 2023
444f0b9
Fix missing averageBackgroundColor when toggling featured image
ajlende Sep 19, 2023
b18da34
Rename userOverlayColor to isUserOverlayColor
ajlende Sep 19, 2023
6228e04
Fix isDark calculation when clearing media
ajlende Sep 19, 2023
4ccdb7b
Fix undo states
ajlende Sep 20, 2023
ecae079
Update docs with new attribute
ajlende Sep 20, 2023
892a60e
Separate out compositeIsDark calls from setAttributes
ajlende Sep 20, 2023
f5ab036
Fix case where isDark wasn't correctly set when toggling off featured…
ajlende Sep 20, 2023
1703365
Move useFeaturedImage: false to index
ajlende Sep 20, 2023
575bc9f
Move focalPoint: undefined to index
ajlende Sep 20, 2023
067652d
Set useFeaturedImage to undefined instead of false for slightly clean…
ajlende Sep 20, 2023
a91418e
Shorten #FFFFFF to #FFF
ajlende Sep 20, 2023
8261b2e
Fix overlayColor in onUpdateDimRatio
ajlende Sep 20, 2023
52c7744
Fix use newDimRatio for isDark on updateDimRatio
ajlende Sep 20, 2023
9e6269e
Fix use newDimRatio for isDark on toggleUseFeaturedImage
ajlende Sep 20, 2023
8c30ee0
Rename DEFAULT_AVERAGE_COLOR to DEFAULT_BACKGROUND_COLOR to better de…
ajlende Sep 20, 2023
c68d792
Better JSDoc comment
ajlende Sep 20, 2023
fb055f6
Fix DEFAULT_BACKGROUND_COLOR import
ajlende Sep 20, 2023
d7e721d
Update tests to use playwright toHaveCSS
ajlende Sep 21, 2023
3aa6f84
Variable naming
ajlende Sep 21, 2023
56b3b50
Replace remaining instance of checking backgrounds with toHaveCSS
ajlende Sep 21, 2023
b031035
Update image upload test
ajlende Sep 21, 2023
b846d12
Update image transform test
ajlende Sep 21, 2023
bc603ff
Add block deprecation for auto overlay color
ajlende Sep 21, 2023
284a95e
Fix lint
ajlende Sep 21, 2023
013d180
Remove unnecessary expect for the default value
ajlende Sep 21, 2023
a3cb7f8
update mobile snapshots due to new attribute
draganescu Sep 22, 2023
80ff046
updated snapshots for transforms on mobile tests due to new attribute
draganescu Sep 22, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/reference-guides/core-blocks.md
Expand Up @@ -239,7 +239,7 @@ Add an image or video with a text overlay. ([Source](https://github.com/WordPres
- **Name:** core/cover
- **Category:** media
- **Supports:** align, anchor, color (heading, text, ~~background~~, ~~enableContrastChecker~~), layout (~~allowJustification~~), spacing (blockGap, margin, padding), typography (fontSize, lineHeight), ~~html~~
- **Attributes:** allowedBlocks, alt, backgroundType, contentPosition, customGradient, customOverlayColor, dimRatio, focalPoint, gradient, hasParallax, id, isDark, isRepeated, minHeight, minHeightUnit, overlayColor, tagName, templateLock, url, useFeaturedImage
- **Attributes:** allowedBlocks, alt, backgroundType, contentPosition, customGradient, customOverlayColor, dimRatio, focalPoint, gradient, hasParallax, id, isDark, isRepeated, isUserOverlayColor, minHeight, minHeightUnit, overlayColor, tagName, templateLock, url, useFeaturedImage

## Details

Expand Down
3 changes: 3 additions & 0 deletions packages/block-library/src/cover/block.json
Expand Up @@ -42,6 +42,9 @@
"customOverlayColor": {
"type": "string"
},
"isUserOverlayColor": {
"type": "boolean"
},
draganescu marked this conversation as resolved.
Show resolved Hide resolved
"backgroundType": {
"type": "string",
"default": "image"
Expand Down
248 changes: 236 additions & 12 deletions packages/block-library/src/cover/deprecated.js
Expand Up @@ -97,7 +97,7 @@ const blockAttributes = {
},
};

const v8ToV10BlockAttributes = {
const v8ToV11BlockAttributes = {
url: {
type: 'string',
},
Expand Down Expand Up @@ -164,7 +164,19 @@ const v8ToV10BlockAttributes = {
},
};

const v7toV10BlockSupports = {
const v12BlockAttributes = {
...v8ToV11BlockAttributes,
useFeaturedImage: {
type: 'boolean',
default: false,
},
tagName: {
type: 'string',
default: 'div',
},
};

const v7toV11BlockSupports = {
anchor: true,
align: true,
html: false,
Expand All @@ -182,10 +194,222 @@ const v7toV10BlockSupports = {
},
};

const v12BlockSupports = {
...v7toV11BlockSupports,
spacing: {
padding: true,
margin: [ 'top', 'bottom' ],
blockGap: true,
__experimentalDefaultControls: {
padding: true,
blockGap: true,
},
},
__experimentalBorder: {
color: true,
radius: true,
style: true,
width: true,
__experimentalDefaultControls: {
color: true,
radius: true,
style: true,
width: true,
},
},
color: {
__experimentalDuotone:
'> .wp-block-cover__image-background, > .wp-block-cover__video-background',
heading: true,
text: true,
background: false,
__experimentalSkipSerialization: [ 'gradients' ],
enableContrastChecker: false,
},
typography: {
fontSize: true,
lineHeight: true,
__experimentalFontFamily: true,
__experimentalFontWeight: true,
__experimentalFontStyle: true,
__experimentalTextTransform: true,
__experimentalTextDecoration: true,
__experimentalLetterSpacing: true,
__experimentalDefaultControls: {
fontSize: true,
},
},
layout: {
allowJustification: false,
},
};

// Deprecation for blocks to prevent auto overlay color from overriding previously set values.
const v12 = {
attributes: v12BlockAttributes,
supports: v12BlockSupports,
isEligible( attributes ) {
return (
attributes.customOverlayColor !== undefined ||
attributes.overlayColor !== undefined
);
},
migrate( attributes ) {
return {
...attributes,
isUserOverlayColor: true,
};
},
save( { attributes } ) {
const {
backgroundType,
gradient,
contentPosition,
customGradient,
customOverlayColor,
dimRatio,
focalPoint,
useFeaturedImage,
hasParallax,
isDark,
isRepeated,
overlayColor,
url,
alt,
id,
minHeight: minHeightProp,
minHeightUnit,
tagName: Tag,
} = attributes;
const overlayColorClass = getColorClassName(
'background-color',
overlayColor
);
const gradientClass = __experimentalGetGradientClass( gradient );
const minHeight =
minHeightProp && minHeightUnit
? `${ minHeightProp }${ minHeightUnit }`
: minHeightProp;

const isImageBackground = IMAGE_BACKGROUND_TYPE === backgroundType;
const isVideoBackground = VIDEO_BACKGROUND_TYPE === backgroundType;

const isImgElement = ! ( hasParallax || isRepeated );

const style = {
minHeight: minHeight || undefined,
};

const bgStyle = {
backgroundColor: ! overlayColorClass
? customOverlayColor
: undefined,
background: customGradient ? customGradient : undefined,
};

const objectPosition =
// prettier-ignore
focalPoint && isImgElement
? mediaPosition(focalPoint)
: undefined;

const backgroundImage = url ? `url(${ url })` : undefined;

const backgroundPosition = mediaPosition( focalPoint );

const classes = classnames(
{
'is-light': ! isDark,
'has-parallax': hasParallax,
'is-repeated': isRepeated,
'has-custom-content-position':
! isContentPositionCenter( contentPosition ),
},
getPositionClassName( contentPosition )
);

const imgClasses = classnames(
'wp-block-cover__image-background',
id ? `wp-image-${ id }` : null,
{
'has-parallax': hasParallax,
'is-repeated': isRepeated,
}
);

const gradientValue = gradient || customGradient;

return (
<Tag { ...useBlockProps.save( { className: classes, style } ) }>
<span
aria-hidden="true"
className={ classnames(
'wp-block-cover__background',
overlayColorClass,
dimRatioToClass( dimRatio ),
{
'has-background-dim': dimRatio !== undefined,
// For backwards compatibility. Former versions of the Cover Block applied
// `.wp-block-cover__gradient-background` in the presence of
// media, a gradient and a dim.
'wp-block-cover__gradient-background':
url && gradientValue && dimRatio !== 0,
'has-background-gradient': gradientValue,
[ gradientClass ]: gradientClass,
}
) }
style={ bgStyle }
/>

{ ! useFeaturedImage &&
isImageBackground &&
url &&
( isImgElement ? (
<img
className={ imgClasses }
alt={ alt }
src={ url }
style={ { objectPosition } }
data-object-fit="cover"
data-object-position={ objectPosition }
/>
) : (
<div
role="img"
className={ imgClasses }
style={ { backgroundPosition, backgroundImage } }
/>
) ) }
{ isVideoBackground && url && (
<video
className={ classnames(
'wp-block-cover__video-background',
'intrinsic-ignore'
) }
autoPlay
muted
loop
playsInline
src={ url }
style={ { objectPosition } }
data-object-fit="cover"
data-object-position={ objectPosition }
/>
) }
<div
{ ...useInnerBlocksProps.save( {
className: 'wp-block-cover__inner-container',
} ) }
/>
</Tag>
);
},
};

// Deprecation for blocks that does not have a HTML tag option.
const v11 = {
attributes: v8ToV10BlockAttributes,
supports: v7toV10BlockSupports,
attributes: v8ToV11BlockAttributes,
supports: v7toV11BlockSupports,
save( { attributes } ) {
const {
backgroundType,
Expand Down Expand Up @@ -334,8 +558,8 @@ const v11 = {

// Deprecation for blocks that renders fixed background as backgroud from the main block container.
const v10 = {
attributes: v8ToV10BlockAttributes,
supports: v7toV10BlockSupports,
attributes: v8ToV11BlockAttributes,
supports: v7toV11BlockSupports,
save( { attributes } ) {
const {
backgroundType,
Expand Down Expand Up @@ -471,8 +695,8 @@ const v10 = {

// Deprecation for blocks with `minHeightUnit` set but no `minHeight`.
const v9 = {
attributes: v8ToV10BlockAttributes,
supports: v7toV10BlockSupports,
attributes: v8ToV11BlockAttributes,
supports: v7toV11BlockSupports,
save( { attributes } ) {
const {
backgroundType,
Expand Down Expand Up @@ -603,8 +827,8 @@ const v9 = {

// v8: deprecated to remove duplicated gradient classes and swap `wp-block-cover__gradient-background` for `wp-block-cover__background`.
const v8 = {
attributes: v8ToV10BlockAttributes,
supports: v7toV10BlockSupports,
attributes: v8ToV11BlockAttributes,
supports: v7toV11BlockSupports,
save( { attributes } ) {
const {
backgroundType,
Expand Down Expand Up @@ -758,7 +982,7 @@ const v7 = {
default: '',
},
},
supports: v7toV10BlockSupports,
supports: v7toV11BlockSupports,
save( { attributes } ) {
const {
backgroundType,
Expand Down Expand Up @@ -1449,4 +1673,4 @@ const v1 = {
},
};

export default [ v11, v10, v9, v8, v7, v6, v5, v4, v3, v2, v1 ];
export default [ v12, v11, v10, v9, v8, v7, v6, v5, v4, v3, v2, v1 ];