Skip to content
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

Fix responder chain in reactViewController on macOS #2380

Conversation

gabrieldonadel
Copy link

Summary:

I first noticed this while working on SwiftUI views for expo/expo#35078. Calling reactViewController on iOS always returns the correct view controller but on macOS, this same function will return nil for nested views.

So this PR addresses the differences in responder chain behavior between iOS and macOS when aliasing UIViewController to NSViewController. On iOS, a view's nextResponder is naturally its own UIViewController, ensuring that helper functions like reactViewController work as expected. However, on macOS, an NSView's nextResponder does not include its NSViewController by default, which causes issues when trying to retrieve the view controller directly from the responder chain.
This change ensures that traversals will properly locate the NSViewController using the superview chain, mirroring iOS behavior.

Test Plan:

Run RNTester and we've been using this same function to render SwiftUI views on macOS in expo's repo

@gabrieldonadel gabrieldonadel requested a review from a team as a code owner February 25, 2025 13:56
@Saadnajmi
Copy link
Collaborator

However, on macOS, an NSView's nextResponder does not include its NSViewController by default, which causes issues when trying to retrieve the view controller directly from the responder chain.

I thought it did? What are you seeing as the nextResponder on your end? I'm going off of this doc fwiw https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/EventOverview/EventArchitecture/EventArchitecture.html

@gabrieldonadel
Copy link
Author

I thought it did? What are you seeing as the nextResponder on your end? I'm going off of this doc fwiw https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/EventOverview/EventArchitecture/EventArchitecture.html

When I call reactViewController in

https://github.com/expo/expo/blob/6027354a2fd21d1c8c678780050697c1919732a0/packages/expo-modules-core/ios/Core/Views/SwiftUI/SwiftUIHostingView.swift#L165-L175

this internally will call nextResponder and that is nil

@Saadnajmi
Copy link
Collaborator

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@Saadnajmi
Copy link
Collaborator

/azp run

Copy link

Azure Pipelines successfully started running 1 pipeline(s).

@Saadnajmi Saadnajmi merged commit 104e9f8 into microsoft:main Feb 28, 2025
10 checks passed
@Saadnajmi
Copy link
Collaborator

@gabrieldonadel If you'd like this on 0.76 or 0.77, feel free to make a backport PR, and I'll make sure we get a published patch release with it. Otherwise this will land with RNM 0.78

@gabrieldonadel gabrieldonadel deleted the @gabrieldonadel/fix-react-view-controller branch March 3, 2025 13:29
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