@@ -16,9 +16,13 @@ import type {
1616} from '../View/ViewPropTypes' ;
1717import 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' ;
1921import LayoutAnimation from '../../LayoutAnimation/LayoutAnimation' ;
2022import StyleSheet from '../../StyleSheet/StyleSheet' ;
23+ import NativeDeviceInfo from '../../Utilities/NativeDeviceInfo' ;
2124import Platform from '../../Utilities/Platform' ;
25+ import warnOnce from '../../Utilities/warnOnce' ;
2226import { type EventSubscription } from '../../vendor/emitter/EventEmitter' ;
2327import AccessibilityInfo from '../AccessibilityInfo/AccessibilityInfo' ;
2428import 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' :
0 commit comments