forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: make SwiftUI React Native entry point (#68)
* feat: add Swift entrypoint [wip] add module maps to some RN modules to allow for swift c++ imports feat: implement RCTReactController and RCTSwiftUIAppDelegate feat: introduce new method to RCTAppDelegate * feat: modify template to use SwiftUI * fix: dimensions, use RCTMainWindow() * fix: fallback to DarkMode on visionOS * fix: use KeyWindow() in RCTPerfMonitor
- Loading branch information
1 parent
c33c583
commit 319f3a8
Showing
23 changed files
with
340 additions
and
220 deletions.
There are no files selected for viewing
This file contains 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 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
34 changes: 34 additions & 0 deletions
34
packages/react-native/Libraries/SwiftExtensions/RCTMainWindow.swift
This file contains 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,34 @@ | ||
import SwiftUI | ||
|
||
/** | ||
This SwiftUI struct returns main React Native scene. It should be used only once as it conains setup code. | ||
|
||
Example: | ||
```swift | ||
@main | ||
struct YourApp: App { | ||
@UIApplicationDelegateAdaptor var delegate: AppDelegate | ||
|
||
var body: some Scene { | ||
RCTMainWindow(moduleName: "YourApp") | ||
} | ||
} | ||
``` | ||
|
||
Note: If you want to create additional windows in your app, create a new `WindowGroup {}` and pass it a `RCTRootViewRepresentable`. | ||
*/ | ||
public struct RCTMainWindow: Scene { | ||
var moduleName: String | ||
var initialProps: RCTRootViewRepresentable.InitialPropsType | ||
|
||
public init(moduleName: String, initialProps: RCTRootViewRepresentable.InitialPropsType = nil) { | ||
self.moduleName = moduleName | ||
self.initialProps = initialProps | ||
} | ||
|
||
public var body: some Scene { | ||
WindowGroup { | ||
RCTRootViewRepresentable(moduleName: moduleName, initialProps: initialProps) | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
packages/react-native/Libraries/SwiftExtensions/RCTReactViewController.h
This file contains 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,16 @@ | ||
#import <UIKit/UIKit.h> | ||
|
||
/** | ||
A `UIViewController` responsible for embeding `RCTRootView` inside. Uses Factory pattern to retrive new view instances. | ||
Note: Used to in `RCTRootViewRepresentable` to display React views. | ||
*/ | ||
@interface RCTReactViewController : UIViewController | ||
|
||
@property (nonatomic, strong, nonnull) NSString *moduleName; | ||
@property (nonatomic, strong, nullable) NSDictionary *initialProps; | ||
|
||
- (instancetype _Nonnull)initWithModuleName:(NSString *_Nonnull)moduleName | ||
initProps:(NSDictionary *_Nullable)initProps; | ||
|
||
@end |
36 changes: 36 additions & 0 deletions
36
packages/react-native/Libraries/SwiftExtensions/RCTReactViewController.m
This file contains 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,36 @@ | ||
#import "RCTReactViewController.h" | ||
#import <React/RCTConstants.h> | ||
|
||
@protocol RCTRootViewFactoryProtocol <NSObject> | ||
|
||
- (UIView *)viewWithModuleName:(NSString *)moduleName initialProperties:(NSDictionary*)initialProperties launchOptions:(NSDictionary*)launchOptions; | ||
|
||
@end | ||
|
||
@implementation RCTReactViewController | ||
|
||
- (instancetype)initWithModuleName:(NSString *)moduleName initProps:(NSDictionary *)initProps { | ||
if (self = [super init]) { | ||
_moduleName = moduleName; | ||
_initialProps = initProps; | ||
} | ||
return self; | ||
} | ||
|
||
- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator { | ||
[[NSNotificationCenter defaultCenter] postNotificationName:RCTWindowFrameDidChangeNotification object:self]; | ||
} | ||
|
||
// TODO: Temporary solution for creating RCTRootView on demand. This should be done through factory pattern, see here: https://github.com/facebook/react-native/pull/42263 | ||
- (void)loadView { | ||
id<UIApplicationDelegate> appDelegate = [UIApplication sharedApplication].delegate; | ||
if ([appDelegate respondsToSelector:@selector(viewWithModuleName:initialProperties:launchOptions:)]) { | ||
id<RCTRootViewFactoryProtocol> delegate = (id<RCTRootViewFactoryProtocol>)appDelegate; | ||
self.view = [delegate viewWithModuleName:_moduleName initialProperties:_initialProps launchOptions:@{}]; | ||
} else { | ||
[NSException raise:@"UIApplicationDelegate:viewWithModuleName:initialProperties:launchOptions: not implemented" | ||
format:@"Make sure you subclass RCTAppDelegate"]; | ||
} | ||
} | ||
|
||
@end |
32 changes: 32 additions & 0 deletions
32
packages/react-native/Libraries/SwiftExtensions/RCTRootViewRepresentable.swift
This file contains 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,32 @@ | ||
import SwiftUI | ||
|
||
/** | ||
SwiftUI view enclosing `RCTReactViewController`. Its main purpose is to display React Native views inside of SwiftUI lifecycle. | ||
|
||
Use it create new windows in your app: | ||
Example: | ||
```swift | ||
WindowGroup { | ||
RCTRootViewRepresentable(moduleName: "YourAppName") | ||
} | ||
``` | ||
*/ | ||
public struct RCTRootViewRepresentable: UIViewControllerRepresentable { | ||
public typealias InitialPropsType = [AnyHashable: Any]? | ||
|
||
var moduleName: String | ||
var initialProps: InitialPropsType | ||
|
||
public init(moduleName: String, initialProps: InitialPropsType = nil) { | ||
self.moduleName = moduleName | ||
self.initialProps = initialProps | ||
} | ||
|
||
public func makeUIViewController(context: Context) -> UIViewController { | ||
RCTReactViewController(moduleName: moduleName, initProps: initialProps) | ||
} | ||
|
||
public func updateUIViewController(_ uiViewController: UIViewController, context: Context) { | ||
// noop | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
packages/react-native/Libraries/SwiftExtensions/React-RCTSwiftExtensions.podspec
This file contains 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,27 @@ | ||
require "json" | ||
|
||
package = JSON.parse(File.read(File.join(__dir__, "..", "..", "package.json"))) | ||
version = package['version'] | ||
|
||
source = { :git => 'https://github.com/facebook/react-native.git' } | ||
if version == '1000.0.0' | ||
# This is an unpublished version, use the latest commit hash of the react-native repo, which we’re presumably in. | ||
source[:commit] = `git rev-parse HEAD`.strip if system("git rev-parse --git-dir > /dev/null 2>&1") | ||
else | ||
source[:tag] = "v#{version}" | ||
end | ||
|
||
Pod::Spec.new do |s| | ||
s.name = "React-RCTSwiftExtensions" | ||
s.version = version | ||
s.summary = "A library for easier React Native integration with SwiftUI." | ||
s.homepage = "https://reactnative.dev/" | ||
s.license = package["license"] | ||
s.author = "Callstack" | ||
s.platforms = min_supported_versions | ||
s.source = source | ||
s.source_files = "*.{swift,h,m}" | ||
s.frameworks = ["UIKit", "SwiftUI"] | ||
|
||
s.dependency "React-Core" | ||
end |
This file contains 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 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 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 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.