-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Background images: ensure appropriate default values #64192
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
https://github.com/WordPress/wordpress-develop/pull/7137 | ||
|
||
* https://github.com/WordPress/gutenberg/pull/64192 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -318,8 +318,12 @@ function BackgroundImageControls( { | |
return; | ||
} | ||
|
||
const sizeValue = style?.background?.backgroundSize; | ||
const positionValue = style?.background?.backgroundPosition; | ||
const sizeValue = | ||
style?.background?.backgroundSize || | ||
inheritedValue?.background?.backgroundSize; | ||
const positionValue = | ||
style?.background?.backgroundPosition || | ||
inheritedValue?.background?.backgroundPosition; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This ensures inherited values (from global styles) are taken into account. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Whoops, I think I introduced a bug here. We don't want the inherited styles to creep into the block supports styles - see the Note to self for tomorrow: To test, insert a group block that has global styles. Replace the image with an uploaded one. The styles from global styles should apply, but not be saved to the inline CSS. The easiest thing to do would be to reset the styles once an image is uploaded, but that would ignore any theme styles. 🤔 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. WIP #64328 |
||
|
||
onChange( | ||
setImmutably( style, [ 'background' ], { | ||
|
@@ -334,6 +338,7 @@ function BackgroundImageControls( { | |
! positionValue && ( 'auto' === sizeValue || ! sizeValue ) | ||
? '50% 0' | ||
: positionValue, | ||
backgroundSize: sizeValue, | ||
} ) | ||
); | ||
}; | ||
|
@@ -448,6 +453,9 @@ function BackgroundSizeControls( { | |
const imageValue = | ||
style?.background?.backgroundImage?.url || | ||
inheritedValue?.background?.backgroundImage?.url; | ||
const isUploadedImage = | ||
style?.background?.backgroundImage?.id || | ||
inheritedValue?.background?.backgroundImage?.id; | ||
const positionValue = | ||
style?.background?.backgroundPosition || | ||
inheritedValue?.background?.backgroundPosition; | ||
|
@@ -456,19 +464,15 @@ function BackgroundSizeControls( { | |
inheritedValue?.background?.backgroundAttachment; | ||
|
||
/* | ||
* An `undefined` value is replaced with any supplied | ||
* default control value for the toggle group control. | ||
* An empty string is treated as `auto` - this allows a user | ||
* to select "Size" and then enter a custom value, with an | ||
* empty value being treated as `auto`. | ||
* Set default values for uploaded images. | ||
* The default values are passed by the consumer. | ||
* Block-level controls may have different defaults to root-level controls. | ||
* A falsy value is treated by default as `auto` (Tile). | ||
*/ | ||
const currentValueForToggle = | ||
( sizeValue !== undefined && | ||
sizeValue !== 'cover' && | ||
sizeValue !== 'contain' ) || | ||
sizeValue === '' | ||
? 'auto' | ||
: sizeValue || defaultValues?.backgroundSize; | ||
! sizeValue && isUploadedImage | ||
? defaultValues?.backgroundSize | ||
: sizeValue || 'auto'; | ||
|
||
/* | ||
* If the current value is `cover` and the repeat value is `undefined`, then | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the same as the assignments performed in background.php
Thinking this could be abstracted to a function either in background.php, or the style engine?
What do you reckon @andrewserong ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, I think the style engine shouldn't provide defaults willy-nilly, but maybe it could "register" defaults or accepts them in the options argument.
Maybe a separate function is better here, e.g.,
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be honest, I think I'd go with the duplication for now or until we have another block support that needs default values in this way, as what we're doing with background image seems fairly unique right now. Are there other use cases for
gutenberg_apply_style_defaults
we know of in the near-term?If not, I'd lean a little toward going with the duplication, especially since it's only ~5 lines or so, and revisit if and when we have other defaults to apply. What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not that I know.
Thanks for the feedback. I agree with your reasoning. I'll leave it for now then. 🙇🏻