Skip to content

Conversation

@Saadnajmi
Copy link
Contributor

Summary:

Mirrors microsoft#2749 but implemented upstream for iOS.

Currently, the hotkeys to open the dev menu, toggle element inspector, and reload are registered through RCTKeyCommands. This is a global method that swizzles the application methods. This has a couple of issues:

  1. Swizzling is an escape hatch, and can get you flagged in app review for using private APIs
  2. The hotkeys are registered globally, not per instance. If you have more than one instance of React Native open, I think each of them would try to reload or show a dev menu.

Lets instead set the key commands on the root view of each RN surface (RCTSurfaceHostingView). To do this, we need to have access to RCTDevMenu, so I added it as a property to RCTSurfaceHostingView that RCTRootViewFactory can set (since it has access to the module registery.

Note 1: This does not replace the "reload" command yet, as that's registered from RCTHost, and would need some more refactoring to switch.

Note 2: I had originally implemented this by adding a ContextContainer to RCTSurfaceHostingView, but that proved to be complicated, since that class is in React-Core and must compile as a swift module, which we can't do if we import stuff from ReactCommon. I found a way around this using private headers.. but the latest iteration seems cleaner. You can look at my earlier commits if you want to see what the ContextContainer apporach involved.

Changelog:

[IOS] [CHANGED] - Add a dev menu to RCTSurfaceHostingView

Test Plan:

I can launch the dev menu and toggle the element inspector from an iPhone or iPad Simulator

@meta-cla meta-cla bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Nov 6, 2025
@facebook-github-bot facebook-github-bot added p: Microsoft Partner: Microsoft Partner Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. labels Nov 6, 2025
Comment on lines -123 to -136
#ifndef RCT_REMOVE_LEGACY_ARCH
// Necessary to allow NativeModules to lookup TurboModules
[bridge setRCTTurboModuleRegistry:turboModuleManager];

#if RCT_DEV
/**
* Instantiating DevMenu has the side-effect of registering
* shortcuts for CMD + d, CMD + i, and CMD + n via RCTDevMenu.
* Therefore, when TurboModules are enabled, we must manually create this
* NativeModule.
*/
[turboModuleManager moduleForName:"RCTDevMenu"];
#endif // end RCT_DEV

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because this is inside RCT_REMOVE_LEGACY_ARCH, I guess this code is technically frozen?

Comment on lines +223 to +228
if ([rootView isKindOfClass:[RCTSurfaceHostingView class]]) {
RCTDevMenu *devMenu = [bridge moduleForClass:[RCTDevMenu class]];
if (devMenu) {
[(RCTSurfaceHostingView *)rootView setDevMenu:devMenu];
}
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not entirely a fan of the class check here..

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

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. p: Microsoft Partner: Microsoft Partner Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants