-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathButton.tsx
executable file
·96 lines (94 loc) · 2.04 KB
/
Button.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
import React, {
CSSProperties,
PropsWithChildren,
useMemo,
useState,
} from "react";
export default function Button({
className,
width,
height = 32,
color,
compact,
strong,
disabled,
activated,
round,
icon,
iconPosition,
onClick,
children,
}: PropsWithChildren<{
className?: string;
width?: number | string;
height?: number;
color?: string;
compact?: boolean;
strong?: boolean;
disabled?: boolean;
activated?: boolean;
round?: boolean;
icon?: React.ReactNode;
iconPosition?: { top?: number | string; left?: number | string };
onClick?(e: React.MouseEvent): void;
}>) {
const [hover, setHover] = useState(false);
const style = useMemo(() => {
const css: CSSProperties = {
display: "inline-flex",
alignItems: "center",
justifyContent: "center",
height,
border: "1px solid #000",
borderRadius: round === false ? "5px" : height / 2 + "px",
lineHeight: 1,
fontSize: 16,
fontWeight: 500,
padding: "0 36px",
transition: "all 0.21s ease-in-out",
cursor: "pointer",
};
if (color) {
css.color = color;
css.borderColor = color;
}
if (hover || activated) {
if (color === "white") {
css.color = "black";
css.background = "white";
} else {
css.color = "white";
css.background = "black";
}
}
if (compact) {
css.padding = "0 16px";
}
if (strong) {
css.borderWidth = "2px";
css.fontWeight = 600;
}
if (width) {
css.width = width;
css.padding = 0;
}
if (disabled) {
css.color = "#999";
css.borderColor = "#ccc";
css.pointerEvents = "none";
}
return css;
}, [height, color, hover, activated, strong]);
return (
<button
className={className}
style={style}
onClick={onClick}
onMouseEnter={() => setHover(true)}
onMouseLeave={() => setHover(false)}
>
{icon && <span style={iconPosition} className="icon">{icon}</span>}
{children}
</button>
);
}