Skip to content

Commit 8a21edc

Browse files
authored
Merge pull request #35 from okgrow/fix/copilotRefBeingNullSometimes
Fixes ref being null every other time
2 parents bb377b7 + a6bf0e5 commit 8a21edc

1 file changed

Lines changed: 24 additions & 12 deletions

File tree

src/components/CopilotModal.js

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// @flow
22
import React, { Component } from 'react';
33
import { Animated, Easing, View, Text, NativeModules, Modal, StatusBar, Platform } from 'react-native';
4-
54
import Tooltip from './Tooltip';
65
import styles, { MARGIN, ARROW_SIZE, STEP_NUMBER_DIAMETER, STEP_NUMBER_RADIUS } from './style';
76

@@ -33,6 +32,8 @@ type State = {
3332
},
3433
};
3534

35+
const noop = () => {};
36+
3637
class CopilotModal extends Component<Props, State> {
3738
static defaultProps = {
3839
easing: Easing.elastic(0.7),
@@ -62,20 +63,32 @@ class CopilotModal extends Component<Props, State> {
6263
}
6364
}
6465

66+
layout = {
67+
width: 0,
68+
height: 0,
69+
}
70+
71+
handleLayoutChange = ({ nativeEvent: { layout } }) => {
72+
this.layout = layout;
73+
}
74+
6575
measure(): Promise {
6676
if (typeof __TEST__ !== 'undefined' && __TEST__) { // eslint-disable-line no-undef
6777
return new Promise(resolve => resolve({
6878
x: 0, y: 0, width: 0, height: 0,
6979
}));
7080
}
7181

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();
7992
});
8093
}
8194

@@ -253,19 +266,18 @@ class CopilotModal extends Component<Props, State> {
253266

254267
render() {
255268
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;
257270

258271
return (
259272
<Modal
260273
animationType="none"
261274
visible={containerVisible}
262-
onRequestClose={() => { }}
275+
onRequestClose={noop}
263276
transparent
264277
>
265278
<View
266279
style={styles.container}
267-
ref={(element) => { this.wrapper = element; }}
268-
onLayout={() => { }}
280+
onLayout={this.handleLayoutChange}
269281
>
270282
{contentVisible && this.renderMask()}
271283
{contentVisible && this.renderTooltip()}

0 commit comments

Comments
 (0)