|
1 | 1 | import React, {ReactNode, forwardRef} from 'react'; |
2 | 2 | import type * as Polymorphic from '@radix-ui/react-polymorphic'; |
| 3 | +import type {spacing} from '@shopify/polaris-tokens'; |
3 | 4 |
|
4 | 5 | import {classNames} from '../../utilities/css'; |
5 | 6 |
|
6 | 7 | import styles from './Box.scss'; |
7 | | -import type {spacing} from '@shopify/polaris-tokens'; |
8 | 8 |
|
9 | 9 | type Background = |
10 | 10 | | 'background' |
@@ -48,10 +48,18 @@ type Shadow = 'default' | 'transparent' | 'faint' | 'deep'; |
48 | 48 | type SpacingTokenGroup = typeof spacing; |
49 | 49 | type SpacingTokenName = keyof SpacingTokenGroup; |
50 | 50 |
|
| 51 | +// TODO: Bring this logic into tokens |
51 | 52 | type SpacingTokenScale = SpacingTokenName extends `space-${infer Scale}` |
52 | 53 | ? Scale |
53 | 54 | : never; |
54 | 55 |
|
| 56 | +type Spacing = { |
| 57 | + bottom: SpacingTokenScale | ''; |
| 58 | + left: SpacingTokenScale | ''; |
| 59 | + right: SpacingTokenScale | ''; |
| 60 | + top: SpacingTokenScale | ''; |
| 61 | +}; |
| 62 | + |
55 | 63 | interface BoxBaseProps { |
56 | 64 | /** Background color of the Box */ |
57 | 65 | background?: Background; |
@@ -94,41 +102,57 @@ export const Box = forwardRef( |
94 | 102 | background, |
95 | 103 | borderRadius, |
96 | 104 | children, |
97 | | - margin, |
98 | | - marginBottom, |
99 | | - marginLeft, |
100 | | - marginRight, |
101 | | - marginTop, |
102 | | - padding, |
103 | | - paddingBottom, |
104 | | - paddingLeft, |
105 | | - paddingRight, |
106 | | - paddingTop, |
| 105 | + margin = '', |
| 106 | + marginBottom = '', |
| 107 | + marginLeft = '', |
| 108 | + marginRight = '', |
| 109 | + marginTop = '', |
| 110 | + padding = '', |
| 111 | + paddingBottom = '', |
| 112 | + paddingLeft = '', |
| 113 | + paddingRight = '', |
| 114 | + paddingTop = '', |
107 | 115 | shadow, |
108 | 116 | }, |
109 | 117 | ref, |
110 | 118 | ) => { |
| 119 | + const margins = { |
| 120 | + bottom: marginBottom ? marginBottom : margin, |
| 121 | + left: marginLeft ? marginLeft : margin, |
| 122 | + right: marginRight ? marginRight : margin, |
| 123 | + top: marginTop ? marginTop : margin, |
| 124 | + } as Spacing; |
| 125 | + |
| 126 | + const paddings = { |
| 127 | + bottom: paddingBottom ? paddingBottom : padding, |
| 128 | + left: paddingLeft ? paddingLeft : padding, |
| 129 | + right: paddingRight ? paddingRight : padding, |
| 130 | + top: paddingTop ? paddingTop : padding, |
| 131 | + } as Spacing; |
| 132 | + |
111 | 133 | const style = { |
112 | | - '--pc-box-margin': margin ? `var(--p-space-${margin})` : '', |
113 | | - '--pc-box-margin-bottom': marginBottom |
114 | | - ? `var(--p-space-${marginBottom})` |
| 134 | + '--pc-box-margin-bottom': margins.bottom |
| 135 | + ? `var(--p-space-${margins.bottom})` |
| 136 | + : '', |
| 137 | + '--pc-box-margin-left': margins.left |
| 138 | + ? `var(--p-space-${margins.left})` |
| 139 | + : '', |
| 140 | + '--pc-box-margin-right': margins.right |
| 141 | + ? `var(--p-space-${margins.right})` |
115 | 142 | : '', |
116 | | - '--pc-box-margin-left': marginLeft ? `var(--p-space-${marginLeft})` : '', |
117 | | - '--pc-box-margin-right': marginRight |
118 | | - ? `var(--p-space-${marginRight})` |
| 143 | + '--pc-box-margin-top': margins.top ? `var(--p-space-${margins.top})` : '', |
| 144 | + '--pc-box-padding-bottom': paddings.bottom |
| 145 | + ? `var(--p-space-${paddings.bottom})` |
119 | 146 | : '', |
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})` |
| 147 | + '--pc-box-padding-left': paddings.left |
| 148 | + ? `var(--p-space-${paddings.left})` |
124 | 149 | : '', |
125 | | - '--pc-box-padding-left': paddingLeft |
126 | | - ? `var(--p-space-${paddingLeft})` |
| 150 | + '--pc-box-padding-right': paddings.right |
| 151 | + ? `var(--p-space-${paddings.right})` |
127 | 152 | : '', |
128 | | - '--pc-box-padding-right': paddingRight |
129 | | - ? `var(--p-space-${paddingRight})` |
| 153 | + '--pc-box-padding-top': paddings.top |
| 154 | + ? `var(--p-space-${paddings.top})` |
130 | 155 | : '', |
131 | | - '--pc-box-padding-top': paddingTop ? `var(--p-space-${paddingTop})` : '', |
132 | 156 | } as React.CSSProperties; |
133 | 157 |
|
134 | 158 | const className = classNames( |
|
0 commit comments