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

Fix <KeyboardAvoidingView> with floating keyboard on iPad #44859

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/

import type {ViewStyleProp} from '../../StyleSheet/StyleSheet';
import type {DimensionsPayload} from '../../Utilities/NativeDeviceInfo';
import type {
ViewLayout,
ViewLayoutEvent,
Expand All @@ -18,6 +19,7 @@ import type {KeyboardEvent, KeyboardMetrics} from './Keyboard';

import LayoutAnimation from '../../LayoutAnimation/LayoutAnimation';
import StyleSheet from '../../StyleSheet/StyleSheet';
import Dimensions from '../../Utilities/Dimensions';
import Platform from '../../Utilities/Platform';
import {type EventSubscription} from '../../vendor/emitter/EventEmitter';
import AccessibilityInfo from '../AccessibilityInfo/AccessibilityInfo';
Expand Down Expand Up @@ -66,6 +68,7 @@ class KeyboardAvoidingView extends React.Component<Props, State> {
viewRef: {current: React.ElementRef<typeof View> | null, ...};
_initialFrameHeight: number = 0;
_bottom: number = 0;
_windowWidth: number = Dimensions.get('window').width;

constructor(props: Props) {
super(props);
Expand Down Expand Up @@ -130,6 +133,10 @@ class KeyboardAvoidingView extends React.Component<Props, State> {
}
};

_onDimensionsChange = ({window}: DimensionsPayload) => {
this._windowWidth = window?.width ?? 0;
};

// Avoid unnecessary renders if the KeyboardAvoidingView is disabled.
_setBottom = (value: number) => {
const enabled = this.props.enabled ?? true;
Expand All @@ -145,6 +152,15 @@ class KeyboardAvoidingView extends React.Component<Props, State> {
return;
}

if (
Platform.OS === 'ios' &&
this._windowWidth !== this._keyboardEvent.endCoordinates.width
) {
// The keyboard is not the standard bottom-of-the-screen keyboard. For example, floating keyboard on iPadOS.
this._setBottom(0);
return;
}

const {duration, easing, endCoordinates} = this._keyboardEvent;
const height = await this._relativeKeyboardHeight(endCoordinates);

Expand Down Expand Up @@ -178,6 +194,7 @@ class KeyboardAvoidingView extends React.Component<Props, State> {
if (Platform.OS === 'ios') {
this._subscriptions = [
Keyboard.addListener('keyboardWillChangeFrame', this._onKeyboardChange),
Dimensions.addEventListener('change', this._onDimensionsChange),
];
} else {
this._subscriptions = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1780,10 +1780,12 @@ declare class KeyboardAvoidingView extends React.Component<Props, State> {
viewRef: { current: React.ElementRef<typeof View> | null, ... };
_initialFrameHeight: number;
_bottom: number;
_windowWidth: number;
constructor(props: Props): void;
_relativeKeyboardHeight(keyboardFrame: KeyboardMetrics): Promise<number>;
_onKeyboardChange: $FlowFixMe;
_onLayout: $FlowFixMe;
_onDimensionsChange: $FlowFixMe;
_setBottom: $FlowFixMe;
_updateBottomIfNecessary: $FlowFixMe;
componentDidUpdate(_: Props, prevState: State): void;
Expand Down
Loading