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

Is there a way to scroll the screen on top of the keyboard for the multi line input? #154

Open
Saad-Bashar opened this issue Mar 23, 2023 · 4 comments
Labels
enhancement New feature or request

Comments

@Saad-Bashar
Copy link

Feature request

Thanks for this amazing library! I have a question. I have a multiline text input. The keyboard avoid is working great. But let's say the user keeps writing and go to the next line and exceeds the viewport, the text gets overlapped by the keyboard. Users can scroll down manually again to see the new line. Is there a way to automatically scroll the screen to the new line that got overlapped by the keyboard? Please check the demo video

auto.focus.mov
@Saad-Bashar Saad-Bashar added the enhancement New feature or request label Mar 23, 2023
@mateusz1913
Copy link
Owner

Hi @Saad-Bashar, thx for asking the question.

In your case, I guess that you display your input inside a ScrollView - in that case OS doesn't emit any notification indicating that the keyboard height changed, or the input's cursor is being covered. The library depends on the keyboard events and I'm afraid it's out of the library's scope to handle what IMO should be handled by OS.

The only thing I can suggest here is to refactor the way the keyboard padding is applied, however, I am not sure if the final UX will be satisfying for you. Currently, the library is applying the padding to the Scrollview where your multiline input is displayed. You could try to apply the padding manually at the bottom of the screen (you can look at this recipe - instead of button, you would just have empty spacer element at the bottom). That way, you will be sure that the ScrollView bounds are always above the keyboard and in theory, OS should automatically handle your input. The downside is that when keyboard is dismissed, the blank spacer may be visible for a fraction of a second. You have to check yourself if the final effect is feasible.

I'll the issue open for a while, as I'm curious, if there're more people affected.

@Saad-Bashar
Copy link
Author

I tried that however still the text was getting overlapped by the keyboard. This seems like a famous issue, facebook/react-native#16826

@a-eid
Copy link

a-eid commented Mar 31, 2023

I'm think this works with lib, however I'm not 100%.

@saadi-ninjasCode
Copy link

@Saad-Bashar , I solved the same issue by following steps.

For multiline textinput,
I wrap the input in view and add listen to onLayout change.
As I get difference in the height, I use scrollTo method of Scrollview to scroll programmatically.

function useSoftInputKeyboard(scrollRef: RefObject<ScrollView>, scrollYRef: RefObject<number>) {
  const booleanRef = useRef(false);
  const inputHeightRef = useRef<number>(0);

  const onFocus = useCallback(() => {
    booleanRef.current = true;
  }, []);

  const onBlur = useCallback(() => {
    booleanRef.current = false;
  }, []);

  const inputOnLayoutView = useCallback(
    (isMultiLine = false) =>
      ({
        nativeEvent: {
          layout: { height },
        },
      }: LayoutChangeEvent) => {
        if (isMultiLine && booleanRef.current) {
          scrollRef.current?.scrollTo({ y: (scrollYRef.current ?? 0) + (height - inputHeightRef.current) });
          inputHeightRef.current = height;
        }
      },
    [scrollRef, scrollYRef],
  );

  return {
    onFocus,
    onBlur,
    inputOnLayoutView,
  };
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants