Skip to content

Commit

Permalink
Check for string types in background image so it will apply defaults …
Browse files Browse the repository at this point in the history
…for blocks.

Update comments
  • Loading branch information
ramonjd committed Aug 1, 2024
1 parent 19f71bd commit 6f2a83e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -396,15 +396,27 @@ export function getStylesDeclarations(

/*
* Preprocess background image values.
* 1. Sets default values for blocks (not root).
* 2. .
*
* Note: A refactor is for the style engine to handle ref resolution (and possibly defaults)
* Note: As we absorb more and more styles into the engine, we could simplify this function.
* A refactor is for the style engine to handle ref resolution (and possibly defaults)
* via a public util used internally and externally. Theme.json tree and defaults could be passed
* as options.
*/
if ( !! blockStyles.background ) {
// 1. Set default values for block styles except the top-level site background
/*
* Resolve dynamic values before they are compiled by the style engine,
* which doesn't (yet) resolve dynamic values.
*/
if ( blockStyles.background?.backgroundImage?.ref ) {
const refPath =
blockStyles.background.backgroundImage.ref.split( '.' );
blockStyles.background.backgroundImage = getValueFromObjectPath(
tree,
refPath
);
}

// Set default values for block styles except the top-level site background
if ( ! isRoot ) {
blockStyles = {
...blockStyles,
Expand All @@ -414,25 +426,8 @@ export function getStylesDeclarations(
},
};
}

/*
* 2. Resolve dynamic values before they are compiled by the style engine,
* which doesn't (yet) resolve dynamic values.
*/
if ( blockStyles.background?.backgroundImage?.ref ) {
if ( typeof blockStyles.background.backgroundImage !== 'string' ) {
const refPath =
blockStyles.background.backgroundImage.ref.split( '.' );
blockStyles.background.backgroundImage = getValueFromObjectPath(
tree,
refPath
);
}
}
}

// The goal is to move everything to server side generated engine styles
// This is temporary as we absorb more and more styles into the engine.
const extraRules = getCSSRules( blockStyles );
extraRules.forEach( ( rule ) => {
// Don't output padding properties if padding variables are set or if we're not editing a full template.
Expand Down
2 changes: 1 addition & 1 deletion packages/block-editor/src/hooks/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export function setBackgroundStyleDefaults( backgroundStyle ) {
let backgroundStylesWithDefaults;

// Set block background defaults.
if ( !! backgroundImage?.url ) {
if ( !! backgroundImage?.url || typeof backgroundImage === 'string' ) {
if ( ! backgroundStyle?.backgroundSize ) {
backgroundStylesWithDefaults = {
backgroundSize: 'cover',
Expand Down

0 comments on commit 6f2a83e

Please sign in to comment.