This repository has been archived by the owner on Mar 24, 2024. It is now read-only.
/
ButtonBase.tsx
284 lines (260 loc) · 7.39 KB
/
ButtonBase.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
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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/
//
// This file incorporates work covered by the following copyright and
// permission notice:
//
// Copyright 2018-2021 Cruise LLC
//
// This source code is licensed under the Apache License, Version 2.0,
// found at http://www.apache.org/licenses/LICENSE-2.0
// You may not use this file except in compliance with the License.
import cx from "classnames";
import { $Values } from "utility-types";
export const PROGRESS_DIRECTION = Object.freeze({
HORIZONTAL: "horizontal",
PULSE: "pulse",
VERTICAL: "vertical",
});
export type ProgressDirection = $Values<typeof PROGRESS_DIRECTION>;
let keyframesInjected = false;
function injectKeyframes() {
if (keyframesInjected) {
return;
}
const style = document.createElement("style");
style.innerHTML = `
@keyframes cruise-automation-button-pulse {
0%,
100% {
opacity: 0.9;
}
50% {
opacity: 0.2;
}
}
`;
if (!document.body) {
throw new Error("no document.body");
}
document.body.appendChild(style);
keyframesInjected = true;
}
type ProgressProps = {
customStyle?: {
[key: string]: any;
};
direction?: ProgressDirection;
percentage: number;
};
function getProgressStyle(props: ProgressProps) {
const { direction, percentage, customStyle } = props;
if (direction === PROGRESS_DIRECTION.PULSE) {
return {
...customStyle,
width: "100%",
height: "100%",
};
}
return {
...customStyle,
width: direction === PROGRESS_DIRECTION.HORIZONTAL ? `${percentage}%` : "100%",
height: direction !== PROGRESS_DIRECTION.HORIZONTAL ? `${percentage}%` : "100%",
};
}
type Props = {
id?: string;
small?: boolean;
large?: boolean;
primary?: boolean;
warning?: boolean;
danger?: boolean;
disabled?: boolean;
onClick?: (event: React.SyntheticEvent<HTMLButtonElement>) => void;
onFocus?: (event: React.SyntheticEvent<HTMLButtonElement>) => void;
onMouseUp?: (event: React.SyntheticEvent<HTMLButtonElement>) => void;
onMouseLeave?: (event: React.SyntheticEvent<HTMLButtonElement>) => void;
children: React.ReactNode;
className?: string;
delay?: number;
progressClassName?: string;
progressDirection?: ProgressDirection;
progressStyle?: {
[key: string]: any;
};
style?: {
[key: string]: any;
};
tooltip?: string;
};
type State = {
mouseDown: boolean;
progressPercentage: number;
};
export default class Button extends React.Component<Props, State> {
animationId?: ReturnType<typeof requestAnimationFrame>;
cancelTimeoutId?: ReturnType<typeof setTimeout>;
static defaultProps = {
progressDirection: PROGRESS_DIRECTION.VERTICAL,
};
state = {
mouseDown: false,
progressPercentage: 0,
};
componentWillUnmount() {
if (this.animationId) {
cancelAnimationFrame(this.animationId);
}
if (this.cancelTimeoutId) {
clearTimeout(this.cancelTimeoutId);
}
}
onDelayFinished = (e: React.SyntheticEvent<HTMLButtonElement>) => {
const { onClick } = this.props;
// slightly delay reseting to a non-progress state
// this allows the consumer to apply some kind of 'toggled on' class to the button
// and also makes the interaction slightly less jarring
this.cancelTimeoutId = setTimeout(this.cancelDown, 100);
if (!onClick) {
return;
}
onClick(e);
};
onMouseUp = (e: React.SyntheticEvent<HTMLButtonElement>) => {
const { onMouseUp } = this.props;
this.cancelDown();
if (onMouseUp) {
onMouseUp(e);
}
};
onMouseLeave = (e: React.SyntheticEvent<HTMLButtonElement>) => {
const { onMouseLeave } = this.props;
this.cancelDown();
if (onMouseLeave) {
onMouseLeave(e);
}
};
onMouseDown = (e: React.SyntheticEvent<HTMLButtonElement>) => {
const { delay } = this.props;
if (!delay) {
return;
}
// we need to remove the event from the pool to use it after a delay
e.persist();
// call animate after state change because this.setState is async
const onComplete = () => this.onDelayFinished(e);
const start = Date.now();
this.setState({ mouseDown: true }, () => this.animate(start, onComplete));
};
animate(startStamp: number, doneCallback: () => void) {
const { mouseDown } = this.state;
const { delay } = this.props;
if (!mouseDown || !delay) {
return;
}
// if just finshed a previous click there might be a pending cancel operation
// so clear it out
if (this.cancelTimeoutId) {
clearTimeout(this.cancelTimeoutId);
}
this.animationId = requestAnimationFrame(() => {
const tickStamp = Date.now();
const progressPercentage = Math.min(((tickStamp - startStamp) / delay) * 100, 100);
this.setState({ progressPercentage });
if (progressPercentage < 100) {
this.animate(startStamp, doneCallback);
return;
}
doneCallback();
});
}
onClick = (e: React.SyntheticEvent<HTMLButtonElement>) => {
const { onClick, delay } = this.props;
e.stopPropagation();
e.preventDefault();
if (onClick && !delay) {
onClick(e);
}
};
cancelDown = () => {
this.setState({ mouseDown: false, progressPercentage: 0 });
};
renderProgressBar() {
const { delay, progressClassName, progressStyle, progressDirection } = this.props;
const { mouseDown, progressPercentage } = this.state;
// don't render a bar if we aren't a delay button
if (!delay || !mouseDown) {
return null;
}
if (progressDirection === PROGRESS_DIRECTION.PULSE) {
injectKeyframes();
}
// allow user supplied classname to supercede built in class
return (
<span
style={{
content: "",
position: "absolute",
bottom: 0,
left: 0,
zIndex: 1,
backgroundColor: "rgba(0, 0, 0, 0.5)",
animation:
progressDirection === PROGRESS_DIRECTION.PULSE
? "cruise-automation-button-pulse 1s linear infinite"
: undefined,
...getProgressStyle({
direction: progressDirection,
percentage: progressPercentage,
customStyle: progressStyle,
}),
}}
className={progressClassName}
/>
);
}
render() {
const {
children,
id,
small,
large,
primary,
danger,
warning,
disabled,
className,
style,
tooltip,
onFocus,
} = this.props;
const classes = cx("button", className || "", {
// support some bulma classes to be supplied in consumer either through bulma or custom classes
// these provide backwards compatibility with webviz
"is-small": small,
"is-large": large,
"is-primary": primary,
"is-warning": warning,
"is-danger": danger,
});
return (
<button
type="button"
className={classes}
id={id}
onClick={this.onClick}
onFocus={onFocus}
onMouseDown={this.onMouseDown}
onMouseLeave={this.onMouseLeave}
onMouseUp={this.onMouseUp}
style={{ position: "relative", zIndex: 0, overflow: "hidden", ...style }}
title={tooltip}
disabled={disabled}
>
{children}
{this.renderProgressBar()}
</button>
);
}
}