Skip to content

Commit

Permalink
KeyboardAvoider [nfc]: Allow setting keyboardVerticalOffset.
Browse files Browse the repository at this point in the history
And explain something confusing about it that's easy to run into.
  • Loading branch information
chrisbobbe committed Apr 16, 2021
1 parent bbab920 commit 70eca07
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion src/common/KeyboardAvoider.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,25 @@ type Props = $ReadOnly<{|
children: React$Node,
style?: ViewStyleProp,
contentContainerStyle?: ViewStyleProp,

/** How much the top of `KeyboardAvoider`'s layout *parent* is
* displaced downward from the top of the screen.
*
* If this isn't set correctly, the keyboard will hide some of
* the bottom of the screen (an area whose height is what this
* value should have been set to).
*
* I think `KeyboardAvoidingView`'s implementation mistakes `x`
* and `y` from `View#onLayout` to be a `View`'s position
* relative to the top left of the screen. In reality, I'm
* pretty sure they represent a `View`'s position relative to
* its parent:
* https://github.com/facebook/react-native-website/issues/2056#issuecomment-773618381
*
* But at least `KeyboardAvoidingView` exposes this prop, which
* we can use to balance the equation if we need to.
*/
keyboardVerticalOffset?: number,
|}>;

/**
Expand All @@ -20,7 +39,7 @@ type Props = $ReadOnly<{|
*/
export default class KeyboardAvoider extends PureComponent<Props> {
render() {
const { behavior, children, style, contentContainerStyle } = this.props;
const { behavior, children, style, contentContainerStyle, keyboardVerticalOffset } = this.props;

if (Platform.OS === 'android') {
return <View style={style}>{children}</View>;
Expand All @@ -30,6 +49,8 @@ export default class KeyboardAvoider extends PureComponent<Props> {
<KeyboardAvoidingView
behavior={behavior}
contentContainerStyle={contentContainerStyle}
// See comment on this prop in the jsdoc.
keyboardVerticalOffset={keyboardVerticalOffset}
style={style}
>
{children}
Expand Down

0 comments on commit 70eca07

Please sign in to comment.