Skip to content

Commit

Permalink
fix: fast ios modal push (#1326)
Browse files Browse the repository at this point in the history
Fix bug with pushing modals on iOS too fast. See #1299 for more info.

Co-authored-by: kacperkapusciak <kacper.kapusciak@swmansion.com>
  • Loading branch information
WoLewicki and kacperkapusciak authored Feb 28, 2022
1 parent fdbefc6 commit dc3d433
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
1 change: 1 addition & 0 deletions TestsExample/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ import Test1227 from './src/Test1227';
import Test1228 from './src/Test1228';
import Test1259 from './src/Test1259';
import Test1260 from './src/Test1260';
import Test1299 from './src/Test1299';

enableFreeze(true);

Expand Down
62 changes: 62 additions & 0 deletions TestsExample/src/Test1299.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import * as React from 'react';
import { View, StyleSheet, Button } from 'react-native';
import { createNativeStackNavigator } from 'react-native-screens/native-stack';
import { NavigationContainer, useNavigation } from '@react-navigation/native';

export const Modal1 = React.memo(() => {
return <View style={{ flexGrow: 1, backgroundColor: 'red', opacity: 0.7 }} />;
});

export const Modal2 = React.memo(() => {
return (
<View style={{ flexGrow: 1, backgroundColor: 'blue', opacity: 0.7 }} />
);
});
export const MainScreen = React.memo(() => {
const navigation = useNavigation();
return (
<View
style={{ flexGrow: 1, justifyContent: 'center', alignItems: 'center' }}>
<Button title={'Open modal 1'} onPress={() => {
navigation.navigate('modalScreen-1')
}}/>
<Button title={'Open modal 2'} onPress={() => {
navigation.navigate('modalScreen-2')
}}/>
</View>
);
});

const Stack = createNativeStackNavigator();

export default function App() {
return (
<View style={styles.container}>
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen key={'main'} name={'main'} component={MainScreen} />
<Stack.Screen
key={'modalScreen-1'}
name={'modalScreen-1'}
component={Modal1}
options={{ stackPresentation: 'modal', headerShown: false }}
/>
<Stack.Screen
key={'modalScreen-2'}
name={'modalScreen-2'}
component={Modal2}
options={{ stackPresentation: 'modal', headerShown: false }}
/>
</Stack.Navigator>
</NavigationContainer>
</View>
);
}

const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
backgroundColor: '#ecf0f1',
},
});
12 changes: 12 additions & 0 deletions ios/RNSScreenStack.m
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ - (void)presentationControllerDidDismiss:(UIPresentationController *)presentatio
// that can handle it when dismissing a modal, the same for orientation
[RNSScreenWindowTraits updateWindowTraits];
[_presentedModals removeObject:presentationController.presentedViewController];
// we double check if there are no new controllers pending to be presented since someone could
// have tried to push another one during the transition
_updatingModals = NO;
[self updateContainer];
if (self.onFinishTransitioning) {
// instead of directly triggering onFinishTransitioning this time we enqueue the event on the
// main queue. We do that because onDismiss event is also enqueued and we want for the transition
Expand Down Expand Up @@ -362,6 +366,14 @@ - (void)setModalViewControllers:(NSArray<UIViewController *> *)controllers
BOOL shouldAnimate = lastModal && [next isKindOfClass:[RNSScreen class]] &&
((RNSScreenView *)next.view).stackAnimation != RNSScreenStackAnimationNone;

// if you want to present another modal quick enough after dismissing the previous one,
// it will result in wrong changeRootController, see repro in
// https://github.com/software-mansion/react-native-screens/issues/1299 We call `updateContainer` again in
// `presentationControllerDidDismiss` to cover this case and present new controller
if (previous.beingDismissed) {
return;
}

[previous presentViewController:next
animated:shouldAnimate
completion:^{
Expand Down

0 comments on commit dc3d433

Please sign in to comment.