Skip to content

Commit 9413350

Browse files
authored
fix: avoid events to be trigger twice (#130)
1 parent 9d06edf commit 9413350

File tree

4 files changed

+12
-42
lines changed

4 files changed

+12
-42
lines changed

examples/expo/src/components/modals/FixedContent.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export class FixedContent extends React.PureComponent {
3939

4040
render() {
4141
return (
42-
<Modalize ref={this.modal} adjustToContentHeight>
42+
<Modalize ref={this.modal} scrollViewProps={{ scrollEnabled: false }} adjustToContentHeight>
4343
{this.renderContent()}
4444
</Modalize>
4545
);

examples/react-native-navigation/src/screens/FixedContent.js

+6-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,12 @@ export class FixedContent extends React.PureComponent {
4848

4949
render() {
5050
return (
51-
<Modalize ref={this.modal} onClosed={this.onClosed} adjustToContentHeight>
51+
<Modalize
52+
ref={this.modal}
53+
onClosed={this.onClosed}
54+
scrollViewProps={{ scrollEnabled: false }}
55+
adjustToContentHeight
56+
>
5257
{this.renderContent()}
5358
</Modalize>
5459
);

examples/react-navigation/src/components/modals/FixedContent.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export class FixedContent extends React.PureComponent {
3939

4040
render() {
4141
return (
42-
<Modalize ref={this.modal} adjustToContentHeight>
42+
<Modalize ref={this.modal} scrollViewProps={{ scrollEnabled: false }} adjustToContentHeight>
4343
{this.renderContent()}
4444
</Modalize>
4545
);

src/index.tsx

+4-39
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@ export class Modalize<FlatListItem = any, SectionListItem = any> extends React.C
6969
private snaps: number[] = [];
7070
private snapEnd: number;
7171
private beginScrollYValue: number = 0;
72-
private contentAlreadyCalculated: boolean = false;
7372
private beginScrollY: Animated.Value = new Animated.Value(0);
7473
private dragY: Animated.Value = new Animated.Value(0);
7574
private translateY: Animated.Value = new Animated.Value(screenHeight);
@@ -158,17 +157,13 @@ export class Modalize<FlatListItem = any, SectionListItem = any> extends React.C
158157
}
159158

160159
public open = (): void => {
161-
const { adjustToContentHeight, onOpen } = this.props;
160+
const { onOpen } = this.props;
162161

163162
if (onOpen) {
164163
onOpen();
165164
}
166165

167-
if (!adjustToContentHeight || this.contentAlreadyCalculated) {
168-
this.onAnimateOpen();
169-
} else {
170-
this.setState({ isVisible: true });
171-
}
166+
this.onAnimateOpen();
172167
};
173168

174169
public close = (dest: 'alwaysOpen' | 'default' = 'default'): void => {
@@ -375,36 +370,6 @@ export class Modalize<FlatListItem = any, SectionListItem = any> extends React.C
375370
});
376371
};
377372

378-
private onContentViewLayout = ({ nativeEvent }: LayoutChangeEvent): void => {
379-
const { adjustToContentHeight, snapPoint, alwaysOpen } = this.props;
380-
const { contentHeight, modalHeight } = this.state;
381-
382-
if (
383-
!adjustToContentHeight ||
384-
(modalHeight || 0) <= nativeEvent.layout.height ||
385-
snapPoint ||
386-
this.contentAlreadyCalculated
387-
) {
388-
if ((modalHeight || 0) <= nativeEvent.layout.height) {
389-
this.onAnimateOpen(alwaysOpen);
390-
}
391-
392-
return;
393-
}
394-
395-
// @todo: modalHeight should be equal to the nativeEvent's height,
396-
// and not to the state's value which is 0 at the first mount
397-
this.setState(
398-
{
399-
contentHeight: nativeEvent.layout.height || contentHeight,
400-
},
401-
() => {
402-
this.contentAlreadyCalculated = true;
403-
this.onAnimateOpen();
404-
},
405-
);
406-
};
407-
408373
private onHandleComponent = ({ nativeEvent }: PanGestureHandlerStateChangeEvent): void => {
409374
if (nativeEvent.oldState === State.BEGAN) {
410375
this.beginScrollY.setValue(0);
@@ -611,7 +576,7 @@ export class Modalize<FlatListItem = any, SectionListItem = any> extends React.C
611576
{ useNativeDriver: false },
612577
),
613578
scrollEventThrottle: 16,
614-
onLayout: this.onContentViewLayout,
579+
keyboardDismissMode,
615580
};
616581

617582
if (flatListProps) {
@@ -623,7 +588,7 @@ export class Modalize<FlatListItem = any, SectionListItem = any> extends React.C
623588
}
624589

625590
return (
626-
<Animated.ScrollView {...opts} {...scrollViewProps} keyboardDismissMode={keyboardDismissMode}>
591+
<Animated.ScrollView {...opts} {...scrollViewProps}>
627592
{children}
628593
</Animated.ScrollView>
629594
);

0 commit comments

Comments
 (0)