-
Notifications
You must be signed in to change notification settings - Fork 9
/
IconButton.tsx
172 lines (163 loc) · 4.2 KB
/
IconButton.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
import classNames from "classnames";
import {
classicForegroundColor,
cambioForegroundColor,
} from "../colors/foregroundColor";
import React, { forwardRef } from "react";
import { type Size } from "../constants";
import styles from "./IconButton.module.css";
import useIsHydrated from "../useIsHydrated";
import { useTheme } from "../ThemeProvider/ThemeProvider";
import { classicColor, cambioColor } from "../Button/constants/color";
import buttonStyles from "../Button/Button.module.css";
import {
classicBackgroundColor,
cambioBackgroundColor,
} from "../colors/backgroundColor";
const classicIconSize = {
sm: styles.smIcon,
md: styles.mdIcon,
lg: styles.lgIcon,
};
const cambioIconSize = {
sm: styles.smIconCambio,
md: styles.mdIconCambio,
lg: styles.lgIconCambio,
xl: styles.xlIconCambio,
};
type IconButtonProps = {
/**
* The color of the button
*
* Classic only:
* * `inverse`
* * `success`
*
* Cambio only:
* * `success-primary`
* * `success-secondary`
* * `success-tertiary`
*
* @defaultValue "primary"
*/
color?:
| "primary"
| "secondary"
| "tertiary"
| "destructive-primary"
| "destructive-secondary"
| "destructive-tertiary"
| "branded"
| "success"
| "success-primary"
| "success-secondary"
| "inverse";
/**
* Test id for the button
*/
"data-testid"?: string;
/**
* The size of the button
*
* Classic:
* * `sm`: 32px
* * `md`: 40px
* * `lg`: 48px
*
* Cambio:
* * `sm`: 32px
* * `md`: 48px
* * `lg`: 64px
*
* @defaultValue "md"
*/
size?: (typeof Size)[number];
/**
* The label to be used for accessibility
*/
accessibilityLabel: string;
/**
* The icon to be displayed. Please use a [Rounded Material Icon](https://material.io/resources/icons/?style=round)
*/
icon: React.ComponentType<{ className?: string }>;
/**
* If `true`, the button will be disabled
*
* @defaultValue false
*/
disabled?: boolean;
/**
* The callback to be called when the button is clicked
*/
onClick?: React.MouseEventHandler<HTMLButtonElement>;
/**
* The tooltip to be displayed when the user hovers over the button
*/
tooltip?: string;
};
/**
* [IconButton](https://cambly-syntax.vercel.app/?path=/docs/components-iconbutton--docs) is a clickable element that is used to perform an action.
*/
const IconButton = forwardRef<HTMLButtonElement, IconButtonProps>(
(
{
accessibilityLabel,
color = "primary",
"data-testid": dataTestId,
disabled = false,
icon: Icon,
size = "md",
tooltip,
onClick,
}: IconButtonProps,
ref,
) => {
const isHydrated = useIsHydrated();
const { themeName } = useTheme();
const foregroundColorClass =
themeName === "classic"
? classicForegroundColor(classicColor(color))
: cambioForegroundColor(cambioColor(color));
const backgroundColorClass =
themeName === "classic"
? classicBackgroundColor(classicColor(color))
: cambioBackgroundColor(cambioColor(color));
return (
<button
aria-label={accessibilityLabel}
data-testid={dataTestId}
type="button"
title={tooltip}
disabled={!isHydrated || disabled}
onClick={onClick}
className={classNames(
styles.iconButton,
foregroundColorClass,
backgroundColorClass,
themeName === "classic" ? styles[size] : styles[`${size}Cambio`],
{
[buttonStyles.secondaryBorder]:
themeName === "classic" && color === "secondary",
[buttonStyles.secondaryDestructiveBorder]:
themeName === "classic" && color === "destructive-secondary",
[styles.iconButtonNoBorder]:
(themeName === "classic" &&
!["secondary", "destructive-secondary"].includes(color)) ||
themeName === "cambio",
},
)}
ref={ref}
>
<Icon
className={
themeName === "classic"
? classicIconSize[size]
: cambioIconSize[size]
}
/>
</button>
);
},
);
IconButton.displayName = "IconButton";
export default IconButton;