Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ChatScreen: Fix blank strip between compose box and keyboard on iOS #5564

Merged
merged 1 commit into from
Nov 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/chat/ChatScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,14 @@ export default function ChatScreen(props: Props): Node {
);

return (
<KeyboardAvoider style={[componentStyles.screen, { backgroundColor }]} behavior="padding">
<KeyboardAvoider
style={[componentStyles.screen, { backgroundColor }]}
behavior="padding"
compensateOverpadding={
// We let the compose box pad the bottom inset.
showComposeBox
Comment on lines +204 to +206
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's have a comment at the use of SafeAreaView in ComposeBox, too, saying that when the keyboard is open it's compensated for here.

}
>
<ChatNavBar narrow={narrow} editMessage={editMessage} />
<UnreadNotice narrow={narrow} />
{(() => {
Expand Down
38 changes: 34 additions & 4 deletions src/common/KeyboardAvoider.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import React from 'react';
import type { Node } from 'react';
import { Platform, View } from 'react-native';
import type { ViewStyleProp } from 'react-native/Libraries/StyleSheet/StyleSheet';
import { useSafeAreaInsets } from 'react-native-safe-area-context';

import KeyboardAvoidingView from '../third/react-native/KeyboardAvoidingView';

Expand All @@ -12,8 +13,9 @@ type Props = $ReadOnly<{|
style?: ViewStyleProp,
contentContainerStyle?: ViewStyleProp,

/** How much the top of `KeyboardAvoider`'s layout *parent* is
* displaced downward from the top of the screen.
/**
* 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
Expand All @@ -30,6 +32,19 @@ type Props = $ReadOnly<{|
* we can use to balance the equation if we need to.
*/
keyboardVerticalOffset?: number,

/**
* Whether to shorten the excursion by the height of the bottom inset.
*
* Use this when a child component uses SafeAreaView to pad the bottom
* inset, and you want the open keyboard to cover that padding.
*
* (SafeAreaView has a bug where it applies a constant padding no matter
* where it's positioned onscreen -- so in particular, if you ask for
* bottom padding, you'll get bottom padding even when KeyboardAvoider
* pushes the SafeAreaView up and out of the bottom inset.)
Comment on lines +39 to +45
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems very clear, thanks!

*/
compensateOverpadding?: boolean,
|}>;

/**
Expand All @@ -40,7 +55,16 @@ type Props = $ReadOnly<{|
* to that component.
*/
export default function KeyboardAvoider(props: Props): Node {
const { behavior, children, style, contentContainerStyle, keyboardVerticalOffset } = props;
const {
behavior,
children,
style,
contentContainerStyle,
keyboardVerticalOffset = 0, // Same default value as KeyboardAvoidingView
compensateOverpadding = false,
} = props;

const bottomInsetHeight = useSafeAreaInsets().bottom;

if (Platform.OS === 'android') {
return <View style={style}>{children}</View>;
Expand All @@ -51,7 +75,13 @@ export default function KeyboardAvoider(props: Props): Node {
behavior={behavior}
contentContainerStyle={contentContainerStyle}
// See comment on this prop in the jsdoc.
keyboardVerticalOffset={keyboardVerticalOffset}
keyboardVerticalOffset={
keyboardVerticalOffset
// A negative term here reduces the bottom inset we use for the
// child, when the keyboard is open, so that the keyboard covers its
// bottom padding.
+ (compensateOverpadding ? -bottomInsetHeight : 0)
}
style={style}
>
{children}
Expand Down
2 changes: 2 additions & 0 deletions src/compose/ComposeBox.js
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,8 @@ const ComposeBox: React$AbstractComponent<Props, ImperativeHandle> = forwardRef(
</View>
<SafeAreaView
mode="padding"
// When the keyboard is open, this bottom padding is compensated
// for: we pass KeyboardAvoider's compensateOverpadding prop.
edges={['bottom']}
style={styles.composeBox}
onLayout={handleLayoutChange}
Expand Down