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

useAnimatedKeyboard returns wrong height #6727

Closed
Martinocom-Switcho opened this issue Nov 18, 2024 · 2 comments · Fixed by #6755
Closed

useAnimatedKeyboard returns wrong height #6727

Martinocom-Switcho opened this issue Nov 18, 2024 · 2 comments · Fixed by #6755
Labels
Platform: iOS This issue is specific to iOS Repro provided A reproduction with a snippet of code, snack or repo is provided

Comments

@Martinocom-Switcho
Copy link

Martinocom-Switcho commented Nov 18, 2024

Description

When using useAnimatedKeyboard there are times when the height returned by the hook is not right, causing the same "jump" effect as KeyboardAvoidingView. This is caused by the height changing from 0 to max almost instantly (or in few 3-5 steps). Usually it happens when pressing rapidly on components, causing keyboard to open and close frequently. This happens both during opening and during closing. It is happening only on iOS, since on Android I'm not able to reproduce this.

It is happening also in a barebone react-native project, just with Reanimated.

Screen.Recording.2024-11-18.at.17.35.30.mp4

Steps to reproduce

  1. Have some inputs and a "sticky" component
  2. Try to press multiple times in order to open-close the keyboard rapidly
  3. --> The height will be wrong at some point

I configured a barebone app with just Reanimated and I was able to reproduce it. I'll leave the code of that App.tsx here.

App.tsx code
import React from 'react';

import {
  Button,
  SafeAreaView,
  ScrollView,
  StyleSheet,
  Text,
  TextInput,
} from 'react-native';
import Animated, { useAnimatedKeyboard, useAnimatedProps, useAnimatedStyle, useSharedValue } from 'react-native-reanimated';


Animated.addWhitelistedNativeProps({text: true});

const AnimatedText = Animated.createAnimatedComponent(TextInput);

function App(): React.JSX.Element {


  const { height } = useAnimatedKeyboard();
  const oldHeight = useSharedValue(0);
  const animatedProps = useAnimatedProps(() => {

    if (oldHeight.value - height.value > 100) {
      console.log('Keyboard moved too quickly!');
    }
    oldHeight.value = height.value;

    return {
      text: `${height.value}`,
      defaultValue: `${height.value}`,
    };
  }, [height]);

  const animatedStyle = useAnimatedStyle(() => {
    return {
      marginBottom: height.value,
      backgroundColor: 'yellow',
      padding: 16,
    };
  });

  return (
    <SafeAreaView style={style.container}>
      <ScrollView contentInsetAdjustmentBehavior="automatic">
          <AnimatedText style={{ fontSize: 24 }} animatedProps={animatedProps} />

          <Text style={{ marginTop: 16 }}>Name</Text>
          <TextInput />
          <Text style={{ marginTop: 16 }}>Surname</Text>
          <TextInput />
          <Text style={{ marginTop: 16 }}>Secret</Text>
        </ScrollView>
        <Animated.View style={animatedStyle}>
          <Button title="Sticky Me"/>
        </Animated.View>
    </SafeAreaView>
  );
}

const style = StyleSheet.create({
  container: {
    backgroundColor: 'white',
    flex: 1,
  },
}
);

export default App;

Snack or a link to a repository

https://snack.expo.dev/mtxg_VNC73hJaIz9hoMG0?platform=ios
Code of App.tsx available also in this issue

Reanimated version

3.16.1

React Native version

0.76.1

Platforms

iOS

JavaScript runtime

None

Workflow

React Native

Architecture

Fabric (New Architecture)

Build type

Debug app & production bundle

Device

iOS simulator

Device model

No response

Acknowledgements

Yes

@github-actions github-actions bot added Missing repro This issue need minimum repro scenario Platform: iOS This issue is specific to iOS Repro provided A reproduction with a snippet of code, snack or repo is provided and removed Missing repro This issue need minimum repro scenario labels Nov 18, 2024
@mhoran
Copy link
Contributor

mhoran commented Nov 26, 2024

This will happen when the keyboard animation takes longer than expected. The keyboard height is estimated during animation here based on the keyboard taking 30 frames, or 0.48 seconds at 60 FPS, to show up. If the keyboard animation takes longer than that, the estimation will return the target height.

