Skip to content
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: add margin support to frontend #39790

Merged
merged 2 commits into from
Mar 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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