Skip to content

Commit 732330a

Browse files
madhurisandbhorjoshuaellis
authored andcommitted
feat: margin and padding logical props (#1685)
1 parent 47a038e commit 732330a

File tree

8 files changed

+136
-32
lines changed

8 files changed

+136
-32
lines changed

.changeset/fresh-turkeys-approve.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@strapi/design-system': minor
3+
---
4+
5+
feat: add logic spacing properties

docs/stories/Box.mdx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,18 @@ The `Box` component allows creation of containers according to the guidelines of
2424

2525
<Canvas of={BoxStories.Base} />
2626

27-
## Box with padding
27+
## Box with responsive spaces
2828

29-
The `Box` component with padding added.
29+
The `Box` component with responsive padding added.
3030

3131
<Canvas of={BoxStories.ResponsiveSpaces} />
3232

33+
## Box with logical properties
34+
35+
The `Box` component is internally mapped to logical properties such as `paddingBlock`, `paddingInline`, `marginBlock` and `marginInline` added.
36+
37+
<Canvas of={BoxStories.LogicalProperties} />
38+
3339
## Props
3440

3541
<ArgsTable of={Box} />

docs/stories/Box.stories.tsx

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,26 @@ export const ResponsiveSpaces = {
3737

3838
name: 'responsive spaces',
3939
} satisfies Story;
40+
41+
export const LogicalProperties = {
42+
render: () => (
43+
<Box
44+
paddingTop={4}
45+
padding={[6, 4, 1]}
46+
paddingRight={2}
47+
marginBottom={4}
48+
margin={2}
49+
marginLeft={[8, 4, 2]}
50+
background="primary700"
51+
shadow="filterShadow"
52+
hiddenXS
53+
borderColor="danger600"
54+
borderStyle="dotted"
55+
borderWidth="2px"
56+
>
57+
<Typography textColor="neutral0">Hello world</Typography>
58+
</Box>
59+
),
60+
61+
name: 'logical properties',
62+
} satisfies Story;

packages/strapi-design-system/src/Box/Box.tsx

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ export type BoxProps<TElement extends keyof JSX.IntrinsicElements = 'div'> = Rea
8282
* Padding top. Supports responsive values
8383
*/
8484
paddingTop?: ResponsiveValue<'paddingTop'>;
85+
/**
86+
* Margin. Supports responsive values
87+
*/
88+
margin?: ResponsiveValue<'margin'>;
8589
marginLeft?: ResponsiveValue<'marginLeft'>;
8690
marginBottom?: ResponsiveValue<'marginBottom'>;
8791
marginRight?: ResponsiveValue<'marginRight'>;
@@ -137,15 +141,19 @@ export const Box = styled.div.withConfig<BoxProps>({
137141
color: ${({ theme, color }) => extractStyleFromTheme(theme.colors, color, undefined)};
138142
139143
// Spaces
140-
${({ theme, padding }) => handleResponsiveValues('padding', padding, theme)}
141-
${({ theme, paddingTop }) => handleResponsiveValues('padding-top', paddingTop, theme)}
142-
${({ theme, paddingRight }) => handleResponsiveValues('padding-right', paddingRight, theme)}
143-
${({ theme, paddingBottom }) => handleResponsiveValues('padding-bottom', paddingBottom, theme)}
144-
${({ theme, paddingLeft }) => handleResponsiveValues('padding-left', paddingLeft, theme)}
145-
${({ theme, marginLeft }) => handleResponsiveValues('margin-left', marginLeft, theme)}
146-
${({ theme, marginRight }) => handleResponsiveValues('margin-right', marginRight, theme)}
147-
${({ theme, marginTop }) => handleResponsiveValues('margin-top', marginTop, theme)}
148-
${({ theme, marginBottom }) => handleResponsiveValues('margin-bottom', marginBottom, theme)}
144+
${({ theme, padding }) => handleResponsiveValues('padding-block', padding, theme)}
145+
${({ theme, padding }) => handleResponsiveValues('padding-inline', padding, theme)}
146+
${({ theme, paddingTop }) => handleResponsiveValues('padding-block-start', paddingTop, theme)}
147+
${({ theme, paddingRight }) => handleResponsiveValues('padding-inline-end', paddingRight, theme)}
148+
${({ theme, paddingBottom }) => handleResponsiveValues('padding-block-end', paddingBottom, theme)}
149+
${({ theme, paddingLeft }) => handleResponsiveValues('padding-inline-start', paddingLeft, theme)}
150+
151+
${({ theme, margin }) => handleResponsiveValues('margin-block', margin, theme)}
152+
${({ theme, margin }) => handleResponsiveValues('margin-inline', margin, theme)}
153+
${({ theme, marginLeft }) => handleResponsiveValues('margin-inline-start', marginLeft, theme)}
154+
${({ theme, marginRight }) => handleResponsiveValues('margin-inline-end', marginRight, theme)}
155+
${({ theme, marginTop }) => handleResponsiveValues('margin-block-start', marginTop, theme)}
156+
${({ theme, marginBottom }) => handleResponsiveValues('margin-block-end', marginBottom, theme)}
149157
150158
// Responsive hiding
151159
${({ theme, hiddenS }) => (hiddenS ? `${theme.mediaQueries.tablet} { display: none; }` : undefined)}

packages/strapi-design-system/src/Box/__tests__/Box.spec.tsx

Lines changed: 51 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,57 @@ describe('Box', () => {
1010
expect(container.children[0]).toHaveStyle(`${colorProp}: #7b79ff`);
1111
});
1212

13-
it.each(['padding', 'paddingTop', 'paddingRight', 'paddingBottom', 'paddingLeft'])(
14-
'retrieves the theme value corresponding to the %s props',
15-
(spacingProp) => {
16-
const { container } = setup({ [spacingProp]: 4 });
17-
expect(container.children[0]).toHaveStyle(`${spacingProp}: 16px`);
18-
},
19-
);
13+
it('retrieves the theme value corresponding to the padding', () => {
14+
const { container } = setup({ padding: 4 });
15+
expect(container.children[0]).toHaveStyle(`padding-block: 16px`);
16+
expect(container.children[0]).toHaveStyle(`padding-inline: 16px`);
17+
});
18+
19+
it('retrieves the theme value corresponding to the paddingTop', () => {
20+
const { container } = setup({ paddingTop: 1 });
21+
expect(container.children[0]).toHaveStyle(`padding-block-start: 4px`);
22+
});
23+
24+
it('retrieves the theme value corresponding to the paddingBottom', () => {
25+
const { container } = setup({ paddingBottom: 2 });
26+
expect(container.children[0]).toHaveStyle(`padding-block-end: 8px`);
27+
});
28+
29+
it('retrieves the theme value corresponding to the paddingLeft', () => {
30+
const { container } = setup({ paddingLeft: 1 });
31+
expect(container.children[0]).toHaveStyle(`padding-inline-start: 4px`);
32+
});
33+
34+
it('retrieves the theme value corresponding to the paddingRight', () => {
35+
const { container } = setup({ paddingRight: 2 });
36+
expect(container.children[0]).toHaveStyle(`padding-inline-end: 8px`);
37+
});
38+
39+
it('retrieves the theme value corresponding to the margin', () => {
40+
const { container } = setup({ margin: 4 });
41+
expect(container.children[0]).toHaveStyle(`margin-block: 16px`);
42+
expect(container.children[0]).toHaveStyle(`margin-inline: 16px`);
43+
});
44+
45+
it('retrieves the theme value corresponding to the marginTop', () => {
46+
const { container } = setup({ marginTop: 1 });
47+
expect(container.children[0]).toHaveStyle(`margin-block-start: 4px`);
48+
});
49+
50+
it('retrieves the theme value corresponding to the marginBottom', () => {
51+
const { container } = setup({ marginBottom: 2 });
52+
expect(container.children[0]).toHaveStyle(`margin-block-end: 8px`);
53+
});
54+
55+
it('retrieves the theme value corresponding to the marginLeft', () => {
56+
const { container } = setup({ marginLeft: 1 });
57+
expect(container.children[0]).toHaveStyle(`margin-inline-start: 4px`);
58+
});
59+
60+
it('retrieves the theme value corresponding to the marginRight', () => {
61+
const { container } = setup({ marginRight: 2 });
62+
expect(container.children[0]).toHaveStyle(`margin-inline-end: 8px`);
63+
});
2064

2165
it.each(['color', 'cursor', 'height', 'width'])(
2266
'does not render color or cursor props as HTML attributes',

packages/strapi-design-system/src/ModalLayout/__tests__/__snapshots__/ModalLayout.spec.tsx.snap

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ exports[`ModalLayout should render component and match snapshot 1`] = `
2121
}
2222
2323
.c1 {
24-
padding-right: 40px;
25-
padding-left: 40px;
24+
padding-inline-end: 40px;
25+
padding-inline-start: 40px;
2626
position: fixed;
2727
z-index: 4;
2828
}
@@ -36,30 +36,33 @@ exports[`ModalLayout should render component and match snapshot 1`] = `
3636
3737
.c5 {
3838
background: #f6f6f9;
39-
padding-top: 16px;
40-
padding-right: 20px;
41-
padding-bottom: 16px;
42-
padding-left: 20px;
39+
padding-block-start: 16px;
40+
padding-inline-end: 20px;
41+
padding-block-end: 16px;
42+
padding-inline-start: 20px;
4343
}
4444
4545
.c8 {
4646
background: #ffffff;
47-
padding: 8px;
47+
padding-block: 8px;
48+
padding-inline: 8px;
4849
border-radius: 4px;
4950
border-color: #dcdce4;
5051
border: 1px solid #dcdce4;
5152
cursor: pointer;
5253
}
5354
5455
.c11 {
55-
padding: 32px;
56+
padding-block: 32px;
57+
padding-inline: 32px;
5658
}
5759
5860
.c15 {
5961
background: #4945ff;
60-
padding: 8px;
61-
padding-right: 16px;
62-
padding-left: 16px;
62+
padding-block: 8px;
63+
padding-inline: 8px;
64+
padding-inline-end: 16px;
65+
padding-inline-start: 16px;
6366
border-radius: 4px;
6467
border-color: #4945ff;
6568
border: 1px solid #4945ff;

packages/strapi-design-system/src/Tooltip/__tests__/__snapshots__/Tooltip.spec.tsx.snap

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ exports[`Tooltip snapshots document.body when the button is focused (aria-descri
1515
1616
.c1 {
1717
background: #212134;
18-
padding: 8px;
18+
padding-block: 8px;
19+
padding-inline: 8px;
1920
border-radius: 4px;
2021
position: absolute;
2122
}
@@ -106,7 +107,8 @@ exports[`Tooltip snapshots document.body when the tooltip is not visible but exi
106107
107108
.c1 {
108109
background: #212134;
109-
padding: 8px;
110+
padding-block: 8px;
111+
padding-inline: 8px;
110112
border-radius: 4px;
111113
position: absolute;
112114
}
@@ -190,7 +192,8 @@ exports[`Tooltip snapshots document.body with a label 1`] = `
190192
191193
.c1 {
192194
background: #212134;
193-
padding: 8px;
195+
padding-block: 8px;
196+
padding-inline: 8px;
194197
border-radius: 4px;
195198
position: absolute;
196199
}

packages/strapi-design-system/src/helpers/handleResponsiveValues.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,23 @@ type ResponsiveCSSProperties = Pick<
2323
| 'marginRight'
2424
| 'marginTop'
2525
| 'marginBottom'
26+
| 'marginBlock'
27+
| 'marginBlockStart'
28+
| 'marginBlockEnd'
29+
| 'marginInline'
30+
| 'marginInlineStart'
31+
| 'marginInlineEnd'
2632
| 'padding'
2733
| 'paddingLeft'
2834
| 'paddingRight'
2935
| 'paddingTop'
3036
| 'paddingBottom'
37+
| 'paddingBlock'
38+
| 'paddingBlockStart'
39+
| 'paddingBlockEnd'
40+
| 'paddingInline'
41+
| 'paddingInlineStart'
42+
| 'paddingInlineEnd'
3143
>;
3244

3345
export type ResponsiveValue<TCSSProp extends keyof ResponsiveCSSProperties = any> =

0 commit comments

Comments
 (0)