@@ -16,8 +16,12 @@ 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' ;
21+ import Animated from '../../Animated/Animated' ;
1922import LayoutAnimation from '../../LayoutAnimation/LayoutAnimation' ;
2023import StyleSheet from '../../StyleSheet/StyleSheet' ;
24+ import NativeDeviceInfo from '../../Utilities/NativeDeviceInfo' ;
2125import Platform from '../../Utilities/Platform' ;
2226import { type EventSubscription } from '../../vendor/emitter/EventEmitter' ;
2327import AccessibilityInfo from '../AccessibilityInfo/AccessibilityInfo' ;
@@ -44,6 +48,15 @@ type KeyboardAvoidingViewPropsCore = Readonly<{
4448 */
4549 enabled ?: ?boolean ,
4650
51+ /**
52+ * Follow the keyboard natively. Requires the shared animation backend, and
53+ * on Android, an edge-to-edge app. Before Android 11, the view animates on
54+ * its own curve once the keyboard insets change.
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,52 @@ type KeyboardAvoidingViewState = {
6376 bottom : number ,
6477} ;
6578
79+ const AnimatedKeyboardAvoidingViewNativeComponent =
80+ Animated . createAnimatedComponent ( KeyboardAvoidingViewNativeComponent ) ;
81+
82+ let isNativeSupported : ?boolean = null ;
83+
84+ function shouldUseNative ( props : KeyboardAvoidingViewProps ) : boolean {
85+ if ( props . experimental_native !== true || props . behavior == null ) {
86+ return false ;
87+ }
88+ if ( isNativeSupported != null ) {
89+ return isNativeSupported ;
90+ }
91+
92+ isNativeSupported = false ;
93+
94+ if (
95+ ! ReactNativeFeatureFlags . cxxNativeAnimatedEnabled ( ) ||
96+ // eslint-disable-next-line react-hooks/rules-of-hooks
97+ ! ReactNativeFeatureFlags . useSharedAnimatedBackend ( )
98+ ) {
99+ if ( __DEV__ ) {
100+ console . warn (
101+ 'KeyboardAvoidingView: `experimental_native` has no effect because ' +
102+ 'the shared animation backend is disabled.' ,
103+ ) ;
104+ }
105+ return false ;
106+ }
107+
108+ if (
109+ Platform . OS === 'android' &&
110+ NativeDeviceInfo . getConstants ( ) . isEdgeToEdge !== true
111+ ) {
112+ if ( __DEV__ ) {
113+ console . warn (
114+ 'KeyboardAvoidingView: `experimental_native` has no effect because ' +
115+ 'the app is not edge-to-edge.' ,
116+ ) ;
117+ }
118+ return false ;
119+ }
120+
121+ isNativeSupported = true ;
122+ return true ;
123+ }
124+
66125/**
67126 * Automatically adjusts its height, position, or bottom padding based on the
68127 * keyboard height to remain visible while the virtual keyboard is displayed.
@@ -79,6 +138,9 @@ class KeyboardAvoidingView extends React.Component<
79138 viewRef : { current : React . ElementRef < typeof View > | null , ...} ;
80139 _initialFrameHeight : number = 0 ;
81140 _bottom : number = 0 ;
141+ _keyboardInset : Animated . Value = new Animated . Value ( 0 ) ;
142+ // Yoga reads an infinite `maxHeight` as unset, unlike 0.
143+ _keyboardMaxHeight : Animated . Value = new Animated . Value ( Infinity ) ;
82144
83145 constructor ( props : KeyboardAvoidingViewProps ) {
84146 super ( props ) ;
@@ -125,6 +187,19 @@ class KeyboardAvoidingView extends React.Component<
125187 this . _updateBottomIfNecessary ( ) ;
126188 } ;
127189
190+ _onKeyboardInsetChange: ( ...args : ReadonlyArray < unknown > ) => void =
191+ Animated . event (
192+ [
193+ {
194+ nativeEvent : {
195+ inset : this . _keyboardInset ,
196+ maxHeight : this . _keyboardMaxHeight ,
197+ } ,
198+ } ,
199+ ] ,
200+ { useNativeDriver : true } ,
201+ ) ;
202+
128203 _onKeyboardHide = ( event : ?KeyboardEvent ) => {
129204 this . _keyboardEvent = null ;
130205 // $FlowFixMe[unused-promise]
@@ -161,6 +236,10 @@ class KeyboardAvoidingView extends React.Component<
161236 } ;
162237
163238 _updateBottomIfNecessary = async ( ) => {
239+ if ( shouldUseNative ( this . props ) ) {
240+ return ;
241+ }
242+
164243 if ( this . _keyboardEvent == null ) {
165244 this . _setBottom ( 0 ) ;
166245 return ;
@@ -196,13 +275,39 @@ class KeyboardAvoidingView extends React.Component<
196275 if ( enabled && this . _bottom !== prevState . bottom ) {
197276 this . setState ( { bottom : this . _bottom } ) ;
198277 }
278+ this . _updateSubscriptions ( ) ;
199279 }
200280
201281 componentDidMount ( ) : void {
202282 if ( ! Keyboard . isVisible ( ) ) {
203283 this . _keyboardEvent = null ;
204284 this . _setBottom ( 0 ) ;
205285 }
286+ this . _updateSubscriptions ( ) ;
287+ }
288+
289+ _updateSubscriptions ( ) : void {
290+ const shouldSubscribe = ! shouldUseNative ( this . props ) ;
291+
292+ if ( shouldSubscribe === this . _subscriptions . length > 0 ) {
293+ return ;
294+ }
295+
296+ if ( ! shouldSubscribe ) {
297+ this . _subscriptions . forEach ( subscription => {
298+ subscription . remove ( ) ;
299+ } ) ;
300+
301+ this . _subscriptions = [ ] ;
302+ // The last event goes stale while unsubscribed.
303+ this . _keyboardEvent = null ;
304+
305+ if ( this . _bottom !== 0 ) {
306+ this . _setBottom ( 0 ) ;
307+ }
308+
309+ return ;
310+ }
206311
207312 if ( Platform . OS === 'ios' ) {
208313 this . _subscriptions = [
@@ -235,12 +340,39 @@ class KeyboardAvoidingView extends React.Component<
235340 children ,
236341 contentContainerStyle ,
237342 enabled = true ,
238- // eslint-disable-next-line no-unused-vars
343+ experimental_native ,
239344 keyboardVerticalOffset = 0 ,
240345 style ,
241346 onLayout ,
242347 ...props
243348 } = this . props ;
349+
350+ if ( shouldUseNative ( this . props ) ) {
351+ return (
352+ < AnimatedKeyboardAvoidingViewNativeComponent
353+ ref = { this . viewRef }
354+ enabled = { enabled === true }
355+ keyboardVerticalOffset = { keyboardVerticalOffset }
356+ onKeyboardInsetChange = { this . _onKeyboardInsetChange }
357+ onLayout = { onLayout }
358+ style = { [
359+ style ,
360+ behavior === 'height' && { maxHeight : this . _keyboardMaxHeight } ,
361+ behavior === 'padding' && { paddingBottom : this . _keyboardInset } ,
362+ ] }
363+ { ...props } >
364+ { behavior === 'position' ? (
365+ < Animated . View
366+ style = { [ contentContainerStyle , { bottom : this . _keyboardInset } ] } >
367+ { children }
368+ </ Animated . View >
369+ ) : (
370+ children
371+ ) }
372+ </ AnimatedKeyboardAvoidingViewNativeComponent >
373+ ) ;
374+ }
375+
244376 const bottomHeight = enabled === true ? this . state . bottom : 0 ;
245377 switch ( behavior ) {
246378 case 'height' :
0 commit comments