Skip to content

fix(iOS): don't restore first responder of screens in ScreenContainer - #5443

Open
Bardiamist wants to merge 2 commits into
software-mansion:mainfrom
Bardiamist:fix/ios-container-first-responder
Open

Bardiamist wants to merge 2 commits into
software-mansion:mainfrom
Bardiamist:fix/ios-container-first-responder

Conversation

@Bardiamist

Copy link
Copy Markdown

Description

Closes #5442

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.

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):

  1. Tap "Street", tap "Pick, then focus City", tap "Select". Before: "City" is focused, then "Street" takes focus after the transition. After: "City" stays focused.
  2. 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.
  • For API changes, updated relevant public types.
  • Ensured that CI passes

@coderabbitai

coderabbitai Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Explain 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.
Screen removal responder lookup
ios/legacy/RNSScreen.mm
Skips first-responder lookup when the screen’s parent is an RNSViewController. The previous lookup remains for other parents.

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix · Severity of issue fixed: Medium

Suggested reviewers: t0maboro, kmichalikk, kkafar

Merge Risk: Minimal · up to 66328

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: Passed 4

Check name Status Explanation
Title check Passed 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 Passed 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 Passed [#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 Passed 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.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Repository UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: ce228e04-7dc8-4b22-9f8a-55aff77215c6
Commits

Reviewing files that changed from the base of the PR and between 9d84877 and 6632811.

Files selected for processing: 3

  • apps/src/tests/issue-tests/TestScreenContainerFirstResponder.tsx
  • apps/src/tests/issue-tests/index.ts
  • ios/legacy/RNSScreen.mm

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
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.

[iOS] ScreenContainer restores the first responder after the transition, overriding focus set from JS

1 participant