Skip to content

Commit f44dfae

Browse files
ramonjdandrewserongjasmussen
authored
Background images: add defaults for background size (#62046)
Sets a default background position of 50% 0 for media library images. Sets a placeholder for the background image size input control of "Auto" Co-authored-by: ramonjd <ramonopoly@git.wordpress.org> Co-authored-by: andrewserong <andrewserong@git.wordpress.org> Co-authored-by: jasmussen <joen@git.wordpress.org>
1 parent 6c9b70f commit f44dfae

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

packages/block-editor/src/components/global-styles/background-panel.js

+29-5
Original file line numberDiff line numberDiff line change
@@ -245,12 +245,22 @@ function BackgroundImageToolsPanelItem( {
245245
return;
246246
}
247247

248+
const sizeValue = style?.background?.backgroundSize;
249+
const positionValue = style?.background?.backgroundPosition;
250+
248251
onChange(
249-
setImmutably( style, [ 'background', 'backgroundImage' ], {
250-
url: media.url,
251-
id: media.id,
252-
source: 'file',
253-
title: media.title || undefined,
252+
setImmutably( style, [ 'background' ], {
253+
...style?.background,
254+
backgroundImage: {
255+
url: media.url,
256+
id: media.id,
257+
source: 'file',
258+
title: media.title || undefined,
259+
},
260+
backgroundPosition:
261+
! positionValue && ( 'auto' === sizeValue || ! sizeValue )
262+
? '50% 0'
263+
: positionValue,
254264
} )
255265
);
256266
};
@@ -426,13 +436,16 @@ function BackgroundSizeToolsPanelItem( {
426436
const updateBackgroundSize = ( next ) => {
427437
// When switching to 'contain' toggle the repeat off.
428438
let nextRepeat = repeatValue;
439+
let nextPosition = positionValue;
429440

430441
if ( next === 'contain' ) {
431442
nextRepeat = 'no-repeat';
443+
nextPosition = undefined;
432444
}
433445

434446
if ( next === 'cover' ) {
435447
nextRepeat = undefined;
448+
nextPosition = undefined;
436449
}
437450

438451
if (
@@ -441,6 +454,15 @@ function BackgroundSizeToolsPanelItem( {
441454
next === 'auto'
442455
) {
443456
nextRepeat = undefined;
457+
/*
458+
* A background image uploaded and set in the editor (an image with a record id),
459+
* receives a default background position of '50% 0',
460+
* when the toggle switches to "Tile". This is to increase the chance that
461+
* the image's focus point is visible.
462+
*/
463+
if ( !! style?.background?.backgroundImage?.id ) {
464+
nextPosition = '50% 0';
465+
}
444466
}
445467

446468
/*
@@ -454,6 +476,7 @@ function BackgroundSizeToolsPanelItem( {
454476
onChange(
455477
setImmutably( style, [ 'background' ], {
456478
...style?.background,
479+
backgroundPosition: nextPosition,
457480
backgroundRepeat: nextRepeat,
458481
backgroundSize: next,
459482
} )
@@ -545,6 +568,7 @@ function BackgroundSizeToolsPanelItem( {
545568
size="__unstable-large"
546569
__unstableInputWidth="100px"
547570
min={ 0 }
571+
placeholder={ __( 'Auto' ) }
548572
/>
549573
) : null }
550574
{ currentValueForToggle !== 'cover' && (

0 commit comments

Comments
 (0)