@@ -4,6 +4,7 @@ import type * as Polymorphic from '@radix-ui/react-polymorphic';
44import { classNames } from '../../utilities/css' ;
55
66import styles from './Box.scss' ;
7+ import type { spacing } from '@shopify/polaris-tokens' ;
78
89type Background =
910 | 'background'
@@ -44,15 +45,42 @@ type BorderRadius =
4445
4546type 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+
4755interface 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
5886type PolymorphicBox = Polymorphic . ForwardRefComponent < 'div' , BoxBaseProps > ;
@@ -61,9 +89,48 @@ export type BoxProps = Polymorphic.OwnProps<PolymorphicBox>;
6189
6290export 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