Skip to content
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

KeyboardAvoidingView: update bottom height when frame height is changed #36970

Closed
wants to merge 1 commit into from

Conversation

lyqandy
Copy link
Contributor

@lyqandy lyqandy commented Apr 19, 2023

Summary:

Fix this issue: #29499

  • We should change the bottom height if the frame height of KeyboardAvoidingView is changed

In some scenarios, the height of KeyboardAvoidingView would be changed because its container is re-layouted. So onLayout of KeyboardAvoidingView may be triggered more than once.

_onLayout = async (event: ViewLayoutEvent) => {
const wasFrameNull = this._frame == null;
this._frame = event.nativeEvent.layout;
if (!this._initialFrameHeight) {
// save the initial frame height, before the keyboard is visible
this._initialFrameHeight = this._frame.height;
}
if (wasFrameNull) {
await this._updateBottomIfNecessary();
}

But at line 122 above, _updateBottomIfNecessary would be called only for the first trigger of onLayout. That means, if the height of KeyboardAvoidingView is changed, the bottom height can't be updated.

See the videos below:

before

In this video, KeyboardAvoidingView is rendered twice, and the height is changed from 844 to 753, bottomHeight is not updated when the height changed, so there is a white gap below the continue button. Once I re-open the keyboard, _updateBottomIfNecessary is called again, then the white gap disappeared.

before-fix.mp4
after
after-fix.mp4

Changelog:

[GENERAL] [CHANGED] - change _onLayout to update bottom height when frame height is changed

Test Plan:

@facebook-github-bot
Copy link
Contributor

Hi @lyqandy!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@lyqandy lyqandy marked this pull request as ready for review April 19, 2023 03:12
@analysis-bot
Copy link

Platform Engine Arch Size (bytes) Diff
android hermes arm64-v8a 8,620,320 +29
android hermes armeabi-v7a 7,932,992 +20
android hermes x86 9,106,281 +32
android hermes x86_64 8,961,112 +42
android jsc arm64-v8a 9,186,904 +14
android jsc armeabi-v7a 8,377,199 +27
android jsc x86 9,244,478 +26
android jsc x86_64 9,502,982 +21

Base commit: 54f7b8b
Branch: main

@facebook-github-bot
Copy link
Contributor

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Apr 19, 2023
@NickGerleman
Copy link
Contributor

NickGerleman commented Apr 19, 2023

Reading around this I saw that the same function is called in the event of keyboardWillChangeFrame. Do we not see the event in this case?

edit: Oh the frame here is that of the view, and not of the keyboard.

@NickGerleman
Copy link
Contributor

This change seems good to me, but for someone not very familiar with this logic, could you explain the sequence of events/the math here a bit?

@lyqandy lyqandy closed this Apr 20, 2023
@lyqandy lyqandy reopened this Apr 20, 2023
@lyqandy
Copy link
Contributor Author

lyqandy commented Apr 20, 2023

This change seems good to me, but for someone not very familiar with this logic, could you explain the sequence of events/the math here a bit?

Hi @NickGerleman , I have updated the Summary, you can take a look :).

@facebook-github-bot
Copy link
Contributor

@NickGerleman has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

@facebook-github-bot facebook-github-bot added the Merged This PR has been merged. label Apr 20, 2023
@facebook-github-bot
Copy link
Contributor

@NickGerleman merged this pull request in 5059ddc.

jeongshin pushed a commit to jeongshin/react-native that referenced this pull request May 7, 2023
…ed (facebook#36970)

Summary:
Fix this issue: facebook#29499

- We should change the bottom height if the frame height of KeyboardAvoidingView is changed

In some scenarios, the height of `KeyboardAvoidingView` would be changed because its container is re-layouted. So `onLayout` of `KeyboardAvoidingView` may be triggered more than once.

https://github.com/facebook/react-native/blob/bbc3657ff4efd0218e02ad9a3c73725a7f8a366c/packages/react-native/Libraries/Components/Keyboard/KeyboardAvoidingView.js#L114-L125

But at line 122 above, `_updateBottomIfNecessary ` would be called only for the first trigger of `onLayout`. That means, if the height of `KeyboardAvoidingView` is changed, the bottom height can't be updated.

#### See the videos below:

##### before

In this video, `KeyboardAvoidingView ` is rendered twice, and the height is changed from 844 to 753, `bottomHeight ` is not updated when the height changed, so there is a white gap below the `continue` button. Once I re-open the keyboard, `_updateBottomIfNecessary` is called again, then the white gap disappeared.

https://user-images.githubusercontent.com/25719782/232962924-c69adc11-deb9-4426-9b5c-4e990a0470db.mp4

##### after

https://user-images.githubusercontent.com/25719782/232962956-a163020f-5f40-4d82-9f6c-5ee67416c489.mp4

## Changelog:

[GENERAL] [CHANGED] - change `_onLayout` to update bottom height when frame height is changed

Pull Request resolved: facebook#36970

Reviewed By: rshest

Differential Revision: D45138176

Pulled By: NickGerleman

fbshipit-source-id: b7ce6d75622ed6e8f104ae0d8441e1cb97cfa15b
OlimpiaZurek pushed a commit to OlimpiaZurek/react-native that referenced this pull request May 22, 2023
…ed (facebook#36970)

Summary:
Fix this issue: facebook#29499

- We should change the bottom height if the frame height of KeyboardAvoidingView is changed

In some scenarios, the height of `KeyboardAvoidingView` would be changed because its container is re-layouted. So `onLayout` of `KeyboardAvoidingView` may be triggered more than once.

https://github.com/facebook/react-native/blob/bbc3657ff4efd0218e02ad9a3c73725a7f8a366c/packages/react-native/Libraries/Components/Keyboard/KeyboardAvoidingView.js#L114-L125

But at line 122 above, `_updateBottomIfNecessary ` would be called only for the first trigger of `onLayout`. That means, if the height of `KeyboardAvoidingView` is changed, the bottom height can't be updated.

#### See the videos below:

##### before

In this video, `KeyboardAvoidingView ` is rendered twice, and the height is changed from 844 to 753, `bottomHeight ` is not updated when the height changed, so there is a white gap below the `continue` button. Once I re-open the keyboard, `_updateBottomIfNecessary` is called again, then the white gap disappeared.

https://user-images.githubusercontent.com/25719782/232962924-c69adc11-deb9-4426-9b5c-4e990a0470db.mp4

##### after

https://user-images.githubusercontent.com/25719782/232962956-a163020f-5f40-4d82-9f6c-5ee67416c489.mp4

## Changelog:

[GENERAL] [CHANGED] - change `_onLayout` to update bottom height when frame height is changed

Pull Request resolved: facebook#36970

Reviewed By: rshest

Differential Revision: D45138176

Pulled By: NickGerleman

fbshipit-source-id: b7ce6d75622ed6e8f104ae0d8441e1cb97cfa15b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged This PR has been merged.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants