Skip to content

Commit a9ac94e

Browse files
committed
Add native KeyboardAvoidingView
# Conflicts: # scripts/cxx-api/api-snapshots/ReactAndroidFrameworksCxx.api # scripts/cxx-api/api-snapshots/ReactAppleFrameworksCxx.api # scripts/cxx-api/api-snapshots/ReactCommonFrameworksCxx.api
1 parent 4eaa1bb commit a9ac94e

38 files changed

Lines changed: 1565 additions & 3 deletions

‎packages/react-native/Libraries/Components/Keyboard/KeyboardAvoidingView.js‎

Lines changed: 117 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,13 @@ import type {
1616
} from '../View/ViewPropTypes';
1717
import type {KeyboardEvent, KeyboardMetrics} from './Keyboard';
1818

19+
import KeyboardAvoidingViewNativeComponent from '../../../src/private/components/keyboardavoidingview/specs/KeyboardAvoidingViewNativeComponent';
20+
import * as ReactNativeFeatureFlags from '../../../src/private/featureflags/ReactNativeFeatureFlags';
1921
import LayoutAnimation from '../../LayoutAnimation/LayoutAnimation';
2022
import StyleSheet from '../../StyleSheet/StyleSheet';
23+
import NativeDeviceInfo from '../../Utilities/NativeDeviceInfo';
2124
import Platform from '../../Utilities/Platform';
25+
import warnOnce from '../../Utilities/warnOnce';
2226
import {type EventSubscription} from '../../vendor/emitter/EventEmitter';
2327
import AccessibilityInfo from '../AccessibilityInfo/AccessibilityInfo';
2428
import View from '../View/View';
@@ -44,6 +48,15 @@ type KeyboardAvoidingViewPropsCore = Readonly<{
4448
*/
4549
enabled?: ?boolean,
4650

51+
/**
52+
* Follow the keyboard animation natively, frame by frame.
53+
* Requires the `useSharedAnimatedBackend` feature flag and an edge-to-edge
54+
* app; otherwise, this prop has no effect.
55+
*
56+
* @default `false`
57+
*/
58+
experimental_native?: ?boolean,
59+
4760
/**
4861
* Distance between the top of the user screen and the React Native view,
4962
* may be non-zero in some use cases.
@@ -63,6 +76,46 @@ type KeyboardAvoidingViewState = {
6376
bottom: number,
6477
};
6578

79+
function getIsNativeSupported(): boolean {
80+
// eslint-disable-next-line react-hooks/rules-of-hooks
81+
if (!ReactNativeFeatureFlags.useSharedAnimatedBackend()) {
82+
if (__DEV__) {
83+
warnOnce(
84+
'KeyboardAvoidingView-experimental_native-flag',
85+
'KeyboardAvoidingView: `experimental_native` has no effect because ' +
86+
'the `useSharedAnimatedBackend` feature flag is disabled.',
87+
);
88+
}
89+
return false;
90+
}
91+
if (
92+
Platform.OS === 'android' &&
93+
NativeDeviceInfo.getConstants().isEdgeToEdge !== true
94+
) {
95+
if (__DEV__) {
96+
warnOnce(
97+
'KeyboardAvoidingView-experimental_native-edge-to-edge',
98+
'KeyboardAvoidingView: `experimental_native` has no effect because ' +
99+
'the app is not edge-to-edge.',
100+
);
101+
}
102+
return false;
103+
}
104+
return true;
105+
}
106+
107+
let isNativeSupported: ?boolean = null;
108+
109+
function shouldUseNative(props: KeyboardAvoidingViewProps): boolean {
110+
if (props.experimental_native !== true || props.behavior == null) {
111+
return false;
112+
}
113+
if (isNativeSupported == null) {
114+
isNativeSupported = getIsNativeSupported();
115+
}
116+
return isNativeSupported;
117+
}
118+
66119
/**
67120
* Automatically adjusts its height, position, or bottom padding based on the
68121
* keyboard height to remain visible while the virtual keyboard is displayed.
@@ -161,6 +214,10 @@ class KeyboardAvoidingView extends React.Component<
161214
};
162215

163216
_updateBottomIfNecessary = async () => {
217+
if (shouldUseNative(this.props)) {
218+
return;
219+
}
220+
164221
if (this._keyboardEvent == null) {
165222
this._setBottom(0);
166223
return;
@@ -196,13 +253,37 @@ class KeyboardAvoidingView extends React.Component<
196253
if (enabled && this._bottom !== prevState.bottom) {
197254
this.setState({bottom: this._bottom});
198255
}
256+
this._updateSubscriptions();
199257
}
200258

201259
componentDidMount(): void {
202260
if (!Keyboard.isVisible()) {
203261
this._keyboardEvent = null;
204262
this._setBottom(0);
205263
}
264+
this._updateSubscriptions();
265+
}
266+
267+
// Only the JavaScript implementation needs keyboard events.
268+
_updateSubscriptions(): void {
269+
const shouldSubscribe = !shouldUseNative(this.props);
270+
271+
if (shouldSubscribe === this._subscriptions.length > 0) {
272+
return;
273+
}
274+
275+
if (!shouldSubscribe) {
276+
this._subscriptions.forEach(subscription => {
277+
subscription.remove();
278+
});
279+
this._subscriptions = [];
280+
// The last event goes stale while unsubscribed.
281+
this._keyboardEvent = null;
282+
if (this._bottom !== 0) {
283+
this._setBottom(0);
284+
}
285+
return;
286+
}
206287

207288
if (Platform.OS === 'ios') {
208289
this._subscriptions = [
@@ -235,12 +316,47 @@ class KeyboardAvoidingView extends React.Component<
235316
children,
236317
contentContainerStyle,
237318
enabled = true,
238-
// eslint-disable-next-line no-unused-vars
319+
experimental_native,
239320
keyboardVerticalOffset = 0,
240321
style,
241322
onLayout,
242323
...props
243324
} = this.props;
325+
326+
if (shouldUseNative(this.props)) {
327+
if (behavior === 'position') {
328+
return (
329+
<View
330+
ref={this.viewRef}
331+
style={style}
332+
onLayout={onLayout}
333+
collapsable={false}
334+
{...props}>
335+
<KeyboardAvoidingViewNativeComponent
336+
behavior="position"
337+
enabled={enabled === true}
338+
keyboardVerticalOffset={keyboardVerticalOffset}
339+
style={contentContainerStyle}>
340+
{children}
341+
</KeyboardAvoidingViewNativeComponent>
342+
</View>
343+
);
344+
}
345+
346+
return (
347+
<KeyboardAvoidingViewNativeComponent
348+
ref={this.viewRef}
349+
behavior={behavior}
350+
enabled={enabled === true}
351+
keyboardVerticalOffset={keyboardVerticalOffset}
352+
style={style}
353+
onLayout={onLayout}
354+
{...props}>
355+
{children}
356+
</KeyboardAvoidingViewNativeComponent>
357+
);
358+
}
359+
244360
const bottomHeight = enabled === true ? this.state.bottom : 0;
245361
switch (behavior) {
246362
case 'height':

‎packages/react-native/Package.swift‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,7 @@ let reactFabric = RNTarget(
490490
"consistency",
491491
"uimanager/consistency/tests",
492492
"components/inputaccessory",
493+
"components/keyboardavoidingview",
493494
"components/modal",
494495
"components/rncore",
495496
"components/safeareaview",
@@ -513,6 +514,12 @@ let reactFabricInputAccessory = RNTarget(
513514
dependencies: [.reactNativeDependencies, .reactCore, .reactJsiExecutor, .reactTurboModuleCore, .jsi, .logger, .reactDebug, .reactFeatureFlags, .reactUtils, .reactRuntimeScheduler, .reactCxxReact, .yoga, .reactRendererDebug, .reactGraphics, .reactFabric, .reactTurboModuleBridging]
514515
)
515516

517+
let reactFabricKeyboardAvoidingView = RNTarget(
518+
name: .reactFabricKeyboardAvoidingView,
519+
path: "ReactCommon/react/renderer/components/keyboardavoidingview",
520+
dependencies: [.reactNativeDependencies, .reactCore, .reactJsiExecutor, .reactTurboModuleCore, .jsi, .logger, .reactDebug, .reactFeatureFlags, .reactUtils, .reactRuntimeScheduler, .reactCxxReact, .yoga, .reactRendererDebug, .reactGraphics, .reactFabric, .reactTurboModuleBridging]
521+
)
522+
516523
let reactFabricModal = RNTarget(
517524
name: .reactFabricModal,
518525
path: "ReactCommon/react/renderer/components/modal",
@@ -588,7 +595,7 @@ let reactRCTFabric = RNTarget(
588595
name: .reactRCTFabric,
589596
path: "React/Fabric",
590597
searchPaths: [virtualViewPath],
591-
dependencies: [.reactNativeDependencies, .reactCore, .reactRCTImage, .yoga, .reactRCTText, .jsi, .reactFabricInputAccessory, .reactFabricModal, .reactFabricSafeAreaView, .reactFabricSwitch, .reactFabricText, .reactFabricTextInput, .reactFabricUnimplementedView, .reactFabricTextLayoutManager, .reactGraphics, .reactImageManager, .reactDebug, .reactUtils, .reactPerformanceTimeline, .reactRendererDebug, .reactRendererConsistency, .reactRuntimeScheduler, .reactRCTAnimation, .reactJsInspector, .reactJsInspectorNetwork, .reactJsInspectorTracing, .reactFabric, .reactFabricImage, .rctSwiftUIWrapper]
598+
dependencies: [.reactNativeDependencies, .reactCore, .reactRCTImage, .yoga, .reactRCTText, .jsi, .reactFabricInputAccessory, .reactFabricKeyboardAvoidingView, .reactFabricModal, .reactFabricSafeAreaView, .reactFabricSwitch, .reactFabricText, .reactFabricTextInput, .reactFabricUnimplementedView, .reactFabricTextLayoutManager, .reactGraphics, .reactImageManager, .reactDebug, .reactUtils, .reactPerformanceTimeline, .reactRendererDebug, .reactRendererConsistency, .reactRuntimeScheduler, .reactRCTAnimation, .reactJsInspector, .reactJsInspectorNetwork, .reactJsInspectorTracing, .reactFabric, .reactFabricImage, .rctSwiftUIWrapper]
592599
)
593600

594601
/// React-ImageManagerApple.podspec
@@ -710,6 +717,7 @@ let targets = [
710717
reactRCTFabric,
711718
reactFabricImage,
712719
reactFabricInputAccessory,
720+
reactFabricKeyboardAvoidingView,
713721
reactFabricModal,
714722
reactFabricSafeAreaView,
715723
reactFabricSwitch,
@@ -897,6 +905,7 @@ extension String {
897905

898906
static let reactFabricImage = "React-FabricImage"
899907
static let reactFabricInputAccessory = "React-FabricInputAccessory"
908+
static let reactFabricKeyboardAvoidingView = "React-FabricKeyboardAvoidingView"
900909
static let reactFabricModal = "React-FabricModal"
901910
static let reactFabricSafeAreaView = "React-FabricSafeAreaView"
902911
static let reactFabricSwitch = "React-FabricSwitch"
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright (c) Meta Platforms, Inc. and affiliates.
3+
*
4+
* This source code is licensed under the MIT license found in the
5+
* LICENSE file in the root directory of this source tree.
6+
*/
7+
8+
#import <UIKit/UIKit.h>
9+
10+
#import <React/RCTViewComponentView.h>
11+
12+
NS_ASSUME_NONNULL_BEGIN
13+
14+
/**
15+
* UIView class for <KeyboardAvoidingView> component.
16+
*
17+
* Follows the keyboard with `UIKeyboardLayoutGuide` and sends the keyboard
18+
* inset to the shared animation backend on each frame.
19+
*/
20+
@interface RCTKeyboardAvoidingViewComponentView : RCTViewComponentView
21+
22+
@end
23+
24+
NS_ASSUME_NONNULL_END

0 commit comments

Comments
 (0)