Skip to content

Commit

Permalink
Switch getCSSRules to use camelCase and generate to switch to kebabCase
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewserong committed Jan 25, 2022
1 parent 26c9767 commit a756e44
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 5 deletions.
4 changes: 2 additions & 2 deletions packages/style-engine/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { groupBy } from 'lodash';
import { groupBy, kebabCase } from 'lodash';

/**
* Internal dependencies
Expand All @@ -26,7 +26,7 @@ export function generate( style: Style, selector: string ): string {
`${ subSelector } { ${ groupedRules[ subSelector ]
.map(
( rule: GeneratedCSSRule ) =>
`${ rule.key }: ${ rule.value };`
`${ kebabCase( rule.key ) }: ${ rule.value };`
)
.join( ' ' ) } }`
);
Expand Down
4 changes: 2 additions & 2 deletions packages/style-engine/src/styles/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { get } from 'lodash';
import { get, upperFirst } from 'lodash';

/**
* Internal dependencies
Expand Down Expand Up @@ -29,7 +29,7 @@ export function generateBoxRules(
if ( value ) {
acc.push( {
selector,
key: `${ ruleKey }-${ side }`,
key: `${ ruleKey }${ upperFirst( side ) }`,
value,
} );
}
Expand Down
45 changes: 44 additions & 1 deletion packages/style-engine/src/test/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Internal dependencies
*/
import { generate } from '../index';
import { getCSSRules, generate } from '../index';

describe( 'generate', () => {
it( 'should generate empty style', () => {
Expand Down Expand Up @@ -30,3 +30,46 @@ describe( 'generate', () => {
);
} );
} );

describe( 'getCSSRules', () => {
it( 'should return an empty rules array', () => {
expect( getCSSRules( {}, '.some-selector' ) ).toEqual( [] );
} );

it( 'should return a rules array with CSS keys formatted in camelCase', () => {
expect(
getCSSRules(
{
spacing: { padding: '10px' },
},
'.some-selector'
)
).toEqual( [
{
selector: '.some-selector',
key: 'padding',
value: '10px',
},
] );

expect(
getCSSRules(
{
spacing: { padding: { top: '10px', bottom: '5px' } },
},
'.some-selector'
)
).toEqual( [
{
selector: '.some-selector',
key: 'paddingTop',
value: '10px',
},
{
selector: '.some-selector',
key: 'paddingBottom',
value: '5px',
},
] );
} );
} );
4 changes: 4 additions & 0 deletions packages/style-engine/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ export interface Style {
export type GeneratedCSSRule = {
selector: string;
value: string;
/**
* The CSS key in JS style attribute format, compatible with React.
* E.g. `paddingTop` instead of `padding-top`.
*/
key: string;
};

Expand Down

0 comments on commit a756e44

Please sign in to comment.