-
Notifications
You must be signed in to change notification settings - Fork 24.5k
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
Feature: Improvements to automaticallyAdjustKeyboardInsets
#35224
Feature: Improvements to automaticallyAdjustKeyboardInsets
#35224
Conversation
Base commit: 04c286c |
Base commit: 04c286c |
PR build artifact for 6cd100a is ready. |
PR build artifact for 6cd100a is ready. |
// [32] is a placeholder. It's the distance between the bottom of the textfield and the top of the keyboard | ||
// What value should be used? Should it be customizable with a prop? | ||
CGFloat textFieldBottom = textFieldFrame.origin.y + textFieldFrame.size.height + 32; |
This comment was marked as resolved.
This comment was marked as resolved.
Sorry, something went wrong.
This comment was marked as resolved.
This comment was marked as resolved.
Hi, the hardcoded value represents the distance between the bottom of the Please see the screenshot below for reference. IMO the default value should be 0 and we should allow the user to customize this distance. |
Got it.. Thanks. 😄 Then adding a prop for it and keeping its default value resonable; makes sense.. |
I've just patched it into 0.68.5, works as a charm. I had to hard code the distance between the input and the keyboard because I was using an absolute positioned element at the bottom of the screen so I believe a prop would be great to adjust it. |
Hey, glad to hear it worked! Do you have any suggestions regarding what this prop should be called? Looking for ideas :) |
|
PR build artifact for 24ce8b5 is ready. |
PR build artifact for 24ce8b5 is ready. |
Added the prop and named it |
PR build artifact for 42088b6 is ready. |
PR build artifact for 42088b6 is ready. |
PR build artifact for e2891a7 is ready. |
PR build artifact for e2891a7 is ready. |
@isidoro98 why did you close this ? |
This is super! Why is this closed? |
Summary: This is a reopened version of #35224 by isidoro98 which was closed without explanation, updated to resolve new merge conflicts and now includes an example in the RN-Tester app. Aside from that it is unchanged. Here is isidoro98's description from their original PR: This PR builds on top of #31402, which introduced the `automaticallyAdjustsScrollIndicatorInsets` functionality. It aims to fix one of RN's longstanding pain point regarding the keyboard. The changes provide a better way of handling the `ScrollView` offset when a keyboard opens. Currently, when a keyboard opens we apply an **offset** to the `Scrollview` that matches the size of the keyboard. This approach is great if we are using an `InputAccessoryView` but if we have multiple `TextInputs` in a `ScrollView`; offsetting the content by the size of the keyboard doesn't yield the best user experience. ## Changelog: [iOS] [Changed] - Scroll `ScrollView` text fields into view with `automaticallyAdjustsScrollIndicatorInsets` Pull Request resolved: #37766 Test Plan: The videos below compare the current and proposed behaviors for the following code: ```js <ScrollView automaticallyAdjustKeyboardInsets keyboardDismissMode="interactive"> {[...Array(10).keys()].map(item => ( <CustomTextInput placeholder={item.toString()} key={item} /> ))} </ScrollView> ``` | Current behaviour | Proposal | |-|-| | ![https://user-images.githubusercontent.com/25139053/200194972-1ac5f1cd-2d61-4118-ad77-95c04d30c98d.mov](https://user-images.githubusercontent.com/25139053/200194972-1ac5f1cd-2d61-4118-ad77-95c04d30c98d.mov) | ![https://user-images.githubusercontent.com/25139053/200194990-53f28296-be11-4a47-be70-cec917d7deb1.mov](https://user-images.githubusercontent.com/25139053/200194990-53f28296-be11-4a47-be70-cec917d7deb1.mov) | As can be seen in the video, the **current behavior** applies an offset to the `ScrollView` content regardless of where the `TextInput` sits on the screen. The proposal checks if the `TextInput` will be covered by the keyboard, and only then applies an offset. The offset applied is not the full size of the keyboard but instead only the required amount so that the `TextInput` is a **specific** distance above the top of the keyboard (customizable using the new `bottomKeyboardOffset` prop). This achieves a less "jumpy" experience for the user. The proposal doesn't change the behavior of the `ScrollView` offset when an `InputAccessory` view is used, since it checks if the `TextField` that triggered the keyboard is a **descendant** of the `ScrollView` or not. ## Why not use other existing solutions? RN ecosystem offers other alternatives for dealing with a keyboard inside a ScrollView, such as a `KeyboardAvoidingView` or using third party libraries like `react-native-keyboard-aware-scroll-view`. But as shown in the recordings below, these solutions don't provide the smoothness or behavior that can be achieved with `automaticallyAdjustsScrollIndicatorInsets`. | KeyboardAvoidingView | rn-keyboard-aware-scroll-view | |-|-| | ![https://user-images.githubusercontent.com/25139053/200195145-de742f0a-6913-4099-83c4-7693448a8933.mov](https://user-images.githubusercontent.com/25139053/200195145-de742f0a-6913-4099-83c4-7693448a8933.mov) | ![https://user-images.githubusercontent.com/25139053/200195151-80745533-16b5-4aa0-b6cd-d01041dbd001.mov](https://user-images.githubusercontent.com/25139053/200195151-80745533-16b5-4aa0-b6cd-d01041dbd001.mov) | As shown in the videos, the `TextInput` is hidden by the keyboard for a split second before becoming visible. Code for the videos above: ```js // KeyboardAvoidingView <KeyboardAvoidingView style={{flex: 1, flexDirection: 'column', justifyContent: 'center'}} behavior="padding" enabled> <ScrollView> {[...Array(10).keys()].map(item => ( <CustomTextInput placeholder={item.toString()} key={item} /> ))} </ScrollView> </KeyboardAvoidingView> ``` ```js // rn-keyboard-aware-scroll-view <KeyboardAwareScrollView> {[...Array(10).keys()].map(item => ( <CustomTextInput placeholder={item.toString()} key={item} /> ))} </KeyboardAwareScrollView> ``` Reviewed By: sammy-SC Differential Revision: D49269426 Pulled By: javache fbshipit-source-id: 6ec2e7b45f6854dd34b9dbb06ab77053b6419733
Summary
This PR builds on top of #31402, which introduced the
automaticallyAdjustsScrollIndicatorInsets
functionality. It aims to fix one of RN's longstanding pain point regarding the keyboard.The changes provide a better way of handling the
ScrollView
offset when a keyboard opens. Currently, when a keyboard opens we apply an offset to theScrollview
that matches the size of the keyboard. This approach is great if we are using anInputAccessoryView
but if we have multipleTextInputs
in aScrollView
; offsetting the content by the size of the keyboard doesn't yield the best user experience.Changelog
[General] [Changed] - Changes offset behaviour when keyboard opens in
ScrollView
withautomaticallyAdjustsScrollIndicatorInsets
Test Plan
The videos below compare the current and proposed behaviors for the following code:
current.mov
proposal.mov
As can be seen in the video, the current behavior applies an offset to the
ScrollView
content regardless of where theTextInput
sits on the screen.The proposal checks if the
TextInput
will be covered by the keyboard, and only then applies an offset. The offset applied is not the full size of the keyboard but instead only the required amount so that theTextInput
is a specific distance above the top of the keyboard (customizable using the newbottomKeyboardOffset
prop). This achieves a less "jumpy" experience for the user.The proposal doesn't change the behavior of the
ScrollView
offset when anInputAccessory
view is used, since it checks if theTextField
that triggered the keyboard is a descendant of theScrollView
or not.Why not use other existing solutions?
RN ecosystem offers other alternatives for dealing with a keyboard inside a ScrollView, such as a
KeyboardAvoidingView
or using third party libraries likereact-native-keyboard-aware-scroll-view
. But as shown in the recordings below, these solutions don't provide the smoothness or behavior that can be achieved withautomaticallyAdjustsScrollIndicatorInsets
.KeyboardAvoidingView.mov
rn-keyboard-aware-scroll-view.mov
As shown in the videos, the
TextInput
is hidden by the keyboard for a split second before becoming visible.Code for the videos above: