Skip to content

Commit 20380ce

Browse files
committed
[Layout foundations] Refactor and add border and borderRadius props
1 parent 2f36dda commit 20380ce

File tree

2 files changed

+105
-85
lines changed

2 files changed

+105
-85
lines changed

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

Lines changed: 8 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@
99
padding-left: var(--pc-box-padding-left, initial);
1010
padding-right: var(--pc-box-padding-right, initial);
1111
padding-top: var(--pc-box-padding-top, initial);
12+
border-bottom-left-radius: var(--pc-box-border-radius-bottom-left, initial);
13+
border-bottom-right-radius: var(--pc-box-border-radius-bottom-right, initial);
14+
border-top-left-radius: var(--pc-box-border-radius-top-left, initial);
15+
border-top-right-radius: var(--pc-box-border-radius-top-right, initial);
16+
border-bottom: var(--pc-box-border-bottom, initial);
17+
border-left: var(--pc-box-border-left, initial);
18+
border-right: var(--pc-box-border-right, initial);
19+
border-top: var(--pc-box-border-top, initial);
1220
/* stylelint-enable declaration-block-no-redundant-longhand-properties */
1321
}
1422

@@ -104,86 +112,6 @@
104112
background-color: var(--p-overlay);
105113
}
106114

107-
.radius-05 {
108-
border-radius: var(--p-border-radius-05);
109-
}
110-
111-
.radius-1 {
112-
border-radius: var(--p-border-radius-1);
113-
}
114-
115-
.radius-2 {
116-
border-radius: var(--p-border-radius-2);
117-
}
118-
119-
.radius-3 {
120-
border-radius: var(--p-border-radius-3);
121-
}
122-
123-
.radius-4 {
124-
border-radius: var(--p-border-radius-4);
125-
}
126-
127-
.radius-5 {
128-
border-radius: var(--p-border-radius-5);
129-
}
130-
131-
.radius-6 {
132-
border-radius: var(--p-border-radius-6);
133-
}
134-
135-
.radius-base {
136-
border-radius: var(--p-border-radius-base);
137-
}
138-
139-
.radius-large {
140-
border-radius: var(--p-border-radius-large);
141-
}
142-
143-
.radius-half {
144-
border-radius: var(--p-border-radius-half);
145-
}
146-
147-
.width-1 {
148-
border: var(--p-border-width-1) solid;
149-
}
150-
151-
.width-2 {
152-
border: var(--p-border-width-2) solid;
153-
}
154-
155-
.width-3 {
156-
border: var(--p-border-width-3) solid;
157-
}
158-
159-
.width-4 {
160-
border: var(--p-border-width-4) solid;
161-
}
162-
163-
.width-5 {
164-
border: var(--p-border-width-5) solid;
165-
}
166-
167-
.base {
168-
border: var(--p-border-base);
169-
}
170-
171-
.dark {
172-
border: var(--p-border-dark);
173-
}
174-
175-
.transparent {
176-
border: var(--p-border-transparent);
177-
}
178-
179-
.divider {
180-
border: var(--p-border-divider);
181-
}
182-
183-
.divider-on-dark {
184-
border: var(--p-border-divider-on-dark);
185-
}
186-
187115
.shadowBase {
188116
box-shadow: var(--p-shadow-base);
189117
}

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

Lines changed: 97 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,34 @@ type Shadow = 'default' | 'transparent' | 'faint' | 'deep';
3636
type ShapeTokenGroup = typeof shape;
3737
type ShapeTokenName = keyof ShapeTokenGroup;
3838

39-
type BorderTokenScale = ShapeTokenName extends `border-${infer Scale}`
39+
type BorderShapeTokenScale = ShapeTokenName extends `border-${infer Scale}`
4040
? Scale
4141
: never;
4242

43+
type BorderTokenScale = Exclude<
44+
BorderShapeTokenScale,
45+
`radius-${string}` | `width-${string}`
46+
>;
47+
48+
interface Border {
49+
bottom: BorderTokenScale;
50+
left: BorderTokenScale;
51+
right: BorderTokenScale;
52+
top: BorderTokenScale;
53+
}
54+
55+
type BorderRadiusTokenScale = Extract<
56+
BorderShapeTokenScale,
57+
`radius-${string}`
58+
>;
59+
60+
interface BorderRadius {
61+
bottomLeft: BorderRadiusTokenScale | '';
62+
bottomRight: BorderRadiusTokenScale | '';
63+
topLeft: BorderRadiusTokenScale | '';
64+
topRight: BorderRadiusTokenScale | '';
65+
}
66+
4367
type SpacingTokenGroup = typeof spacing;
4468
type SpacingTokenName = keyof SpacingTokenGroup;
4569

@@ -48,18 +72,36 @@ type SpacingTokenScale = SpacingTokenName extends `space-${infer Scale}`
4872
? Scale
4973
: never;
5074

