Skip to content

Commit 3353f60

Browse files
committed
Add translate-with-padding
1 parent eec2ff5 commit 3353f60

15 files changed

Lines changed: 312 additions & 30 deletions

File tree

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ type KeyboardAvoidingViewPropsCore = Readonly<{
3434
/**
3535
* Specify how to react to the presence of the keyboard.
3636
*/
37-
behavior?: ?('height' | 'position' | 'padding'),
37+
behavior?: ?('height' | 'position' | 'padding' | 'translate-with-padding'),
3838

3939
/**
4040
* The style of the content container (View) when `behavior` is 'position'.
@@ -138,9 +138,15 @@ class KeyboardAvoidingView extends React.Component<
138138
viewRef: {current: React.ElementRef<typeof View> | null, ...};
139139
_initialFrameHeight: number = 0;
140140
_bottom: number = 0;
141+
141142
_keyboardInset: Animated.Value = new Animated.Value(0);
142143
// Yoga reads an infinite `maxHeight` as unset, unlike 0.
143144
_keyboardMaxHeight: Animated.Value = new Animated.Value(Infinity);
145+
_keyboardSettledInset: Animated.Value = new Animated.Value(0);
146+
_keyboardTranslateY: Animated.Node = Animated.multiply(
147+
this._keyboardInset,
148+
-1,
149+
);
144150

145151
constructor(props: KeyboardAvoidingViewProps) {
146152
super(props);
@@ -194,6 +200,7 @@ class KeyboardAvoidingView extends React.Component<
194200
nativeEvent: {
195201
inset: this._keyboardInset,
196202
maxHeight: this._keyboardMaxHeight,
203+
settledInset: this._keyboardSettledInset,
197204
},
198205
},
199206
],
@@ -359,6 +366,10 @@ class KeyboardAvoidingView extends React.Component<
359366
style,
360367
behavior === 'height' && {maxHeight: this._keyboardMaxHeight},
361368
behavior === 'padding' && {paddingBottom: this._keyboardInset},
369+
behavior === 'translate-with-padding' && {
370+
paddingTop: this._keyboardSettledInset,
371+
transform: [{translateY: this._keyboardTranslateY}],
372+
},
362373
]}
363374
{...props}>
364375
{behavior === 'position' ? (
@@ -414,6 +425,7 @@ class KeyboardAvoidingView extends React.Component<
414425
);
415426

416427
case 'padding':
428+
case 'translate-with-padding':
417429
return (
418430
<View
419431
ref={this.viewRef}

‎packages/react-native/React/Fabric/Mounting/ComponentViews/KeyboardAvoidingView/RCTKeyboardAvoidingViewComponentView.mm‎

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,11 @@ @implementation RCTKeyboardAvoidingViewComponentView {
4545
CGFloat _guideOwnerBottomSafeArea;
4646
BOOL _isTrackingViewLaidOut;
4747
CGFloat _keyboardInset;
48+
// Changes once per keyboard animation: at its end, or at the start of a hide.
49+
CGFloat _settledInset;
50+
NSUInteger _keyboardAnimationCount;
4851
// `height` shrinks this view, so measure it while the keyboard doesn't overlap.
49-
CGFloat _restingHeight;
52+
CGFloat _heightWithoutKeyboard;
5053
}
5154

5255
- (instancetype)initWithFrame:(CGRect)frame
@@ -148,13 +151,28 @@ - (void)_keyboardWillChangeFrame:(NSNotification *)notification
148151
UIViewAnimationCurve curve =
149152
(UIViewAnimationCurve)[notification.userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
150153

154+
_keyboardAnimationCount++;
155+
__weak __typeof(self) weakSelf = self;
156+
151157
[UIView animateWithDuration:duration
152-
delay:0
153-
options:(UIViewAnimationOptions)curve << 16 | UIViewAnimationOptionBeginFromCurrentState
154-
animations:^{
155-
[trackingContainerView layoutIfNeeded];
156-
}
157-
completion:nil];
158+
delay:0
159+
options:(UIViewAnimationOptions)curve << 16 | UIViewAnimationOptionBeginFromCurrentState
160+
animations:^{
161+
[trackingContainerView layoutIfNeeded];
162+
}
163+
completion:^(__unused BOOL finished) {
164+
__typeof(self) strongSelf = weakSelf;
165+
166+
if (strongSelf == nil) {
167+
return;
168+
}
169+
170+
strongSelf->_keyboardAnimationCount--;
171+
172+
if (strongSelf->_keyboardAnimationCount == 0) {
173+
[strongSelf _updateKeyboardInset];
174+
}
175+
}];
158176
}
159177

160178
- (CGFloat)_hiddenKeyboardTrackingHeight
@@ -210,27 +228,32 @@ - (void)_updateKeyboardInset
210228
const auto &props = static_cast<const KeyboardAvoidingViewProps &>(*_props);
211229

212230
if (_keyboardInset == 0) {
213-
_restingHeight = _layoutMetrics.frame.size.height;
231+
_heightWithoutKeyboard = _layoutMetrics.frame.size.height;
214232
}
215233

216234
CGFloat inset = 0;
217235
BOOL isKeyboardVisible = _trackingView.bounds.size.height > [self _hiddenKeyboardTrackingHeight];
218236

219237
if (props.enabled && isKeyboardVisible) {
220238
CGFloat keyboardY = CGRectGetMinY(_trackingView.frame) - props.keyboardVerticalOffset;
221-
inset = MAX(_layoutMetrics.frame.origin.y + _restingHeight - keyboardY, 0);
239+
inset = MAX(_layoutMetrics.frame.origin.y + _heightWithoutKeyboard - keyboardY, 0);
222240
}
223241

224-
if (inset == _keyboardInset) {
242+
// During an animation, `inset` is already the end value.
243+
CGFloat settledInset = (_keyboardAnimationCount == 0 || inset == 0) ? inset : _settledInset;
244+
245+
if (inset == _keyboardInset && settledInset == _settledInset) {
225246
return;
226247
}
227248

228249
_keyboardInset = inset;
229-
CGFloat maxHeight = inset > 0 ? MAX(_restingHeight - inset, 0) : INFINITY;
250+
_settledInset = settledInset;
251+
CGFloat maxHeight = inset > 0 ? MAX(_heightWithoutKeyboard - inset, 0) : INFINITY;
230252

231253
// A `folly::dynamic` payload, which C++ Native Animated can read.
232254
_eventEmitter->dispatchUniqueEvent(
233-
"keyboardInsetChange", folly::dynamic::object("inset", inset)("maxHeight", maxHeight));
255+
"keyboardInsetChange",
256+
folly::dynamic::object("inset", inset)("maxHeight", maxHeight)("settledInset", settledInset));
234257
}
235258

236259
#pragma mark - RCTComponentViewProtocol
@@ -251,7 +274,8 @@ - (void)prepareForRecycle
251274
{
252275
[super prepareForRecycle];
253276
_keyboardInset = 0;
254-
_restingHeight = 0;
277+
_settledInset = 0;
278+
_heightWithoutKeyboard = 0;
255279
}
256280

257281
@end

‎packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/keyboardavoidingview/KeyboardInsetChangeEvent.kt‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ internal class KeyboardInsetChangeEvent(
1616
viewTag: Int,
1717
private val inset: Float,
1818
private val maxHeight: Float,
19+
private val settledInset: Float,
1920
) : Event<KeyboardInsetChangeEvent>(surfaceId, viewTag) {
2021

2122
override fun getEventName(): String = EVENT_NAME
@@ -24,6 +25,7 @@ internal class KeyboardInsetChangeEvent(
2425
Arguments.createMap().apply {
2526
putDouble("inset", inset.toDouble())
2627
putDouble("maxHeight", maxHeight.toDouble())
28+
putDouble("settledInset", settledInset.toDouble())
2729
}
2830

2931
companion object {

‎packages/react-native/ReactAndroid/src/main/java/com/facebook/react/views/keyboardavoidingview/ReactKeyboardAvoidingView.kt‎

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,10 @@ internal class ReactKeyboardAvoidingView(context: Context) : ReactViewGroup(cont
4242

4343
// All values below are in dp.
4444
private var keyboardInset = 0f
45+
// Changes once per keyboard animation: at its end, or at the start of a hide.
46+
private var settledInset = 0f
4547
// `height` shrinks this view, so measure it while the keyboard doesn't overlap.
46-
private var restingHeight = 0f
48+
private var heightWithoutKeyboard = 0f
4749

4850
private val updateRunnable = Runnable {
4951
isUpdateScheduled = false
@@ -68,6 +70,21 @@ internal class ReactKeyboardAvoidingView(context: Context) : ReactViewGroup(cont
6870
}
6971
}
7072

73+
override fun onStart(
74+
animation: WindowInsetsAnimationCompat,
75+
bounds: WindowInsetsAnimationCompat.BoundsCompat,
76+
): WindowInsetsAnimationCompat.BoundsCompat {
77+
// Root insets hold the end value.
78+
if (
79+
animation.isImeAnimation() &&
80+
settledInset != 0f &&
81+
(!isAvoidanceEnabled || keyboardOverlap(currentKeyboardHeight()) == 0f)
82+
) {
83+
dispatchInsetChange(keyboardInset, 0f)
84+
}
85+
return bounds
86+
}
87+
7188
override fun onProgress(
7289
insets: WindowInsetsCompat,
7390
runningAnimations: MutableList<WindowInsetsAnimationCompat>,
@@ -134,28 +151,42 @@ internal class ReactKeyboardAvoidingView(context: Context) : ReactViewGroup(cont
134151

135152
val frameY = top.toFloat().pxToDp()
136153
val keyboardY = rootView.height.toFloat().pxToDp() - keyboardHeight - keyboardVerticalOffset
137-
return maxOf(frameY + restingHeight - keyboardY, 0f)
154+
return maxOf(frameY + heightWithoutKeyboard - keyboardY, 0f)
138155
}
139156

140157
@UiThread
141158
private fun updateKeyboardInset(keyboardHeight: Float) {
142159
if (keyboardInset == 0f) {
143-
restingHeight = height.toFloat().pxToDp()
160+
heightWithoutKeyboard = height.toFloat().pxToDp()
144161
}
145162

146163
val inset = if (isAvoidanceEnabled) keyboardOverlap(keyboardHeight) else 0f
164+
dispatchInsetChange(inset, if (!isAnimating || inset == 0f) inset else settledInset)
165+
}
147166

148-
if (inset == keyboardInset) {
167+
@UiThread
168+
private fun dispatchInsetChange(inset: Float, settled: Float) {
169+
if (inset == keyboardInset && settled == settledInset) {
149170
return
150171
}
151172

152173
val reactContext = context as? ReactContext ?: return
153174
val eventDispatcher = UIManagerHelper.getEventDispatcher(reactContext) ?: return
175+
154176
keyboardInset = inset
155-
val maxHeight = if (inset > 0f) maxOf(restingHeight - inset, 0f) else Float.POSITIVE_INFINITY
177+
settledInset = settled
178+
179+
val maxHeight =
180+
if (inset > 0f) maxOf(heightWithoutKeyboard - inset, 0f) else Float.POSITIVE_INFINITY
156181

157182
eventDispatcher.dispatchEvent(
158-
KeyboardInsetChangeEvent(UIManagerHelper.getSurfaceId(this), id, inset, maxHeight)
183+
KeyboardInsetChangeEvent(
184+
UIManagerHelper.getSurfaceId(this),
185+
id,
186+
inset,
187+
maxHeight,
188+
settled,
189+
)
159190
)
160191
}
161192
}

‎packages/react-native/ReactNativeApi.d.ts‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @generated SignedSource<<5fb88c816966453fdc33d4cf9c2caa29>>
7+
* @generated SignedSource<<fc2cc0660fe3ae00dfd97c144490c424>>
88
*
99
* This file was generated by scripts/js-api/build-types/index.js.
1010
*/
@@ -2810,7 +2810,8 @@ declare interface KeyboardAvoidingViewProps extends Readonly<
28102810
ViewProps & KeyboardAvoidingViewPropsCore
28112811
> {}
28122812
declare type KeyboardAvoidingViewPropsCore = {
2813-
readonly behavior?: "height" | "padding" | "position"
2813+
readonly behavior?:
2814+
"height" | "padding" | "position" | "translate-with-padding"
28142815
readonly contentContainerStyle?: ViewStyleProp
28152816
readonly enabled?: boolean
28162817
readonly keyboardVerticalOffset?: number
@@ -5969,9 +5970,9 @@ export {
59695970
KeyEvent, // 20fa4267
59705971
KeyUpEvent, // c4e5a70b
59715972
Keyboard, // a26f7bd4
5972-
KeyboardAvoidingView, // 668b6171
5973-
KeyboardAvoidingViewInstance, // ce03459c
5974-
KeyboardAvoidingViewProps, // 53cc84d8
5973+
KeyboardAvoidingView, // a126f0b2
5974+
KeyboardAvoidingViewInstance, // 207e22aa
5975+
KeyboardAvoidingViewProps, // 81d8f93f
59755976
KeyboardEvent, // c3f895d4
59765977
KeyboardEventEasing, // af4091c8
59775978
KeyboardEventName, // 59299ad6

‎packages/react-native/src/private/components/keyboardavoidingview/specs/KeyboardAvoidingViewNativeComponent.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import codegenNativeComponent from '../../../../../Libraries/Utilities/codegenNa
2121
type KeyboardInsetChangeEvent = Readonly<{
2222
inset: Float,
2323
maxHeight: Float,
24+
settledInset: Float,
2425
}>;
2526

2627
type KeyboardAvoidingViewNativeProps = Readonly<{

0 commit comments

Comments
 (0)