-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathButton.tsx
More file actions
411 lines (392 loc) · 15.1 KB
/
Copy pathButton.tsx
File metadata and controls
411 lines (392 loc) · 15.1 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
import React from 'react';
import {
Button as MuiButton,
ButtonProps as MuiButtonProps,
IconButton as MuiIconButton,
useTheme,
} from '@mui/material';
import { globalFocusShadow, colors, elevations } from '..';
import { isDarkColor } from 'utils';
import { IconParams, IconProp } from '@fortawesome/fontawesome-svg-core';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { RouterLinkRenderer } from '../Navigation/Link';
const buttonStyles = {
padding: '0 1.5rem',
fontWeight: 500,
fontSize: '16px',
};
type ButtonProps = {
children?: React.ReactNode;
color?: 'default' | 'danger' | 'warning' | 'success' | string;
onClick?: React.MouseEventHandler<HTMLButtonElement>;
size?: 'small' | 'medium' | 'large';
icon?: React.ReactNode;
iconPosition?: 'left' | 'right';
disabled?: boolean;
target?: React.HTMLAttributeAnchorTarget;
} & Omit<MuiButtonProps, 'size' | 'color' | 'variant'>; // Exclude conflicting props
const sizeMap = {
small: '40px',
medium: '48px',
large: '56px',
};
const getSecondaryButtonStyles = (color: ButtonProps['color'], isDarkMode: boolean) => {
const notificationColor = colors.notification[color as keyof typeof colors.notification]?.shade;
const resolvedColor = notificationColor ?? color;
const isCustom = color !== 'default';
const baseBackground = isDarkMode ? 'transparent' : 'white';
const textColor = isCustom ? resolvedColor : 'text.primary';
const darkTextColor = isCustom ? `color-mix(in srgb, ${textColor}, black 20%)` : textColor;
let borderColor = colors.surface.gray[80];
if (isCustom) {
borderColor = resolvedColor;
} else if (isDarkMode) {
borderColor = 'white';
}
let darkBorderColor = colors.surface.gray[100];
if (isCustom) {
darkBorderColor = `color-mix(in srgb, ${borderColor}, black 20%)`;
} else if (isDarkMode) {
darkBorderColor = 'white';
}
let darkBackgroundColor = '#F8F8F8';
if (!isCustom) {
const mixAmount = isDarkMode ? '10%' : '3%';
darkBackgroundColor = `color-mix(in srgb, ${baseBackground}, black ${mixAmount})`;
}
return {
baseBackground,
textColor,
darkTextColor,
borderColor,
darkBorderColor,
darkBackgroundColor,
};
};
export const PrimaryButton: React.FC<ButtonProps> = ({
children,
color = 'default',
onClick,
size = 'medium',
icon,
iconPosition = 'left',
disabled,
sx,
...buttonProps
}) => {
const theme = useTheme();
const isDarkMode = theme.palette.mode === 'dark';
const height: string = sizeMap[size];
if (color === 'default' && isDarkMode) {
color = '#ffffff';
}
const customColor = colors.button[color as keyof typeof colors.button]?.shade ?? color;
const bgColor = customColor;
const darkBgColor = `color-mix(in srgb, ${bgColor}, black 20%)`;
// Use inverted text color for dark backgrounds
const textColor = () => {
if (isDarkMode && color === '#ffffff') {
return colors.surface.blue[90];
}
if (isDarkColor(bgColor, 0.4)) {
return colors.type.inverted.primary;
}
return colors.type.regular.primary;
};
return (
<MuiButton
variant="contained"
onClick={onClick}
disabled={disabled}
startIcon={icon && iconPosition === 'left' ? icon : undefined}
endIcon={icon && iconPosition === 'right' ? icon : undefined}
sx={[
{
background: bgColor,
color: textColor(),
height: height,
'&:focus': {
backgroundColor: darkBgColor,
boxShadow: elevations.hover,
},
'&:focus-visible': {
backgroundColor: darkBgColor,
outline: `2px solid ${colors.focus.regular.outer}`,
boxShadow: [globalFocusShadow, elevations.hover].join(','),
},
'&:hover': {
backgroundColor: darkBgColor,
boxShadow: elevations.hover,
'&:focus-visible': {
boxShadow: [globalFocusShadow, elevations.hover].join(','),
},
},
'&:active': {
backgroundColor: darkBgColor,
boxShadow: elevations.pressed,
'&:focus-visible': {
boxShadow: [globalFocusShadow, elevations.pressed].join(','),
},
},
'&:disabled': {
backgroundColor: '#EDEBE9',
boxShadow: 'none',
color: colors.type.regular.disabled,
},
},
buttonStyles,
...(Array.isArray(sx) ? sx : [sx]),
]}
{...buttonProps}
>
{children}
</MuiButton>
);
};
export const SecondaryButton: React.FC<ButtonProps> = ({
children,
color = 'default',
onClick,
size = 'medium',
icon,
iconPosition = 'left',
disabled,
sx,
...buttonProps
}) => {
const height: string = sizeMap[size];
const theme = useTheme();
const isDarkMode = theme.palette.mode === 'dark';
const { baseBackground, textColor, darkTextColor, borderColor, darkBorderColor, darkBackgroundColor } =
getSecondaryButtonStyles(color, isDarkMode);
return (
<MuiButton
variant="outlined"
onClick={onClick}
disabled={disabled}
startIcon={icon && iconPosition === 'left' ? icon : undefined}
endIcon={icon && iconPosition === 'right' ? icon : undefined}
sx={[
{
...buttonStyles,
height: height,
background: baseBackground,
color: textColor,
boxShadow: elevations.default,
border: `1px solid ${borderColor}`,
'&:focus': {
backgroundColor: darkBackgroundColor,
boxShadow: elevations.hover,
color: darkTextColor,
border: `1px solid ${darkBorderColor}`,
},
'&:hover': {
backgroundColor: darkBackgroundColor,
boxShadow: elevations.hover,
color: darkTextColor,
border: `1px solid ${darkBorderColor}`,
'&:focus-visible': {
boxShadow: elevations.hover,
},
},
'&:active': {
backgroundColor: darkBackgroundColor,
boxShadow: elevations.pressed,
color: darkTextColor,
border: `1px solid ${darkBorderColor}`,
'&:focus-visible': {
boxShadow: elevations.pressed,
},
},
'&:focus-visible': {
backgroundColor: darkBackgroundColor,
outline: `2px solid ${colors.focus.regular.outer}`,
outlineOffset: '0px',
boxShadow: elevations.hover,
color: darkTextColor,
},
'&:disabled': {
backgroundColor: 'white',
boxShadow: 'none',
color: colors.type.regular.disabled,
border: `1px solid ${colors.type.regular.disabled}`,
},
},
...(Array.isArray(sx) ? sx : [sx]),
]}
{...buttonProps}
>
{children}
</MuiButton>
);
};
const TertiaryButton = ({
children,
color = 'default',
onClick,
size = 'medium',
icon,
iconPosition = 'left',
disabled,
sx,
...buttonProps
}: ButtonProps) => {
const height: string = sizeMap[size];
const colorobject = {
tint: color === 'default' ? 'white' : color,
shade: color === 'default' ? 'gray.110' : color,
};
const customColor =
(color !== 'default' && colors.notification[color as keyof typeof colors.notification]) || colorobject;
return (
<MuiButton
variant="text"
onClick={onClick}
disabled={disabled}
startIcon={icon && iconPosition === 'left' ? icon : undefined}
endIcon={icon && iconPosition === 'right' ? icon : undefined}
sx={[
{
...buttonStyles,
height: height,
bgcolor: 'transparent',
color: (theme) => (theme.palette.mode === 'dark' ? customColor.tint : customColor.shade),
boxShadow: (theme) =>
theme.palette.mode === 'dark' ? elevations.tertiaryDark : elevations.tertiary,
'&:focus, &:focus-visible, &:hover, &:active': {
bgcolor: (theme) =>
theme.palette.mode === 'dark'
? `color-mix(in srgb, transparent, #000000 5%)`
: `color-mix(in srgb, transparent, #F1F8FF 50%)`,
},
'&:active': {
boxShadow: (theme) =>
theme.palette.mode === 'dark' ? elevations.tertiaryDarkPressed : elevations.tertiaryPressed,
},
'&:focus-visible': {
outline: `2px solid ${colors.focus.regular.outer}`,
boxShadow: globalFocusShadow,
},
'&:disabled': {
color: colors.type.regular.disabled,
bgColor: 'transparent',
boxShadow: 'none',
},
},
...(Array.isArray(sx) ? sx : [sx]),
]}
{...buttonProps}
>
{children}
</MuiButton>
);
};
/**
* The main DEP-styled button component. Matches the DEP design system styling
* and provides a consistent interface for different button variants.
* @param {ButtonProps} props - Button properties including variant, children, onClick, etc.
* @param {('primary' | 'secondary' | 'tertiary')} props.variant - The variant of the button, can be 'primary', 'secondary', or 'tertiary'. Default is 'secondary'.
* @param {React.ReactNode} props.children - The content of the button, typically text or icons.
* @param {() => void} props.onClick - The function to call when the button is clicked.
* @param {'small' | 'medium' | 'large'} props.size - The size of the button, can be 'small' (40px), 'medium' (48px), or 'large' (56px). Default is 'medium'.
* @param {'default' | 'danger' | 'warning' | 'success' | string} props.color - The color of the button, can be 'default', 'danger', 'warning', 'success', or a custom color.
* @param {React.ReactNode} props.icon - An optional icon to display in the button.
* @param {'left' | 'right'} props.iconPosition - The position of the icon, can be 'left' or 'right'. Default is 'left'.
*
* @returns A button component that renders different styles based on the variant.
*/
export const Button = ({
variant = 'secondary',
...props
}: ButtonProps & {
variant?: 'primary' | 'secondary' | 'tertiary';
}) => {
props.LinkComponent = props.LinkComponent || RouterLinkRenderer;
switch (variant) {
case 'primary':
return <PrimaryButton {...props} />;
case 'secondary':
return <SecondaryButton {...props} />;
case 'tertiary':
return <TertiaryButton {...props} />;
default:
return <SecondaryButton {...props} />;
}
};
type IconButtonProps = {
icon: IconProp;
onClick?: React.MouseEventHandler<HTMLButtonElement>;
title?: string;
sx?: React.CSSProperties;
size?: string;
iconSize?: string;
color?: string;
backgroundColor?: string;
hoverBackgroundColor?: string;
disabled?: boolean;
iconProps?: IconParams;
};
/**
* IconButton component that renders a circular button with an icon.
* Provides full customization of size, colors, and positioning while maintaining
* consistent focus and hover behavior.
*
* @param {IconButtonProps} props - The properties for the icon button.
* @param {IconProp} props.icon - The icon to display in the button.
* @param {() => void} props.onClick - The function to call when the button is clicked.
* @param {string} props.title - The title for the button, used for accessibility.
* @param {React.CSSProperties} props.sx - Additional styles to apply to the button.
* @param {string} props.size - Button diameter (e.g., '40px', '3rem'). Default is '40px'.
* @param {string} props.iconSize - Icon font size (e.g., '20px', '1.5rem'). Default is '20px'.
* @param {string} props.color - Icon color. Default is '#494949'.
* @param {string} props.backgroundColor - Button background color. Default is colors.focus.regular.inner.
* @param {string} props.hoverBackgroundColor - Background color on hover/focus. Default is '#F2F2F2'.
* @param {boolean} props.disabled - Whether the button is disabled.
* @param {IconParams} props.iconProps - Additional properties for the FontAwesomeIcon.
* @returns A styled icon button component.
*/
export const IconButton: React.FC<IconButtonProps> = ({
icon,
onClick,
title,
sx,
size = '40px',
iconSize = '20px',
color = '#494949',
backgroundColor = colors.focus.regular.inner,
hoverBackgroundColor = '#F2F2F2',
disabled = false,
iconProps,
}) => {
return (
<MuiIconButton
onClick={onClick}
title={title}
aria-label={title}
disabled={disabled}
sx={{
width: size,
height: size,
borderRadius: '9999px', // Always fully rounded even if content isn't perfectly square
backgroundColor: backgroundColor,
color: color,
'&:hover': {
backgroundColor: hoverBackgroundColor,
},
'&:focus-visible': {
backgroundColor: hoverBackgroundColor,
outline: `2px solid ${colors.focus.regular.outer}`,
outlineOffset: '2px',
boxShadow: globalFocusShadow,
},
'&:disabled': {
backgroundColor: colors.surface.gray[40],
color: colors.type.regular.disabled,
},
...sx,
}}
>
<FontAwesomeIcon icon={icon} style={{ fontSize: iconSize }} {...iconProps} />
</MuiIconButton>
);
};