51-
type Spacing = {
75+
interface Spacing {
5276
bottom: SpacingTokenScale | '';
5377
left: SpacingTokenScale | '';
5478
right: SpacingTokenScale | '';
5579
top: SpacingTokenScale | '';
56-
};
80+
}
5781

5882
interface BoxBaseProps {
5983
/** Background color of the Box */
6084
background?: Background;
6185
/** Border styling of the Box */
6286
border?: BorderTokenScale;
87+
/** Bottom border styling of the Box */
88+
borderBottom?: BorderTokenScale;
89+
/** Left border styling of the Box */
90+
borderLeft?: BorderTokenScale;
91+
/** Right border styling of the Box */
92+
borderRight?: BorderTokenScale;
93+
/** Top border styling of the Box */
94+
borderTop?: BorderTokenScale;
95+
/** Border radius of the Box */
96+
borderRadius?: BorderRadiusTokenScale;
97+
/** Bottom left border radius of the Box */
98+
borderRadiusBottomLeft?: BorderRadiusTokenScale;
99+
/** Bottom right border radius of the Box */
100+
borderRadiusBottomRight?: BorderRadiusTokenScale;
101+
/** Top left border radius of the Box */
102+
borderRadiusTopLeft?: BorderRadiusTokenScale;
103+
/** Top right border radius of the Box */
104+
borderRadiusTopRight?: BorderRadiusTokenScale;
63105
/** Inner content of the Box */
64106
children: ReactNode;
65107
/** Spacing outside of the Box */
@@ -95,7 +137,16 @@ export const Box = forwardRef(
95137
{
96138
as: Component = 'div',
97139
background,
98-
border,
140+
border = '',
141+
borderBottom = '',
142+
borderLeft = '',
143+
borderRight = '',
144+
borderTop = '',
145+
borderRadius = '',
146+
borderRadiusBottomLeft = '',
147+
borderRadiusBottomRight = '',
148+
borderRadiusTopLeft = '',
149+
borderRadiusTopRight = '',
99150
children,
100151
margin = '',
101152
marginBottom = '',
@@ -111,6 +162,24 @@ export const Box = forwardRef(
111162
},
112163
ref,
113164
) => {
165+
const borders = {
166+
bottom: borderBottom ? borderBottom : border,
167+
left: borderLeft ? borderLeft : border,
168+
right: borderRight ? borderRight : border,
169+
top: borderTop ? borderTop : border,
170+
} as Border;
171+
172+
const borderRadiuses = {
173+
bottomLeft: borderRadiusBottomLeft
174+
? borderRadiusBottomLeft
175+
: borderRadius,
176+
bottomRight: borderRadiusBottomRight
177+
? borderRadiusBottomRight
178+
: borderRadius,
179+
topLeft: borderRadiusTopLeft ? borderRadiusTopLeft : borderRadius,
180+
topRight: borderRadiusTopRight ? borderRadiusTopRight : borderRadius,
181+
} as BorderRadius;
182+
114183
const margins = {
115184
bottom: marginBottom ? marginBottom : margin,
116185
left: marginLeft ? marginLeft : margin,
@@ -148,12 +217,35 @@ export const Box = forwardRef(
148217
'--pc-box-padding-top': paddings.top
149218
? `var(--p-space-${paddings.top})`
150219
: '',
220+
'--pc-box-border-bottom': borders.bottom
221+
? `var(--p-border-${borders.bottom})`
222+
: '',
223+
'--pc-box-border-left': borders.left
224+
? `var(--p-border-${borders.left})`
225+
: '',
226+
'--pc-box-border-right': borders.right
227+
? `var(--p-border-${borders.right})`
228+
: '',
229+
'--pc-box-border-top': borders.top
230+
? `var(--p-border-${borders.top})`
231+
: '',
232+
'--pc-box-border-radius-bottom-left': borderRadiuses.bottomLeft
233+
? `var(--p-border-${borderRadiuses.bottomLeft})`
234+
: '',
235+
'--pc-box-border-radius-bottom-right': borderRadiuses.bottomRight
236+
? `var(--p-border-${borderRadiuses.bottomRight})`
237+
: '',
238+
'--pc-box-border-radius-top-left': borderRadiuses.topLeft
239+
? `var(--p-border-${borderRadiuses.topLeft})`
240+
: '',
241+
'--pc-box-border-radius-top-right': borderRadiuses.topRight
242+
? `var(--p-border-${borderRadiuses.topRight})`
243+
: '',
151244
} as React.CSSProperties;
152245

153246
const className = classNames(
154247
styles.root,
155248
background && styles[background],
156-
border && styles[border],
157249
shadow && styles[shadow],
158250
);
159251

0 commit comments

Comments
 (0)