-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Style engine: export util to compile CSS custom var from preset string #64490
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import { camelCaseJoin, getCSSValueFromRawStyle, upperFirst } from '../utils'; | ||
|
||
describe( 'utils', () => { | ||
describe( 'upperFirst()', () => { | ||
it( 'should return an string with a capitalized first letter', () => { | ||
expect( upperFirst( 'toontown' ) ).toEqual( 'Toontown' ); | ||
} ); | ||
} ); | ||
|
||
describe( 'camelCaseJoin()', () => { | ||
it( 'should return a camelCase string', () => { | ||
expect( camelCaseJoin( [ 'toon', 'town' ] ) ).toEqual( 'toonTown' ); | ||
} ); | ||
} ); | ||
|
||
describe( 'getCSSValueFromRawStyle()', () => { | ||
it.each( [ | ||
[ 'min(40%, 400px)', 'min(40%, 400px)' ], | ||
[ | ||
'var(--wp--preset--color--yellow-bun)', | ||
'var:preset|color|yellow-bun', | ||
], | ||
[ 'var(--wp--preset--font-size--h-1)', 'var:preset|font-size|h1' ], | ||
[ | ||
'var(--wp--preset--font-size--1-px)', | ||
'var:preset|font-size|1px', | ||
], | ||
[ | ||
'var(--wp--preset--color--orange-11-orange)', | ||
'var:preset|color|orange11orange', | ||
], | ||
[ | ||
'var(--wp--preset--color--heavenly-blue)', | ||
'var:preset|color|heavenlyBlue', | ||
], | ||
[ | ||
'var(--wp--preset--background--dark-secrets-100)', | ||
'var:preset|background|dark_Secrets_100', | ||
], | ||
[ null, null ], | ||
[ false, false ], | ||
[ 1000, 1000 ], | ||
[ undefined, undefined ], | ||
] )( | ||
'should return %s using an incoming value of %s', | ||
( expected, value ) => { | ||
expect( getCSSValueFromRawStyle( value ) ).toEqual( expected ); | ||
} | ||
); | ||
} ); | ||
} ); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,7 +61,7 @@ export function generateRule( | |
{ | ||
selector: options?.selector, | ||
key: ruleKey, | ||
value: getCSSVarFromStyleValue( styleValue ), | ||
value: getCSSValueFromRawStyle( styleValue ), | ||
}, | ||
] | ||
: []; | ||
|
@@ -103,7 +103,7 @@ export function generateBoxRules( | |
} else { | ||
const sideRules = individualProperties.reduce( | ||
( acc: GeneratedCSSRule[], side: string ) => { | ||
const value: string | undefined = getCSSVarFromStyleValue( | ||
const value = getCSSValueFromRawStyle( | ||
getStyleValueByPath( boxStyle, [ side ] ) | ||
); | ||
if ( value ) { | ||
|
@@ -127,13 +127,21 @@ 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: | ||
* | ||
* `getCSSValueFromRawStyle( 'var:preset|color|heavenlyBlue' )` // returns 'var(--wp--preset--color--heavenly-blue)' | ||
* | ||
* @param styleValue A raw style value. | ||
* | ||
* @return string A CSS var value. | ||
* @return A CSS var value. | ||
*/ | ||
export function getCSSVarFromStyleValue( styleValue: string ): string { | ||
|
||
export function getCSSValueFromRawStyle( | ||
styleValue: string | any | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh dang, thanks for the tip. I'll create a quick follow up if you don't mind taking a look. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
): string | unknown { | ||
if ( | ||
typeof styleValue === 'string' && | ||
styleValue.startsWith( VARIABLE_REFERENCE_PREFIX ) | ||
|
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Side note: I think the style engine in general could subsume a lot of style utility functions, even in the backend. E.g., ref resolution, CSS var building and more.
I see the benefit as reducing
WP_Theme_JSON
bloat and grouping 'like' concerns.