Skip to content

Commit

Permalink
Mobile - Global Styles - Fix issue with custom color variables not be…
Browse files Browse the repository at this point in the history
…ing parsed (#56752)
  • Loading branch information
Gerardo Pacheco authored Dec 4, 2023
1 parent 3d389c4 commit 66e0871
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,28 @@ describe( 'parseStylesVariables', () => {
expect.objectContaining( PARSED_GLOBAL_STYLES )
);
} );

it( 'returns the parsed custom color values correctly', () => {
const defaultStyles = {
...DEFAULT_GLOBAL_STYLES,
color: {
text: 'var(--wp--custom--color--blue)',
background: 'var(--wp--custom--color--green)',
},
};
const customValues = parseStylesVariables(
JSON.stringify( RAW_FEATURES.custom ),
MAPPED_VALUES
);
const styles = parseStylesVariables(
JSON.stringify( defaultStyles ),
MAPPED_VALUES,
customValues
);
expect( styles ).toEqual(
expect.objectContaining( PARSED_GLOBAL_STYLES )
);
} );
} );

describe( 'getGlobalStyles', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,20 @@ export function parseStylesVariables( styles, mappedValues, customValues ) {
const customValuesData = customValues ?? JSON.parse( stylesBase );
stylesBase = stylesBase.replace( regex, ( _$1, $2 ) => {
const path = $2.split( '--' );

// Supports cases for variables like var(--wp--custom--color--background)
if ( path[ 0 ] === 'color' ) {
const colorKey = path[ path.length - 1 ];
if ( mappedValues?.color ) {
const matchedValue = mappedValues.color?.values?.find(
( { slug } ) => slug === colorKey
);
if ( matchedValue ) {
return `${ matchedValue?.color }`;
}
}
}

if (
path.reduce(
( prev, curr ) => prev && prev[ curr ],
Expand Down

0 comments on commit 66e0871

Please sign in to comment.