forked from facebook/react-native
-
Notifications
You must be signed in to change notification settings - Fork 30
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
feat: make SwiftUI React Native entry point #68
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
2a21b1c
feat: add Swift entrypoint
okwasniewski d1c07bc
feat: modify template to use SwiftUI
okwasniewski 1f0dbbb
fix: dimensions, use RCTMainWindow()
okwasniewski cf11a72
fix: fallback to DarkMode on visionOS
okwasniewski 0fcfeaf
fix: use KeyWindow() in RCTPerfMonitor
okwasniewski 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 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
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 |
---|---|---|
|
@@ -65,3 +65,4 @@ yarn-error.log | |
|
||
# testing | ||
/coverage | ||
.yarn |
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.
Upstreamed: facebook#42231