Skip to content

Commit 6e8f158

Browse files
[Layout foundations] Add margin and padding props
Co-authored-by: Aaron Casanova <[email protected]>
1 parent 35a1c77 commit 6e8f158

File tree

2 files changed

+92
-4
lines changed

2 files changed

+92
-4
lines changed

polaris-react/src/components/Box/Box.scss

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
11
.root {
2+
--pc-box-margin: initial;
3+
--pc-box-margin-bottom: initial;
4+
--pc-box-margin-left: initial;
5+
--pc-box-margin-right: initial;
6+
--pc-box-margin-top: initial;
7+
--pc-box-padding: initial;
8+
--pc-box-padding-bottom: initial;
9+
--pc-box-padding-left: initial;
10+
--pc-box-padding-right: initial;
11+
--pc-box-padding-top: initial;
12+
213
display: block;
14+
margin: var(--pc-box-margin);
15+
margin-bottom: var(--pc-box-margin-bottom);
16+
margin-left: var(--pc-box-margin-left);
17+
margin-right: var(--pc-box-margin-right);
18+
margin-top: var(--pc-box-margin-top);
19+
padding: var(--pc-box-padding);
20+
padding-bottom: var(--pc-box-padding-bottom);
21+
padding-left: var(--pc-box-padding-left);
22+
padding-right: var(--pc-box-padding-right);
23+
padding-top: var(--pc-box-padding-top);
324
}
425

526
.background {

polaris-react/src/components/Box/Box.tsx

Lines changed: 71 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type * as Polymorphic from '@radix-ui/react-polymorphic';
44
import {classNames} from '../../utilities/css';
55

66
import styles from './Box.scss';
7+
import type {spacing} from '@shopify/polaris-tokens';
78

89
type Background =
910
| 'background'
@@ -44,15 +45,42 @@ type BorderRadius =
4445

4546
type Shadow = 'default' | 'transparent' | 'faint' | 'deep';
4647

48+
type SpacingTokenGroup = typeof spacing;
49+
type SpacingTokenName = keyof SpacingTokenGroup;
50+
51+
type SpacingTokenScale = SpacingTokenName extends `space-${infer Scale}`
52+
? Scale
53+
: never;
54+
4755
interface BoxBaseProps {
4856
/** Background color of the Box */
4957
background?: Background;
5058
/** Border radius of the Box */
5159
borderRadius?: BorderRadius;
52-
/** Shadow on the Box */
53-
shadow?: Shadow;
5460
/** Inner content of the Box */
5561
children: ReactNode;
62+
/** Spacing outside of the Box */
63+
margin?: SpacingTokenScale;
64+
/** Bottom spacing outside of the Box */
65+
marginBottom?: SpacingTokenScale;
66+
/** Left side spacing outside of the Box */
67+
marginLeft?: SpacingTokenScale;
68+
/** Right side spacing outside of the Box */
69+
marginRight?: SpacingTokenScale;
70+
/** Top spacing outside of the Box */
71+
marginTop?: SpacingTokenScale;
72+
/** Spacing inside of the Box */
73+
padding?: SpacingTokenScale;
74+
/** Bottom spacing inside of the Box */
75+
paddingBottom?: SpacingTokenScale;
76+
/** Left side spacing inside of the Box */
77+
paddingLeft?: SpacingTokenScale;
78+
/** Right side spacing inside of the Box */
79+
paddingRight?: SpacingTokenScale;
80+
/** Top spacing inside of the Box */
81+
paddingTop?: SpacingTokenScale;
82+
/** Shadow on the Box */
83+
shadow?: Shadow;
5684
}
5785

5886
type PolymorphicBox = Polymorphic.ForwardRefComponent<'div', BoxBaseProps>;
@@ -61,9 +89,48 @@ export type BoxProps = Polymorphic.OwnProps<PolymorphicBox>;
6189

6290
export const Box = forwardRef(
6391
(
64-
{as: Component = 'div', background, borderRadius, shadow, children},
92+
{
93+
as: Component = 'div',
94+
background,
95+
borderRadius,
96+
children,
97+
margin,
98+
marginBottom,
99+
marginLeft,
100+
marginRight,
101+
marginTop,
102+
padding,
103+
paddingBottom,
104+
paddingLeft,
105+
paddingRight,
106+
paddingTop,
107+
shadow,
108+
},
65109
ref,
66110
) => {
111+
const style = {
112+
'--pc-box-margin': margin ? `var(--p-space-${margin})` : '',
113+
'--pc-box-margin-bottom': marginBottom
114+
? `var(--p-space-${marginBottom})`
115+
: '',
116+
'--pc-box-margin-left': marginLeft ? `var(--p-space-${marginLeft})` : '',
117+
'--pc-box-margin-right': marginRight
118+
? `var(--p-space-${marginRight})`
119+
: '',
120+
'--pc-box-margin-top': marginTop ? `var(--p-space-${marginTop})` : '',
121+
'--pc-box-padding': padding ? `var(--p-space-${padding})` : '',
122+
'--pc-box-padding-bottom': paddingBottom
123+
? `var(--p-space-${paddingBottom})`
124+
: '',
125+
'--pc-box-padding-left': paddingLeft
126+
? `var(--p-space-${paddingLeft})`
127+
: '',
128+
'--pc-box-padding-right': paddingRight
129+
? `var(--p-space-${paddingRight})`
130+
: '',
131+
'--pc-box-padding-top': paddingTop ? `var(--p-space-${paddingTop})` : '',
132+
} as React.CSSProperties;
133+
67134
const className = classNames(
68135
styles.root,
69136
background && styles[background],
@@ -72,7 +139,7 @@ export const Box = forwardRef(
72139
);
73140

74141
return (
75-
<Component ref={ref} className={className}>
142+
<Component ref={ref} className={className} style={style}>
76143
{children}
77144
</Component>
78145
);

0 commit comments

Comments
 (0)