diff --git a/packages/style-engine/phpunit/style-engine-test.php b/packages/style-engine/phpunit/style-engine-test.php index 98c833359474a6..ad39b3016ce4ae 100644 --- a/packages/style-engine/phpunit/style-engine-test.php +++ b/packages/style-engine/phpunit/style-engine-test.php @@ -363,13 +363,13 @@ public function data_wp_style_engine_get_styles() { 'spacing' => array( 'margin' => array( 'left' => 'var:preset|spacing|10', - 'right' => 'var:preset|spacing|20', + 'right' => 'var:preset|spacing|3XL', 'top' => '1rem', 'bottom' => '1rem', ), 'padding' => array( 'left' => 'var:preset|spacing|30', - 'right' => 'var:preset|spacing|40', + 'right' => 'var:preset|spacing|3XL', 'top' => '14px', 'bottom' => '14px', ), @@ -377,14 +377,14 @@ public function data_wp_style_engine_get_styles() { ), 'options' => array(), 'expected_output' => array( - 'css' => 'padding-left:var(--wp--preset--spacing--30);padding-right:var(--wp--preset--spacing--40);padding-top:14px;padding-bottom:14px;margin-left:var(--wp--preset--spacing--10);margin-right:var(--wp--preset--spacing--20);margin-top:1rem;margin-bottom:1rem;', + 'css' => 'padding-left:var(--wp--preset--spacing--30);padding-right:var(--wp--preset--spacing--3-xl);padding-top:14px;padding-bottom:14px;margin-left:var(--wp--preset--spacing--10);margin-right:var(--wp--preset--spacing--3-xl);margin-top:1rem;margin-bottom:1rem;', 'declarations' => array( 'padding-left' => 'var(--wp--preset--spacing--30)', - 'padding-right' => 'var(--wp--preset--spacing--40)', + 'padding-right' => 'var(--wp--preset--spacing--3-xl)', 'padding-top' => '14px', 'padding-bottom' => '14px', 'margin-left' => 'var(--wp--preset--spacing--10)', - 'margin-right' => 'var(--wp--preset--spacing--20)', + 'margin-right' => 'var(--wp--preset--spacing--3-xl)', 'margin-top' => '1rem', 'margin-bottom' => '1rem', ), diff --git a/packages/style-engine/src/styles/utils.ts b/packages/style-engine/src/styles/utils.ts index 64a761ae5d359f..58f5fcc1cccfd9 100644 --- a/packages/style-engine/src/styles/utils.ts +++ b/packages/style-engine/src/styles/utils.ts @@ -1,7 +1,7 @@ /** * External dependencies */ -import { get } from 'lodash'; +import { get, kebabCase } from 'lodash'; /** * Internal dependencies @@ -119,6 +119,7 @@ export function getCSSVarFromStyleValue( styleValue: string ): string { const variable = styleValue .slice( VARIABLE_REFERENCE_PREFIX.length ) .split( VARIABLE_PATH_SEPARATOR_TOKEN_ATTRIBUTE ) + .map( ( presetVariable ) => kebabCase( presetVariable ) ) .join( VARIABLE_PATH_SEPARATOR_TOKEN_STYLE ); return `var(--wp--${ variable })`; } diff --git a/packages/style-engine/src/test/index.js b/packages/style-engine/src/test/index.js index 871dbce08cc5ae..d01eba6428474b 100644 --- a/packages/style-engine/src/test/index.js +++ b/packages/style-engine/src/test/index.js @@ -96,6 +96,19 @@ describe( 'generate', () => { ); } ); + it( 'should parse preset values and kebab-case the slug', () => { + expect( + compileCSS( { + color: { + text: 'var:preset|font-size|h1', + }, + spacing: { margin: { top: 'var:preset|spacing|3XL' } }, + } ) + ).toEqual( + 'color: var(--wp--preset--font-size--h-1); margin-top: var(--wp--preset--spacing--3-xl);' + ); + } ); + it( 'should parse border rules', () => { expect( compileCSS( { diff --git a/packages/style-engine/src/test/utils.js b/packages/style-engine/src/test/utils.js index 7fccd013594420..a45d9256dc1c09 100644 --- a/packages/style-engine/src/test/utils.js +++ b/packages/style-engine/src/test/utils.js @@ -1,7 +1,11 @@ /** * Internal dependencies */ -import { upperFirst } from '../styles/utils'; +import { + camelCaseJoin, + getCSSVarFromStyleValue, + upperFirst, +} from '../styles/utils'; describe( 'utils', () => { describe( 'upperFirst()', () => { @@ -9,4 +13,30 @@ describe( 'utils', () => { expect( upperFirst( 'toontown' ) ).toEqual( 'Toontown' ); } ); } ); + + describe( 'camelCaseJoin()', () => { + it( 'should return a camelCase string', () => { + expect( camelCaseJoin( [ 'toon', 'town' ] ) ).toEqual( 'toonTown' ); + } ); + } ); + + describe( 'getCSSVarFromStyleValue()', () => { + it( 'should return a compiled CSS var', () => { + expect( + getCSSVarFromStyleValue( 'var:preset|color|yellow-bun' ) + ).toEqual( 'var(--wp--preset--color--yellow-bun)' ); + } ); + + it( 'should kebab case numbers', () => { + expect( + getCSSVarFromStyleValue( 'var:preset|font-size|h1' ) + ).toEqual( 'var(--wp--preset--font-size--h-1)' ); + } ); + + it( 'should kebab case camel case', () => { + expect( + getCSSVarFromStyleValue( 'var:preset|color|heavenlyBlue' ) + ).toEqual( 'var(--wp--preset--color--heavenly-blue)' ); + } ); + } ); } );