diff --git a/lib/class-wp-theme-json-gutenberg.php b/lib/class-wp-theme-json-gutenberg.php index 943338e2916b5..9a0298a4d50f0 100644 --- a/lib/class-wp-theme-json-gutenberg.php +++ b/lib/class-wp-theme-json-gutenberg.php @@ -472,10 +472,10 @@ class WP_Theme_JSON_Gutenberg { */ const VALID_STYLES = array( 'background' => array( - 'backgroundImage' => 'top', - 'backgroundPosition' => 'top', - 'backgroundRepeat' => 'top', - 'backgroundSize' => 'top', + 'backgroundImage' => null, + 'backgroundPosition' => null, + 'backgroundRepeat' => null, + 'backgroundSize' => null, ), 'border' => array( 'color' => null, diff --git a/packages/edit-site/src/components/global-styles/screen-block.js b/packages/edit-site/src/components/global-styles/screen-block.js index 9811f10b834da..f3f03d81f177d 100644 --- a/packages/edit-site/src/components/global-styles/screen-block.js +++ b/packages/edit-site/src/components/global-styles/screen-block.js @@ -70,6 +70,8 @@ const { useHasFiltersPanel, useHasImageSettingsPanel, useGlobalStyle, + useHasBackgroundPanel, + BackgroundPanel: StylesBackgroundPanel, BorderPanel: StylesBorderPanel, ColorPanel: StylesColorPanel, TypographyPanel: StylesTypographyPanel, @@ -121,6 +123,7 @@ function ScreenBlock( { name, variation } ) { } const blockVariations = useBlockVariations( name ); + const hasBackgroundPanel = useHasBackgroundPanel( settings ); const hasTypographyPanel = useHasTypographyPanel( settings ); const hasColorPanel = useHasColorPanel( settings ); const hasBorderPanel = useHasBorderPanel( settings ); @@ -232,6 +235,43 @@ function ScreenBlock( { name, variation } ) { setStyle( { ...newStyle, border: { ...updatedBorder, radius } } ); }; + const onChangeBackground = ( newStyle ) => { + console.log( 'newStyle', newStyle ); + const backgroundImage = newStyle?.background?.backgroundImage; + let backgroundStyles; + + // Set block background defaults. + if ( backgroundImage?.source === 'file' && !! backgroundImage?.url ) { + if ( ! newStyle?.background?.backgroundSize ) { + backgroundStyles = { + backgroundSize: 'cover', + }; + } + + if ( + 'contain' === newStyle?.background?.backgroundSize && + ! newStyle?.background?.backgroundPosition + ) { + backgroundStyles = { + backgroundPosition: 'center', + }; + } + } + + setStyle( { + ...newStyle, + background: { + ...newStyle?.background, + ...backgroundStyles, + } + } ); + }; + + // Initial control values where no block style is set. + const defaultBackgroundControlValues = { + backgroundSize: 'cover', + }; + return ( <> ) } + { hasBackgroundPanel && ( + + ) } { canEditCSS && (