Skip to content

Commit

Permalink
Make Modal's onDismiss work on Fabric. (facebook#42601)
Browse files Browse the repository at this point in the history
Summary:

After S390064, the OnDismiss event for Modal from D52445670 was reverted.
The diff was too big and caused the SEV, so we are trying to reimplement it gradually to make sure we don't brake anything.

The most important thing for our short term goal is to make the `OnDismiss` work only for iOS (following the official docs, Android never supported it) for Fabric (Bridge and Bridgeless).

We also want to minimize the changes t the JS infrastructure, so we are trying not to alter the JS APIs.

## The Problem:
The reason why the onDismiss event does not work is because, as soon as the `visible` property is turned to `false`, the component is removed by the React tree.
When this happens, Fabric deallocate the ShadowNode and the EventEmitter. Therefore, the event is not fired.

## The Solution:
We made this work by "delaying" when the component need to be removed from the reacat Tree.

Rather then rendering or node or not based on the `visible` props, we are introducing a `State` object that keeps track when the Modal is rendered or not.

The `state.isRendering` property is set to `true` when the `visible` prop is set to `true`.
For iOS, when `visible` prop is set to `false`, instead, we wait for the Native side to actually dismiss the View and to invoke the event. When the event is fired, we manually set the `state.isRendering` property to false and the Modal can be considered dismissed.

Notice that this makes also useless to have the Modal Native's snapshot to simulate that the modal is still presented.

## Changelog:
[iOS][Fixed] - `onDismiss` now work on iOS with Fabric, in both Bridge and Bridgeless mode.

Differential Revision: D52959996
  • Loading branch information
cipolleschi authored and facebook-github-bot committed Jan 22, 2024
1 parent 94c72d5 commit bc7a7af
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 14 deletions.
52 changes: 44 additions & 8 deletions packages/react-native/Libraries/Modal/Modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,13 @@ function confirmProps(props: Props) {
}
}

class Modal extends React.Component<Props> {
// Create a state to track whether the Modal is rendering or not.
// This is the only prop that controls whether the modal is rendered or not.
type State = {
isRendering: boolean,
};

class Modal extends React.Component<Props, State> {
static defaultProps: {|hardwareAccelerated: boolean, visible: boolean|} = {
visible: true,
hardwareAccelerated: false,
Expand All @@ -190,6 +196,9 @@ class Modal extends React.Component<Props> {
confirmProps(props);
}
this._identifier = uniqueModalIdentifier++;
this.state = {
isRendering: props.visible === true,
};
}

componentDidMount() {
Expand All @@ -212,14 +221,34 @@ class Modal extends React.Component<Props> {
}
}

componentDidUpdate() {
componentDidUpdate(prevProps) {
if (prevProps.visible === true && this.props.visible === false) {
// Update the rendering state based on visibility, platform and previous rendering state.
// On Android, if we set visible to false, we always want to stop rendering the view.
// On iOS, we stop rendering only when the OnDismiss event is received.
if (Platform.OS === 'android') {
this.setState({isRendering: false});
}
} else if (prevProps.visible === false && this.props.visible === true) {
this.setState({isRendering: true});
}

if (__DEV__) {
confirmProps(this.props);
}
}

// Helper function to encapsulate platform specific logic to show or not the Modal.
_shouldShowModal(): boolean {
if (Platform.OS === 'ios') {
return this.props.visible === true || this.state.isRendering === true;
}

return this.props.visible;
}

render(): React.Node {
if (this.props.visible !== true) {
if (!this._shouldShowModal()) {
return null;
}

Expand All @@ -244,6 +273,17 @@ class Modal extends React.Component<Props> {
this.props.children
);

const onDismiss = () => {
// OnDismiss is implemented on iOS only.
if (Platform.OS === 'ios') {
this.setState({isRendering: false}, () => {
if (this.props.onDismiss) {
this.props.onDismiss();
}
});
}
};

return (
<RCTModalHostView
animationType={animationType}
Expand All @@ -252,11 +292,7 @@ class Modal extends React.Component<Props> {
hardwareAccelerated={this.props.hardwareAccelerated}
onRequestClose={this.props.onRequestClose}
onShow={this.props.onShow}
onDismiss={() => {
if (this.props.onDismiss) {
this.props.onDismiss();
}
}}
onDismiss={onDismiss}
visible={this.props.visible}
statusBarTranslucent={this.props.statusBarTranslucent}
identifier={this._identifier}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,15 +162,9 @@ - (void)ensurePresentedOnlyIfNeeded
BOOL shouldBeHidden = _isPresented && (!_shouldPresent || !self.superview);
if (shouldBeHidden) {
_isPresented = NO;
// To animate dismissal of view controller, snapshot of
// view hierarchy needs to be added to the UIViewController.
UIView *snapshot = _modalContentsSnapshot;
[self.viewController.view addSubview:snapshot];

[self dismissViewController:self.viewController
animated:_shouldAnimatePresentation
completion:^{
[snapshot removeFromSuperview];
auto eventEmitter = [self modalEventEmitter];
if (eventEmitter) {
eventEmitter->onDismiss(ModalHostViewEventEmitter::OnDismiss{});
Expand Down

0 comments on commit bc7a7af

Please sign in to comment.