|
1 | 1 | // @flow |
2 | 2 | import React, { Component } from 'react'; |
3 | 3 | import { Animated, Easing, View, Text, NativeModules, Modal, StatusBar, Platform } from 'react-native'; |
4 | | - |
5 | 4 | import Tooltip from './Tooltip'; |
6 | 5 | import styles, { MARGIN, ARROW_SIZE, STEP_NUMBER_DIAMETER, STEP_NUMBER_RADIUS } from './style'; |
7 | 6 |
|
@@ -33,6 +32,8 @@ type State = { |
33 | 32 | }, |
34 | 33 | }; |
35 | 34 |
|
| 35 | +const noop = () => {}; |
| 36 | + |
36 | 37 | class CopilotModal extends Component<Props, State> { |
37 | 38 | static defaultProps = { |
38 | 39 | easing: Easing.elastic(0.7), |
@@ -62,20 +63,32 @@ class CopilotModal extends Component<Props, State> { |
62 | 63 | } |
63 | 64 | } |
64 | 65 |
|
| 66 | + layout = { |
| 67 | + width: 0, |
| 68 | + height: 0, |
| 69 | + } |
| 70 | + |
| 71 | + handleLayoutChange = ({ nativeEvent: { layout } }) => { |
| 72 | + this.layout = layout; |
| 73 | + } |
| 74 | + |
65 | 75 | measure(): Promise { |
66 | 76 | if (typeof __TEST__ !== 'undefined' && __TEST__) { // eslint-disable-line no-undef |
67 | 77 | return new Promise(resolve => resolve({ |
68 | 78 | x: 0, y: 0, width: 0, height: 0, |
69 | 79 | })); |
70 | 80 | } |
71 | 81 |
|
72 | | - return new Promise((resolve, reject) => { |
73 | | - this.wrapper.measure( |
74 | | - (ox, oy, width, height, x, y) => resolve({ |
75 | | - x, y, width, height, |
76 | | - }), |
77 | | - reject, |
78 | | - ); |
| 82 | + |
| 83 | + return new Promise((resolve) => { |
| 84 | + const setLayout = () => { |
| 85 | + if (this.layout.width !== 0) { |
| 86 | + resolve(this.layout); |
| 87 | + } else { |
| 88 | + requestAnimationFrame(setLayout); |
| 89 | + } |
| 90 | + }; |
| 91 | + setLayout(); |
79 | 92 | }); |
80 | 93 | } |
81 | 94 |
|
@@ -253,19 +266,18 @@ class CopilotModal extends Component<Props, State> { |
253 | 266 |
|
254 | 267 | render() { |
255 | 268 | const containerVisible = this.state.containerVisible || this.props.visible; |
256 | | - const contentVisible = this.state.layout && this.state.containerVisible && this.props.visible; |
| 269 | + const contentVisible = this.state.layout && containerVisible; |
257 | 270 |
|
258 | 271 | return ( |
259 | 272 | <Modal |
260 | 273 | animationType="none" |
261 | 274 | visible={containerVisible} |
262 | | - onRequestClose={() => { }} |
| 275 | + onRequestClose={noop} |
263 | 276 | transparent |
264 | 277 | > |
265 | 278 | <View |
266 | 279 | style={styles.container} |
267 | | - ref={(element) => { this.wrapper = element; }} |
268 | | - onLayout={() => { }} |
| 280 | + onLayout={this.handleLayoutChange} |
269 | 281 | > |
270 | 282 | {contentVisible && this.renderMask()} |
271 | 283 | {contentVisible && this.renderTooltip()} |
|
0 commit comments