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

useReanimatedKeyboardAnimation doesn't look smooth like the Reanimated UseAnimatedKeyboard hook in IOS when closing the keyboard #307

Closed
devoren opened this issue Dec 23, 2023 · 10 comments · Fixed by #400
Assignees
Labels
question You wanted to clarify something about the usage of the library or have a question about something

Comments

@devoren
Copy link

devoren commented Dec 23, 2023

Describe the bug
Hello! I'm creating a chat app and using fakeView. I'm using useReanimatedKeyboardAnimation but the height value is faster than useAnimatedKeyboard height value in iOS. Also how to add instets bottom? when i use useReanimatedKeyboardAnimation i use it like this: height: height.value ? Math.abs(height.value) : insets.bottom but it is not working with useAnimatedKeyboard (jumping)

Code snippet
Add your code snippet where error has been occurred.

<>
const { height } = useReanimatedKeyboardAnimation(); // ---- useAnimatedKeyboard ----

const fakeView = useAnimatedStyle(
() => ({
	height: height.value ? Math.abs(height.value) : insets.bottom,
}),[],);

<View style={styles.container}>
	<Chats.MessagesList
		data={allMessages}
	/>
	<Chats.MessageActions
		chat={chat}
		setMessages={setMessages}
	/>
	<Animated.View style={fakeView} />
      </View>
</>

Repo for reproducing
I would be highly appreciate if you can provide repository for reproducing your issue. It can significantly reduce the time for discovering and fixing the problem.
https://github.com/kirillzyusko/react-native-keyboard-controller/blob/main/example/src/screens/Examples/ReanimatedChatFlatlist/index.tsx

To Reproduce
Steps to reproduce the behavior:

  1. Use ReanimatedChatFlatlist example
  2. Open on ios simulator
  3. Change useReanimatedKeyboardAnimation to useAnimatedKeyboard
  4. Check the difference when opening/closing the keyboard.

Expected behavior
A clear and concise description of what you expected to happen.
The keyboard closes smoothly, as when using the useAnimatedKeyboard hook.

Screenshots
If applicable, add screenshots to help explain your problem.

useReanimatedKeyboardAnimation:

useReanimatedKeyboardAnimation.mp4

useAnimatedKeyboard:

useAnimatedKeyboard.mp4

Smartphone (please complete the following information):

  • Desktop OS: [MacOS]
  • Device: [Phone14]
  • OS: [iOS 16.6]
  • RN version: [0.72.6]
  • RN architecture: [old]
  • JS engine: [Hermes]
  • Library version: [1.9.5]

Additional context
Add any other context about the problem here.

@kirillzyusko
Copy link
Owner

Hello @devoren

If you want to have the same behavior as reanimated hook, then you can use something like this:

const useAnimatedKeyboard = () => {
  const height = useSharedValue(0);
  
  useKeyboardHandler({
    onMove: (e) => {
      'worklet';

      height.value = e.height;
    },
  }, []);

  return { height };
}

However it's strange that there is no back animation. In my example app it's present:

RNKC-back-transition.mp4

The only difference I see in RN version (0.72.4 vs 0.72.7), iOS version (17.2 vs 16.6), device (iPhone 15 Pro simulator vs iPhone 14).

The described problem I observed in RN 0.73 🤔 But I haven't figured out what is causing this problem yet

@devoren
Copy link
Author

devoren commented Dec 23, 2023

@kirillzyusko Hey! im sorry, RN version is 0.72.6. Yeah no back animation and height value is jumping from -326 to -0 when closing the keyboard. Android works fine in my case

@kirillzyusko
Copy link
Owner

Yeah no back animation and height value is jumping from -326 to -0 when closing the keyboard

@devoren that how it was designed to work. When keyboard appears it will also instantly change the height from 0 to -326. iOS kind of schedules layout animations and because of that animation actually happens.

