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

Add has-custom-padding class to cover block with custom padding. #30074

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
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
159 changes: 159 additions & 0 deletions packages/block-library/src/cover/deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,165 @@ const blockAttributes = {
};

const deprecated = [
{
attributes: {
...blockAttributes,
title: {
type: 'string',
source: 'html',
selector: 'p',
},
contentAlign: {
type: 'string',
default: 'center',
},
isRepeated: {
type: 'boolean',
default: false,
},
minHeight: {
type: 'number',
},
minHeightUnit: {
type: 'string',
},
gradient: {
type: 'string',
},
customGradient: {
type: 'string',
},
contentPosition: {
type: 'string',
},
},
supports: {
anchor: true,
align: true,
html: false,
spacing: {
padding: true,
},
},
save( { attributes } ) {
const {
backgroundType,
gradient,
contentPosition,
customGradient,
customOverlayColor,
dimRatio,
focalPoint,
hasParallax,
isRepeated,
overlayColor,
url,
id,
minHeight: minHeightProp,
minHeightUnit,
} = attributes;
const overlayColorClass = getColorClassName(
'background-color',
overlayColor
);
const gradientClass = __experimentalGetGradientClass( gradient );
const minHeight = minHeightUnit
? `${ minHeightProp }${ minHeightUnit }`
: minHeightProp;

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

const isImgElement = ! ( hasParallax || isRepeated );

const style = {
...( isImageBackground && ! isImgElement
? backgroundImageStyles( url )
: {} ),
backgroundColor: ! overlayColorClass
? customOverlayColor
: undefined,
background:
customGradient && ! url ? customGradient : undefined,
minHeight: minHeight || undefined,
};

const objectPosition =
// prettier-ignore
focalPoint && isImgElement
? `${ Math.round( focalPoint.x * 100 ) }% ${ Math.round( focalPoint.y * 100 ) }%`
: undefined;

const classes = classnames(
dimRatioToClass( dimRatio ),
overlayColorClass,
{
'has-background-dim': dimRatio !== 0,
'has-parallax': hasParallax,
'is-repeated': isRepeated,
'has-background-gradient': gradient || customGradient,
[ gradientClass ]: ! url && gradientClass,
'has-custom-content-position': ! isContentPositionCenter(
contentPosition
),
},
getPositionClassName( contentPosition )
);

return (
<div { ...useBlockProps.save( { className: classes, style } ) }>
{ url &&
( gradient || customGradient ) &&
dimRatio !== 0 && (
<span
aria-hidden="true"
className={ classnames(
'wp-block-cover__gradient-background',
gradientClass
) }
style={
customGradient
? { background: customGradient }
: undefined
}
/>
) }
{ isImageBackground && isImgElement && url && (
<img
className={ classnames(
'wp-block-cover__image-background',
id ? `wp-image-${ id }` : null
) }
alt=""
src={ url }
style={ { objectPosition } }
data-object-fit="cover"
data-object-position={ objectPosition }
/>
) }
{ 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 className="wp-block-cover__inner-container">
<InnerBlocks.Content />
</div>
</div>
);
},
},
{
attributes: {
...blockAttributes,
Expand Down
4 changes: 4 additions & 0 deletions packages/block-library/src/cover/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export default function save( { attributes } ) {
id,
minHeight: minHeightProp,
minHeightUnit,
style: styleAttribute,
} = attributes;
const overlayColorClass = getColorClassName(
'background-color',
Expand All @@ -56,6 +57,8 @@ export default function save( { attributes } ) {

const isImgElement = ! ( hasParallax || isRepeated );

const hasCustomPadding = !! styleAttribute?.spacing?.padding;

const style = {
...( isImageBackground && ! isImgElement
? backgroundImageStyles( url )
Expand All @@ -76,6 +79,7 @@ export default function save( { attributes } ) {
overlayColorClass,
{
'has-background-dim': dimRatio !== 0,
'has-custom-padding': hasCustomPadding,
'has-parallax': hasParallax,
'is-repeated': isRepeated,
'has-background-gradient': gradient || customGradient,
Expand Down