-
Notifications
You must be signed in to change notification settings - Fork 9
/
Badge.tsx
175 lines (166 loc) · 4.17 KB
/
Badge.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
import Typography from "../Typography/Typography";
import Box from "../Box/Box";
import styles from "./Badge.module.css";
import { useTheme } from "../ThemeProvider/ThemeProvider";
const badgeColorClassic = [
"gray200",
"gray900",
"destructive700",
"orange700",
"yellow700",
"success700",
"primary700",
"purple700",
] as const;
const badgeColorCambio = [
"sky",
"success300",
"destructive300",
"orange",
"tan",
"gray370",
"gray870",
"lilac",
"thistle",
"pink",
] as const;
const badgeColorClassicToCambio = {
gray200: "gray370",
gray900: "gray870",
destructive700: "destructive300",
orange700: "orange",
yellow700: "tan",
success700: "success300",
primary700: "sky",
purple700: "lilac",
sky: "sky",
success300: "success300",
destructive300: "destructive300",
orange: "orange",
tan: "tan",
gray370: "gray370",
gray870: "gray870",
lilac: "lilac",
thistle: "thistle",
pink: "pink",
} as const;
const badgeColorCambioToClassic = {
gray200: "gray200",
gray900: "gray900",
destructive700: "destructive700",
orange700: "orange700",
yellow700: "yellow700",
success700: "success700",
primary700: "primary700",
purple700: "purple700",
sky: "primary700",
success300: "success700",
destructive300: "destructive700",
orange: "orange700",
tan: "yellow700",
gray370: "gray200",
gray870: "gray900",
lilac: "purple700",
thistle: "purple700",
pink: "purple700",
} as const;
const textColorForBackgroundColor = (
color: (typeof badgeColorClassic)[number] | (typeof badgeColorCambio)[number],
): "gray900" | "white" => {
switch (color) {
case "gray200":
case "yellow700":
return "gray900";
case "gray370":
case "destructive300":
case "orange":
case "tan":
case "success300":
case "sky":
case "thistle":
case "pink":
case "lilac":
return "gray900";
default:
return "white";
}
};
/**
* [Badge](https://cambly-syntax.vercel.app/?path=/docs/components-badge--docs) is a component to display short text and give additional context to features and other components.
*/
const Badge = ({
icon: Icon,
text,
color = "primary700",
}: {
/**
* The icon to be displayed. Please use a [Material Icon](https://material.io/resources/icons/)
*/
icon?: React.ComponentType<{ className?: string }>;
/**
* The text to display inside the badge
*/
text: string;
/**
* The color of the badge
*
* Classic colors:
* * `gray200` => maps to neutralLight in Cambio
* * `gray900` => maps to neutralDark in Cambio
* * `destructive700` => maps to destructive in Cambio
* * `orange700` => maps to orange in Cambio
* * `yellow700` => maps to tan in Cambio
* * `success700` => maps to success in Cambio
* * `primary700` => maps to sky in Cambio
* * `purple700` => maps to lilac in Cambio
*
* Cambio colors:
* * `sky`
* * `success300`
* * `destructive300`
* * `orange`
* * `tan`
* * `gray370`
* * `gray870`
* * `lilac`
* * `thistle`
* * `pink`
*
* @defaultValue "primary700"
*/
color?:
| (typeof badgeColorClassic)[number]
| (typeof badgeColorCambio)[number];
}): JSX.Element => {
const { themeName } = useTheme();
const mappedColor =
themeName === "cambio"
? badgeColorClassicToCambio[color]
: badgeColorCambioToClassic[color];
return (
<Box
display={themeName === "classic" ? "inlineBlock" : "inlineFlex"}
paddingX={themeName === "classic" ? 2 : 3}
paddingY={1}
rounding={themeName === "classic" ? "full" : "sm"}
backgroundColor={mappedColor}
alignItems="center"
justifyContent="center"
height={themeName === "cambio" ? 32 : undefined}
>
<Typography color={textColorForBackgroundColor(mappedColor)}>
<Box display="flex" gap={1} alignItems="center" justifyContent="start">
{Icon && <Icon className={styles.icon} />}
<Typography
color={textColorForBackgroundColor(mappedColor)}
size={100}
weight={themeName === "classic" ? "bold" : "medium"}
>
{text}
</Typography>
</Box>
</Typography>
</Box>
);
};
export default Badge;