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

Background image: size controls should show when an image is set #61388

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,9 @@ function BackgroundSizeToolsPanelItem( {
( currentValueForToggle === 'cover' && repeatValue === undefined )
);

const hasValue = hasBackgroundSizeValue( style );
const hasValue =
hasBackgroundSizeValue( inheritedValue ) ||
hasBackgroundSizeValue( style );
Copy link
Member Author

Choose a reason for hiding this comment

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

Note to self:

There's an potential design edge case, where theme.json specifies a background-image string value for background.backgroundImage, e.g., url('http://test.org/image.png'), the controls won't display a preview because the value isn't being parsed.

I think this is correct as we can't possibly parse all possible values, e.g., linear gradients, image urls and multiples of both, but maybe we could pass a dummy image to the position controls at least, e.g., "No preview available" or something, or even disable the position drag controls.

Also, the copy could be changed from "Add background image" to "Update background image" when we detect a string value for background.backgroundImage, e.g.,

	const previewLabel =
		title ||
		( ! style?.background?.backgroundImage?.url &&
		typeof inheritedValue?.background?.backgroundImage === 'string'
			? __( 'Update background image' )
			: title );

andrewserong marked this conversation as resolved.
Show resolved Hide resolved

const resetAllFilter = useCallback( ( previousValue ) => {
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,21 @@ const {
BackgroundPanel: StylesBackgroundPanel,
} = unlock( blockEditorPrivateApis );

/**
* Checks if there is a current value in the background image block support
* attributes.
*
* @param {Object} style Style attribute.
* @return {boolean} Whether the block has a background image value set.
*/
export function hasBackgroundImageValue( style ) {
return (
!! style?.background?.backgroundImage?.id ||
!! style?.background?.backgroundImage?.url ||
typeof style?.background?.backgroundImage === 'string'
);
}

export default function BackgroundPanel() {
const [ style ] = useGlobalStyle( '', undefined, 'user', {
shouldDecodeEncode: false,
Expand All @@ -32,8 +47,8 @@ export default function BackgroundPanel() {
const defaultControls = {
backgroundImage: true,
backgroundSize:
!! style?.background?.backgroundImage &&
!! inheritedStyle?.background?.backgroundImage,
hasBackgroundImageValue( style ) ||
hasBackgroundImageValue( inheritedStyle ),
};

return (
Expand Down
Loading