-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
index.ts
37 lines (34 loc) · 1.75 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import {useCallback, useEffect, useState} from 'react';
import usePrevious from '@hooks/usePrevious';
import useResponsiveLayout from '@hooks/useResponsiveLayout';
import useWindowDimensions from '@hooks/useWindowDimensions';
const useIsWindowHeightReducedByKeyboard = () => {
const [isWindowHeightReducedByKeyboard, setIsWindowHeightReducedByKeyboard] = useState(false);
const {windowHeight} = useWindowDimensions();
const prevWindowHeight = usePrevious(windowHeight);
const {shouldUseNarrowLayout} = useResponsiveLayout();
const toggleKeyboardOnSmallScreens = useCallback(
(isKBOpen: boolean) => {
if (!shouldUseNarrowLayout) {
return;
}
setIsWindowHeightReducedByKeyboard(isKBOpen);
},
[shouldUseNarrowLayout],
);
useEffect(() => {
// Use window height changes to toggle the keyboard. To maintain keyboard state
// on all platforms we also use focus/blur events. So we need to make sure here
// that we avoid redundant keyboard toggling.
// Minus 100px is needed to make sure that when the internet connection is
// disabled in android chrome and a small 'No internet connection' text box appears,
// we do not take it as a sign to open the keyboard
if (!isWindowHeightReducedByKeyboard && windowHeight < prevWindowHeight - 100) {
toggleKeyboardOnSmallScreens(true);
} else if (isWindowHeightReducedByKeyboard && windowHeight > prevWindowHeight) {
toggleKeyboardOnSmallScreens(false);
}
}, [isWindowHeightReducedByKeyboard, prevWindowHeight, toggleKeyboardOnSmallScreens, windowHeight]);
return isWindowHeightReducedByKeyboard;
};
export default useIsWindowHeightReducedByKeyboard;