Skip to content

Conversation

@lodev09
Copy link

@lodev09 lodev09 commented Nov 23, 2025

Description

This PR fixes an issue with the modal presentation chain when non-dismissible external modals (e.g., third-party modals like TrueSheet) are present in the view controller hierarchy.

Previously, when a non-dismissible modal was encountered, the stack didn't properly update the root controller for subsequent modal presentations, which could cause modals to be presented from the wrong controller or fail to dismiss modals that were presented by the non-dismissible modal.

Changes

  • Enhanced non-dismissible modal handling in RNSScreenStack.mm:
  • Refactored the dismissal check into a separate boolean variable for better readability
  • Added an else branch to handle the case when firstModalToBeDismissed is non-dismissible
  • When a non-dismissible modal is detected, the changeRootController is now updated to point to that modal
  • Added logic to check if the non-dismissible modal itself has presented any owned modals, and properly dismisses them before presenting new ones

Screenshots / GIFs

Before

Sheet will not present, will get warning in XCode.

After

Simulator.Screen.Recording.-.iPhone.17.Pro.Max.-.2025-11-23.at.21.47.34.mov

Test code and steps to reproduce

To test this change with a non-dismissible third-party modal (e.g., TrueSheet):

1. First, ensure your third-party modal implements the protocol (library author's responsibility):

// In your third-party library's view controller (e.g., TrueSheet)
#import "RNSDismissibleModalProtocol.h"

@interface YourModalViewController : UIViewController <RNSDismissibleModalProtocol>
@end

@implementation YourModalViewController

- (BOOL)isDismissible {
  return NO; // Mark as non-dismissible
}

@end

2. Then test with this example:

import React from 'react';
import { View, Button } from 'react-native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import { TrueSheet } from '@lodev09/react-native-true-sheet'; // Example third-party modal

const Stack = createNativeStackNavigator();

function HomeScreen({ navigation }) {
  const sheet = React.useRef<TrueSheet>(null);

  return (
    <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
      <Button title="Show TrueSheet" onPress={() => sheet.current?.present()} />
      <TrueSheet ref={sheet}>
        <Button 
          title="Open React Navigation Modal" 
          onPress={() => navigation.navigate('Modal')} 
        />
      </TrueSheet>
    </View>
  );
}

function ModalScreen() {
  return (
    <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
      <Text>This is a modal</Text>
    </View>
  );
}

function App() {
  return (
    <Stack.Navigator>
      <Stack.Screen name="Home" component={HomeScreen} />
      <Stack.Screen 
        name="Modal" 
        component={ModalScreen} 
        options={{ presentation: 'modal' }} 
      />
    </Stack.Navigator>
  );
}
  1. Open the TrueSheet (non-dismissible modal that implements RNSDismissibleModalProtocol)
  2. From within the sheet, open a React Navigation modal
  3. The modal should present correctly from the non-dismissible modal
  4. Subsequent modal presentations and dismissals should work as expected
  5. The TrueSheet should remain visible and not be dismissed

Checklist

When a modal conforms to RNSDismissibleModalProtocol and returns NO from
isDismissible, it should not be dismissed when new modals are presented.

Previously, when isDismissible returned NO, the code would skip the entire
dismissal block, including the logic that updates changeRootController to
handle the presentation hierarchy correctly. This caused iOS to throw an
error because it tried to present a new modal from a view controller that
was already presenting the non-dismissible modal.

This fix:
- Extracts the dismissibility check into a clear boolean variable
- Adds an else clause that runs when a modal is non-dismissible
- Updates changeRootController to be the non-dismissible modal
- Checks if the non-dismissible modal has presented any RN modals
- Properly dismisses RN modals presented by the non-dismissible modal

This enables third-party modal libraries (like TrueSheet, react-native-modal,
etc.) to coexist with React Navigation modals by implementing the protocol.

Example use case:
- User opens a bottom sheet (TrueSheet) from a screen
- User taps a button in the sheet to open a React Navigation modal
- Modal presents successfully from the sheet
- Modal can be dismissed while sheet remains open

Fixes presentation errors like:
"Attempt to present <RNSScreen> on <UIViewController> which is already
presenting <ThirdPartyModal>"

Related: Implements proper support for RNSDismissibleModalProtocol
@lodev09 lodev09 changed the title Fix/non dismissible modal presentation fix(IOS): Fix non dismissible modal presentation Nov 23, 2025
@lodev09
Copy link
Author

lodev09 commented Nov 23, 2025

@kkafar an alternative approach would be to create another method to the protocol to return a ViewController that should update the root controller to keep things safe. Let me know if that's better. Thank you!

…ting controller

- Add presentingControllerForModals optional method to RNSDismissibleModalProtocol
- Allow external non-dismissible modals to provide their own presenting controller
- Only use external controller if explicitly provided (non-nil)
- Falls back to original implementation if method not implemented or returns nil
- Add UIKit import to protocol header for UIViewController support
@lodev09
Copy link
Author

lodev09 commented Dec 1, 2025

Can we merge this @kligarski @kkafar? This should be straightforward and backwards compatible. Tested and works in a production app. Thank you!

FYI @erickreutz

@kkafar
Copy link
Member

kkafar commented Dec 5, 2025

Hey, sorry for very late response here & on other PRs. I'll look into this after we release 4.19.0 (planned for upcoming week).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants