-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathGridCol.tsx
87 lines (79 loc) · 2.53 KB
/
GridCol.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
import React, { ElementType } from 'react';
import classNames from 'classnames';
import {
BackgroundColor,
BackgroundColorDark,
ComponentWithoutRefAsPropParams,
SizeModifier,
} from './types';
export type GridColProps<As extends ElementType = any> =
ComponentWithoutRefAsPropParams<As> & {
/** Supported background colors */
background?: BackgroundColor;
/** Supported dark background colors */
backgroundDark?: BackgroundColorDark;
/** Center text content horizontally */
centerText?: boolean;
/** Center content vertically */
center?: boolean;
/** Size modifiers for small screen sizes */
sm?: SizeModifier;
/** Size modifiers for medium screen sizes */
md?: SizeModifier;
/** Size modifiers for large screen sizes */
lg?: SizeModifier;
};
const sizeClasses = (size: 'sm' | 'md' | 'lg', def?: SizeModifier) => {
if (def === undefined) {
return null;
} else if (typeof def === 'number' || typeof def === 'string') {
return `ffe-grid__col--${size}-${def}`;
}
return classNames({
[`ffe-grid__col--${size}-${def.cols}`]: def.cols || def.cols === 0,
[`ffe-grid__col--${size}-offset-${def.offset}`]:
def.offset || def.offset === 0,
});
};
const centerTextClass = (centerText?: boolean) =>
centerText ? 'ffe-grid__col--center-text' : null;
const centerClass = (center?: boolean) =>
center ? 'ffe-grid__col--center' : null;
const backgroundClass = (background?: BackgroundColor) =>
background ? `ffe-grid__col--bg-${background}` : null;
const backgroundDarkClass = (backgroundDark?: BackgroundColorDark) =>
backgroundDark ? `ffe-grid__col--bg-dark-${backgroundDark}` : null;
export const GridCol: React.FC<GridColProps> = ({
background,
backgroundDark,
className,
as: Comp = 'div',
centerText,
center,
children,
sm,
md,
lg,
...rest
}) => {
return (
<Comp
className={[
'ffe-grid__col',
className,
sizeClasses('lg', lg),
sizeClasses('md', md),
sizeClasses('sm', !sm && !lg && !md ? 12 : sm),
centerTextClass(centerText),
centerClass(center),
backgroundClass(background),
backgroundDarkClass(backgroundDark),
]
.filter(x => x)
.join(' ')}
{...rest}
>
{children}
</Comp>
);
};