diff --git a/packages/block-library/src/cover/deprecated.js b/packages/block-library/src/cover/deprecated.js
index 9500c31fda0162..ac7c2a68a13012 100644
--- a/packages/block-library/src/cover/deprecated.js
+++ b/packages/block-library/src/cover/deprecated.js
@@ -24,12 +24,15 @@ import { __ } from '@wordpress/i18n';
import {
IMAGE_BACKGROUND_TYPE,
VIDEO_BACKGROUND_TYPE,
- backgroundImageStyles,
getPositionClassName,
isContentPositionCenter,
dimRatioToClass,
} from './shared';
+function backgroundImageStyles( url ) {
+ return url ? { backgroundImage: `url(${ url })` } : {};
+}
+
/**
* Original function to determine the background opacity classname
*
@@ -81,91 +84,232 @@ const blockAttributes = {
},
};
-// Deprecation for blocks with `minHeightUnit` set but no `minHeight`.
-const v9 = {
- attributes: {
- url: {
- type: 'string',
- },
- id: {
- type: 'number',
- },
- alt: {
- type: 'string',
- source: 'attribute',
- selector: 'img',
- attribute: 'alt',
- default: '',
- },
- hasParallax: {
- type: 'boolean',
- default: false,
- },
- isRepeated: {
- type: 'boolean',
- default: false,
- },
- dimRatio: {
- type: 'number',
- default: 100,
- },
- overlayColor: {
- type: 'string',
- },
- customOverlayColor: {
- type: 'string',
- },
- backgroundType: {
- type: 'string',
- default: 'image',
- },
- focalPoint: {
- type: 'object',
- },
- minHeight: {
- type: 'number',
- },
- minHeightUnit: {
- type: 'string',
- },
- gradient: {
- type: 'string',
- },
- customGradient: {
- type: 'string',
- },
- contentPosition: {
- type: 'string',
- },
- isDark: {
- type: 'boolean',
- default: true,
- },
- allowedBlocks: {
- type: 'array',
- },
- templateLock: {
- type: [ 'string', 'boolean' ],
- enum: [ 'all', 'insert', false ],
- },
+const v8ToV10BlockAttributes = {
+ url: {
+ type: 'string',
},
- supports: {
- anchor: true,
- align: true,
- html: false,
- spacing: {
+ id: {
+ type: 'number',
+ },
+ alt: {
+ type: 'string',
+ source: 'attribute',
+ selector: 'img',
+ attribute: 'alt',
+ default: '',
+ },
+ hasParallax: {
+ type: 'boolean',
+ default: false,
+ },
+ isRepeated: {
+ type: 'boolean',
+ default: false,
+ },
+ dimRatio: {
+ type: 'number',
+ default: 100,
+ },
+ overlayColor: {
+ type: 'string',
+ },
+ customOverlayColor: {
+ type: 'string',
+ },
+ backgroundType: {
+ type: 'string',
+ default: 'image',
+ },
+ focalPoint: {
+ type: 'object',
+ },
+ minHeight: {
+ type: 'number',
+ },
+ minHeightUnit: {
+ type: 'string',
+ },
+ gradient: {
+ type: 'string',
+ },
+ customGradient: {
+ type: 'string',
+ },
+ contentPosition: {
+ type: 'string',
+ },
+ isDark: {
+ type: 'boolean',
+ default: true,
+ },
+ allowedBlocks: {
+ type: 'array',
+ },
+ templateLock: {
+ type: [ 'string', 'boolean' ],
+ enum: [ 'all', 'insert', false ],
+ },
+};
+
+const v7toV10BlockSupports = {
+ anchor: true,
+ align: true,
+ html: false,
+ spacing: {
+ padding: true,
+ __experimentalDefaultControls: {
padding: true,
- __experimentalDefaultControls: {
- padding: true,
- },
- },
- color: {
- __experimentalDuotone:
- '> .wp-block-cover__image-background, > .wp-block-cover__video-background',
- text: false,
- background: false,
},
},
+ color: {
+ __experimentalDuotone:
+ '> .wp-block-cover__image-background, > .wp-block-cover__video-background',
+ text: false,
+ background: false,
+ },
+};
+
+// Deprecation for blocks that renders fixed background as backgroud from the main block container.
+const v10 = {
+ attributes: v8ToV10BlockAttributes,
+ supports: v7toV10BlockSupports,
+ save( { attributes } ) {
+ const {
+ backgroundType,
+ gradient,
+ contentPosition,
+ customGradient,
+ customOverlayColor,
+ dimRatio,
+ focalPoint,
+ useFeaturedImage,
+ hasParallax,
+ isDark,
+ isRepeated,
+ overlayColor,
+ url,
+ alt,
+ id,
+ minHeight: minHeightProp,
+ minHeightUnit,
+ } = 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 = {
+ ...( isImageBackground && ! isImgElement && ! useFeaturedImage
+ ? backgroundImageStyles( url )
+ : {} ),
+ minHeight: minHeight || undefined,
+ };
+
+ const bgStyle = {
+ backgroundColor: ! overlayColorClass
+ ? customOverlayColor
+ : undefined,
+ background: customGradient ? customGradient : undefined,
+ };
+
+ const objectPosition =
+ // prettier-ignore
+ focalPoint && isImgElement
+ ? `${ Math.round( focalPoint.x * 100 ) }% ${ Math.round( focalPoint.y * 100 ) }%`
+ : undefined;
+
+ const classes = classnames(
+ {
+ 'is-light': ! isDark,
+ 'has-parallax': hasParallax,
+ 'is-repeated': isRepeated,
+ 'has-custom-content-position': ! isContentPositionCenter(
+ contentPosition
+ ),
+ },
+ getPositionClassName( contentPosition )
+ );
+
+ const gradientValue = gradient || customGradient;
+
+ return (
+
+
+
+ { ! useFeaturedImage &&
+ isImageBackground &&
+ isImgElement &&
+ url && (
+
![{]({)
+ ) }
+ { isVideoBackground && url && (
+
+ ) }
+
+
+ );
+ },
+};
+
+// Deprecation for blocks with `minHeightUnit` set but no `minHeight`.
+const v9 = {
+ attributes: v8ToV10BlockAttributes,
+ supports: v7toV10BlockSupports,
save( { attributes } ) {
const {
backgroundType,
@@ -296,89 +440,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: {
- url: {
- type: 'string',
- },
- id: {
- type: 'number',
- },
- alt: {
- type: 'string',
- source: 'attribute',
- selector: 'img',
- attribute: 'alt',
- default: '',
- },
- hasParallax: {
- type: 'boolean',
- default: false,
- },
- isRepeated: {
- type: 'boolean',
- default: false,
- },
- dimRatio: {
- type: 'number',
- default: 100,
- },
- overlayColor: {
- type: 'string',
- },
- customOverlayColor: {
- type: 'string',
- },
- backgroundType: {
- type: 'string',
- default: 'image',
- },
- focalPoint: {
- type: 'object',
- },
- minHeight: {
- type: 'number',
- },
- minHeightUnit: {
- type: 'string',
- },
- gradient: {
- type: 'string',
- },
- customGradient: {
- type: 'string',
- },
- contentPosition: {
- type: 'string',
- },
- isDark: {
- type: 'boolean',
- default: true,
- },
- allowedBlocks: {
- type: 'array',
- },
- templateLock: {
- type: [ 'string', 'boolean' ],
- enum: [ 'all', 'insert', false ],
- },
- },
- supports: {
- anchor: true,
- align: true,
- html: false,
- spacing: {
- padding: true,
- __experimentalDefaultControls: {
- padding: true,
- },
- },
- color: {
- __experimentalDuotone:
- '> .wp-block-cover__image-background, > .wp-block-cover__video-background',
- text: false,
- background: false,
- },
- },
+ attributes: v8ToV10BlockAttributes,
+ supports: v7toV10BlockSupports,
save( { attributes } ) {
const {
backgroundType,
@@ -532,23 +595,7 @@ const v7 = {
default: '',
},
},
- supports: {
- anchor: true,
- align: true,
- html: false,
- spacing: {
- padding: true,
- __experimentalDefaultControls: {
- padding: true,
- },
- },
- color: {
- __experimentalDuotone:
- '> .wp-block-cover__image-background, > .wp-block-cover__video-background',
- text: false,
- background: false,
- },
- },
+ supports: v7toV10BlockSupports,
save( { attributes } ) {
const {
backgroundType,
@@ -1231,4 +1278,4 @@ const v1 = {
},
};
-export default [ v9, v8, v7, v6, v5, v4, v3, v2, v1 ];
+export default [ v10, v9, v8, v7, v6, v5, v4, v3, v2, v1 ];
diff --git a/packages/block-library/src/cover/edit/index.js b/packages/block-library/src/cover/edit/index.js
index 53756a7669de2a..677c25035cf0ae 100644
--- a/packages/block-library/src/cover/edit/index.js
+++ b/packages/block-library/src/cover/edit/index.js
@@ -33,7 +33,6 @@ import {
attributesFromMedia,
IMAGE_BACKGROUND_TYPE,
VIDEO_BACKGROUND_TYPE,
- backgroundImageStyles,
dimRatioToClass,
isContentPositionCenter,
getPositionClassName,
@@ -158,12 +157,13 @@ function CoverEdit( {
const isImgElement = ! ( hasParallax || isRepeated );
const style = {
- ...( isImageBackground && ! isImgElement
- ? backgroundImageStyles( url )
- : undefined ),
minHeight: minHeightWithUnit || undefined,
};
+ const backgroundImage = url ? `url(${ url })` : undefined;
+
+ const backgroundPosition = mediaPosition( focalPoint );
+
const bgStyle = { backgroundColor: overlayColor.color };
const mediaStyle = {
objectPosition:
@@ -344,15 +344,27 @@ function CoverEdit( {
style={ { backgroundImage: gradientValue, ...bgStyle } }
/>
- { url && isImageBackground && isImgElement && (
-
- ) }
+ { url &&
+ isImageBackground &&
+ ( isImgElement ? (
+
+ ) : (
+
+ ) ) }
{ url && isVideoBackground && (
- ) }
+ ) : (
+
+ ) ) }
{ isVideoBackground && url && (