Skip to content

Commit

Permalink
Remove compileStyleValue
Browse files Browse the repository at this point in the history
Use style engine getCSSVarFromStyleValue
Update style engine tests to cover bugfix in #43116
Moves style engine utils tests into correct directory
  • Loading branch information
ramonjd committed Aug 14, 2024
1 parent 46f9fe0 commit cfeb080
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from '@wordpress/blocks';
import { useSelect } from '@wordpress/data';
import { useContext, useMemo } from '@wordpress/element';
import { getCSSRules } from '@wordpress/style-engine';
import { getCSSRules, getCSSVarFromStyleValue } from '@wordpress/style-engine';
import { privateApis as componentsPrivateApis } from '@wordpress/components';

/**
Expand All @@ -24,7 +24,6 @@ import {
scopeFeatureSelectors,
appendToSelector,
getBlockStyleVariationSelector,
compileStyleValue,
getResolvedValue,
} from './utils';
import { getBlockCSSSelector } from './get-block-css-selector';
Expand Down Expand Up @@ -357,7 +356,7 @@ export function getStylesDeclarations(
? name
: kebabCase( name );
declarations.push(
`${ cssProperty }: ${ compileStyleValue(
`${ cssProperty }: ${ getCSSVarFromStyleValue(
getValueFromObjectPath( styleValue, [ prop ] )
) }`
);
Expand All @@ -369,7 +368,7 @@ export function getStylesDeclarations(
? key
: kebabCase( key );
declarations.push(
`${ cssProperty }: ${ compileStyleValue(
`${ cssProperty }: ${ getCSSVarFromStyleValue(
getValueFromObjectPath( blockStyles, pathToValue )
) }`
);
Expand Down
31 changes: 2 additions & 29 deletions packages/block-editor/src/components/global-styles/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import fastDeepEqual from 'fast-deep-equal/es6';
* WordPress dependencies
*/
import { useViewportMatch } from '@wordpress/compose';
import { getCSSVarFromStyleValue } from '@wordpress/style-engine';

/**
* Internal dependencies
Expand Down Expand Up @@ -526,34 +527,6 @@ export function getBlockStyleVariationSelector( variation, blockSelector ) {
return result.join( ',' );
}

/**
* Converts style preset values `var:` to CSS custom var values.
* TODO: Export and use the style engine util: getCSSVarFromStyleValue().
*
* Example:
*
* compileStyleValue( 'var:preset|color|primary' ) // returns 'var(--wp--color-primary)'
*
* @param {string} uncompiledValue A block style value.
* @return {string} The compiled, or original value.
*/
export function compileStyleValue( uncompiledValue ) {
const VARIABLE_REFERENCE_PREFIX = 'var:';
if (
'string' === typeof uncompiledValue &&
uncompiledValue?.startsWith?.( VARIABLE_REFERENCE_PREFIX )
) {
const VARIABLE_PATH_SEPARATOR_TOKEN_ATTRIBUTE = '|';
const VARIABLE_PATH_SEPARATOR_TOKEN_STYLE = '--';
const variable = uncompiledValue
.slice( VARIABLE_REFERENCE_PREFIX.length )
.split( VARIABLE_PATH_SEPARATOR_TOKEN_ATTRIBUTE )
.join( VARIABLE_PATH_SEPARATOR_TOKEN_STYLE );
return `var(--wp--${ variable })`;
}
return uncompiledValue;
}

/**
* Looks up a theme file URI based on a relative path.
*
Expand Down Expand Up @@ -591,7 +564,7 @@ export function getResolvedRefValue( ruleValue, tree ) {

if ( typeof ruleValue !== 'string' && ruleValue?.ref ) {
const refPath = ruleValue.ref.split( '.' );
const resolvedRuleValue = compileStyleValue(
const resolvedRuleValue = getCSSVarFromStyleValue(
getValueFromObjectPath( tree, refPath )
);

Expand Down
16 changes: 16 additions & 0 deletions packages/style-engine/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,22 @@ _Changelog_

`6.1.0` Introduced in WordPress core.

### getCSSVarFromStyleValue

Returns a WordPress CSS custom var value from incoming style preset value. The preset value follows the pattern `var:description|context|slug`.

Example:

`getCSSVarFromStyleValue( 'var:preset|color|heavenlyBlue' )` // returns 'var(--wp--preset--color--heavenly-blue)'

_Parameters_

- _styleValue_ `string`: A raw style value.

_Returns_

- `string`: string A CSS var value.

<!-- END TOKEN(Autogenerated API docs) -->

## Glossary
Expand Down
3 changes: 3 additions & 0 deletions packages/style-engine/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,6 @@ export function getCSSRules(

return rules;
}

// Export style utils.
export { getCSSVarFromStyleValue } from './styles/utils';
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
/**
* Internal dependencies
*/
import {
camelCaseJoin,
getCSSVarFromStyleValue,
upperFirst,
} from '../styles/utils';
import { camelCaseJoin, getCSSVarFromStyleValue, upperFirst } from '../utils';

describe( 'utils', () => {
describe( 'upperFirst()', () => {
Expand Down Expand Up @@ -58,5 +54,14 @@ describe( 'utils', () => {
)
).toEqual( 'var(--wp--preset--background--dark-secrets-100)' );
} );
it( 'should handle null gracefully', () => {
expect( getCSSVarFromStyleValue( null ) ).toEqual( null );
} );
it( 'should handle boolean gracefully', () => {
expect( getCSSVarFromStyleValue( false ) ).toEqual( false );
} );
it( 'should handle integers gracefully', () => {
expect( getCSSVarFromStyleValue( 1000 ) ).toEqual( 1000 );
} );
} );
} );
7 changes: 6 additions & 1 deletion packages/style-engine/src/styles/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,12 @@ export function generateBoxRules(
}

/**
* Returns a CSS var value from incoming style value following the pattern `var:description|context|slug`.
* Returns a WordPress CSS custom var value from incoming style preset value.
* The preset value follows the pattern `var:description|context|slug`.
*
* Example:
*
* `getCSSVarFromStyleValue( 'var:preset|color|heavenlyBlue' )` // returns 'var(--wp--preset--color--heavenly-blue)'
*
* @param styleValue A raw style value.
*
Expand Down

0 comments on commit cfeb080

Please sign in to comment.