-
Notifications
You must be signed in to change notification settings - Fork 9
/
LinkButton.tsx
207 lines (199 loc) · 5.55 KB
/
LinkButton.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
import { forwardRef, type HtmlHTMLAttributes } from "react";
import classNames from "classnames";
import React from "react";
import { type Size } from "../constants";
import Typography from "../Typography/Typography";
import buttonStyles from "../Button/Button.module.css";
import iconSize from "../Button/constants/iconSize";
import textVariant from "../Button/constants/textVariant";
import styles from "./LinkButton.module.css";
import { classicColor, cambioColor } from "../Button/constants/color";
import {
classicBackgroundColor,
cambioBackgroundColor,
} from "../colors//backgroundColor";
import {
classicForegroundColor,
cambioForegroundColor,
} from "../colors/foregroundColor";
import { useTheme } from "../ThemeProvider/ThemeProvider";
type LinkButtonProps = {
/**
* Test id for the button
*/
"data-testid"?: string;
/**
* The text to be displayed inside the button
*/
text: string;
/**
* The link that the LinkButton should route to.
*
*/
href?: string;
/**
* The target attribute specifies where to open the linked document.
*
*/
target?: "_blank" | "_self" | "_parent" | "_top";
/**
* The rel attribute specifies the relationship between the document and the link.
*
*/
rel?: HtmlHTMLAttributes<HTMLAnchorElement>["rel"];
/**
* 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"
| "success-tertiary"
| "inverse";
/**
* 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];
/**
* If `true`, the button will take up the full width of its container
*
* @defaultValue false
*/
fullWidth?: boolean;
/**
* The icon to be displayed at the start of the button. Please use a [Rounded Material Icon](https://material.io/resources/icons/?style=round)
* Note: startIcon is not supported in the Cambio theme
*/
startIcon?: React.ComponentType<{ className?: string }>;
/**
* The icon to be displayed at the end of the button. Please use a [Rounded Material Icon](https://material.io/resources/icons/?style=round)
* Note: endIcon is not supported in the Cambio theme
*/
endIcon?: React.ComponentType<{ className?: string }>;
/**
* An optional onClick event. This is used for certain wrapper's support (such as react-router-dom).
*/
onClick?: React.MouseEventHandler<HTMLAnchorElement>;
};
/**
* [LinkButton](https://cambly-syntax.vercel.app/?path=/docs/components-linkbutton--docs) is a "variation" of Button that should look identical to Button, but should be used to render links instead.
*/
const LinkButton = forwardRef<HTMLAnchorElement, LinkButtonProps>(
(
{
text,
href,
target,
rel,
"data-testid": dataTestId,
color = "primary",
size = "md",
fullWidth = false,
startIcon: StartIcon,
endIcon: EndIcon,
onClick,
}: LinkButtonProps,
ref,
) => {
const { themeName } = useTheme();
const foregroundColorClass =
themeName === "classic"
? classicForegroundColor(classicColor(color))
: cambioForegroundColor(cambioColor(color));
const backgroundColorClass =
themeName === "classic"
? classicBackgroundColor(classicColor(color))
: cambioBackgroundColor(cambioColor(color));
return (
<a
href={href}
data-testid={dataTestId}
target={target}
ref={ref}
rel={rel}
className={classNames(
styles.linkButton,
buttonStyles.button,
foregroundColorClass,
backgroundColorClass,
themeName === "classic"
? buttonStyles[size]
: buttonStyles[`${size}Cambio`],
{
[buttonStyles.fullWidth]: fullWidth,
[styles.fitContent]: !fullWidth,
[buttonStyles.buttonGap]:
themeName === "classic" && (size === "lg" || size === "md"),
[buttonStyles.secondaryBorder]:
themeName === "classic" && color === "secondary",
[buttonStyles.secondaryDestructiveBorder]:
themeName === "classic" && color === "destructive-secondary",
},
)}
onClick={onClick}
>
{StartIcon && themeName === "classic" && (
<StartIcon
className={classNames(
buttonStyles.icon,
iconSize[size],
foregroundColorClass,
)}
/>
)}
<Typography
size={themeName === "classic" ? textVariant[size] : textVariant[size]}
>
<span
// Temporary - until we have cambio colors on Typogrphay
className={foregroundColorClass}
style={{ fontWeight: 500 }}
>
{text}
</span>
</Typography>
{EndIcon && themeName === "classic" && (
<EndIcon
className={classNames(
buttonStyles.icon,
iconSize[size],
foregroundColorClass,
)}
/>
)}
</a>
);
},
);
LinkButton.displayName = "LinkButton";
export default LinkButton;