Skip to content

Commit

Permalink
Adding background panel for blocks.
Browse files Browse the repository at this point in the history
  • Loading branch information
ramonjd committed Mar 22, 2024
1 parent 3872394 commit 7788359
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 4 deletions.
8 changes: 4 additions & 4 deletions lib/class-wp-theme-json-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
49 changes: 49 additions & 0 deletions packages/edit-site/src/components/global-styles/screen-block.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ const {
useHasFiltersPanel,
useHasImageSettingsPanel,
useGlobalStyle,
useHasBackgroundPanel,
BackgroundPanel: StylesBackgroundPanel,
BorderPanel: StylesBorderPanel,
ColorPanel: StylesColorPanel,
TypographyPanel: StylesTypographyPanel,
Expand Down Expand Up @@ -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 );
Expand Down Expand Up @@ -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 (
<>
<ScreenHeader
Expand Down Expand Up @@ -295,6 +335,15 @@ function ScreenBlock( { name, variation } ) {
inheritedValue={ settings }
/>
) }
{ hasBackgroundPanel && (
<StylesBackgroundPanel
inheritedValue={ inheritedStyle }
value={ style }
onChange={ onChangeBackground }
settings={ settings }
defaultControlValues={ defaultBackgroundControlValues }
/>
) }

{ canEditCSS && (
<PanelBody title={ __( 'Advanced' ) } initialOpen={ false }>
Expand Down

0 comments on commit 7788359

Please sign in to comment.