I see this on an iPad with physical keyboard. When using a physical keyboard, there is a small auto-suggest window docked at the bottom of the screen. When bringing up the on screen keyboard, this docked keyboard hides, then bring up the on screen keyboard. That takes longer than 0.48 seconds, resulting in the same behavior.

In your case, I think that the keyboard is still finishing the closing animation when it starts opening again. That takes longer than 0.48 seconds, so you see this bug.

mhoran added a commit to mhoran/react-native-reanimated that referenced this issue Nov 28, 2024
The interactive dismissal logic would get tripped up if the keyboard was
rapidly opened and closed, which can happen if a hardware keyboard is
used and the autosuggest dock appears and then disappears when the on
screen keyboard is opened.

Fixes software-mansion#6727.
@mhoran
Copy link
Contributor

mhoran commented Nov 28, 2024

I may have accidentally fixed this while working on something else. I cannot reproduce anymore with the above patch applied.

mhoran added a commit to mhoran/react-native-reanimated that referenced this issue Nov 28, 2024
The interactive dismissal logic would get tripped up if the keyboard was
rapidly opened and closed, which can happen if a hardware keyboard is
used and the autosuggest dock appears and then disappears when the on
screen keyboard is opened.

Fixes software-mansion#6727.
mhoran added a commit to mhoran/react-native-reanimated that referenced this issue Nov 28, 2024
The interactive dismissal logic would get tripped up if the keyboard was
rapidly opened and closed, which can happen if a hardware keyboard is
used and the autosuggest dock appears and then disappears when the on
screen keyboard is opened.

Fixes software-mansion#6727.
mhoran added a commit to mhoran/react-native-reanimated that referenced this issue Nov 28, 2024
The interactive dismissal logic would get tripped up if the keyboard was
rapidly opened and closed, which can happen if a hardware keyboard is
used and the autosuggest dock appears and then disappears when the on
screen keyboard is opened.

Fixes software-mansion#6727.
mhoran added a commit to mhoran/react-native-reanimated that referenced this issue Dec 19, 2024
The interactive dismissal logic would get tripped up if the keyboard was
rapidly opened and closed, which can happen if a hardware keyboard is
used and the autosuggest dock appears and then disappears when the on
screen keyboard is opened.

Fixes software-mansion#6727.
mhoran added a commit to mhoran/react-native-reanimated that referenced this issue Dec 19, 2024
The interactive dismissal logic would get tripped up if the keyboard was
rapidly opened and closed, which can happen if a hardware keyboard is
used and the autosuggest dock appears and then disappears when the on
screen keyboard is opened.

Fixes software-mansion#6727.
mhoran added a commit to mhoran/react-native-reanimated that referenced this issue Jan 9, 2025
The interactive dismissal logic would get tripped up if the keyboard was
rapidly opened and closed, which can happen if a hardware keyboard is
used and the autosuggest dock appears and then disappears when the on
screen keyboard is opened.

Fixes software-mansion#6727.
github-merge-queue bot pushed a commit that referenced this issue Jan 23, 2025
<!-- Thanks for submitting a pull request! We appreciate you spending
the time to work on these changes. Please follow the template so that
the reviewers can easily understand what the code changes affect. -->

## Summary

fix: return correct height for detached keyboard on iOS (fixes #5584,
#6440)
fix: don't clobber keyboard height when keyboard did not animate on iOS
fix: convert keyboard position to window coordinate space on iOS (Stage
Manager compatibility)
fix: don't prematurely update keyboard height during dismissal on iOS
fix: don't jump to height when iOS keyboard is rapidly opened/closed
(fixes #6727)
fix: correct height when keyboard opened from shortcuts bar on iOS

## Test plan

Can be tested with the useAnimatedKeyboard example.

1. Enable iPad for the example project.
2. Launch the useAnimatedKeyboard example.
3. Open the keyboard, then undock or float the keyboard. The view should
not transform when undocked or floating.
4. Dock the keyboard
5. Hide the keyboard, note that the view should now be transformed by
the keyboard height
6. Enable Reduce Motion in Accessibility, and Prefers Cross-Fade
Transitions
7. Open the keyboard, note that he transform should work as expected
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Platform: iOS This issue is specific to iOS Repro provided A reproduction with a snippet of code, snack or repo is provided
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants