Skip to content

Commit

Permalink
iOS: 5/7 Use visible prop to dismiss Modal for Paper
Browse files Browse the repository at this point in the history
Summary:
Changelog: [iOS] Use visible prop to dismiss Modal on old renderer.

Visible prop is used on Fabric so that onDismiss can be passed with the the bridgeless per-component event emitter, rather than the bridge global event emitter. The old renderer still uses the global event emitter.

I needed to use the visible prop for Paper too because in diff 6/7, Modal.js [no longer uses visible prop to return null](https://github.com/facebook/react-native/blob/dc80b2dcb52fadec6a573a9dd1824393f8c29fdc/Libraries/Modal/Modal.js#L221-L222), and Modal.js can't distinguish on whether it's a Fabric or Paper component.

Reviewed By: JoshuaGross

Differential Revision: D28137929

fbshipit-source-id: f6ede0019fbe498a10b822ff09fc135a9fff8ec0
  • Loading branch information
p-sun authored and facebook-github-bot committed May 2, 2021
1 parent 9ea2950 commit 7bf78ea
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 21 deletions.
4 changes: 4 additions & 0 deletions React/Views/RCTModalHostView.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
@property (nonatomic, assign, getter=isTransparent) BOOL transparent;

@property (nonatomic, copy) RCTDirectEventBlock onShow;
@property (nonatomic, assign) BOOL visible;

@property (nonatomic, copy) NSNumber *identifier;

Expand All @@ -31,6 +32,9 @@
@property (nonatomic, copy) NSArray<NSString *> *supportedOrientations;
@property (nonatomic, copy) RCTDirectEventBlock onOrientationChange;

// Fabric only
@property (nonatomic, copy) RCTBubblingEventBlock onDismiss;

- (instancetype)initWithBridge:(RCTBridge *)bridge NS_DESIGNATED_INITIALIZER;

@end
Expand Down
56 changes: 36 additions & 20 deletions React/Views/RCTModalHostView.m
Original file line number Diff line number Diff line change
Expand Up @@ -119,31 +119,13 @@ - (void)didMoveToWindow
return;
}

if (!_isPresented && self.window) {
RCTAssert(self.reactViewController, @"Can't present modal view controller without a presenting view controller");

_modalViewController.supportedInterfaceOrientations = [self supportedOrientationsMask];

if ([self.animationType isEqualToString:@"fade"]) {
_modalViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
} else if ([self.animationType isEqualToString:@"slide"]) {
_modalViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
}
if (self.presentationStyle != UIModalPresentationNone) {
_modalViewController.modalPresentationStyle = self.presentationStyle;
}
[_delegate presentModalHostView:self withViewController:_modalViewController animated:[self hasAnimationType]];
_isPresented = YES;
}
[self ensurePresentedOnlyIfNeeded];
}

- (void)didMoveToSuperview
{
[super didMoveToSuperview];

if (_isPresented && !self.superview) {
[self dismissModalViewController];
}
[self ensurePresentedOnlyIfNeeded];
}

- (void)invalidate
Expand All @@ -163,6 +145,40 @@ - (BOOL)hasAnimationType
return ![self.animationType isEqualToString:@"none"];
}

- (void)setVisible:(BOOL)visible
{
if (_visible != visible) {
_visible = visible;
[self ensurePresentedOnlyIfNeeded];
}
}

- (void)ensurePresentedOnlyIfNeeded
{
BOOL shouldBePresented = !_isPresented && _visible && self.window;
if (shouldBePresented) {
RCTAssert(self.reactViewController, @"Can't present modal view controller without a presenting view controller");

_modalViewController.supportedInterfaceOrientations = [self supportedOrientationsMask];

if ([self.animationType isEqualToString:@"fade"]) {
_modalViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
} else if ([self.animationType isEqualToString:@"slide"]) {
_modalViewController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
}
if (self.presentationStyle != UIModalPresentationNone) {
_modalViewController.modalPresentationStyle = self.presentationStyle;
}
[_delegate presentModalHostView:self withViewController:_modalViewController animated:[self hasAnimationType]];
_isPresented = YES;
}

BOOL shouldBeHidden = _isPresented && (!_visible || !self.superview);
if (shouldBeHidden) {
[self dismissModalViewController];
}
}

- (void)setTransparent:(BOOL)transparent
{
if (self.isTransparent != transparent) {
Expand Down
2 changes: 1 addition & 1 deletion React/Views/RCTModalHostViewManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ - (void)invalidate
RCT_EXPORT_VIEW_PROPERTY(identifier, NSNumber)
RCT_EXPORT_VIEW_PROPERTY(supportedOrientations, NSArray)
RCT_EXPORT_VIEW_PROPERTY(onOrientationChange, RCTDirectEventBlock)
RCT_EXPORT_VIEW_PROPERTY(visible, BOOL)

// Fabric only
RCT_EXPORT_VIEW_PROPERTY(visible, BOOL)
RCT_EXPORT_VIEW_PROPERTY(onDismiss, RCTDirectEventBlock)

@end

0 comments on commit 7bf78ea

Please sign in to comment.