-
-
Notifications
You must be signed in to change notification settings - Fork 420
/
Copy pathCopilotModal.js
333 lines (298 loc) · 8.98 KB
/
CopilotModal.js
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
// @flow
import React, { Component } from 'react';
import { Animated, Easing, View, NativeModules, Modal, StatusBar, Platform } from 'react-native';
import Tooltip from './Tooltip';
import StepNumber from './StepNumber';
import styles, { MARGIN, ARROW_SIZE, STEP_NUMBER_DIAMETER, STEP_NUMBER_RADIUS } from './style';
import type { SvgMaskPathFn } from '../types';
type Props = {
stop: () => void,
next: () => void,
prev: () => void,
currentStepNumber: number,
currentStep: ?Step,
visible: boolean,
isFirstStep: boolean,
isLastStep: boolean,
easing: ?func,
animationDuration: ?number,
tooltipComponent: ?React$Component,
tooltipStyle?: Object,
stepNumberComponent: ?React$Component,
overlay: 'svg' | 'view',
animated: boolean,
androidStatusBarVisible: boolean,
backdropColor: string,
labels: Object,
svgMaskPath?: SvgMaskPathFn,
stopOnOutsideClick?: boolean,
arrowColor?: string,
};
type State = {
tooltip: Object,
arrow: Object,
animatedValues: Object,
notAnimated: boolean,
layout: ?{
width: number,
height: number,
},
};
class CopilotModal extends Component<Props, State> {
static defaultProps = {
easing: Easing.elastic(0.7),
animationDuration: 400,
tooltipComponent: Tooltip,
tooltipStyle: {},
stepNumberComponent: StepNumber,
// If react-native-svg native module was avaialble, use svg as the default overlay component
overlay: typeof NativeModules.RNSVGSvgViewManager !== 'undefined' ? 'svg' : 'view',
// If animated was not specified, rely on the default overlay type
animated: typeof NativeModules.RNSVGSvgViewManager !== 'undefined',
onBackButton: 'noop',
androidStatusBarVisible: false,
backdropColor: 'rgba(0, 0, 0, 0.4)',
labels: {},
stopOnOutsideClick: false,
arrowColor: '#fff',
};
state = {
tooltip: {},
arrow: {},
animatedValues: {
top: new Animated.Value(0),
stepNumberLeft: new Animated.Value(0),
},
animated: false,
containerVisible: false,
};
componentDidUpdate(prevProps: Props) {
if (prevProps.visible === true && this.props.visible === false) {
this.reset();
}
}
layout = {
width: 0,
height: 0,
}
handleLayoutChange = ({ nativeEvent: { layout } }) => {
this.layout = layout;
}
measure(): Promise {
if (typeof __TEST__ !== 'undefined' && __TEST__) { // eslint-disable-line no-undef
return new Promise(resolve => resolve({
x: 0, y: 0, width: 0, height: 0,
}));
}
return new Promise((resolve) => {
const setLayout = () => {
if (this.layout.width !== 0) {
resolve(this.layout);
} else {
requestAnimationFrame(setLayout);
}
};
setLayout();
});
}
async _animateMove(obj = {}): void {
const layout = await this.measure();
if (!this.props.androidStatusBarVisible && Platform.OS === 'android') {
obj.top -= StatusBar.currentHeight; // eslint-disable-line no-param-reassign
}
let stepNumberLeft = obj.left - STEP_NUMBER_RADIUS;
if (stepNumberLeft < 0) {
stepNumberLeft = (obj.left + obj.width) - STEP_NUMBER_RADIUS;
if (stepNumberLeft > layout.width - STEP_NUMBER_DIAMETER) {
stepNumberLeft = layout.width - STEP_NUMBER_DIAMETER;
}
}
const center = {
x: obj.left + (obj.width / 2),
y: obj.top + (obj.height / 2),
};
const relativeToLeft = center.x;
const relativeToTop = center.y;
const relativeToBottom = Math.abs(center.y - layout.height);
const relativeToRight = Math.abs(center.x - layout.width);
const verticalPosition = relativeToBottom > relativeToTop ? 'bottom' : 'top';
const horizontalPosition = relativeToLeft > relativeToRight ? 'left' : 'right';
const tooltip = {};
const arrow = {};
if (verticalPosition === 'bottom') {
tooltip.top = obj.top + obj.height + MARGIN;
arrow.borderBottomColor = this.props.arrowColor;
arrow.top = tooltip.top - (ARROW_SIZE * 2);
} else {
tooltip.bottom = layout.height - (obj.top - MARGIN);
arrow.borderTopColor = this.props.arrowColor;
arrow.bottom = tooltip.bottom - (ARROW_SIZE * 2);
}
if (horizontalPosition === 'left') {
tooltip.right = Math.max(layout.width - (obj.left + obj.width), 0);
tooltip.right = tooltip.right === 0 ? tooltip.right + MARGIN : tooltip.right;
tooltip.maxWidth = layout.width - tooltip.right - MARGIN;
arrow.right = tooltip.right + MARGIN;
} else {
tooltip.left = Math.max(obj.left, 0);
tooltip.left = tooltip.left === 0 ? tooltip.left + MARGIN : tooltip.left;
tooltip.maxWidth = layout.width - tooltip.left - MARGIN;
arrow.left = tooltip.left + MARGIN;
}
const animate = {
top: obj.top,
stepNumberLeft,
};
if (this.state.animated) {
Animated
.parallel(Object.keys(animate)
.map(key => Animated.timing(this.state.animatedValues[key], {
toValue: animate[key],
duration: this.props.animationDuration,
easing: this.props.easing,
useNativeDriver: false,
})))
.start();
} else {
Object.keys(animate).forEach((key) => {
this.state.animatedValues[key].setValue(animate[key]);
});
}
this.setState({
tooltip,
arrow,
layout,
animated: this.props.animated,
size: {
x: obj.width,
y: obj.height,
},
position: {
x: Math.floor(Math.max(obj.left, 0)),
y: Math.floor(Math.max(obj.top, 0)),
},
});
}
animateMove(obj = {}): void {
return new Promise((resolve) => {
this.setState(
{ containerVisible: true },
() => requestAnimationFrame(async () => {
await this._animateMove(obj);
resolve();
}),
);
});
}
reset(): void {
this.setState({
animated: false,
containerVisible: false,
layout: undefined,
});
}
handleNext = () => {
this.props.next();
}
handlePrev = () => {
this.props.prev();
}
handleStop = () => {
this.reset();
this.props.stop();
}
handleMaskClick = () => {
if (this.props.stopOnOutsideClick) {
this.handleStop();
}
};
handleBackButton = () => {
if (this.props.onBackButton === 'stop') {
this.handleStop();
} else if (this.props.onBackButton === 'prev') {
this.handlePrev();
}
// if not otherwise specified, do nothing
}
renderMask() {
/* eslint-disable global-require */
const MaskComponent = this.props.overlay === 'svg'
? require('./SvgMask').default
: require('./ViewMask').default;
/* eslint-enable */
return (
<MaskComponent
animated={this.props.animated}
layout={this.state.layout}
style={styles.overlayContainer}
size={this.state.size}
position={this.state.position}
easing={this.props.easing}
animationDuration={this.props.animationDuration}
backdropColor={this.props.backdropColor}
svgMaskPath={this.props.svgMaskPath}
onClick={this.handleMaskClick}
currentStep={this.props.currentStep}
/>
);
}
renderTooltip() {
const {
tooltipComponent: TooltipComponent,
stepNumberComponent: StepNumberComponent,
} = this.props;
return [
<Animated.View
key="stepNumber"
style={[
styles.stepNumberContainer,
{
left: this.state.animatedValues.stepNumberLeft,
top: Animated.add(this.state.animatedValues.top, -STEP_NUMBER_RADIUS),
},
]}
>
<StepNumberComponent
isFirstStep={this.props.isFirstStep}
isLastStep={this.props.isLastStep}
currentStep={this.props.currentStep}
currentStepNumber={this.props.currentStepNumber}
/>
</Animated.View>,
<Animated.View key="arrow" style={[styles.arrow, this.state.arrow]} />,
<Animated.View key="tooltip" style={[styles.tooltip, this.state.tooltip, this.props.tooltipStyle]}>
<TooltipComponent
isFirstStep={this.props.isFirstStep}
isLastStep={this.props.isLastStep}
currentStep={this.props.currentStep}
handleNext={this.handleNext}
handlePrev={this.handlePrev}
handleStop={this.handleStop}
labels={this.props.labels}
/>
</Animated.View>,
];
}
render() {
const containerVisible = this.state.containerVisible || this.props.visible;
const contentVisible = this.state.layout && containerVisible;
return (
<Modal
animationType="none"
visible={containerVisible}
onRequestClose={this.handleBackButton}
transparent
supportedOrientations={['portrait', 'landscape']}
>
<View
style={styles.container}
onLayout={this.handleLayoutChange}
>
{contentVisible && this.renderMask()}
{contentVisible && this.renderTooltip()}
</View>
</Modal>
);
}
}
export default CopilotModal;