-
Notifications
You must be signed in to change notification settings - Fork 9
/
Chip.tsx
174 lines (165 loc) · 4.28 KB
/
Chip.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
import React, { forwardRef, useMemo } from "react";
import classnames from "classnames";
import Typography from "../Typography/Typography";
import Box from "../Box/Box";
import styles from "./Chip.module.css";
import useIsHydrated from "../useIsHydrated";
import { useTheme } from "../ThemeProvider/ThemeProvider";
function typographyColor({
themeName,
selected,
on,
}: {
themeName: "cambio" | "classic";
selected: boolean;
on: "lightBackground" | "darkBackground";
}): "white" | "gray900" {
if (themeName === "cambio" && on === "darkBackground") {
if (selected) {
return "gray900";
} else {
return "white";
}
} else {
if (selected) {
return "white";
} else {
return "gray900";
}
}
}
type ChipProps = {
/**
* If true, the chip will be disabled.
*/
disabled?: boolean;
/**
* Sets the initial status of this chip component.
* * `true` will display a grey chip.
* * `false` will display a premium800 color chip.
*
* @defaultValue "false"
*/
selected?: boolean;
/**
* Test id for the button
*/
"data-testid"?: string;
/**
* Size of the chip.
*
* Classic:
* * `sm`: 32px
* * `lg`: 48px
*
* Cambio:
* * `sm`: 32px
*
* @defaultValue sm
*/
size?: "sm" | "lg";
/**
* The text to be displayed on the chip
*/
text: string;
/**
* Indicate whether the badge renders on a light or dark background. Changes the color of the chip (Cambio only)
*
* @defaulValue `lightBackground`
*/
on?: "lightBackground" | "darkBackground";
/**
* The callback to be called when the chip is clicked
*/
onChange: React.MouseEventHandler<HTMLButtonElement>;
/**
* The icon to be displayed.
*/
icon?: React.ComponentType<{ className?: string }>;
/** forces focus ring styling */
dangerouslyForceFocusStyles?: boolean;
};
/**
* [Chip](https://cambly-syntax.vercel.app/?path=/docs/components-chip--docs) is used to show status (selected/not selected) like a toggle button.
*/
const Chip = forwardRef<HTMLButtonElement, ChipProps>(
(
{
disabled: disabledProp = false,
selected = false,
"data-testid": dataTestId,
size = "sm",
text,
on = "lightBackground",
onChange,
icon: Icon,
dangerouslyForceFocusStyles,
}: ChipProps,
ref,
) => {
const { themeName } = useTheme();
const transformedSize = themeName === "cambio" ? "sm" : size;
const isHydrated = useIsHydrated();
const disabled = !isHydrated || disabledProp;
const selectedChipCambioStyle =
on === "lightBackground"
? styles.selectedChipCambio
: styles.selectedChipCambioOnDarkBackground;
const deselectedChipCambioStyle =
on === "lightBackground"
? styles.deselectedChipCambio
: styles.deselectedChipCambioOnDarkBackground;
const chipStyles = classnames(
styles.chip,
themeName === "classic" ? styles.chipClassic : styles.chipCambio,
styles[transformedSize],
{
[themeName === "classic"
? styles.selectedChip
: selectedChipCambioStyle]: selected,
[themeName === "classic"
? styles.deselectedChip
: deselectedChipCambioStyle]: !selected,
[styles.disabled]: disabled,
[styles.forceFocus]: dangerouslyForceFocusStyles,
},
);
const iconStyles = classnames(styles.icon, {
[styles.selectedIcon]: selected,
});
const typographySize = {
["sm"]: 200,
["lg"]: 300,
} as const;
const color = useMemo(
() => typographyColor({ themeName, selected, on }),
[themeName, selected, on],
);
return (
<button
className={chipStyles}
disabled={disabled}
data-testid={dataTestId}
ref={ref}
type="button"
aria-pressed={selected}
onClick={onChange}
>
{Icon && <Icon className={iconStyles} />}
<Box paddingX={Icon ? 1 : 0}>
<Typography
size={
themeName === "classic" ? typographySize[transformedSize] : 100
}
color={color}
weight={themeName === "classic" ? "regular" : "medium"}
>
{text}
</Typography>
</Box>
</button>
);
},
);
Chip.displayName = "Chip";
export default Chip;