You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
fix(iOS): don't restore first responder of screens in ScreenContainer - #5443
On iOS, a screen in ScreenContainer saves its first responder when another screen is pushed and makes it first responder again after it's back on top. This overrides whatever the app focused (or blurred) from JS after returning to the screen. Android doesn't have this behavior.
ScreenContainer with transitions is used by @react-navigation/stack 7.x. Since 7.12.2 the stack restores the focused input itself in JS, on both platforms. With react-navigation/react-navigation#13359 a screen can also skip that restore. Because of the native restore, both behave differently on iOS.
Changes
RNSScreen doesn't save the first responder in willMoveToParentViewController: when its parent is RNSViewController, i.e. when the screen is in ScreenContainer.
Native stack (RNSNavigationController) and ScreenNavigationContainer (RNSContainerNavigationController, used by tabs and drawer with hasTwoStates) keep the current behavior.
Added TestScreenContainerFirstResponder.
I didn't add a prop to control this. The JS stack is the only consumer that needs it, and it would always turn the native restore off, because it handles focus itself. A prop would add a native prop to the legacy Screen for that one case.
Trade-off: with @react-navigation/stack older than 7.12.2, iOS no longer refocuses the input after going back. That's the same as on Android. From 7.12.2 the stack does it in JS on both platforms.
Tap "Street", tap "Pick, then focus City", tap "Select". Before: "City" is focused, then "Street" takes focus after the transition. After: "City" stays focused.
Tap "Street", tap "Pick, then hide keyboard", tap "Select". Before: "Street" is focused and the keyboard opens after the transition. After: the keyboard stays hidden, "Focused input: none".
Checklist
Included code example that can be used to test this change.
For visual changes, included screenshots / GIFs / recordings documenting the change.
No actionable comments were generated in the recent review.
Walkthrough
The change adds an issue-test navigation flow for restoring focus after returning to a screen. On iOS, screen removal now skips first-responder lookup when the screen’s parent is an RNSViewController.
Changes: First-responder restoration
Layer / File(s)
Summary
Address and Picker reproduction apps/src/tests/issue-tests/TestScreenContainerFirstResponder.tsx, apps/src/tests/issue-tests/index.ts
Adds Address and Picker screens. Returning from Picker focuses City or dismisses the keyboard based on the selected action. Exports the test from the issue-test index.
This change stops iOS ScreenContainer screens from restoring a saved keyboard focus after returning, so focus set from JS is preserved. The change is narrow and easy to revert. With older @react-navigation/stack versions, iOS will no longer refocus the input after going back; this is a disclosed trade-off. Merge risk is minimal.
Pre-merge checks: 4
Check name
Status
Explanation
Title check
The title names the main change: on iOS, screens in ScreenContainer no longer restore their saved first responder. It uses the conventional fix(iOS) prefix and matches the RNSScreen.mm change and the …
Description check
The description explains the iOS first-responder restore problem, links issue #5442, and lists the RNSScreen change, the unchanged native stack and ScreenNavigationContainer behavior, the added test s…
Linked Issues check
[#5442] The issue requires that focus set from JS after returning to a ScreenContainer screen stays in effect on iOS. The root cause is that RNSScreen saves the first responder when it leaves the top …
Out of Scope Changes check
The PR changes three files. (1) ios/legacy/RNSScreen.mm adds a guard in willMoveToParentViewController: that directly implements the requested ScreenContainer behavior. The PR description states that …
Full details: Title check
Explanation
The title names the main change: on iOS, screens in ScreenContainer no longer restore their saved first responder. It uses the conventional fix(iOS) prefix and matches the RNSScreen.mm change and the stated objective. It is specific and concise.
Full details: Description check
Explanation
The description explains the iOS first-responder restore problem, links issue #5442, and lists the RNSScreen change, the unchanged native stack and ScreenNavigationContainer behavior, the added test screen, the trade-off for older @react-navigation/stack versions, and test steps. It is directly related to the changeset.
Full details: Linked Issues check
Explanation
[#5442] The issue requires that focus set from JS after returning to a ScreenContainer screen stays in effect on iOS. The root cause is that RNSScreen saves the first responder when it leaves the top and restores it after the transition. The diff in ios/legacy/RNSScreen.mm changes willMoveToParentViewController: so that, when the current parentViewController is RNSViewController (the ScreenContainer case), the responder lookup is skipped and _previousFirstResponder is not set from that path. Other parents keep the previous lookup. The added TestScreenContainerFirstResponder reproduces both cases from the issue: focusing City after the Picker returns, and dismissing the keyboard after the Picker returns. The PR description reports the expected results for both cases on iOS simulator. The unchecked 'Ensured that CI passes' item is not a coding requirement. Limitation: I did not read notifyFinishTransitioning in full. The PASS assumes that the restore path uses only _previousFirstResponder, which the skipped save leaves unset for this case.
Full details: Out of Scope Changes check
Explanation
The PR changes three files. (1) ios/legacy/RNSScreen.mm adds a guard in willMoveToParentViewController: that directly implements the requested ScreenContainer behavior. The PR description states that native stack (RNSNavigationController) and ScreenNavigationContainer (RNSContainerNavigationController) keep their behavior; the guard only checks for RNSViewController. (2) apps/src/tests/issue-tests/TestScreenContainerFirstResponder.tsx adds the reproduction screen for #5442. (3) apps/src/tests/issue-tests/index.ts exports that test. Files (2) and (3) support the fix and its verification. No unrelated change was found in the reviewed summary or in the inspected hunk.
Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.
This branch has not been deployed
No deployments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Closes #5442
On iOS, a screen in
ScreenContainersaves its first responder when another screen is pushed and makes it first responder again after it's back on top. This overrides whatever the app focused (or blurred) from JS after returning to the screen. Android doesn't have this behavior.ScreenContainerwith transitions is used by@react-navigation/stack7.x. Since 7.12.2 the stack restores the focused input itself in JS, on both platforms. With react-navigation/react-navigation#13359 a screen can also skip that restore. Because of the native restore, both behave differently on iOS.Changes
RNSScreendoesn't save the first responder inwillMoveToParentViewController:when its parent isRNSViewController, i.e. when the screen is inScreenContainer.RNSNavigationController) andScreenNavigationContainer(RNSContainerNavigationController, used by tabs and drawer withhasTwoStates) keep the current behavior.TestScreenContainerFirstResponder.I didn't add a prop to control this. The JS stack is the only consumer that needs it, and it would always turn the native restore off, because it handles focus itself. A prop would add a native prop to the legacy
Screenfor that one case.Trade-off: with
@react-navigation/stackolder than 7.12.2, iOS no longer refocuses the input after going back. That's the same as on Android. From 7.12.2 the stack does it in JS on both platforms.Before & after - visual documentation
Before
rns-before-city.mp4
After
rns-after-city.mp4
Test plan
TestScreenContainerFirstResponder(FabricExample, iOS simulator, iPhone 17, iOS 27.2):Checklist