Skip to content

Commit

Permalink
Cover block: fix duotone when background is set to fixed (#40554)
Browse files Browse the repository at this point in the history
  • Loading branch information
matiasbenedetto authored May 24, 2022
1 parent 88cb8e4 commit 81dc943
Show file tree
Hide file tree
Showing 13 changed files with 470 additions and 238 deletions.
413 changes: 230 additions & 183 deletions packages/block-library/src/cover/deprecated.js

Large diffs are not rendered by default.

38 changes: 25 additions & 13 deletions packages/block-library/src/cover/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import {
attributesFromMedia,
IMAGE_BACKGROUND_TYPE,
VIDEO_BACKGROUND_TYPE,
backgroundImageStyles,
dimRatioToClass,
isContentPositionCenter,
getPositionClassName,
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -344,15 +344,27 @@ function CoverEdit( {
style={ { backgroundImage: gradientValue, ...bgStyle } }
/>

{ url && isImageBackground && isImgElement && (
<img
ref={ mediaElement }
className="wp-block-cover__image-background"
alt={ alt }
src={ url }
style={ mediaStyle }
/>
) }
{ url &&
isImageBackground &&
( isImgElement ? (
<img
ref={ mediaElement }
className="wp-block-cover__image-background"
alt={ alt }
src={ url }
style={ mediaStyle }
/>
) : (
<div
ref={ mediaElement }
role="img"
className={ classnames(
classes,
'wp-block-cover__image-background'
) }
style={ { backgroundImage, backgroundPosition } }
/>
) ) }
{ url && isVideoBackground && (
<video
ref={ mediaElement }
Expand Down
39 changes: 26 additions & 13 deletions packages/block-library/src/cover/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ import {
import {
IMAGE_BACKGROUND_TYPE,
VIDEO_BACKGROUND_TYPE,
backgroundImageStyles,
dimRatioToClass,
isContentPositionCenter,
getPositionClassName,
mediaPosition,
} from './shared';

export default function save( { attributes } ) {
Expand Down Expand Up @@ -61,9 +61,6 @@ export default function save( { attributes } ) {
const isImgElement = ! ( hasParallax || isRepeated );

const style = {
...( isImageBackground && ! isImgElement && ! useFeaturedImage
? backgroundImageStyles( url )
: {} ),
minHeight: minHeight || undefined,
};

Expand All @@ -75,8 +72,12 @@ export default function save( { attributes } ) {
const objectPosition =
// prettier-ignore
focalPoint && isImgElement
? `${ Math.round( focalPoint.x * 100 ) }% ${ Math.round( focalPoint.y * 100 ) }%`
: undefined;
? mediaPosition(focalPoint)
: undefined;

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

const backgroundPosition = mediaPosition( focalPoint );

const classes = classnames(
{
Expand All @@ -90,6 +91,15 @@ export default function save( { attributes } ) {
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 (
Expand All @@ -116,20 +126,23 @@ export default function save( { attributes } ) {

{ ! useFeaturedImage &&
isImageBackground &&
isImgElement &&
url && (
url &&
( isImgElement ? (
<img
className={ classnames(
'wp-block-cover__image-background',
id ? `wp-image-${ id }` : null
) }
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(
Expand Down
12 changes: 5 additions & 7 deletions packages/block-library/src/cover/shared.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ export const VIDEO_BACKGROUND_TYPE = 'video';
export const COVER_MIN_HEIGHT = 50;
export const COVER_MAX_HEIGHT = 1000;
export const COVER_DEFAULT_HEIGHT = 300;
export function backgroundImageStyles( url ) {
return url ? { backgroundImage: `url(${ url })` } : {};
}
export const DEFAULT_FOCAL_POINT = { x: 0.5, y: 0.5 };
export const ALLOWED_MEDIA_TYPES = [ 'image', 'video' ];

export function mediaPosition( { x, y } = DEFAULT_FOCAL_POINT ) {
return `${ Math.round( x * 100 ) }% ${ Math.round( y * 100 ) }%`;
}

export function dimRatioToClass( ratio ) {
return ratio === 50 || ! ratio === undefined
? null
Expand Down Expand Up @@ -106,7 +108,3 @@ export function getPositionClassName( contentPosition ) {

return POSITION_CLASSNAMES[ contentPosition ];
}

export function mediaPosition( { x, y } ) {
return `${ Math.round( x * 100 ) }% ${ Math.round( y * 100 ) }%`;
}
49 changes: 27 additions & 22 deletions packages/block-library/src/cover/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,6 @@
// This block has customizable padding, border-box makes that more predictable.
box-sizing: border-box;

&.has-parallax {
background-attachment: fixed;

// Mobile Safari does not support fixed background attachment properly.
// See also https://stackoverflow.com/questions/24154666/background-size-cover-not-working-on-ios
// Chrome on Android does not appear to support the attachment at all: https://issuetracker.google.com/issues/36908439
@supports (-webkit-touch-callout: inherit) {
background-attachment: scroll;
}

// Remove the appearance of scrolling based on OS-level animation preferences.
@media (prefers-reduced-motion: reduce) {
background-attachment: scroll;
}
}

&.is-repeated {
background-repeat: repeat;
background-size: auto;
}

/**
* Set a default background color for has-background-dim _unless_ it includes another
* background-color class (e.g. has-green-background-color). The presence of another
Expand Down Expand Up @@ -196,7 +175,7 @@
}

// Extra specificity for in edit mode where other styles would override it otherwise.
img.wp-block-cover__image-background,
.wp-block-cover__image-background,
video.wp-block-cover__video-background {
position: absolute;
top: 0;
Expand All @@ -216,6 +195,32 @@
}
}

.wp-block-cover-image,
.wp-block-cover,
.wp-block-cover__image-background,
video.wp-block-cover__video-background {
&.has-parallax {
background-attachment: fixed;

// Mobile Safari does not support fixed background attachment properly.
// See also https://stackoverflow.com/questions/24154666/background-size-cover-not-working-on-ios
// Chrome on Android does not appear to support the attachment at all: https://issuetracker.google.com/issues/36908439
@supports (-webkit-overflow-scrolling: touch) {
background-attachment: scroll;
}

// Remove the appearance of scrolling based on OS-level animation preferences.
@media (prefers-reduced-motion: reduce) {
background-attachment: scroll;
}
}

&.is-repeated {
background-repeat: repeat;
background-size: auto;
}
}

.wp-block-cover__video-background {
z-index: z-index(".wp-block-cover__video-background");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<!-- wp:cover {"url":"https://w.wiki/59Sd","id":609,"dimRatio":50} -->
<div class="wp-block-cover"><span aria-hidden="true" class="wp-block-cover__background has-background-dim"></span><img class="wp-block-cover__image-background wp-image-609" alt="" src="https://w.wiki/59Sd" data-object-fit="cover"/><div class="wp-block-cover__inner-container"><!-- wp:paragraph {"align":"center","placeholder":"Write title…","fontSize":"large"} -->
<p class="has-text-align-center has-large-font-size"></p>
<!-- /wp:paragraph --></div></div>
<!-- /wp:cover -->
31 changes: 31 additions & 0 deletions test/integration/fixtures/blocks/core__cover__deprecated-10.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
[
{
"name": "core/cover",
"isValid": true,
"attributes": {
"url": "https://w.wiki/59Sd",
"useFeaturedImage": false,
"id": 609,
"alt": "",
"hasParallax": false,
"isRepeated": false,
"dimRatio": 50,
"backgroundType": "image",
"isDark": true
},
"innerBlocks": [
{
"name": "core/paragraph",
"isValid": true,
"attributes": {
"align": "center",
"content": "",
"dropCap": false,
"placeholder": "Write title…",
"fontSize": "large"
},
"innerBlocks": []
}
]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
[
{
"blockName": "core/cover",
"attrs": {
"url": "https://w.wiki/59Sd",
"id": 609,
"dimRatio": 50
},
"innerBlocks": [
{
"blockName": "core/paragraph",
"attrs": {
"align": "center",
"placeholder": "Write title…",
"fontSize": "large"
},
"innerBlocks": [],
"innerHTML": "\n\t<p class=\"has-text-align-center has-large-font-size\"></p>\n\t",
"innerContent": [
"\n\t<p class=\"has-text-align-center has-large-font-size\"></p>\n\t"
]
}
],
"innerHTML": "\n<div class=\"wp-block-cover\"><span aria-hidden=\"true\" class=\"wp-block-cover__background has-background-dim\"></span><img class=\"wp-block-cover__image-background wp-image-609\" alt=\"\" src=\"https://w.wiki/59Sd\" data-object-fit=\"cover\"/><div class=\"wp-block-cover__inner-container\"></div></div>\n\t",
"innerContent": [
"\n<div class=\"wp-block-cover\"><span aria-hidden=\"true\" class=\"wp-block-cover__background has-background-dim\"></span><img class=\"wp-block-cover__image-background wp-image-609\" alt=\"\" src=\"https://w.wiki/59Sd\" data-object-fit=\"cover\"/><div class=\"wp-block-cover__inner-container\">",
null,
"</div></div>\n\t"
]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<!-- wp:cover {"url":"https://w.wiki/59Sd","id":609,"dimRatio":50} -->
<div class="wp-block-cover"><span aria-hidden="true" class="wp-block-cover__background has-background-dim"></span><img class="wp-block-cover__image-background wp-image-609" alt="" src="https://w.wiki/59Sd" data-object-fit="cover"/><div class="wp-block-cover__inner-container"><!-- wp:paragraph {"align":"center","placeholder":"Write title…","fontSize":"large"} -->
<p class="has-text-align-center has-large-font-size"></p>
<!-- /wp:paragraph --></div></div>
<!-- /wp:cover -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<!-- wp:cover {"url":"https://w.wiki/59So","id":612,"hasParallax":true,"isRepeated":true,"dimRatio":10,"isDark":false,"style":{"color":{"duotone":["#8c00b7","#fcff41"]}}} -->
<div class="wp-block-cover is-light has-parallax is-repeated"><span aria-hidden="true" class="wp-block-cover__background has-background-dim-10 has-background-dim"></span><div role="img" class="wp-block-cover__image-background wp-image-612 has-parallax is-repeated" style="background-position:50% 50%;background-image:url(https://w.wiki/59So)"></div><div class="wp-block-cover__inner-container"><!-- wp:paragraph {"align":"center","placeholder":"Write title…","fontSize":"large"} -->
<p class="has-text-align-center has-large-font-size"></p>
<!-- /wp:paragraph --></div></div>
<!-- /wp:cover -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
[
{
"name": "core/cover",
"isValid": true,
"attributes": {
"url": "https://w.wiki/59So",
"useFeaturedImage": false,
"id": 612,
"alt": "",
"hasParallax": true,
"isRepeated": true,
"dimRatio": 10,
"backgroundType": "image",
"isDark": false,
"style": {
"color": {
"duotone": [ "#8c00b7", "#fcff41" ]
}
}
},
"innerBlocks": [
{
"name": "core/paragraph",
"isValid": true,
"attributes": {
"align": "center",
"content": "",
"dropCap": false,
"placeholder": "Write title…",
"fontSize": "large"
},
"innerBlocks": []
}
]
}
]
Loading

0 comments on commit 81dc943

Please sign in to comment.