-
Notifications
You must be signed in to change notification settings - Fork 9
/
Box.tsx
653 lines (643 loc) · 16.3 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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
import classNames from "classnames";
import { type AriaRole, type ReactElement, type ReactNode } from "react";
import styles from "./Box.module.css";
import marginStyles from "./margin.module.css";
import paddingStyles from "./padding.module.css";
import type allColors from "../colors/allColors";
import colorStyles from "../colors/colors.module.css";
import roundingStyles from "../rounding.module.css";
import { forwardRef } from "react";
import { useTheme } from "../ThemeProvider/ThemeProvider";
type AlignItems = "baseline" | "center" | "end" | "start" | "stretch";
type As =
| "article"
| "aside"
| "caption"
| "div"
| "figcaption"
| "figure"
| "footer"
| "header"
| "main"
| "nav"
| "section"
| "summary";
type Dimension = number | string;
type Direction = "row" | "column";
type Display =
| "none"
| "flex"
| "block"
| "inlineBlock"
| "inlineFlex"
| "visuallyHidden";
type Gap = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12;
type JustifyContent =
| "start"
| "end"
| "center"
| "between"
| "around"
| "evenly";
type Margin =
| -12
| -11
| -10
| -9
| -8
| -7
| -6
| -5
| -4
| -3
| -2
| -1
| 0
| 1
| 2
| 3
| 4
| 5
| 6
| 7
| 8
| 9
| 10
| 11
| 12
| "auto";
type Padding = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12;
type Overflow =
| "visible"
| "hidden"
| "scroll"
| "auto"
| "inherit"
| "initial";
type BoxProps = {
/**
* The alignment of the box on the cross axis.
*
* Responsive props:
* * `smAlignItems`
* * `lgAlignItems`
*/
alignItems?: AlignItems;
/**
* Allows the default alignment (or the one specified by align-items) to be overridden for individual flex items.
*/
alignSelf?: "auto" | "start" | "end" | "center" | "baseline" | "stretch";
/**
* The underlying DOM element usually set for accessibility or SEO reasons.
*
* @defaultValue `div`
*/
as?: As;
/**
* The background color of the box.
*/
backgroundColor?: (typeof allColors)[number];
/**
* The children to be rendered inside the box.
*/
children?: ReactNode;
/**
* An "escape hatch" used to apply styles not otherwise available on Box.
*
* Please use this sparingly and only when you have a good reason to.
*/
dangerouslySetInlineStyle?: {
__style: Record<string, string | number | null>;
};
/**
* The flex direction of the box.
*
* Responsive props:
* * `smDirection`
* * `lgDirection`
*
* @defaultValue `row`
*/
direction?: Direction;
/**
* The display property specifies the display behavior (the type of rendering box) of an element.
*
* Responsive props:
* * `smDisplay`
* * `lgDisplay`
*
* @defaultValue `block`
*/
display?: Display;
/**
* Sets the flex behavior of a flex item.
*
* * `none`: The item will not grow or shrink
* * `shrink`: The item will shrink if necessary (default browser behavior)
* * `grow`: The item will grow if necessary
*
* @defaultValue `shrink`
*/
flex?: "none" | "shrink" | "grow";
/**
* By default, flex items will all try to fit onto one line. But if you specify `flexWrap="wrap"`, the flex items will wrap onto multiple lines.
*
* @defaultValue `nowrap`
*/
flexWrap?: "wrap" | "nowrap";
/**
* The gap between the children of the box.
*/
gap?: Gap;
/**
* The id of the element.
*/
id?: string;
/**
* The alignment of the box on the cross axis on lg (960px) or larger viewports.
*/
lgAlignItems?: AlignItems;
/**
* The flex direction on lg (960px) or larger viewports.
*/
lgDirection?: Direction;
/**
* The display style on lg (960px) or larger viewports.
*/
lgDisplay?: Display;
/**
* The alignment of the box on the cross axis on lg (960px) or larger viewports.
*/
lgJustifyContent?: JustifyContent;
/**
* Margin on lg (960px) or larger viewports.
*/
lgMargin?: Margin;
/**
* Bottom margin on lg (960px) or larger viewports.
*/
lgMarginBottom?: Margin;
/**
* Margin to the right in left-to-right languages, and to the left in right-to-left languages on lg (960px) or larger viewports.
*/
lgMarginEnd?: Margin;
/**
* Margin to the left in left-to-right languages, and to the right in right-to-left languages on lg (960px) or larger viewports.
*/
lgMarginStart?: Margin;
/**
* Top margin on lg (960px) or larger viewports.
*/
lgMarginTop?: Margin;
/**
* The padding of the box on lg (960px) or larger viewports.
*/
lgPadding?: Padding;
/**
* The padding of the box on the x-axis on lg (960px) or larger viewports.
*/
lgPaddingX?: Padding;
/**
* The padding of the box on the y-axis on lg (960px) or larger viewports.
*/
lgPaddingY?: Padding;
/**
* The margin of the box.
*
* Responsive props:
* * `smMargin`
* * `lgMargin`
*
* @defaultValue 0
*/
margin?: Margin;
/**
* Bottom margin of the box.
*
* Responsive props:
* * `smMarginBottom`
* * `lgMarginBottom`
*
*/
marginBottom?: Margin;
/**
* Margin to the right in left-to-right languages, and to the left in right-to-left languages.
*
* Responsive props:
* * `smMarginEnd`
* * `lgMarginEnd`
*
*/
marginEnd?: Margin;
/**
* Margin to the left in left-to-right languages, and to the right in right-to-left languages.
*
* Responsive props:
* * `smMarginStart`
* * `lgMarginStart`
*
*/
marginStart?: Margin;
/**
* Top margin of the box.
*
* Responsive props:
* * `smMarginTop`
* * `lgMarginTop`
*
*/
marginTop?: Margin;
/**
* The maximum height of the box.
*/
maxHeight?: Dimension;
/**
* The maximum width of the box.
*/
maxWidth?: Dimension;
/**
* The minimum height of the box.
*/
minHeight?: Dimension;
/**
* The minimum width of the box.
*/
minWidth?: Dimension;
/**
* The height of the box.
*/
height?: Dimension;
/**
* The alignment of the box on the main axis.
*
* Responsive props:
* * `smJustifyContent`
* * `lgJustifyContent`
*
* @defaultValue "start"
*/
justifyContent?: JustifyContent;
/**
* How box behaves when content does not fit in the box on both axes.
*
* * `visible`
* * `hidden`
* * `scroll`
* * `auto`
* * `inherit`
* * `initial`
*
* @defaultValue "visible"
*/
overflow?: Overflow;
/**
* How box behaves when content does not fit in the box on the X axis.
*
* * `visible`
* * `hidden`
* * `scroll`
* * `auto`
* * `inherit`
* * `initial`
*
* @defaultValue "visible"
*/
overflowX?: Overflow;
/**
* How box behaves when content does not fit in the box on the Y axis.
*
* * `visible`
* * `hidden`
* * `scroll`
* * `auto`
* * `inherit`
* * `initial`
*
* @defaultValue "visible"
*/
overflowY?: Overflow;
/**
* The padding of the box.
*
* Responsive props:
* * `smPadding`
* * `lgPadding`
*
* @defaultValue 0
*/
padding?: Padding;
/**
* The padding of the box on the x-axis.
*
* Responsive props:
* * `smPaddingX`
* * `lgPaddingX`
*
*/
paddingX?: Padding;
/**
* The padding of the box on the y-axis.
*
* Responsive props:
* * `smPaddingY`
* * `lgPaddingY`
*/
paddingY?: Padding;
/**
* The position of the box.
*
* @defaultValue "static"
*/
position?: "absolute" | "fixed" | "relative" | "static" | "sticky";
/**
* The role attribute of the box.
*
* See [MDN](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Roles) for the list of valid roles.
*/
role?: AriaRole;
/**
* Border radius of the box.
*
* Classic:
* * `none`: 0px
* * `sm`: 8px
* * `md`: 12px
* * `lg`: 16px
* * `xl`: 24px
* * `full`: 999px
*
* Cambio:
* * `none`: 0px
* * `sm`: 4px
* * `md`: 8px
* * `lg`: 8px (maps to `md`)
* * `xl`: 8px (maps to `md`)
* * `full`: 999px
*
* @defaultValue "none"
*/
rounding?: "xl" | "lg" | "md" | "sm" | "full" | "none";
/**
* The alignment of the box on the cross axis on sm (480px) or larger viewports.
*/
smAlignItems?: AlignItems;
/**
* The flex direction on sm (480px) or larger viewports.
*/
smDirection?: Direction;
/**
* The display style on sm (480px) or larger viewports.
*/
smDisplay?: Display;
/**
* The alignment of the box on the main axis on sm (480px) or larger viewports.
*/
smJustifyContent?: JustifyContent;
/**
* Margin on sm (480px) or larger viewports.
*/
smMargin?: Margin;
/**
* Bottom margin on sm (480px) or larger viewports.
*/
smMarginBottom?: Margin;
/**
* Margin to the right in left-to-right languages, and to the left in right-to-left languages on sm (480px) or larger viewports.
*/
smMarginEnd?: Margin;
/**
* Margin to the left in left-to-right languages, and to the right in right-to-left languages on sm (480px) or larger viewports.
*/
smMarginStart?: Margin;
/**
* Top margin on sm (480px) or larger viewports.
*/
smMarginTop?: Margin;
/**
* The padding of the box on sm (480px) or larger viewports.
*/
smPadding?: Padding;
/**
* The padding of the box on the x-axis on sm (480px) or larger viewports.
*/
smPaddingX?: Padding;
/**
* The padding of the box on the y-axis on sm (480px) or larger viewports.
*/
smPaddingY?: Padding;
/**
* The width of the box.
*/
width?: Dimension;
};
function roundingCambio(
rounding: "sm" | "md" | "lg" | "xl" | "full",
): "sm" | "md" | "full" {
if (rounding === "lg" || rounding === "xl") {
return "md";
}
return rounding;
}
/**
* [Box](https://cambly-syntax.vercel.app/?path=/docs/components-box--docs) is primitive design component and is used by lots of other components. It keeps details like spacing, borders and colors consistent across all of Syntax.
*
* Passthrough props:
* * `aria-*`
* * `data-testid`
*/
const Box = forwardRef<HTMLDivElement, BoxProps>(function Box(
props: BoxProps,
ref,
): ReactElement {
const { as: BoxElement = "div", children, ...boxProps } = props;
const { themeName } = useTheme();
const {
// Classname
alignItems,
smAlignItems,
lgAlignItems,
alignSelf,
backgroundColor,
direction,
smDirection,
lgDirection,
display,
smDisplay,
lgDisplay,
flex,
flexWrap,
gap,
justifyContent,
smJustifyContent,
lgJustifyContent,
// Margin
margin,
marginBottom,
marginEnd,
marginStart,
marginTop,
smMargin,
smMarginBottom,
smMarginEnd,
smMarginStart,
smMarginTop,
lgMargin,
lgMarginBottom,
lgMarginEnd,
lgMarginStart,
lgMarginTop,
// Overflow
overflow,
overflowX,
overflowY,
// Padding
padding,
paddingX,
paddingY,
smPadding,
smPaddingX,
smPaddingY,
lgPadding,
lgPaddingX,
lgPaddingY,
position,
rounding,
// Style
dangerouslySetInlineStyle,
height,
maxHeight,
maxWidth,
minHeight,
minWidth,
width,
...maybePassThroughProps
} = boxProps;
const classicRoundingStyle =
themeName === "classic" && rounding && rounding !== "none"
? roundingStyles[`rounding${rounding}`]
: undefined;
const cambioRoundingStyles =
themeName === "cambio" && rounding && rounding !== "none"
? roundingStyles[`rounding${roundingCambio(rounding)}Cambio`]
: undefined;
const parsedProps = {
className: classNames(
styles.box,
alignItems && styles[`alignItems${alignItems}`],
smAlignItems && styles[`alignItems${smAlignItems}Small`],
lgAlignItems && styles[`alignItems${lgAlignItems}Large`],
alignSelf && styles[`alignSelf${alignSelf}`],
backgroundColor && colorStyles[`${backgroundColor}BackgroundColor`],
direction && styles[direction],
smDirection && styles[`${smDirection}Small`],
lgDirection && styles[`${lgDirection}Large`],
display && styles[display],
smDisplay && styles[`${smDisplay}Small`],
lgDisplay && styles[`${lgDisplay}Large`],
flex && (flex === "none" || flex === "grow") && styles[`flex${flex}`],
flexWrap && styles.flexWrap,
gap != null && styles[`gap${gap}`],
margin != null && !marginBottom && marginStyles[`marginBottom${margin}`],
margin != null && !marginEnd && marginStyles[`marginEnd${margin}`],
margin != null && !marginStart && marginStyles[`marginStart${margin}`],
margin != null && !marginTop && marginStyles[`marginTop${margin}`],
marginBottom != null && marginStyles[`marginBottom${marginBottom}`],
marginEnd != null && marginStyles[`marginEnd${marginEnd}`],
marginStart != null && marginStyles[`marginStart${marginStart}`],
marginTop != null && marginStyles[`marginTop${marginTop}`],
smMargin != null &&
!smMarginBottom &&
marginStyles[`marginBottom${smMargin}Small`],
smMargin != null &&
!smMarginEnd &&
marginStyles[`marginEnd${smMargin}Small`],
smMargin != null &&
!smMarginStart &&
marginStyles[`marginStart${smMargin}Small`],
smMargin != null &&
!smMarginTop &&
marginStyles[`marginTop${smMargin}Small`],
smMarginBottom != null &&
marginStyles[`marginBottom${smMarginBottom}Small`],
smMarginEnd != null && marginStyles[`marginEnd${smMarginEnd}Small`],
smMarginStart != null && marginStyles[`marginStart${smMarginStart}Small`],
smMarginTop != null && marginStyles[`marginTop${smMarginTop}Small`],
lgMargin != null &&
!lgMarginBottom &&
marginStyles[`marginBottom${lgMargin}Large`],
lgMargin != null &&
!lgMarginEnd &&
marginStyles[`marginEnd${lgMargin}Large`],
lgMargin != null &&
!lgMarginStart &&
marginStyles[`marginStart${lgMargin}Large`],
lgMargin != null &&
!lgMarginTop &&
marginStyles[`marginTop${lgMargin}Large`],
lgMarginBottom != null &&
marginStyles[`marginBottom${lgMarginBottom}Large`],
lgMarginEnd != null && marginStyles[`marginEnd${lgMarginEnd}Large`],
lgMarginStart != null && marginStyles[`marginStart${lgMarginStart}Large`],
lgMarginTop != null && marginStyles[`marginTop${lgMarginTop}Large`],
padding != null && !paddingX && paddingStyles[`paddingX${padding}`],
padding != null && !paddingY && paddingStyles[`paddingY${padding}`],
paddingX != null && paddingStyles[`paddingX${paddingX}`],
paddingY != null && paddingStyles[`paddingY${paddingY}`],
smPadding != null &&
!smPaddingX &&
paddingStyles[`paddingX${smPadding}Small`],
smPadding != null &&
!smPaddingY &&
paddingStyles[`paddingY${smPadding}Small`],
smPaddingX != null && paddingStyles[`paddingX${smPaddingX}Small`],
smPaddingY != null && paddingStyles[`paddingY${smPaddingY}Small`],
lgPadding != null &&
!lgPaddingX &&
paddingStyles[`paddingX${lgPadding}Large`],
lgPadding != null &&
!lgPaddingX &&
paddingStyles[`paddingY${lgPadding}Large`],
lgPaddingX != null && paddingStyles[`paddingX${lgPaddingX}Large`],
lgPaddingY != null && paddingStyles[`paddingY${lgPaddingY}Large`],
justifyContent && styles[`justifyContent${justifyContent}`],
smJustifyContent && styles[`justifyContent${smJustifyContent}Small`],
lgJustifyContent && styles[`justifyContent${lgJustifyContent}Large`],
position && position !== "static" && styles[position],
classicRoundingStyle,
cambioRoundingStyles,
overflow && styles[`overflow${overflow}`],
overflowX && styles[`overflowX${overflowX}`],
overflowY && styles[`overflowY${overflowY}`],
),
style: {
height,
maxHeight,
maxWidth,
minHeight,
minWidth,
width,
...(dangerouslySetInlineStyle?.__style ?? {}),
},
};
const passthroughProps = Object.entries(maybePassThroughProps).reduce<
Record<string, number | string | undefined>
>((acc, [key]) => {
if (
key === "id" ||
key === "role" ||
key.startsWith("aria-") ||
key.startsWith("data-testid")
) {
// @ts-expect-error unsafe assignment
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
acc[key] = maybePassThroughProps[key];
}
return acc;
}, {});
return (
<BoxElement {...parsedProps} {...passthroughProps} ref={ref}>
{children}
</BoxElement>
);
});
export default Box;