Skip to content

Commit

Permalink
Background image: Add has-background classname when background image …
Browse files Browse the repository at this point in the history
…is applied (#57495)

* Add has-background classname when background image is applied

* Update tests

* Move background image classname output to be handled alongside background color classnames
  • Loading branch information
andrewserong authored Jan 5, 2024
1 parent bb2a39f commit 946e056
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 7 deletions.
1 change: 1 addition & 0 deletions lib/block-supports/background.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ function gutenberg_render_background_support( $block_content, $block ) {

$updated_style .= $styles['css'];
$tags->set_attribute( 'style', $updated_style );
$tags->add_class( 'has-background' );
}

return $tags->get_updated_html();
Expand Down
11 changes: 11 additions & 0 deletions packages/block-editor/src/hooks/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,17 @@ function resetBackgroundSize( style = {}, setAttributes ) {
} );
}

/**
* Generates a CSS class name if an background image is set.
*
* @param {Object} style A block's style attribute.
*
* @return {string} CSS class name.
*/
export function getBackgroundImageClasses( style ) {
return hasBackgroundImageValue( style ) ? 'has-background' : '';
}

function InspectorImagePreview( { label, filename, url: imgUrl } ) {
const imgLabel = label || getFilename( imgUrl );
return (
Expand Down
18 changes: 17 additions & 1 deletion packages/block-editor/src/hooks/color.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
transformStyles,
shouldSkipSerialization,
} from './utils';
import { getBackgroundImageClasses } from './background';
import { useSettings } from '../components/use-settings';
import InspectorControls from '../components/inspector-controls';
import {
Expand Down Expand Up @@ -383,12 +384,27 @@ function useBlockProps( {
)?.color;
}

return addSaveProps( { style: extraStyles }, name, {
const saveProps = addSaveProps( { style: extraStyles }, name, {
textColor,
backgroundColor,
gradient,
style,
} );

const hasBackgroundValue =
backgroundColor ||
style?.color?.background ||
gradient ||
style?.color?.gradient;

return {
...saveProps,
className: classnames(
saveProps.className,
// Add background image classes in the editor, if not already handled by background color values.
! hasBackgroundValue && getBackgroundImageClasses( style )
),
};
}

export default {
Expand Down
12 changes: 6 additions & 6 deletions phpunit/block-supports/background-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ public function data_background_block_support() {
'source' => 'file',
),
),
'expected_wrapper' => '<div style="background-image:url(&#039;https://example.com/image.jpg&#039;);background-size:cover;">Content</div>',
'expected_wrapper' => '<div class="has-background" style="background-image:url(&#039;https://example.com/image.jpg&#039;);background-size:cover;">Content</div>',
'wrapper' => '<div>Content</div>',
),
'background image style with contain, position, and repeat is applied' => array(
Expand All @@ -151,7 +151,7 @@ public function data_background_block_support() {
'backgroundRepeat' => 'no-repeat',
'backgroundSize' => 'contain',
),
'expected_wrapper' => '<div style="background-image:url(&#039;https://example.com/image.jpg&#039;);background-position:center;background-repeat:no-repeat;background-size:contain;">Content</div>',
'expected_wrapper' => '<div class="has-background" style="background-image:url(&#039;https://example.com/image.jpg&#039;);background-position:center;background-repeat:no-repeat;background-size:contain;">Content</div>',
'wrapper' => '<div>Content</div>',
),
'background image style is appended if a style attribute already exists' => array(
Expand All @@ -166,8 +166,8 @@ public function data_background_block_support() {
'source' => 'file',
),
),
'expected_wrapper' => '<div classname="wp-block-test" style="color: red;background-image:url(&#039;https://example.com/image.jpg&#039;);background-size:cover;">Content</div>',
'wrapper' => '<div classname="wp-block-test" style="color: red">Content</div>',
'expected_wrapper' => '<div class="wp-block-test has-background" style="color: red;background-image:url(&#039;https://example.com/image.jpg&#039;);background-size:cover;">Content</div>',
'wrapper' => '<div class="wp-block-test" style="color: red">Content</div>',
),
'background image style is appended if a style attribute containing multiple styles already exists' => array(
'theme_name' => 'block-theme-child-with-fluid-typography',
Expand All @@ -181,8 +181,8 @@ public function data_background_block_support() {
'source' => 'file',
),
),
'expected_wrapper' => '<div classname="wp-block-test" style="color: red;font-size: 15px;background-image:url(&#039;https://example.com/image.jpg&#039;);background-size:cover;">Content</div>',
'wrapper' => '<div classname="wp-block-test" style="color: red;font-size: 15px;">Content</div>',
'expected_wrapper' => '<div class="wp-block-test has-background" style="color: red;font-size: 15px;background-image:url(&#039;https://example.com/image.jpg&#039;);background-size:cover;">Content</div>',
'wrapper' => '<div class="wp-block-test" style="color: red;font-size: 15px;">Content</div>',
),
'background image style is not applied if the block does not support background image' => array(
'theme_name' => 'block-theme-child-with-fluid-typography',
Expand Down

0 comments on commit 946e056

Please sign in to comment.