1- import React , { ReactNode , forwardRef } from 'react' ;
2- import type * as Polymorphic from '@radix-ui/react-polymorphic' ;
1+ import React , { createElement , forwardRef , ReactNode } from 'react' ;
32import type { colors , depth , shape , spacing } from '@shopify/polaris-tokens' ;
43
54import { classNames } from '../../utilities/css' ;
@@ -53,10 +52,10 @@ type BorderRadiusTokenScale = Extract<
5352 : never ;
5453
5554interface BorderRadius {
56- bottomLeft : BorderRadiusTokenScale | '' ;
57- bottomRight : BorderRadiusTokenScale | '' ;
58- topLeft : BorderRadiusTokenScale | '' ;
59- topRight : BorderRadiusTokenScale | '' ;
55+ bottomLeft : BorderRadiusTokenScale ;
56+ bottomRight : BorderRadiusTokenScale ;
57+ topLeft : BorderRadiusTokenScale ;
58+ topRight : BorderRadiusTokenScale ;
6059}
6160
6261type SpacingTokenGroup = typeof spacing ;
@@ -68,13 +67,14 @@ type SpacingTokenScale = SpacingTokenName extends `space-${infer Scale}`
6867 : never ;
6968
7069interface Spacing {
71- bottom : SpacingTokenScale | '' ;
72- left : SpacingTokenScale | '' ;
73- right : SpacingTokenScale | '' ;
74- top : SpacingTokenScale | '' ;
70+ bottom : SpacingTokenScale ;
71+ left : SpacingTokenScale ;
72+ right : SpacingTokenScale ;
73+ top : SpacingTokenScale ;
7574}
7675
77- interface BoxBaseProps {
76+ export interface BoxProps {
77+ as ?: 'div' | 'span' ;
7878 /** Background color of the Box */
7979 background ?: BackgroundColorTokenScale ;
8080 /** Border styling of the Box */
@@ -123,36 +123,32 @@ interface BoxBaseProps {
123123 shadow ?: DepthTokenScale ;
124124}
125125
126- type PolymorphicBox = Polymorphic . ForwardRefComponent < 'div' , BoxBaseProps > ;
127-
128- export type BoxProps = Polymorphic . OwnProps < PolymorphicBox > ;
129-
130- export const Box = forwardRef (
126+ export const Box = forwardRef < HTMLElement , BoxProps > (
131127 (
132128 {
133- as : Component = 'div' ,
129+ as = 'div' ,
134130 background,
135- border = '' ,
136- borderBottom = '' ,
137- borderLeft = '' ,
138- borderRight = '' ,
139- borderTop = '' ,
140- borderRadius = '' ,
141- borderRadiusBottomLeft = '' ,
142- borderRadiusBottomRight = '' ,
143- borderRadiusTopLeft = '' ,
144- borderRadiusTopRight = '' ,
131+ border,
132+ borderBottom,
133+ borderLeft,
134+ borderRight,
135+ borderTop,
136+ borderRadius,
137+ borderRadiusBottomLeft,
138+ borderRadiusBottomRight,
139+ borderRadiusTopLeft,
140+ borderRadiusTopRight,
145141 children,
146- margin = '' ,
147- marginBottom = '' ,
148- marginLeft = '' ,
149- marginRight = '' ,
150- marginTop = '' ,
151- padding = '' ,
152- paddingBottom = '' ,
153- paddingLeft = '' ,
154- paddingRight = '' ,
155- paddingTop = '' ,
142+ margin,
143+ marginBottom,
144+ marginLeft,
145+ marginRight,
146+ marginTop,
147+ padding,
148+ paddingBottom,
149+ paddingLeft,
150+ paddingRight,
151+ paddingTop,
156152 shadow,
157153 } ,
158154 ref ,
@@ -190,64 +186,78 @@ export const Box = forwardRef(
190186 } as Spacing ;
191187
192188 const style = {
193- '--pc-box-background' : background ? `var(--p-${ background } )` : '' ,
194- '--pc-box-margin-bottom' : margins . bottom
195- ? `var(--p-space-${ margins . bottom } )`
196- : '' ,
197- '--pc-box-margin-left' : margins . left
198- ? `var(--p-space-${ margins . left } )`
199- : '' ,
200- '--pc-box-margin-right' : margins . right
201- ? `var(--p-space-${ margins . right } )`
202- : '' ,
203- '--pc-box-margin-top' : margins . top ? `var(--p-space-${ margins . top } )` : '' ,
204- '--pc-box-padding-bottom' : paddings . bottom
205- ? `var(--p-space-${ paddings . bottom } )`
206- : '' ,
207- '--pc-box-padding-left' : paddings . left
208- ? `var(--p-space-${ paddings . left } )`
209- : '' ,
210- '--pc-box-padding-right' : paddings . right
211- ? `var(--p-space-${ paddings . right } )`
212- : '' ,
213- '--pc-box-padding-top' : paddings . top
214- ? `var(--p-space-${ paddings . top } )`
215- : '' ,
216- '--pc-box-border-bottom' : borders . bottom
217- ? `var(--p-border-${ borders . bottom } )`
218- : '' ,
219- '--pc-box-border-left' : borders . left
220- ? `var(--p-border-${ borders . left } )`
221- : '' ,
222- '--pc-box-border-right' : borders . right
223- ? `var(--p-border-${ borders . right } )`
224- : '' ,
225- '--pc-box-border-top' : borders . top
226- ? `var(--p-border-${ borders . top } )`
227- : '' ,
228- '--pc-box-border-radius-bottom-left' : borderRadiuses . bottomLeft
229- ? `var(--p-border-radius-${ borderRadiuses . bottomLeft } )`
230- : '' ,
231- '--pc-box-border-radius-bottom-right' : borderRadiuses . bottomRight
232- ? `var(--p-border-radius-${ borderRadiuses . bottomRight } )`
233- : '' ,
234- '--pc-box-border-radius-top-left' : borderRadiuses . topLeft
235- ? `var(--p-border-radius-${ borderRadiuses . topLeft } )`
236- : '' ,
237- '--pc-box-border-radius-top-right' : borderRadiuses . topRight
238- ? `var(--p-border-radius-${ borderRadiuses . topRight } )`
239- : '' ,
240- '--pc-box-shadow' : shadow ? `var(--p-shadow-${ shadow } )` : '' ,
189+ ...( background ? { '--pc-box-background' : `var(--p-${ background } )` } : { } ) ,
190+ ...( borders . bottom
191+ ? { '--pc-box-border-bottom' : `var(--p-border-${ borders . bottom } )` }
192+ : { } ) ,
193+ ...( borders . left
194+ ? { '--pc-box-border-left' : `var(--p-border-${ borders . left } )` }
195+ : { } ) ,
196+ ...( borders . right
197+ ? { '--pc-box-border-right' : `var(--p-border-${ borders . right } )` }
198+ : { } ) ,
199+ ...( borders . top
200+ ? { '--pc-box-border-top' : `var(--p-border-${ borders . top } )` }
201+ : { } ) ,
202+ ...( borderRadiuses . bottomLeft
203+ ? {
204+ '--pc-box-border-radius-bottom-left' : `var(--p-border-radius-${ borderRadiuses . bottomLeft } )` ,
205+ }
206+ : { } ) ,
207+ ...( borderRadiuses . bottomRight
208+ ? {
209+ '--pc-box-border-radius-bottom-right' : `var(--p-border-radius-${ borderRadiuses . bottomRight } )` ,
210+ }
211+ : { } ) ,
212+ ...( borderRadiuses . topLeft
213+ ? {
214+ '--pc-box-border-radius-top-left' : `var(--p-border-radius-${ borderRadiuses . topLeft } )` ,
215+ }
216+ : { } ) ,
217+ ...( borderRadiuses . topRight
218+ ? {
219+ '--pc-box-border-radius-top-right' : `var(--p-border-radius-${ borderRadiuses . topRight } )` ,
220+ }
221+ : { } ) ,
222+ ...( margins . bottom
223+ ? { '--pc-box-margin-bottom' : `var(--p-space-${ margins . bottom } )` }
224+ : { } ) ,
225+ ...( margins . left
226+ ? { '--pc-box-margin-left' : `var(--p-space-${ margins . left } )` }
227+ : { } ) ,
228+ ...( margins . right
229+ ? { '--pc-box-margin-right' : `var(--p-space-${ margins . right } )` }
230+ : { } ) ,
231+ ...( margins . top
232+ ? { '--pc-box-margin-top' : `var(--p-space-${ margins . top } )` }
233+ : { } ) ,
234+ ...( paddings . bottom
235+ ? { '--pc-box-padding-bottom' : `var(--p-space-${ paddings . bottom } )` }
236+ : { } ) ,
237+ ...( paddings . left
238+ ? { '--pc-box-padding-left' : `var(--p-space-${ paddings . left } )` }
239+ : { } ) ,
240+ ...( paddings . right
241+ ? { '--pc-box-padding-right' : `var(--p-space-${ paddings . right } )` }
242+ : { } ) ,
243+ ...( paddings . top
244+ ? { '--pc-box-padding-top' : `var(--p-space-${ paddings . top } )` }
245+ : { } ) ,
246+ ...( shadow ? { '--pc-box-shadow' : `var(--p-shadow-${ shadow } )` } : { } ) ,
241247 } as React . CSSProperties ;
242248
243- const className = classNames ( styles . root ) ;
249+ const className = classNames ( styles . Box ) ;
244250
245- return (
246- < Component ref = { ref } className = { className } style = { style } >
247- { children }
248- </ Component >
251+ return createElement (
252+ as ,
253+ {
254+ className,
255+ style,
256+ ref,
257+ } ,
258+ children ,
249259 ) ;
250260 } ,
251- ) as PolymorphicBox ;
261+ ) ;
252262
253263Box . displayName = 'Box' ;
0 commit comments