Skip to content

Commit

Permalink
Fix dynamic references on the site editor (#42976)
Browse files Browse the repository at this point in the history
* resolve references on the site editor

* Apply suggestions from code review

Co-authored-by: Dave Smith <getdavemail@gmail.com>

* refactor to remove the extra function

* fix refs on the global styles sidebar

* encapsulate into getValueFromRef

* move logic inside getValueFromVariable

Co-authored-by: Dave Smith <getdavemail@gmail.com>
  • Loading branch information
MaggieCabrera and getdave authored Aug 11, 2022
1 parent ff5c800 commit 7670811
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 13 deletions.
6 changes: 3 additions & 3 deletions packages/edit-site/src/components/global-styles/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,21 +128,21 @@ export function useStyle( path, blockName, source = 'all' ) {
switch ( source ) {
case 'all':
result = getValueFromVariable(
mergedConfig.settings,
mergedConfig,
blockName,
get( userConfig, finalPath ) ?? get( baseConfig, finalPath )
);
break;
case 'user':
result = getValueFromVariable(
mergedConfig.settings,
mergedConfig,
blockName,
get( userConfig, finalPath )
);
break;
case 'base':
result = getValueFromVariable(
baseConfig.settings,
baseConfig,
blockName,
get( baseConfig, finalPath )
);
Expand Down
6 changes: 3 additions & 3 deletions packages/edit-site/src/components/global-styles/test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ describe( 'editor utils', () => {
describe( 'when provided an invalid variable', () => {
it( 'returns the originally provided value', () => {
const actual = getValueFromVariable(
styles.settings,
styles,
'root',
undefined
);
Expand All @@ -122,7 +122,7 @@ describe( 'editor utils', () => {
describe( 'when provided a preset variable', () => {
it( 'retrieves the correct preset value', () => {
const actual = getValueFromVariable(
styles.settings,
styles,
'root',
'var:preset|color|primary'
);
Expand All @@ -134,7 +134,7 @@ describe( 'editor utils', () => {
describe( 'when provided a custom variable', () => {
it( 'retrieves the correct custom value', () => {
const actual = getValueFromVariable(
styles.settings,
styles,
'root',
'var(--wp--custom--color--secondary)'
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,15 @@ function flattenTree( input = {}, prefix, token ) {
*
* @param {boolean} useRootPaddingAlign Whether to use CSS custom properties in root selector.
*
* @param {Object} tree A theme.json tree containing layout definitions.
*
* @return {Array} An array of style declarations.
*/
export function getStylesDeclarations(
blockStyles = {},
selector = '',
useRootPaddingAlign
useRootPaddingAlign,
tree = {}
) {
const isRoot = ROOT_BLOCK_SELECTOR === selector;
const output = reduce(
Expand Down Expand Up @@ -270,7 +273,12 @@ export function getStylesDeclarations(
const cssProperty = rule.key.startsWith( '--' )
? rule.key
: kebabCase( rule.key );
output.push( `${ cssProperty }: ${ compileStyleValue( rule.value ) }` );
let ruleValue = rule.value;
if ( typeof ruleValue !== 'string' && ruleValue?.ref ) {
const refPath = ruleValue.ref.split( '.' );
ruleValue = get( tree, refPath );
}
output.push( `${ cssProperty }: ${ compileStyleValue( ruleValue ) }` );
} );

return output;
Expand Down Expand Up @@ -654,7 +662,8 @@ export const toStyles = (
const declarations = getStylesDeclarations(
styles,
selector,
useRootPaddingAlign
useRootPaddingAlign,
tree
);
if ( declarations?.length ) {
ruleset =
Expand Down
13 changes: 9 additions & 4 deletions packages/edit-site/src/components/global-styles/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ function getValueFromPresetVariable(
}

const presetObject = findInPresetsBy(
features,
features.settings,
blockName,
metadata.path,
'slug',
Expand All @@ -213,8 +213,8 @@ function getValueFromPresetVariable(

function getValueFromCustomVariable( features, blockName, variable, path ) {
const result =
get( features, [ 'blocks', blockName, 'custom', ...path ] ) ??
get( features, [ 'custom', ...path ] );
get( features.settings, [ 'blocks', blockName, 'custom', ...path ] ) ??
get( features.settings, [ 'custom', ...path ] );
if ( ! result ) {
return variable;
}
Expand All @@ -224,7 +224,12 @@ function getValueFromCustomVariable( features, blockName, variable, path ) {

export function getValueFromVariable( features, blockName, variable ) {
if ( ! variable || typeof variable !== 'string' ) {
return variable;
if ( variable?.ref && typeof variable?.ref === 'string' ) {
const refPath = variable.ref.split( '.' );
variable = get( features, refPath );
} else {
return variable;
}
}
const USER_VALUE_PREFIX = 'var:';
const THEME_VALUE_PREFIX = 'var(--wp--';
Expand Down

0 comments on commit 7670811

Please sign in to comment.