Skip to content

Commit

Permalink
Style Engine: add margin support (#39790)
Browse files Browse the repository at this point in the history
* Add margin support and tests

* Telling margin to use the style engine in global styles
Fixing unit tests
  • Loading branch information
ramonjd authored Mar 28, 2022
1 parent 4c109f6 commit 7490f12
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 8 deletions.
1 change: 1 addition & 0 deletions packages/blocks/src/api/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export const __EXPERIMENTAL_STYLE_PROPERTY = {
marginBottom: 'bottom',
marginLeft: 'left',
},
useEngine: true,
},
padding: {
value: [ 'spacing', 'padding' ],
Expand Down
3 changes: 2 additions & 1 deletion packages/style-engine/src/styles/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
* Internal dependencies
*/
import padding from './padding';
import margin from './margin';

export const styleDefinitions = [ padding ];
export const styleDefinitions = [ margin, padding ];
19 changes: 19 additions & 0 deletions packages/style-engine/src/styles/margin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Internal dependencies
*/
import type { Style, StyleOptions } from '../types';
import { generateBoxRules } from './utils';

const margin = {
name: 'margin',
generate: ( style: Style, options: StyleOptions ) => {
return generateBoxRules(
style,
options,
[ 'spacing', 'margin' ],
'margin'
);
},
};

export default margin;
62 changes: 55 additions & 7 deletions packages/style-engine/src/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,37 @@ describe( 'generate', () => {
expect( generate( {}, '.some-selector' ) ).toEqual( '' );
} );

it( 'should generate padding styles', () => {
it( 'should generate spacing styles', () => {
expect(
generate(
{
spacing: { padding: '10px' },
spacing: { padding: '10px', margin: '12px' },
},
{
selector: '.some-selector',
}
)
).toEqual( '.some-selector { padding: 10px; }' );
).toEqual( '.some-selector { margin: 12px; padding: 10px; }' );

expect(
generate(
{
spacing: { padding: { top: '10px', bottom: '5px' } },
spacing: {
padding: { top: '10px', bottom: '5px' },
margin: {
top: '11px',
right: '12px',
bottom: '13px',
left: '14px',
},
},
},
{
selector: '.some-selector',
}
)
).toEqual(
'.some-selector { padding-top: 10px; padding-bottom: 5px; }'
'.some-selector { margin-top: 11px; margin-right: 12px; margin-bottom: 13px; margin-left: 14px; padding-top: 10px; padding-bottom: 5px; }'
);
} );
} );
Expand All @@ -40,10 +48,13 @@ describe( 'getCSSRules', () => {
expect( getCSSRules( {}, '.some-selector' ) ).toEqual( [] );
} );

it( 'should return a rules array with CSS keys formatted in camelCase', () => {
it( 'should ignore unsupported styles', () => {
expect(
getCSSRules(
{
typography: {
fontVariantLigatures: 'no-common-ligatures',
},
spacing: { padding: '10px' },
},
{
Expand All @@ -57,17 +68,54 @@ describe( 'getCSSRules', () => {
value: '10px',
},
] );
} );

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

expect(
getCSSRules(
{
spacing: {
padding: { top: '10px', bottom: '5px' },
margin: { right: '2em', left: '1vw' },
},
},
{
selector: '.some-selector',
}
)
).toEqual( [
{
selector: '.some-selector',
key: 'marginRight',
value: '2em',
},
{
selector: '.some-selector',
key: 'marginLeft',
value: '1vw',
},
{
selector: '.some-selector',
key: 'paddingTop',
Expand Down

0 comments on commit 7490f12

Please sign in to comment.