react-navigation icon indicating copy to clipboard operation
react-navigation copied to clipboard

Keyboard hides => shows => hides when navigating back (from ios gesture only) from a screen where keyboard is shown

Open nikolasgioannou opened this issue 2 years ago • 4 comments

Current behavior

Apr-24-2024 17-12-33

As you can see, when i start the gesture, it dismiss keyboard as expected. But when the gesture ends, it pops back up the keyboard.

Expected behavior

I expect the keyboard to stay dismissed when the gesture finishes.

Reproduction

https://github.com/nikolasgioannou/react-navigation-bug

Platform

  • [ ] Android
  • [X] iOS
  • [ ] Web
  • [ ] Windows
  • [ ] MacOS

Packages

  • [ ] @react-navigation/bottom-tabs
  • [ ] @react-navigation/drawer
  • [ ] @react-navigation/material-top-tabs
  • [X] @react-navigation/stack
  • [ ] @react-navigation/native-stack
  • [ ] react-native-tab-view

Environment

  • [] I've removed the packages that I don't use
package version
@react-navigation/native "^6.1.17",
@react-navigation/stack "^6.3.29"
react-native-safe-area-context "4.8.2",
react-native-screens "~3.29.0"
react-native-gesture-handler "~2.14.0",
react-native "0.73.6",
expo "~50.0.14",
node 18.14.2
npm or yarn 1.22.19

nikolasgioannou avatar Apr 24 '24 21:04 nikolasgioannou

I'm having the same problem. If I use the native stack navigator, instead of the js based stack navigator, the issue doesn't occur. Unfortunately, the project I'm working on is large and we have already bought into the unique customization offered by the js stack navigator so switching now is not an option.

For any one else running into this problem I did find a work-around for the time being by toggling the gestureEnabled screen option when the keyboard visibility changes. Here's a link applying it to the reproduction example @nikolasgioannou posted with the bug https://snack.expo.dev/BePQKvC4CL3Y1k5668sHT

ach5910 avatar Apr 27 '24 17:04 ach5910

I'm still seeing this issue with a JS stack navigator.

The issue is that the blurred TextInput is focused after the gesture is finalized.

In CardContainer, the callback passed to useKeyboardManager fires twice when the gesture triggers a navigation. Specifically, navigation.isFocused() returns true on the first call the false on the second, which results in the keyboard being shown then hidden.

https://github.com/react-navigation/react-navigation/blob/3101d0406c32163c0576cb5c4712755c01b1a17c/packages/stack/src/views/Stack/CardContainer.tsx#L105

I tried to follow the callers but I got a bit lost and it's not really clear to me whether this should be fixed in CardContainer or further up the chain.

A quick and dirty fix is to set all TextInputs to readOnly when their parent route is being removed to prevent them from receiving focus again. To avoid cluttering every screen with this logic, I created a custom TextInput component encapsulating this logic:

import { useEffect, useState } from "react";
import { TextInput as RNTextInput, type TextInputProps } from "react-native";
import { useNavigation } from "expo-router";

export function TextInput({ readOnly, ...props }: TextInputProps) {
  const navigation = useNavigation();
  const [isRemoving, setIsRemoving] = useState(false);

  useEffect(() => {
    const unsubscribe = navigation.addListener("beforeRemove", () => {
      setIsRemoving(true);
    });

    return unsubscribe;
  }, [navigation]);

  return <RNTextInput {...props} readOnly={isRemoving || readOnly} />;
}

levymetal avatar Sep 10 '24 09:09 levymetal

Same issue here, tried to use the screen options keyboardHandlingEnabled: false instead, and manually dismissing the keyboard, but that didn't seem to change anything.

valentinchelle avatar Sep 26 '24 21:09 valentinchelle

Hey @nikolasgioannou! Thanks for opening the issue. It seems that the issue doesn't contain a link to a repro, or the provided repro is not valid (e.g. broken link, private repo, code doesn't run etc.).

The best way to get attention to your issue is to provide an easy way for a developer to reproduce the issue.

You can provide a repro using any of the following:

Try to keep the repro as small as possible by narrowing down the minimal amount of code needed to reproduce the issue. See "How to create a Minimal, Reproducible Example" for more information.

Don't:

  • Link to your entire project or a project containing code unrelated to the issue.
  • Link to a specific file in a GitHub repo, as it won't be detected.

You can edit your original issue to include a link to the repro, or leave it as a comment. The issue will be closed automatically after a while if you don't provide a valid repro.

github-actions[bot] avatar Oct 25 '26 15:10 github-actions[bot]