Repository navigation
Conversation
0008452 to
a9ac94e
Compare
kirillzyusko
left a comment
There was a problem hiding this comment.
This PR is huge, so I'll focus only on what concerned me the most (what I think is crucial to improve before merging - once it will be resolved I'll be happy to do another round of review)
What scares me in this implementation the most is the usage of CADisplayLink. Correct me if I'm wrong, but from what I can see we use next thing:
- we add
KeyboardLayoutGuideas a tracking-view to thewindow; - when keyboard begin its animation we animate it via
UIView animateWithDuration:duration - our tracking view is changing layout and fires
CADisplayLinktimer CADisplayLinkruns a callback every frame, calculates the keyboard height and sends it directly to the shared animation backend (so Yoga is still responsible for layouting out our children)
And if my understanding is correct, then the biggest problem is the usage of CADisplayLink. It will run its callback before next draw. It means that we have ~15ms for next frame and all that time keyboard animation is running. So in reality we read previous frame of the keyboard and our UI will be chasing a keyboard because there will be always one frame difference.
I'll be honest, I didn't run your app (my Mac is running out of disk space, haha 😅). But may I kindly ask you to try to write a chat-layout, where we need to use KeyboardAvoidingView and our UI should move linearly with a keyboard? I. e. without the interpolation, let's say keyboard height is 336dp, then our layout should be moved by 336dp. On current video it's very hard to notice, because we interpolate keyboard movement (i. e. keyboard moves 336dp, but we shift layout only by ~150dp).
What I assume may happen is that on first frames we'll see, how our input gets obscured by keyboard and we'll get a feeling that keyboard and UI are still out of sync 😓 Also worth to test on physical iOS device and on simulator, because the difference can be quite significant.
And if my concern is valid, then I would suggest to change the approach on iOS. Maybe we should let KeyboardLayoutGuide to animate our KeyboardAvoidingView and sync shadow tree only to be sure that Pressable/measure logic is still correct? So that our ShadowNodes are aware about external animations? What do you think?
In react-native-keyboard-controller I chose a different path (I use math formulas to predict next frame based on animation params), and it kind of makes sense to use it there, but in this particular implementation it will add even more complexity without explicit benefits 🤷♂️
|
@kirillzyusko Thanks a lot for the review! To be honest, I'm not an expert on this topic at all, that's why I asked you to check it 😄. Your point about the one-frame lag with Some context on the goal: |
Yeah, I understand that. Again now, when native driver supports
Yeah, it's definitely solid. One thing that I would change - I would try to use native primitives for handling keyboard, i. e. So I would try to use But I'm happy to discuss any other directions that you have in your mind! |
a9ac94e to
97dcf59
Compare
|
@kirillzyusko Thanks for the suggestions! Should be better now. demo.mp4 |
Visual regressionNo visual differences at |
c2a9f7e to
eec2ff5
Compare
|
@kirillzyusko As the new Animated backend is now enabled by default, I switched to a JS driven animation. Should be OK to review now. I also added the demo.mp4 |
|
Warning JavaScript API change detected This PR commits an update to
This change was flagged as: |
3353f60 to
26ec411
Compare
# Conflicts: # packages/react-native/ReactNativeApi.d.ts
51e126b to
365a6ed
Compare
Summary:
KeyboardAvoidingViewfollows the keyboard from JavaScript: it waits for a keyboard event, then animates its layout once withLayoutAnimation. The content does not move in sync with the keyboard, and interactive dismissal is not followed.This PR adds an
experimental_nativeprop, inspired byreact-native-keyboard-controller. A native view tracks the keyboard and sends the inset in anonKeyboardInsetChangeevent. AnAnimated.eventwith the native driver applies it aspaddingBottom,maxHeightorbottom, depending onbehavior:UIKeyboardLayoutGuide, and each of its layout passes sends the inset, including during interactive dismissal.WindowInsetsAnimationCompatgives the keyboard height on each frame. Before Android 11, the view animates on its own curve when the insets change.It also adds a
translate-with-paddingbehavior. The view moves withtranslateYon each frame, and setspaddingToponce per keyboard animation: when the keyboard finishes showing, and when it starts hiding. This avoids a layout on each frame, which suits chat screens. Withoutexperimental_native, it acts aspadding.It requires the shared animation backend (
cxxNativeAnimatedEnabledanduseSharedAnimatedBackend), and an edge-to-edge app on Android. Otherwise, the current JavaScript implementation is used.In following PRs, I plan to add
keyboardWillShowandkeyboardWillHideevents, and maybe aKeyboardAwareScrollViewcomponent.Changelog:
[GENERAL] [ADDED] - Add
experimental_nativeprop andtranslate-with-paddingbehavior toKeyboardAvoidingViewTest Plan:
In RNTester, open KeyboardAvoidingView > Keyboard Avoiding View with different behaviors, turn on
experimental_native, then open and dismiss the keyboard with each behavior. Compare with the switch off.Then open Keyboard Avoiding View with a chat, which uses
translate-with-padding, and send a few messages.Also ran
yarn flow-check,yarn lint,yarn test packages/react-native/Libraries/Components/Keyboard,yarn build-types, the format checks, andyarn android.Videos
Android
Android.mp4
iOS
iOS.mp4