When you are using onMove handler then you will get intermediate values, but they will not be perfectly synchronized with a keyboard (they will be always kind of one frame behind, because I'm pulling keyboard coordinates via DisplayLink and it fires a callback before next frame).

Can you try to clone the repo and run example app? Are you observing the same problem there? What is the react-native-reanimated version you are using?

@devoren
Copy link
Author

devoren commented Dec 24, 2023

"react-native-reanimated": "^3.6.1". Okay i will try it today, thank you!

@kirillzyusko kirillzyusko added the question You wanted to clarify something about the usage of the library or have a question about something label Dec 27, 2023
@devoren
Copy link
Author

devoren commented Dec 27, 2023

@kirillzyusko Hello! Sorry for the late feedback. I tried the example but the back animation is still not working. Can you check flatlist example? Chat works but chat flatlist example not. RN 0.72.4v, ios 16.4, iphone 13
UPD: If I press the return button it works as expected, but if I press a message from the list the keyboard closes without animation. In the video I pressed messages
UPD 2: If i press message in chat screen (not flatlist) it also works with animation
UPD 3: if i use useAnimatedKeyboard and press message works with back animation

Simulator.Screen.Recording.-.iPhone.13.-.2023-12-27.at.22.03.53.online-video-cutter.com.mp4

@kirillzyusko
Copy link
Owner

kirillzyusko commented Dec 29, 2023

Hm, very interesting @devoren

I'll investigate it, but for now I'd suggest you to go with:

const useAnimatedKeyboard = () => {
  const height = useSharedValue(0);
  
  useKeyboardHandler({
    onMove: (e) => {
      'worklet';

      height.value = e.height;
    },
  }, []);

  return { height };
}

Since you are animating height (i. e. non UI props) it's better to sync up values on every frame 🙂

@devoren
Copy link
Author

devoren commented Jan 11, 2024

In production, the "back animation" is better than DEV mode, and also the back animation only doesn't show up if I dismiss the keyboard by pressing the list rather than the keyboard back button. @kirillzyusko thank you ❤️

@devoren devoren closed this as completed Jan 11, 2024
kirillzyusko added a commit that referenced this issue Mar 25, 2024
## 📜 Description

Schedule layout animations on before keyboard appear/disappear.

## 💡 Motivation and Context

Looks like after RN 0.71 the disappear animation happens instantly if
user pressed on the `ScrollView`. I don't know what was exactly broken,
but I discovered, that if we schedule `LayoutAnimation` with `keyboard`
type, then animation happens as it should.

So in this PR I decided to schedule layout animations manually and
remove them when keyboard finished its movement.

Theoretically it's a little bit dangerous to schedule global animations,
but:
- on iOS they are already kind of getting scheduled;
- we remove our animation after keyboard movement completed.

So it's kind of relatively safe 🙃 

Closes
#307

## 📢 Changelog

### JS

- added `reanimated chat flat list` example to Fabric project;

### iOS

- added `scheduleKeyboardAnimation`/`unscheduleKeyboardAnimation`
methods to `RCTUIManager` (as extension);
- added `onRequestAnimation`/`onCancelAnimation` methods to
`KeyboardMovementObserver`;
- call `onRequestAnimation` on `keyboardWill` events and
`onCancelAnimation` on `keyboardDid` events;

## 🤔 How Has This Been Tested?

Tested on iPhone 15 Pro (both paper and fabric).

## 📸 Screenshots (if appropriate):

|Before|After|
|------|------|
|<video
src="https://github.com/kirillzyusko/react-native-keyboard-controller/assets/22820318/f1a550e4-bb45-48e6-a4ee-7ef8a0bf1e0f">|<video
src="https://github.com/kirillzyusko/react-native-keyboard-controller/assets/22820318/daafb826-df26-4dce-98af-d2d344f77699">|

## 📝 Checklist

- [x] CI successfully passed
- [x] I added new mocks and corresponding unit-tests if library API was
changed
@kirillzyusko
Copy link
Owner

@focux @nandorojo @devoren @jongbelegen looks like you are all experienced this problem.

I fixed it in 1.11.5 version and published it on npm so you can use useReanimatedKeyboardAnimation/useKeyboardAnimation hooks as before and it should work as expected 👀

If it doesn't work in your case (for some reasons), please open a new issue 🙏

@focux
Copy link

focux commented Mar 26, 2024

Sounds good. Thank you so much!

@alantoa
Copy link

alantoa commented Mar 26, 2024

Confirm it got fixed in v1.11.5, thanks! 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question You wanted to clarify something about the usage of the library or have a question about something
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants