Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix dynamic references on the site editor #42976

Merged
merged 6 commits into from
Aug 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 );
MaggieCabrera marked this conversation as resolved.
Show resolved Hide resolved
}
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 @@ -192,7 +192,7 @@ function getValueFromPresetVariable(
}

const presetObject = findInPresetsBy(
features,
features.settings,
blockName,
metadata.path,
'slug',
Expand All @@ -210,8 +210,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 @@ -221,7 +221,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