Skip to content

Commit

Permalink
Remove ref to "control" in defaultControlValues
Browse files Browse the repository at this point in the history
  • Loading branch information
ramonjd committed Mar 26, 2024
1 parent 26256e2 commit 653d32a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ function BackgroundSizeToolsPanelItem( {
onChange,
style,
inheritedValue,
defaultControlValues,
defaultValues,
} ) {
const sizeValue =
style?.background?.backgroundSize ||
Expand Down Expand Up @@ -363,7 +363,7 @@ function BackgroundSizeToolsPanelItem( {
sizeValue !== 'contain' ) ||
sizeValue === ''
? 'auto'
: sizeValue || defaultControlValues?.backgroundSize;
: sizeValue || defaultValues?.backgroundSize;

/*
* If the current value is `cover` and the repeat value is `undefined`, then
Expand Down Expand Up @@ -394,16 +394,11 @@ function BackgroundSizeToolsPanelItem( {
const updateBackgroundSize = ( next ) => {
// When switching to 'contain' toggle the repeat off.
let nextRepeat = repeatValue;
let nextPosition = positionValue;

if ( next === 'contain' ) {
nextRepeat = 'no-repeat';
}

if ( next !== 'contain' && nextPosition === 'center' ) {
nextPosition = undefined;
}

if ( next === 'cover' ) {
nextRepeat = undefined;
}
Expand All @@ -419,7 +414,6 @@ function BackgroundSizeToolsPanelItem( {
onChange(
setImmutably( style, [ 'background' ], {
...style?.background,
backgroundPosition: nextPosition,
backgroundRepeat: nextRepeat,
backgroundSize: next,
} )
Expand Down Expand Up @@ -557,7 +551,7 @@ export default function BackgroundPanel( {
settings,
panelId,
defaultControls = DEFAULT_CONTROLS,
defaultControlValues = {},
defaultValues = {},
} ) {
const resetAllFilter = useCallback( ( previousValue ) => {
return {
Expand Down Expand Up @@ -589,7 +583,7 @@ export default function BackgroundPanel( {
isShownByDefault={ defaultControls.backgroundSize }
style={ value }
inheritedValue={ inheritedValue }
defaultControlValues={ defaultControlValues }
defaultValues={ defaultValues }
/>
) }
</Wrapper>
Expand Down
62 changes: 36 additions & 26 deletions packages/block-editor/src/hooks/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ import {

export const BACKGROUND_SUPPORT_KEY = 'background';

// Initial control values where no block style is set.
const BACKGROUND_BLOCK_DEFAULT_VALUES = {
backgroundSize: 'cover',
};

/**
* Determine whether there is block support for background.
*
Expand All @@ -45,44 +50,54 @@ export function hasBackgroundSupport( blockName, feature = 'any' ) {
return !! support?.[ feature ];
}

function useBlockProps( { name, style } ) {
if (
! hasBackgroundSupport( name ) ||
! style?.background?.backgroundImage
) {
export function setBackgroundStyleDefaults( backgroundStyle ) {
if ( ! backgroundStyle ) {
return;
}

const backgroundImage = style?.background?.backgroundImage;
let props;
const backgroundImage = backgroundStyle?.backgroundImage;
let backgroundStylesWithDefaults;

// Set block background defaults.
if ( backgroundImage?.source === 'file' && !! backgroundImage?.url ) {
if ( ! style?.background?.backgroundSize ) {
props = {
style: {
backgroundSize: 'cover',
},
if ( ! backgroundStyle?.backgroundSize ) {
backgroundStylesWithDefaults = {
backgroundSize: 'cover',
};
}

if (
'contain' === style?.background?.backgroundSize &&
! style?.background?.backgroundPosition
'contain' === backgroundStyle?.backgroundSize &&
! backgroundStyle?.backgroundPosition
) {
props = {
style: {
backgroundPosition: 'center',
},
backgroundStylesWithDefaults = {
backgroundPosition: 'center',
};
}
}

if ( ! props ) {
return backgroundStylesWithDefaults;
}

function useBlockProps( { name, style } ) {
if (
! hasBackgroundSupport( name ) ||
! style?.background?.backgroundImage
) {
return;
}

const backgroundStyles = setBackgroundStyleDefaults( style?.background );

if ( ! backgroundStyles ) {
return;
}

return props;
return {
style: {
...backgroundStyles,
},
};
}

/**
Expand Down Expand Up @@ -153,17 +168,12 @@ export function BackgroundImagePanel( {
},
};

// Initial control values where no block style is set.
const defaultControlValues = {
backgroundSize: 'cover',
};

return (
<StylesBackgroundPanel
as={ BackgroundInspectorControl }
panelId={ clientId }
defaultControls={ defaultControls }
defaultControlValues={ defaultControlValues }
defaultValues={ BACKGROUND_BLOCK_DEFAULT_VALUES }
settings={ updatedSettings }
onChange={ onChange }
value={ style }
Expand Down

0 comments on commit 653d32a

Please sign in to comment.