Skip to content

Commit

Permalink
Use visual viewport to detect keyboard show/hide
Browse files Browse the repository at this point in the history
  • Loading branch information
huult committed Dec 19, 2024
1 parent 65345c4 commit 968a125
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 29 deletions.
30 changes: 1 addition & 29 deletions src/components/ScreenWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 16,6 @@ import useWindowDimensions from '@hooks/useWindowDimensions';
import * as Browser from '@libs/Browser';
import type {PlatformStackNavigationProp} from '@libs/Navigation/PlatformStackNavigation/types';
import type {AuthScreensParamList, RootStackParamList} from '@libs/Navigation/types';
import addViewportResizeListener from '@libs/VisualViewport';
import toggleTestToolsModal from '@userActions/TestTool';
import CONST from '@src/CONST';
import CustomDevMenu from './CustomDevMenu';
Expand Down Expand Up @@ -171,8 170,6 @@ function ScreenWrapper(
return !!route?.params && 'singleNewDotEntry' in route.params && route.params.singleNewDotEntry === 'true';
}, [route?.params]);

const initVisualViewport = Browser.isSafari() && window.visualViewport ? window.visualViewport.height : undefined;
const [isMaxHeightReady, setIsMaxHeightReady] = useState(!Browser.isSafari());
UNSTABLE_usePreventRemove(shouldReturnToOldDot, () => {
NativeModules.HybridAppModule?.closeReactNativeApp(false, false);
});
Expand All @@ -196,26 193,6 @@ function ScreenWrapper(
}),
).current;

useEffect(() => {
if (!Browser.isMobileSafari()) {
return;
}

const handleViewportResize = () => {
if (!window.visualViewport) {
return;
}

setIsMaxHeightReady(window.visualViewport.height === initVisualViewport);
};

const removeViewportResizeListener = addViewportResizeListener(handleViewportResize);

return () => {
removeViewportResizeListener();
};
}, [initVisualViewport]);

useEffect(() => {
// On iOS, the transitionEnd event doesn't trigger some times. As such, we need to set a timeout
const timeout = setTimeout(() => {
Expand Down Expand Up @@ -299,12 276,7 @@ function ScreenWrapper(
{...keyboardDismissPanResponder.panHandlers}
>
<KeyboardAvoidingView
style={[
styles.w100,
styles.h100,
{maxHeight: isMaxHeightReady ? maxHeight : undefined},
isAvoidingViewportScroll ? [styles.overflowAuto, styles.overscrollBehaviorContain] : {},
]}
style={[styles.w100, styles.h100, {maxHeight}, isAvoidingViewportScroll ? [styles.overflowAuto, styles.overscrollBehaviorContain] : {}]}
behavior={keyboardAvoidingViewBehavior}
enabled={shouldEnableKeyboardAvoidingView}
>
Expand Down
File renamed without changes.
48 changes: 48 additions & 0 deletions src/utils/keyboard/index.website.ts
Original file line number Diff line number Diff line change
@@ -0,0 1,48 @@
import {Keyboard} from 'react-native';

let isVisible = false;
const initialViewportHeight = window?.visualViewport?.height;

const handleResize = () => {
const currentHeight = window?.visualViewport?.height;

if (!currentHeight || !initialViewportHeight) {
return;
}

if (currentHeight < initialViewportHeight) {
isVisible = true;
return;
}

if (currentHeight === initialViewportHeight) {
isVisible = false;
}
};

window.visualViewport?.addEventListener('resize', handleResize);

const dismiss = (): Promise<void> => {
return new Promise((resolve) => {
if (!isVisible) {
resolve();
return;
}

const handleDismissResize = () => {
if (window.visualViewport?.height !== initialViewportHeight) {
return;
}

window.visualViewport?.removeEventListener('resize', handleDismissResize);
return resolve();
};

window.visualViewport?.addEventListener('resize', handleDismissResize);
Keyboard.dismiss();
});
};

const utils = {dismiss};

export default utils;

0 comments on commit 968a125

Please sign in to comment.