Repository navigation
fix(ios): pop only the topmost React Native screen on popToNative #470
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
hurali97
merged 2 commits into
callstack:main
from
KisaneNeko:fix/ios-pop-to-native-topmost-surface
Sep 24, 2026
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| --- | ||
| '@callstack/react-native-brownfield': patch | ||
| --- | ||
|
|
||
| Fix `popToNative()` on iOS closing every React Native screen instead of only the topmost one. | ||
|
|
||
| `popToNative()` is delivered as an app-wide notification, so every live React Native surface acted on it — including surfaces buried under other screens, and each surface acted twice when hosted by SwiftUI. In a stack such as RN → native → RN, a single call tore down the whole navigation stack, taking native screens with it. | ||
|
|
||
| A React Native surface now acts only when it is the topmost one on screen, and only once per host. | ||
|
|
||
| Two consequences worth knowing about: | ||
|
|
||
| - When no React Native surface is on screen — for example one hidden behind a full-screen native modal — `popToNative()` is now a no-op instead of popping the hidden screen. It logs when this happens. | ||
| - `popToNative()` no longer dismisses a modally presented native screen that merely *contains* a React Native surface. Only a surface that owns a navigation stack entry pops. |
93 changes: 93 additions & 0 deletions
93
packages/react-native-brownfield/ios/BrownfieldSurfaceRegistry.swift
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| import Foundation | ||
|
|
||
| /// Identifies one live React Native surface. Vended by `BrownfieldSurfaceRegistry`. | ||
| struct BrownfieldSurfaceID: Hashable, CustomStringConvertible { | ||
| fileprivate let rawValue: UInt64 | ||
|
|
||
| var description: String { "surface#\(rawValue)" } | ||
| } | ||
|
|
||
| /// Decides which React Native surface may act on a `popToNative()` request. | ||
| /// | ||
| /// `popToNative()` is delivered as an app-wide notification, so every live React | ||
| /// Native surface receives it — including ones buried under other screens. Acting | ||
| /// on all of them tears down the whole navigation stack | ||
| /// (https://github.com/callstack/react-native-brownfield/issues/354). | ||
| /// | ||
| /// The registry resolves that to a single surface: whichever on-screen surface | ||
| /// was shown most recently. | ||
| /// | ||
| /// Visibility is asked for at query time instead of being cached from lifecycle | ||
| /// callbacks. Hosts recreate view controllers — SwiftUI does it freely — and a | ||
| /// cached flag is stale exactly when the notification arrives. | ||
| /// | ||
| /// Deliberately free of UIKit so it can be unit tested on macOS via `swift test`. | ||
| /// Internal, like the sibling `BrownfieldBundleSupport` target: it is an | ||
| /// implementation detail of the pod, not API for host apps. | ||
| final class BrownfieldSurfaceRegistry { | ||
| static let shared = BrownfieldSurfaceRegistry() | ||
|
|
||
| private struct Surface { | ||
| let isOnScreen: () -> Bool | ||
| var shownAt: UInt64 | ||
| } | ||
|
|
||
| private let lock = NSLock() | ||
| private var nextSurfaceValue: UInt64 = 0 | ||
| private var nextShownValue: UInt64 = 0 | ||
| private var surfaces: [BrownfieldSurfaceID: Surface] = [:] | ||
|
|
||
| init() {} | ||
|
|
||
| /// - Parameter isOnScreen: queried on every `popTarget()`. Capture the host | ||
| /// weakly so a released surface stops winning the election. | ||
| func register(isOnScreen: @escaping () -> Bool) -> BrownfieldSurfaceID { | ||
| lock.lock() | ||
| defer { lock.unlock() } | ||
|
|
||
| nextSurfaceValue += 1 | ||
| let id = BrownfieldSurfaceID(rawValue: nextSurfaceValue) | ||
| surfaces[id] = Surface(isOnScreen: isOnScreen, shownAt: 0) | ||
| return id | ||
| } | ||
|
|
||
| func unregister(_ surface: BrownfieldSurfaceID) { | ||
| lock.lock() | ||
| defer { lock.unlock() } | ||
|
|
||
| surfaces.removeValue(forKey: surface) | ||
| } | ||
|
|
||
| /// Records that a surface was just shown, ordering it ahead of other on-screen | ||
| /// surfaces. Only a tiebreaker — a surface never marked shown can still win | ||
| /// when it is the one on screen. | ||
| func markShown(_ surface: BrownfieldSurfaceID) { | ||
| lock.lock() | ||
| defer { lock.unlock() } | ||
|
|
||
| guard var entry = surfaces[surface] else { return } | ||
|
|
||
| nextShownValue += 1 | ||
| entry.shownAt = nextShownValue | ||
| surfaces[surface] = entry | ||
| } | ||
|
|
||
| /// The single surface allowed to handle `popToNative()`, or `nil` when no | ||
| /// React Native surface is on screen. | ||
| func popTarget() -> BrownfieldSurfaceID? { | ||
| lock.lock() | ||
| let candidates = surfaces | ||
| lock.unlock() | ||
|
|
||
| return candidates | ||
| .filter { $0.value.isOnScreen() } | ||
| .max { lhs, rhs in | ||
| (lhs.value.shownAt, lhs.key.rawValue) < (rhs.value.shownAt, rhs.key.rawValue) | ||
| }? | ||
| .key | ||
| } | ||
|
|
||
| func isPopTarget(_ surface: BrownfieldSurfaceID) -> Bool { | ||
| popTarget() == surface | ||
| } | ||
| } |
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks like an unusual place to put this and related files. Let's put this at
packages/react-native-brownfield/ios/.We can then discard the
Package.swiftchangeThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I followed the BrownfieldBundleURLResolver example. SPM can't reach outside its package root, so moving the file to ios will delete the unit tests. I can do that but just making sure we're fine with that.
For what it's worth they only cover the surface election logic, which is the simple part. The bugs I actually hit were all in the wiring and the e2e caught those, so losing them isn't a big deal.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah let's ignore this test.