Skip to content

Commit

Permalink
Merge with control panel.
Browse files Browse the repository at this point in the history
  • Loading branch information
ramonjd committed Jul 5, 2024
1 parent 846c2e6 commit 81399fe
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 26 deletions.
7 changes: 4 additions & 3 deletions lib/block-supports/background.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,10 @@ function gutenberg_render_background_support( $block_content, $block ) {
$background_styles['backgroundImage'] = isset( $block_attributes['style']['background']['backgroundImage'] ) ? $block_attributes['style']['background']['backgroundImage'] : array();

if ( ! empty( $background_styles['backgroundImage'] ) ) {
$background_styles['backgroundSize'] = isset( $block_attributes['style']['background']['backgroundSize'] ) ? $block_attributes['style']['background']['backgroundSize'] : 'cover';
$background_styles['backgroundPosition'] = isset( $block_attributes['style']['background']['backgroundPosition'] ) ? $block_attributes['style']['background']['backgroundPosition'] : null;
$background_styles['backgroundRepeat'] = isset( $block_attributes['style']['background']['backgroundRepeat'] ) ? $block_attributes['style']['background']['backgroundRepeat'] : null;
$background_styles['backgroundSize'] = isset( $block_attributes['style']['background']['backgroundSize'] ) ? $block_attributes['style']['background']['backgroundSize'] : 'cover';
$background_styles['backgroundPosition'] = isset( $block_attributes['style']['background']['backgroundPosition'] ) ? $block_attributes['style']['background']['backgroundPosition'] : null;
$background_styles['backgroundRepeat'] = isset( $block_attributes['style']['background']['backgroundRepeat'] ) ? $block_attributes['style']['background']['backgroundRepeat'] : null;
$background_styles['backgroundAttachment'] = isset( $block_attributes['style']['background']['backgroundAttachment'] ) ? $block_attributes['style']['background']['backgroundAttachment'] : null;

// If the background size is set to `contain` and no position is set, set the position to `center`.
if ( 'contain' === $background_styles['backgroundSize'] && ! $background_styles['backgroundPosition'] ) {
Expand Down
10 changes: 6 additions & 4 deletions lib/class-wp-theme-json-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ class WP_Theme_JSON_Gutenberg {
'background-position' => array( 'background', 'backgroundPosition' ),
'background-repeat' => array( 'background', 'backgroundRepeat' ),
'background-size' => array( 'background', 'backgroundSize' ),
'background-attachment' => array( 'background', 'backgroundAttachment' ),
'border-radius' => array( 'border', 'radius' ),
'border-top-left-radius' => array( 'border', 'radius', 'topLeft' ),
'border-top-right-radius' => array( 'border', 'radius', 'topRight' ),
Expand Down Expand Up @@ -503,10 +504,11 @@ class WP_Theme_JSON_Gutenberg {
*/
const VALID_STYLES = array(
'background' => array(
'backgroundImage' => 'top',
'backgroundPosition' => 'top',
'backgroundRepeat' => 'top',
'backgroundSize' => 'top',
'backgroundImage' => 'top',
'backgroundAttachment' => 'top',
'backgroundPosition' => 'top',
'backgroundRepeat' => 'top',
'backgroundSize' => 'top',
),
'border' => array(
'color' => null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,9 @@ function BackgroundSizeControls( {
const positionValue =
style?.background?.backgroundPosition ||
inheritedValue?.background?.backgroundPosition;
const attachmentValue = style?.background?.backgroundAttachment || {
...inheritedValue?.background?.backgroundAttachment,
};

/*
* An `undefined` value is replaced with any supplied
Expand Down Expand Up @@ -542,8 +545,17 @@ function BackgroundSizeControls( {
)
);

const toggleScrollWithPage = () =>
onChange(
setImmutably(
style,
[ 'background', 'backgroundAttachment' ],
attachmentValue === 'fixed' ? 'scroll' : 'fixed'
)
);

return (
<VStack spacing={ 3 } className="single-column">
<VStack spacing={ 4 } className="single-column">
<FocalPointPicker
__next40pxDefaultSize
__nextHasNoMarginBottom
Expand All @@ -552,6 +564,14 @@ function BackgroundSizeControls( {
value={ backgroundPositionToCoords( positionValue ) }
onChange={ updateBackgroundPosition }
/>
<ToggleControl
label={ __( 'Scroll with page' ) }
checked={ attachmentValue !== 'fixed' }
onChange={ toggleScrollWithPage }
help={ __(
'Whether your image should scroll with the page or stay fixed in place.'
) }
/>
<ToggleGroupControl
size="__unstable-large"
label={ __( 'Size' ) }
Expand Down
14 changes: 10 additions & 4 deletions packages/style-engine/class-wp-style-engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,31 +42,37 @@ final class WP_Style_Engine {
*/
const BLOCK_STYLE_DEFINITIONS_METADATA = array(
'background' => array(
'backgroundImage' => array(
'backgroundImage' => array(
'property_keys' => array(
'default' => 'background-image',
),
'value_func' => array( self::class, 'get_url_or_value_css_declaration' ),
'path' => array( 'background', 'backgroundImage' ),
),
'backgroundPosition' => array(
'backgroundPosition' => array(
'property_keys' => array(
'default' => 'background-position',
),
'path' => array( 'background', 'backgroundPosition' ),
),
'backgroundRepeat' => array(
'backgroundRepeat' => array(
'property_keys' => array(
'default' => 'background-repeat',
),
'path' => array( 'background', 'backgroundRepeat' ),
),
'backgroundSize' => array(
'backgroundSize' => array(
'property_keys' => array(
'default' => 'background-size',
),
'path' => array( 'background', 'backgroundSize' ),
),
'backgroundAttachment' => array(
'property_keys' => array(
'default' => 'background-attachment',
),
'path' => array( 'background', 'backgroundAttachment' ),
),
),
'color' => array(
'text' => array(
Expand Down
13 changes: 13 additions & 0 deletions packages/style-engine/src/styles/background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,22 @@ const backgroundSize = {
},
};

const backgroundAttachment = {
name: 'backgroundAttachment',
generate: ( style: Style, options: StyleOptions ) => {
return generateRule(
style,
options,
[ 'background', 'backgroundAttachment' ],
'backgroundAttachment'
);
},
};

export default [
backgroundImage,
backgroundPosition,
backgroundRepeat,
backgroundSize,
backgroundAttachment,
];
6 changes: 6 additions & 0 deletions packages/style-engine/src/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ describe( 'getCSSRules', () => {
backgroundPosition: '50% 50%',
backgroundRepeat: 'no-repeat',
backgroundSize: '300px',
backgroundAttachment: 'fixed',
},
color: {
text: '#dddddd',
Expand Down Expand Up @@ -399,6 +400,11 @@ describe( 'getCSSRules', () => {
key: 'backgroundSize',
value: '300px',
},
{
selector: '.some-selector',
key: 'backgroundAttachment',
value: 'fixed',
},
] );
} );

Expand Down
9 changes: 5 additions & 4 deletions phpunit/block-supports/background-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public function data_background_block_support() {
'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(
'background image style with contain, position, attachment, and repeat is applied' => array(
'theme_name' => 'block-theme-child-with-fluid-typography',
'block_name' => 'test/background-rules-are-output',
'background_settings' => array(
Expand All @@ -160,10 +160,11 @@ public function data_background_block_support() {
'url' => 'https://example.com/image.jpg',
'source' => 'file',
),
'backgroundRepeat' => 'no-repeat',
'backgroundSize' => 'contain',
'backgroundRepeat' => 'no-repeat',
'backgroundSize' => 'contain',
'backgroundAttachment' => 'fixed',
),
'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>',
'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;background-attachment:fixed;">Content</div>',
'wrapper' => '<div>Content</div>',
),
'background image style is appended if a style attribute already exists' => array(
Expand Down
22 changes: 12 additions & 10 deletions phpunit/class-wp-theme-json-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -4765,12 +4765,13 @@ public function test_get_top_level_background_image_styles() {
'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA,
'styles' => array(
'background' => array(
'backgroundImage' => array(
'backgroundImage' => array(
'url' => 'http://example.org/image.png',
),
'backgroundSize' => 'contain',
'backgroundRepeat' => 'no-repeat',
'backgroundPosition' => 'center center',
'backgroundSize' => 'contain',
'backgroundRepeat' => 'no-repeat',
'backgroundPosition' => 'center center',
'backgroundAttachment' => 'fixed',
),
),
)
Expand All @@ -4781,24 +4782,25 @@ public function test_get_top_level_background_image_styles() {
'selector' => 'body',
);

$expected_styles = "html{min-height: calc(100% - var(--wp-admin--admin-bar--height, 0px));}:root :where(body){background-image: url('http://example.org/image.png');background-position: center center;background-repeat: no-repeat;background-size: contain;}";
$expected_styles = "html{min-height: calc(100% - var(--wp-admin--admin-bar--height, 0px));}:root :where(body){background-image: url('http://example.org/image.png');background-position: center center;background-repeat: no-repeat;background-size: contain;background-attachment: fixed;}";
$this->assertSame( $expected_styles, $theme_json->get_styles_for_block( $body_node ), 'Styles returned from "::get_styles_for_block()" with top-level background styles type does not match expectations' );

$theme_json = new WP_Theme_JSON_Gutenberg(
array(
'version' => WP_Theme_JSON_Gutenberg::LATEST_SCHEMA,
'styles' => array(
'background' => array(
'backgroundImage' => "url('http://example.org/image.png')",
'backgroundSize' => 'contain',
'backgroundRepeat' => 'no-repeat',
'backgroundPosition' => 'center center',
'backgroundImage' => "url('http://example.org/image.png')",
'backgroundSize' => 'contain',
'backgroundRepeat' => 'no-repeat',
'backgroundPosition' => 'center center',
'backgroundAttachment' => 'fixed',
),
),
)
);

$expected_styles = "html{min-height: calc(100% - var(--wp-admin--admin-bar--height, 0px));}:root :where(body){background-image: url('http://example.org/image.png');background-position: center center;background-repeat: no-repeat;background-size: contain;}";
$expected_styles = "html{min-height: calc(100% - var(--wp-admin--admin-bar--height, 0px));}:root :where(body){background-image: url('http://example.org/image.png');background-position: center center;background-repeat: no-repeat;background-size: contain;background-attachment: fixed;}";
$this->assertSame( $expected_styles, $theme_json->get_styles_for_block( $body_node ), 'Styles returned from "::get_styles_for_block()" with top-level background image as string type does not match expectations' );
}

Expand Down

0 comments on commit 81399fe

Please sign in to comment.