-
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 image: ensure consistency with defaults and fix reset/remove functionality #64328
Conversation
…ontrols. - checks global styles for uploaded images and applies defaults - changes the default position from "center" to "50% 50%" so it displays in the controls - ensures that reset/remove functionality closes the panel and removes image - do not save already-inherited styles to the block, and don't apply defaults where an inherited style already exists.
Size Change: +3.28 kB (+0.19%) Total Size: 1.77 MB
ℹ️ View Unchanged
|
…nel. Add defaults for position '50% 50%' for blocks so it can be displayed in the background panel controls Pass inherited values to setBackgroundStyleDefaults so as not apply/overwrite inherited styles. Tighten up logic in setBackgroundStyleDefaults so that background position isn't set when an inherited value is contain and the block style is something else.
lib/block-supports/background.php
Outdated
if ( isset( $background_styles['backgroundImage']['id'] ) ) { | ||
$inherited_styles = gutenberg_get_global_styles(); | ||
$inherited_block_background_styles = $inherited_styles['blocks'][ $block['blockName'] ]['background'] ?? null; | ||
$inherited_background_size = $inherited_block_background_styles['backgroundSize'] ?? null; | ||
if ( ! isset( $background_styles['backgroundSize'] ) && ! $inherited_background_size ) { | ||
$background_styles['backgroundSize'] = 'cover'; | ||
} |
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.
Apologies if I'm missing something, but just thinking about this one a little more. Why do we need to check the inherited styles in the server-rendered output of the block instance level styles?
In this case, the image will always be one that the user set (or that's bundled in a pattern), so in that case, won't we want to go with the defaults rather than styles inherited from global styles? I.e. if a user sets an image at the block instance level, we assume that they're setting things in the UI to their liking rather than inheriting from global styles at that point 🤔
Mostly I think my question boils down to: do we need to complexity of this look up? In the UI at least we kind of bundle setting an image with all the other properties now, so I wasn't sure how necessary it is to allow this inheritance.
Edit: on the other hand I do see the value in attempting to preserve what's set in global styles so that it overwrites our defaults logic! I suppose I'm torn on this one 😄
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.
Why do we need to check the inherited styles in the server-rendered output of the block instance level styles?
Good question. It's all very complex, you're right.
My assumption on this one is that, where the theme or global styles provide values for a block, the block should use them if no local styles exist to overwrite them.
So if a theme defines "contain" for a block but nothing else, and a user uploads a new image to that block, whether in global styles or at the block level, the default will be the same as if they chose "contain" themselves.
The other, and probably easier option, would be to just set "cover" every time a user adds an image and forget about inherited values?
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.
I very much appreciate the attention to detail here! I think the way you've done is the right way to go about it while handling a complex situation. But my gut feeling is that it's probably more complex than we need it to be now that we have all the controls bundled together in the popover where folks can make adjustments easily.
The other, and probably easier option, would be to just set "cover" every time a user adds an image and forget about inherited values?
That sounds like a good option to try to me!
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.
@andrewserong I've simplified this PR and focussed on bug/inconsistency fixing.
Thank you for helping me move it forward! 🙇🏻
… and block supports, always apply the defaults.
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
@@ -2391,7 +2391,7 @@ protected static function compute_style_properties( $styles, $settings = array() | |||
$styles['background']['backgroundSize'] = $styles['background']['backgroundSize'] ?? 'cover'; | |||
// If the background size is set to `contain` and no position is set, set the position to `center`. | |||
if ( 'contain' === $styles['background']['backgroundSize'] && empty( $styles['background']['backgroundPosition'] ) ) { | |||
$styles['background']['backgroundPosition'] = 'center'; | |||
$styles['background']['backgroundPosition'] = '50% 50%'; |
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.
Changing from "center" to "50% 50%" because it's the same value, but we can display this in the controls.
return ( | ||
<VStack spacing={ 4 } className="single-column"> | ||
<VStack spacing={ 3 } className="single-column"> |
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.
These changes are about reducing space, can be in a separate PR
onChange={ updateBackgroundPosition } | ||
/> | ||
<ToggleControl | ||
__nextHasNoMarginBottom | ||
label={ __( 'Fixed background' ) } | ||
checked={ attachmentValue === 'fixed' } | ||
onChange={ toggleScrollWithPage } | ||
help={ __( |
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.
Another change about reducing space, can be in a separate PR
const shouldShowBackgroundImageControls = | ||
hasImageValue && | ||
'none' !== imageValue && |
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.
Ensure the panel closes where an image is removed
lib/block-supports/background.php
Outdated
if ( isset( $background_styles['backgroundImage']['id'] ) ) { | ||
$inherited_styles = gutenberg_get_global_styles(); | ||
$inherited_block_background_styles = $inherited_styles['blocks'][ $block['blockName'] ]['background'] ?? null; | ||
$inherited_background_size = $inherited_block_background_styles['backgroundSize'] ?? null; | ||
if ( ! isset( $background_styles['backgroundSize'] ) && ! $inherited_background_size ) { | ||
$background_styles['backgroundSize'] = 'cover'; | ||
} |
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.
@andrewserong I've simplified this PR and focussed on bug/inconsistency fixing.
Thank you for helping me move it forward! 🙇🏻
* The incoming value could be a value + unit, e.g. '20px'. | ||
* In this case set the value to 'tile'. | ||
*/ | ||
currentValueForToggle = ! [ 'cover', 'contain', 'auto' ].includes( |
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 a new check to activate the "Tile" toggle for non-word values, e.g., value + unit.
This ensure consistency with toggling sizes, where clicking on tile and updating the width will not deactivate tile - the "tile" effect is still there as far as the user is concerned.
@@ -510,6 +523,7 @@ function BackgroundSizeControls( { | |||
* receives a default background position of '50% 0', | |||
* when the toggle switches to "Tile". This is to increase the chance that | |||
* the image's focus point is visible. | |||
* This is in-editor only to assist with the user experience. |
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.
More comments just to explain all this madness.
@@ -562,24 +576,27 @@ function BackgroundSizeControls( { | |||
) | |||
); | |||
|
|||
// Set a default background position for non-site-wide, uploaded images with a size of 'contain'. | |||
const backgroundPositionValue = |
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.
Display the defaults if any are set.
@@ -725,7 +747,7 @@ export default function BackgroundPanel( { | |||
) } | |||
> | |||
<ToolsPanelItem | |||
hasValue={ () => hasImageValue } | |||
hasValue={ () => !! value?.background } |
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.
Because we can change size, repeat etc values of inherited background images, we should check for all properties, not just the image to indicate whether the controls can be reset.
@@ -55,31 +56,28 @@ export function hasBackgroundSupport( blockName, feature = 'any' ) { | |||
} | |||
|
|||
export function setBackgroundStyleDefaults( backgroundStyle ) { | |||
if ( ! backgroundStyle ) { | |||
if ( ! backgroundStyle || ! backgroundStyle?.backgroundImage?.url ) { |
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.
Just tightening things up here.
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.
Just walked through this code change and demo with @ramonjd on a call, and this is looking terrific to me!
✅ Works nicely at the block level in post content
✅ Block level in global styles is working correctly
✅ As-is site wide background images
LGTM! 🚀
I just cherry-picked this PR to the release/19.0 branch to get it included in the next release: 1521946 |
…ve functionality (#64328) * Fixes miscellaneous bugs in the background image default values and controls. - checks global styles for uploaded images and applies defaults - changes the default position from "center" to "50% 50%" so it displays in the controls - ensures that reset/remove functionality closes the panel and removes image - do not save already-inherited styles to the block, and don't apply defaults where an inherited style already exists. * Reduce vertical height a smidge * Ensure defaults are accurately displayed in the background control panel. Add defaults for position '50% 50%' for blocks so it can be displayed in the background panel controls Pass inherited values to setBackgroundStyleDefaults so as not apply/overwrite inherited styles. Tighten up logic in setBackgroundStyleDefaults so that background position isn't set when an inherited value is contain and the block style is something else. * Simplify default logic for blocks with uploaded images * Update tests. * Adds tests for setBackgroundStyleDefaults * Simplify the inheritence checks. For uploaded images in global styles and block supports, always apply the defaults. * Simplify the inheritence checks. For uploaded images, always apply the defaults. * Changelog * Revert background.php change and comment in background-panel.js Co-authored-by: ramonjd <[email protected]> Co-authored-by: andrewserong <[email protected]>
…ve functionality (#64328) * Fixes miscellaneous bugs in the background image default values and controls. - checks global styles for uploaded images and applies defaults - changes the default position from "center" to "50% 50%" so it displays in the controls - ensures that reset/remove functionality closes the panel and removes image - do not save already-inherited styles to the block, and don't apply defaults where an inherited style already exists. * Reduce vertical height a smidge * Ensure defaults are accurately displayed in the background control panel. Add defaults for position '50% 50%' for blocks so it can be displayed in the background panel controls Pass inherited values to setBackgroundStyleDefaults so as not apply/overwrite inherited styles. Tighten up logic in setBackgroundStyleDefaults so that background position isn't set when an inherited value is contain and the block style is something else. * Simplify default logic for blocks with uploaded images * Update tests. * Adds tests for setBackgroundStyleDefaults * Simplify the inheritence checks. For uploaded images in global styles and block supports, always apply the defaults. * Simplify the inheritence checks. For uploaded images, always apply the defaults. * Changelog * Revert background.php change and comment in background-panel.js Co-authored-by: ramonjd <[email protected]> Co-authored-by: andrewserong <[email protected]>
Follow up to:
What and how?
Block background styles will be part of WordPress 6.7.
This PR fixes miscellaneous bugs in the background image default values and controls. It:
blocks should honour any inherited styles. That means, defaults should not overwrite styles defined in theme.json or global stylesWhy?
To address a lot of inconsistencies in the way defaults are applied to block background styles.
Testing Instructions
This might take some time, sorry!
Using a theme with background images set on blocks (see example theme.json), add some blocks to the editor (see example blocks).
Ensure that you can see inherited styles in the controls and that no background default values are applied.
Now upload/add your own images to these blocks.
Ensure that the frontend matches what you're seeing in the editor.
Site-wide background images should operate as before and be unaffected by any defaults aside from the existing position of
50% 0
position when switching to tiled.Example block HTML
```htmlGroup
example theme.json
Testing Instructions for Keyboard
Screenshots or screencast
Kapture.2024-08-08.at.21.06.07.mp4
TODO