-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Box.tsx
346 lines (334 loc) · 10.7 KB
/
Box.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
import React, {forwardRef} from 'react';
import type {
ColorTextAlias,
ColorBackgroundAlias,
ColorBorderAlias,
BorderWidthScale,
BorderRadiusAliasOrScale,
ShadowAliasOrScale,
SpaceScale,
} from '@shopify/polaris-tokens';
import {
getResponsiveProps,
classNames,
sanitizeCustomProperties,
} from '../../utilities/css';
import type {ResponsiveProp} from '../../utilities/css';
import styles from './Box.module.css';
export type Element = 'div' | 'span' | 'section' | 'legend' | 'ul' | 'li';
type LineStyles = 'solid' | 'dashed';
type Overflow = 'hidden' | 'scroll' | 'clip';
type Position = 'relative' | 'absolute' | 'fixed' | 'sticky';
type Spacing = ResponsiveProp<SpaceScale>;
export interface BoxProps extends React.AriaAttributes {
children?: React.ReactNode;
/** HTML Element type
* @default 'div'
*/
as?: Element;
/** Background color */
background?: ColorBackgroundAlias;
/** Border color */
borderColor?: ColorBorderAlias | 'transparent';
/** Border style */
borderStyle?: LineStyles;
/** Border radius */
borderRadius?: BorderRadiusAliasOrScale;
/** Vertical end horizontal start border radius */
borderEndStartRadius?: BorderRadiusAliasOrScale;
/** Vertical end horizontal end border radius */
borderEndEndRadius?: BorderRadiusAliasOrScale;
/** Vertical start horizontal start border radius */
borderStartStartRadius?: BorderRadiusAliasOrScale;
/** Vertical start horizontal end border radius */
borderStartEndRadius?: BorderRadiusAliasOrScale;
/** Border width */
borderWidth?: BorderWidthScale;
/** Vertical start border width */
borderBlockStartWidth?: BorderWidthScale;
/** Vertical end border width */
borderBlockEndWidth?: BorderWidthScale;
/** Horizontal start border width */
borderInlineStartWidth?: BorderWidthScale;
/** Horizontal end border width */
borderInlineEndWidth?: BorderWidthScale;
/** Color of children */
color?: ColorTextAlias;
/** HTML id attribute */
id?: string;
/** Minimum height of container */
minHeight?: string;
/** Minimum width of container */
minWidth?: string;
/** Maximum width of container */
maxWidth?: string;
/** Clip horizontal content of children */
overflowX?: Overflow;
/** Clip vertical content of children */
overflowY?: Overflow;
/** Spacing around children. Accepts a spacing token or an object of spacing tokens for different screen sizes.
* @example
* padding='400'
* padding={{xs: '200', sm: '300', md: '400', lg: '500', xl: '600'}}
*/
padding?: Spacing;
/** Vertical start and end spacing around children. Accepts a spacing token or an object of spacing tokens for different screen sizes.
* @example
* paddingBlock='400'
* paddingBlock={{xs: '200', sm: '300', md: '400', lg: '500', xl: '600'}}
*/
paddingBlock?: Spacing;
/** Vertical start spacing around children. Accepts a spacing token or an object of spacing tokens for different screen sizes.
* @example
* paddingBlockStart='400'
* paddingBlockStart={{xs: '200', sm: '300', md: '400', lg: '500', xl: '600'}}
*/
paddingBlockStart?: Spacing;
/** Vertical end spacing around children. Accepts a spacing token or an object of spacing tokens for different screen sizes.
* @example
* paddingBlockEnd='400'
* paddingBlockEnd={{xs: '200', sm: '300', md: '400', lg: '500', xl: '600'}}
*/
paddingBlockEnd?: Spacing;
/** Horizontal start and end spacing around children. Accepts a spacing token or an object of spacing tokens for different screen sizes.
* @example
* paddingInline='400'
* paddingInline={{xs: '200', sm: '300', md: '400', lg: '500', xl: '600'}}
*/
paddingInline?: Spacing;
/** Horizontal start spacing around children. Accepts a spacing token or an object of spacing tokens for different screen sizes.
* @example
* paddingInlineStart='400'
* paddingInlineStart={{xs: '200', sm: '300', md: '400', lg: '500', xl: '600'}}
*/
paddingInlineStart?: Spacing;
/** Horizontal end spacing around children. Accepts a spacing token or an object of spacing tokens for different screen sizes.
* @example
* paddingInlineEnd='400'
* paddingInlineEnd={{xs: '200', sm: '300', md: '400', lg: '500', xl: '600'}}
*/
paddingInlineEnd?: Spacing;
/** Aria role */
role?: Extract<
React.AriaRole,
'status' | 'presentation' | 'menu' | 'listbox' | 'combobox' | 'group'
>;
/** Shadow on box */
shadow?: ShadowAliasOrScale;
/** Set tab order */
tabIndex?: Extract<React.AllHTMLAttributes<HTMLElement>['tabIndex'], number>;
/** Width of container */
width?: string;
// These could be moved to new layout component(s) in the future
/** Position of box */
position?: Position;
/** Top position of box */
insetBlockStart?: Spacing;
/** Bottom position of box */
insetBlockEnd?: Spacing;
/** Left position of box */
insetInlineStart?: Spacing;
/** Right position of box */
insetInlineEnd?: Spacing;
/** Opacity of box */
opacity?: string;
/** Outline color */
outlineColor?: ColorBorderAlias;
/** Outline style */
outlineStyle?: LineStyles;
/** Outline width */
outlineWidth?: BorderWidthScale;
/** Visually hide the contents during print */
printHidden?: boolean;
/** Visually hide the contents (still announced by screenreader) */
visuallyHidden?: boolean;
/** z-index of box */
zIndex?: string;
}
export const Box = forwardRef<HTMLElement, BoxProps>(
(
{
as = 'div',
background,
borderColor,
borderStyle,
borderWidth,
borderBlockStartWidth,
borderBlockEndWidth,
borderInlineStartWidth,
borderInlineEndWidth,
borderRadius,
borderEndStartRadius,
borderEndEndRadius,
borderStartStartRadius,
borderStartEndRadius,
children,
color,
id,
minHeight,
minWidth,
maxWidth,
overflowX,
overflowY,
outlineColor,
outlineStyle,
outlineWidth,
padding,
paddingBlock,
paddingBlockStart,
paddingBlockEnd,
paddingInline,
paddingInlineStart,
paddingInlineEnd,
role,
shadow,
tabIndex,
width,
printHidden,
visuallyHidden,
position,
insetBlockStart,
insetBlockEnd,
insetInlineStart,
insetInlineEnd,
zIndex,
opacity,
...restProps
},
ref,
) => {
// eslint-disable-next-line no-nested-ternary
const borderStyleValue = borderStyle
? borderStyle
: borderColor ||
borderWidth ||
borderBlockStartWidth ||
borderBlockEndWidth ||
borderInlineStartWidth ||
borderInlineEndWidth
? 'solid'
: undefined;
// eslint-disable-next-line no-nested-ternary
const outlineStyleValue = outlineStyle
? outlineStyle
: outlineColor || outlineWidth
? 'solid'
: undefined;
const style = {
'--pc-box-color': color ? `var(--p-color-${color})` : undefined,
'--pc-box-background': background
? `var(--p-color-${background})`
: undefined,
// eslint-disable-next-line no-nested-ternary
'--pc-box-border-color': borderColor
? borderColor === 'transparent'
? 'transparent'
: `var(--p-color-${borderColor})`
: undefined,
'--pc-box-border-style': borderStyleValue,
'--pc-box-border-radius': borderRadius
? `var(--p-border-radius-${borderRadius})`
: undefined,
'--pc-box-border-end-start-radius': borderEndStartRadius
? `var(--p-border-radius-${borderEndStartRadius})`
: undefined,
'--pc-box-border-end-end-radius': borderEndEndRadius
? `var(--p-border-radius-${borderEndEndRadius})`
: undefined,
'--pc-box-border-start-start-radius': borderStartStartRadius
? `var(--p-border-radius-${borderStartStartRadius})`
: undefined,
'--pc-box-border-start-end-radius': borderStartEndRadius
? `var(--p-border-radius-${borderStartEndRadius})`
: undefined,
'--pc-box-border-width': borderWidth
? `var(--p-border-width-${borderWidth})`
: undefined,
'--pc-box-border-block-start-width': borderBlockStartWidth
? `var(--p-border-width-${borderBlockStartWidth})`
: undefined,
'--pc-box-border-block-end-width': borderBlockEndWidth
? `var(--p-border-width-${borderBlockEndWidth})`
: undefined,
'--pc-box-border-inline-start-width': borderInlineStartWidth
? `var(--p-border-width-${borderInlineStartWidth})`
: undefined,
'--pc-box-border-inline-end-width': borderInlineEndWidth
? `var(--p-border-width-${borderInlineEndWidth})`
: undefined,
'--pc-box-min-height': minHeight,
'--pc-box-min-width': minWidth,
'--pc-box-max-width': maxWidth,
'--pc-box-outline-color': outlineColor
? `var(--p-color-${outlineColor})`
: undefined,
'--pc-box-outline-style': outlineStyleValue,
'--pc-box-outline-width': outlineWidth
? `var(--p-border-width-${outlineWidth})`
: undefined,
'--pc-box-overflow-x': overflowX,
'--pc-box-overflow-y': overflowY,
...getResponsiveProps(
'box',
'padding-block-start',
'space',
paddingBlockStart || paddingBlock || padding,
),
...getResponsiveProps(
'box',
'padding-block-end',
'space',
paddingBlockEnd || paddingBlock || padding,
),
...getResponsiveProps(
'box',
'padding-inline-start',
'space',
paddingInlineStart || paddingInline || padding,
),
...getResponsiveProps(
'box',
'padding-inline-end',
'space',
paddingInlineEnd || paddingInline || padding,
),
'--pc-box-shadow': shadow ? `var(--p-shadow-${shadow})` : undefined,
'--pc-box-width': width,
position,
'--pc-box-inset-block-start': insetBlockStart
? `var(--p-space-${insetBlockStart})`
: undefined,
'--pc-box-inset-block-end': insetBlockEnd
? `var(--p-space-${insetBlockEnd})`
: undefined,
'--pc-box-inset-inline-start': insetInlineStart
? `var(--p-space-${insetInlineStart})`
: undefined,
'--pc-box-inset-inline-end': insetInlineEnd
? `var(--p-space-${insetInlineEnd})`
: undefined,
zIndex,
opacity,
} as React.CSSProperties;
const className = classNames(
styles.Box,
visuallyHidden && styles.visuallyHidden,
printHidden && styles.printHidden,
as === 'ul' && styles.listReset,
);
return React.createElement(
as,
{
className,
id,
ref,
style: sanitizeCustomProperties(style),
role,
tabIndex,
...restProps,
},
children,
);
},
);
Box.displayName = 'Box';