Skip to content

Commit 1ff4397

Browse files
authored
Merge pull request #5889 from parasharrajat/ESC-confirmModal
Fix: Modal and screen closing with single `esacpe` keypress
2 parents 7f3153a + 132b7e6 commit 1ff4397

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

src/components/Modal/BaseModal.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import styles, {getModalPaddingStyles, getSafeAreaPadding} from '../../styles/st
77
import themeColors from '../../styles/themes/default';
88
import {propTypes as modalPropTypes, defaultProps as modalDefaultProps} from './ModalPropTypes';
99
import getModalStyles from '../../styles/getModalStyles';
10-
import {setModalVisibility} from '../../libs/actions/Modal';
10+
import {setModalVisibility, willAlertModalBecomeVisible} from '../../libs/actions/Modal';
1111

1212
const propTypes = {
1313
...modalPropTypes,
@@ -28,6 +28,12 @@ class BaseModal extends PureComponent {
2828
this.hideModal = this.hideModal.bind(this);
2929
}
3030

31+
componentDidUpdate(prevProps) {
32+
if (prevProps.isVisible !== this.props.isVisible) {
33+
willAlertModalBecomeVisible(this.props.isVisible);
34+
}
35+
}
36+
3137
componentWillUnmount() {
3238
// we don't want to call the onModalHide on unmount
3339
this.hideModal(this.props.isVisible);

src/components/ScreenWrapper.js

+22-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@ import PropTypes from 'prop-types';
44
import {View} from 'react-native';
55
import {withNavigation} from '@react-navigation/compat';
66
import {SafeAreaInsetsContext} from 'react-native-safe-area-context';
7+
import {withOnyx} from 'react-native-onyx';
78
import styles, {getSafeAreaPadding} from '../styles/styles';
89
import HeaderGap from './HeaderGap';
910
import KeyboardShortcut from '../libs/KeyboardShortcut';
1011
import onScreenTransitionEnd from '../libs/onScreenTransitionEnd';
1112
import Navigation from '../libs/Navigation/Navigation';
13+
import compose from '../libs/compose';
14+
import ONYXKEYS from '../ONYXKEYS';
1215

1316
const propTypes = {
1417
/** Array of additional styles to add */
@@ -34,6 +37,13 @@ const propTypes = {
3437
// Method to attach listener to Navigation state.
3538
addListener: PropTypes.func.isRequired,
3639
}),
40+
41+
/** Details about any modals being used */
42+
modal: PropTypes.shape({
43+
/** Indicates when an Alert modal is about to be visible */
44+
willAlertModalBecomeVisible: PropTypes.bool,
45+
}),
46+
3747
};
3848

3949
const defaultProps = {
@@ -44,6 +54,7 @@ const defaultProps = {
4454
navigation: {
4555
addListener: () => {},
4656
},
57+
modal: {},
4758
};
4859

4960
class ScreenWrapper extends React.Component {
@@ -56,7 +67,9 @@ class ScreenWrapper extends React.Component {
5667

5768
componentDidMount() {
5869
this.unsubscribeEscapeKey = KeyboardShortcut.subscribe('Escape', () => {
59-
Navigation.dismissModal();
70+
if (!this.props.modal.willAlertModalBecomeVisible) {
71+
Navigation.dismissModal();
72+
}
6073
}, [], true);
6174

6275
this.unsubscribeTransitionEnd = onScreenTransitionEnd(this.props.navigation, () => {
@@ -116,4 +129,11 @@ class ScreenWrapper extends React.Component {
116129
ScreenWrapper.propTypes = propTypes;
117130
ScreenWrapper.defaultProps = defaultProps;
118131
ScreenWrapper.displayName = 'ScreenWrapper';
119-
export default withNavigation(ScreenWrapper);
132+
export default compose(
133+
withNavigation,
134+
withOnyx({
135+
modal: {
136+
key: ONYXKEYS.MODAL,
137+
},
138+
}),
139+
)(ScreenWrapper);

src/libs/actions/Modal.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,23 @@ import ONYXKEYS from '../../ONYXKEYS';
44
/**
55
* Allows other parts of the app to know when a modal has been opened or closed
66
*
7-
* @param {bool} isVisible
7+
* @param {Boolean} isVisible
88
*/
99
function setModalVisibility(isVisible) {
1010
Onyx.merge(ONYXKEYS.MODAL, {isVisible});
1111
}
1212

13+
/**
14+
* Allows other parts of app to know that an alert modal is about to open.
15+
* This will trigger as soon as a modal is opened but not yet visible while animation is running.
16+
*
17+
* @param {Boolean} isVisible
18+
*/
19+
function willAlertModalBecomeVisible(isVisible) {
20+
Onyx.merge(ONYXKEYS.MODAL, {willAlertModalBecomeVisible: isVisible});
21+
}
22+
1323
export {
14-
// eslint-disable-next-line import/prefer-default-export
1524
setModalVisibility,
25+
willAlertModalBecomeVisible,
1626
};

0 commit comments

Comments
 (0)