diff --git a/packages/style-engine/README.md b/packages/style-engine/README.md index 14e8edbd0dec2..a3e302fab4bb9 100644 --- a/packages/style-engine/README.md +++ b/packages/style-engine/README.md @@ -260,7 +260,9 @@ _Changelog_ ### getCSSValueFromRawStyle -Returns a WordPress CSS custom var value from incoming style preset value. The preset value follows the pattern `var:description|context|slug`. +Returns a WordPress CSS custom var value from incoming style preset value, if one is detected. + +The preset value is a string and follows the pattern `var:description|context|slug`. Example: @@ -268,11 +270,11 @@ Example: _Parameters_ -- _styleValue_ `string | any`: A raw style value. +- _styleValue_ `StyleValue`: A string representing a raw CSS value. Non-strings won't be processed. _Returns_ -- `string | unknown`: A CSS var value. +- `StyleValue`: A CSS custom var value if the incoming style value is a preset value. diff --git a/packages/style-engine/src/styles/utils.ts b/packages/style-engine/src/styles/utils.ts index 0c9b40c384b10..74a18d2c2a6e4 100644 --- a/packages/style-engine/src/styles/utils.ts +++ b/packages/style-engine/src/styles/utils.ts @@ -127,21 +127,23 @@ export function generateBoxRules( } /** - * Returns a WordPress CSS custom var value from incoming style preset value. - * The preset value follows the pattern `var:description|context|slug`. + * Returns a WordPress CSS custom var value from incoming style preset value, + * if one is detected. + * + * The preset value is a string and follows the pattern `var:description|context|slug`. * * Example: * * `getCSSValueFromRawStyle( 'var:preset|color|heavenlyBlue' )` // returns 'var(--wp--preset--color--heavenly-blue)' * - * @param styleValue A raw style value. + * @param styleValue A string representing a raw CSS value. Non-strings won't be processed. * - * @return A CSS var value. + * @return A CSS custom var value if the incoming style value is a preset value. */ -export function getCSSValueFromRawStyle( - styleValue: string | any -): string | unknown { +export function getCSSValueFromRawStyle< StyleValue = string >( + styleValue: StyleValue +): StyleValue { if ( typeof styleValue === 'string' && styleValue.startsWith( VARIABLE_REFERENCE_PREFIX ) @@ -160,7 +162,7 @@ export function getCSSValueFromRawStyle( } ) ) .join( VARIABLE_PATH_SEPARATOR_TOKEN_STYLE ); - return `var(--wp--${ variable })`; + return `var(--wp--${ variable })` as StyleValue; } return styleValue; }