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

Open
Martinocom-Switcho opened this issue Nov 18, 2024 · 2 comments · May be fixed by #6755
Open

useAnimatedKeyboard returns wrong height #6727

Martinocom-Switcho opened this issue Nov 18, 2024 · 2 comments · May be 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

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

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 mhoran linked a pull request Nov 28, 2024 that will close this issue
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.
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