-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathaction-button.component.tsx
More file actions
86 lines (77 loc) · 2.13 KB
/
Copy pathaction-button.component.tsx
File metadata and controls
86 lines (77 loc) · 2.13 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
import React from 'react';
import { isMacOS } from '@/common/helpers/platform.helpers';
import classes from './action-button.component.module.css';
import { ModifierType, ShortcutOptions } from '@/common/shortcut';
import useShortcut from '@/common/shortcut/shortcut.hook';
interface Props {
icon?: React.ReactNode;
label: string;
onClick?: () => void;
className?: string;
disabled?: boolean;
shortcutOptions?: ShortcutOptions;
showLabel?: boolean;
tooltipPosition?: 'top' | 'bottom';
}
export const ActionButton: React.FC<Props> = ({
disabled,
icon,
onClick = () => {},
className,
label,
shortcutOptions,
showLabel = true,
tooltipPosition = 'bottom',
}) => {
const getModifierSymbol = (modifierType: ModifierType = 'system') => {
switch (modifierType) {
case 'none':
return '';
case 'alt':
return 'Alt';
case 'system':
default:
return isMacOS() ? '⌘' : 'Ctrl';
}
};
const shortcutCommand = getModifierSymbol(shortcutOptions?.modifierType);
const showTooltip = shortcutOptions && !disabled;
const tooltipText =
shortcutOptions &&
`(${
shortcutOptions.modifierType === 'none'
? shortcutOptions.targetKeyLabel
: `${shortcutCommand} + ${shortcutOptions.targetKeyLabel}`
})`;
const tooltipPositionClass =
tooltipPosition === 'top' ? classes.tooltipTop : classes.tooltipBottom;
const tooltipClasses = `${classes.tooltip} ${tooltipPositionClass}`;
const buttonClasses = className
? `${classes.button} ${className}`.trim()
: classes.button;
useShortcut({
...shortcutOptions,
targetKey: shortcutOptions?.targetKey || [],
callback: onClick,
});
return (
<button
className={buttonClasses}
onClick={onClick}
disabled={disabled === true}
aria-describedby={shortcutOptions?.id}
>
<span aria-hidden={true}>{icon}</span>
{showLabel && <span>{label}</span>}
{showTooltip && (
<span
className={tooltipClasses}
role="tooltip"
id={shortcutOptions?.id}
>
{tooltipText}
</span>
)}
</button>
);
};