Skip to content

Commit c183ecb

Browse files
Liron Moshkovichfacebook-github-bot
authored andcommitted
Modal "avoidKeyboard" prop (#52526)
Summary: Pull Request resolved: #52526 ## Changelog [iOS] [Added] - Modal "avoidKeyboard" prop ## The problem It seems like the default behavior is that in iOS the modal is not avoiding the keyboard, meaning that anywhere that there is a modal that can trigger the virtual keyboard, this issue can occour. ## The fix Addign support inside the "Modal" component using a new flag called "avoidKeyboard" which is implemented by conditionally wrapping the children of the modal with "KeyboardAvoidingView" with the needed attributes to cause the modal itself to avoid the keyboard to get the desired behavior. Reviewed By: bsalex Differential Revision: D78010672
1 parent 6b8bc5a commit c183ecb

File tree

1 file changed

+38
-11
lines changed
  • packages/react-native/Libraries/Modal

1 file changed

+38
-11
lines changed

packages/react-native/Libraries/Modal/Modal.js

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import type {ViewProps} from '../Components/View/ViewPropTypes';
1313
import type {RootTag} from '../ReactNative/RootTag';
1414
import type {DirectEventHandler} from '../Types/CodegenTypes';
1515

16+
import {KeyboardAvoidingView} from '../..';
1617
import NativeEventEmitter from '../EventEmitter/NativeEventEmitter';
1718
import {type ColorValue} from '../StyleSheet/StyleSheet';
1819
import {type EventSubscription} from '../vendor/emitter/EventEmitter';
@@ -153,6 +154,11 @@ export type ModalPropsIOS = {
153154
* This requires you to implement the `onRequestClose` prop to handle the dismissal.
154155
*/
155156
allowSwipeDismissal?: ?boolean,
157+
158+
/**
159+
* Controls whether the modal adjusts its position when the vurtual keyboard is displayed.
160+
*/
161+
avoidKeyboard?: boolean,
156162
};
157163

158164
export type ModalPropsAndroid = {
@@ -305,12 +311,25 @@ class Modal extends React.Component<ModalProps, ModalState> {
305311
}
306312
}
307313

308-
const innerChildren = __DEV__ ? (
314+
let innerChildren = __DEV__ ? (
309315
<AppContainer rootTag={this.context}>{this.props.children}</AppContainer>
310316
) : (
311317
this.props.children
312318
);
313319

320+
innerChildren = (
321+
<VirtualizedListContextResetter>
322+
<ScrollView.Context.Provider value={null}>
323+
<View
324+
// $FlowFixMe[incompatible-type]
325+
style={[styles.container, containerStyles]}
326+
collapsable={false}>
327+
{innerChildren}
328+
</View>
329+
</ScrollView.Context.Provider>
330+
</VirtualizedListContextResetter>
331+
);
332+
314333
const onDismiss = () => {
315334
// OnDismiss is implemented on iOS only.
316335
if (Platform.OS === 'ios') {
@@ -322,6 +341,18 @@ class Modal extends React.Component<ModalProps, ModalState> {
322341
}
323342
};
324343

344+
const children =
345+
this.props.avoidKeyboard === true ? (
346+
<KeyboardAvoidingView
347+
behavior={Platform.OS === 'ios' ? 'padding' : undefined}
348+
pointerEvents="box-none"
349+
style={styles.keyboardAvoidingView}>
350+
{innerChildren}
351+
</KeyboardAvoidingView>
352+
) : (
353+
innerChildren
354+
);
355+
325356
return (
326357
<RCTModalHostView
327358
/* $FlowFixMe[incompatible-type] Natural Inference rollout. See
@@ -345,16 +376,7 @@ class Modal extends React.Component<ModalProps, ModalState> {
345376
onOrientationChange={this.props.onOrientationChange}
346377
allowSwipeDismissal={this.props.allowSwipeDismissal}
347378
testID={this.props.testID}>
348-
<VirtualizedListContextResetter>
349-
<ScrollView.Context.Provider value={null}>
350-
<View
351-
// $FlowFixMe[incompatible-type]
352-
style={[styles.container, containerStyles]}
353-
collapsable={false}>
354-
{innerChildren}
355-
</View>
356-
</ScrollView.Context.Provider>
357-
</VirtualizedListContextResetter>
379+
{children}
358380
</RCTModalHostView>
359381
);
360382
}
@@ -381,6 +403,11 @@ const styles = StyleSheet.create({
381403
top: 0,
382404
flex: 1,
383405
},
406+
keyboardAvoidingView: {
407+
flex: 1,
408+
margin: 0,
409+
justifyContent: 'center',
410+
},
384411
});
385412

386413
type ModalRefProps = $ReadOnly<{

0 commit comments

Comments
 (0)