1

I have already got the expected behaviour. But not at all times. Sometimes the focused component is hidden behind the on-screen keyboard. I have fixed it by using scrollIntoView and setTimeout. This will help if the focused component is not scrolled up automatically.

My Solution:

    class KeyboardAwayView extends Component {

    constructor(props) {
        super(props);
        this.dummyRef = React.createRef(); 
    }

    render() {
        const Children = this.props.children;
        // Scroll to children component on focus
        const ClonedChildren = React.cloneElement(Children, {
            onFocus: () => {
                this.dummyRef.current.scrollIntoView({ behavior: 'smooth' });
                setTimeout(() => {
                    if (this.dummyRef.current) {
                        this.dummyRef.current.scrollIntoView({ behavior: 'smooth' });
                    }
                }, (this.props.interval | 500));
            }
        });
        return (
            <div className="keyboard-away-container">
                {ClonedChildren}
                <div className="dummy" ref={this.dummyRef}></div>
            </div>
        );
    }
}

Questions:

  1. Is there anything to do to improve my solution?

  2. Is there any standard way to solve this. In React Native there is a KeyboardAvoidingView. Is there anything like that in ReactJS?

I have already gone through this Is there any javascript event fired when the on-screen keyboard on mobile safari or chrome opens?.

Niyas Nazar
  • 1,073
  • 13
  • 20

1 Answers1

0

Question: Is there any standard way to solve this. In React Native there is a KeyboardAvoidingView. Is there anything like that in ReactJS?

No, there is not a standard way to handle this in React (I assume you are referring to react-dom).

Peter
  • 2,796
  • 1
  • 17
  • 29