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

Media & Text block should not use background image (Closes #52789) #58514

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions packages/block-library/src/media-text/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ function MediaTextEdit( { attributes, isSelected, setAttributes } ) {
const imperativeFocalPointPreview = ( value ) => {
const { style } = refMediaContainer.current.resizable;
const { x, y } = value;
style.backgroundPosition = `${ x * 100 }% ${ y * 100 }%`;
style.objectPosition = `${ x * 100 }% ${ y * 100 }%`;
};

const [ temporaryMediaWidth, setTemporaryMediaWidth ] = useState( null );
Expand Down Expand Up @@ -252,7 +252,7 @@ function MediaTextEdit( { attributes, isSelected, setAttributes } ) {
}
/>
) }
{ imageFill && mediaUrl && mediaType === 'image' && (
{ imageFill && mediaUrl && (
<FocalPointPicker
__nextHasNoMarginBottom
__next40pxDefaultSize
Expand Down
38 changes: 20 additions & 18 deletions packages/block-library/src/media-text/media-container.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,26 @@ import { forwardRef } from '@wordpress/element';
import { isBlobURL } from '@wordpress/blob';
import { store as noticesStore } from '@wordpress/notices';
import { media as icon } from '@wordpress/icons';
export const DEFAULT_FOCAL_POINT = { x: 0.5, y: 0.5 };

/**
* Constants
*/
const ALLOWED_MEDIA_TYPES = [ 'image', 'video' ];
const noop = () => {};

export function imageFillStyles( url, focalPoint ) {
return url
? {
backgroundImage: `url(${ url })`,
backgroundPosition: focalPoint
? `${ Math.round( focalPoint.x * 100 ) }% ${ Math.round(
focalPoint.y * 100
) }%`
: `50% 50%`,
}
: {};
export function imageFillStyles( focalPoint ) {
return {
objectPosition: focalPoint
? `${ Math.round( focalPoint.x * 100 ) }% ${ Math.round(
focalPoint.y * 100
) }%`
: `50% 50%`,
};
}

export function focalPosition( { x, y } = DEFAULT_FOCAL_POINT ) {
return `${ Math.round( x * 100 ) }% ${ Math.round( y * 100 ) }%`;
}
Comment on lines +42 to 44
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Having both imageFillStyles and this new function seems like clutter. Previously imageFillStyles was used in a two places and still could be. Or alternatively, focalPosition could replace it in both those places. I’d prefer using one or the other and not both.


const ResizableBoxContainer = forwardRef(
Expand Down Expand Up @@ -132,14 +134,15 @@ function MediaContainer( props, ref ) {
left: enableResize && mediaPosition === 'right',
};

const backgroundStyles =
mediaType === 'image' && imageFill
? imageFillStyles( mediaUrl, focalPoint )
: {};
const mediaStyles = imageFill ? imageFillStyles( focalPoint ) : {};

const mediaTypeRenderers = {
image: () => <img src={ mediaUrl } alt={ mediaAlt } />,
video: () => <video controls src={ mediaUrl } />,
image: () => (
<img src={ mediaUrl } alt={ mediaAlt } style={ mediaStyles } />
),
video: () => (
<video controls src={ mediaUrl } style={ mediaStyles } />
),
};

return (
Expand All @@ -150,7 +153,6 @@ function MediaContainer( props, ref ) {
'editor-media-container__resizer',
{ 'is-transient': isTemporaryMedia }
) }
style={ backgroundStyles }
size={ { width: mediaWidth + '%' } }
minWidth="10%"
maxWidth="100%"
Expand Down
27 changes: 14 additions & 13 deletions packages/block-library/src/media-text/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { useInnerBlocksProps, useBlockProps } from '@wordpress/block-editor';
/**
* Internal dependencies
*/
import { imageFillStyles } from './media-container';
import { focalPosition } from './media-container';
import { DEFAULT_MEDIA_SIZE_SLUG } from './constants';

const DEFAULT_MEDIA_WIDTH = 50;
Expand Down Expand Up @@ -42,10 +42,17 @@ export default function save( { attributes } ) {
[ `size-${ mediaSizeSlug }` ]: mediaId && mediaType === 'image',
} );

const objectPosition =
// prettier-ignore
focalPoint
? focalPosition(focalPoint)
: undefined;
Comment on lines +47 to +49
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It appears that previously even if focalPoint wasn’t set a style would be generated for the default 50% 50% and now this omits a style in such cases. I like this because object-position’s initial value is 50% 50% so there’s no need to set it. Since the save output is changing anyway I think this should be fine but just wanted to note it.


let image = (
<img
src={ mediaUrl }
alt={ mediaAlt }
style={ { objectPosition } }
className={ imageClasses || null }
/>
);
Expand All @@ -56,6 +63,7 @@ export default function save( { attributes } ) {
className={ linkClass }
href={ href }
target={ linkTarget }
style={ { objectPosition } }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There’s no need to add object-position to the a element.

rel={ newRel }
>
{ image }
Expand All @@ -65,17 +73,16 @@ export default function save( { attributes } ) {

const mediaTypeRenders = {
image: () => image,
video: () => <video controls src={ mediaUrl } />,
video: () => (
<video controls src={ mediaUrl } style={ { objectPosition } } />
),
};
const className = classnames( {
'has-media-on-the-right': 'right' === mediaPosition,
'is-stacked-on-mobile': isStackedOnMobile,
[ `is-vertically-aligned-${ verticalAlignment }` ]: verticalAlignment,
'is-image-fill': imageFill,
} );
const backgroundStyles = imageFill
? imageFillStyles( mediaUrl, focalPoint )
: {};

let gridTemplateColumns;
if ( mediaWidth !== DEFAULT_MEDIA_WIDTH ) {
Expand All @@ -96,21 +103,15 @@ export default function save( { attributes } ) {
className: 'wp-block-media-text__content',
} ) }
/>
<figure
className="wp-block-media-text__media"
style={ backgroundStyles }
>
<figure className="wp-block-media-text__media">
{ ( mediaTypeRenders[ mediaType ] || noop )() }
</figure>
</div>
);
}
return (
<div { ...useBlockProps.save( { className, style } ) }>
<figure
className="wp-block-media-text__media"
style={ backgroundStyles }
>
<figure className="wp-block-media-text__media">
{ ( mediaTypeRenders[ mediaType ] || noop )() }
</figure>
<div
Expand Down
26 changes: 17 additions & 9 deletions packages/block-library/src/media-text/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
grid-row: 1;
/*!rtl:end:ignore*/
margin: 0;
position: relative;
}

.wp-block-media-text .wp-block-media-text__content {
Expand Down Expand Up @@ -86,16 +87,23 @@
height: 100%;
}

.wp-block-media-text.is-image-fill .wp-block-media-text__media img {
// The image is visually hidden but accessible to assistive technologies.
position: absolute;
width: 1px;
height: 1px;
.wp-block-media-text.is-image-fill .wp-block-media-text__media img,
.wp-block-media-text.is-image-fill .wp-block-media-text__media video {
border: none;
bottom: 0;
box-shadow: none;
height: 100%;
left: 0;
margin: 0;
max-height: none;
max-width: none;
object-fit: cover;
outline: none;
Comment on lines +92 to +101
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wary of adding more styles here than strictly necessary. In particular border, box-shadow, margin, and outline. I tested without these and it was fine but I only tested with twentytwentyfour. If you can share some reasoning on why these should be added it would help me.

padding: 0;
margin: -1px;
overflow: hidden;
clip: rect(0, 0, 0, 0);
border: 0;
position: absolute;
right: 0;
top: 0;
Comment on lines +104 to +105
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I'd opt to use inset: 0; instead of all the separate sides (including bottom and left in the preceding lines).

width: 100%;
Comment on lines +90 to +106
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here width: 100% is redundant as it’s already applied by a preceding ruleset.

}
/*
* Here we here not able to use a mobile first CSS approach.
Expand Down
17 changes: 6 additions & 11 deletions packages/block-library/src/media-text/test/media-container.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,16 @@
import { imageFillStyles } from '../media-container';

describe( 'imageFillStyles()', () => {
it( 'should return image url', () => {
const { backgroundImage } = imageFillStyles( 'image.jpg' );
expect( backgroundImage ).toBe( 'url(image.jpg)' );
it( 'should return centered object position', () => {
const { objectPosition } = imageFillStyles( { x: 0.5, y: 0.5 } );
expect( objectPosition ).toBe( '50% 50%' );
} );

it( 'should return centered background position', () => {
const { backgroundPosition } = imageFillStyles( 'image.jpg' );
expect( backgroundPosition ).toBe( '50% 50%' );
} );

it( 'should return custom background position', () => {
const { backgroundPosition } = imageFillStyles( 'image.jpg', {
it( 'should return custom object position', () => {
const { objectPosition } = imageFillStyles( {
x: 0.56,
y: 0.57,
} );
expect( backgroundPosition ).toBe( '56% 57%' );
expect( objectPosition ).toBe( '56% 57%' );
} );
} );