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

fix: smooth keyboard handling in BaseModal #54533

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions src/components/Modal/BaseModal.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import {PortalHost} from '@gorhom/portal';
import React, {forwardRef, useCallback, useEffect, useMemo, useRef} from 'react';
import {View} from 'react-native';
import {Animated, View} from 'react-native';
import {useKeyboardAnimation} from 'react-native-keyboard-controller';
import ReactNativeModal from 'react-native-modal';
import ColorSchemeWrapper from '@components/ColorSchemeWrapper';
import FocusTrapForModal from '@components/FocusTrap/FocusTrapForModal';
import useKeyboardState from '@hooks/useKeyboardState';
import usePrevious from '@hooks/usePrevious';
import useResponsiveLayout from '@hooks/useResponsiveLayout';
import useSafeAreaInsets from '@hooks/useSafeAreaInsets';
import useStyledSafeAreaInsets from '@hooks/useStyledSafeAreaInsets';
import useStyleUtils from '@hooks/useStyleUtils';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
Expand Down Expand Up @@ -64,7 +65,6 @@ function BaseModal(
// We need to use isSmallScreenWidth instead of shouldUseNarrowLayout to apply correct modal width
// eslint-disable-next-line rulesdir/prefer-shouldUseNarrowLayout-instead-of-isSmallScreenWidth
const {isSmallScreenWidth} = useResponsiveLayout();
const keyboardStateContextValue = useKeyboardState();

const safeAreaInsets = useSafeAreaInsets();

Expand Down Expand Up @@ -194,7 +194,7 @@ function BaseModal(
safeAreaPaddingRight,
shouldAddBottomSafeAreaMargin,
shouldAddTopSafeAreaMargin,
shouldAddBottomSafeAreaPadding: (!avoidKeyboard || !keyboardStateContextValue?.isKeyboardShown) && shouldAddBottomSafeAreaPadding,
shouldAddBottomSafeAreaPadding,
shouldAddTopSafeAreaPadding,
modalContainerStyleMarginTop: modalContainerStyle.marginTop,
modalContainerStyleMarginBottom: modalContainerStyle.marginBottom,
Expand All @@ -214,6 +214,13 @@ function BaseModal(
}),
[isVisible, type],
);
const {height, progress} = useKeyboardAnimation();
const {unmodifiedPaddings} = useStyledSafeAreaInsets();
const removeSafeAreaPadding = progress.interpolate({
inputRange: [0, 1],
outputRange: [0, unmodifiedPaddings.bottom],
extrapolate: 'clamp',
});

return (
<ModalContext.Provider value={modalContextValue}>
Expand Down Expand Up @@ -260,7 +267,7 @@ function BaseModal(
statusBarTranslucent={statusBarTranslucent}
navigationBarTranslucent={navigationBarTranslucent}
onLayout={onLayout}
avoidKeyboard={avoidKeyboard}
avoidKeyboard={false}
kirillzyusko marked this conversation as resolved.
Show resolved Hide resolved
customBackdrop={shouldUseCustomBackdrop ? <Overlay onPress={handleBackdropPress} /> : undefined}
>
<ModalContent
Expand All @@ -272,12 +279,18 @@ function BaseModal(
active={isVisible}
initialFocus={initialFocus}
>
<View
style={[styles.defaultModalContainer, modalPaddingStyles, modalContainerStyle, !isVisible && styles.pointerEventsNone]}
<Animated.View
style={[
styles.defaultModalContainer,
modalPaddingStyles,
modalContainerStyle,
!isVisible && styles.pointerEventsNone,
avoidKeyboard && {transform: [{translateY: Animated.add(height, removeSafeAreaPadding)}]},
]}
ref={ref}
>
<ColorSchemeWrapper>{children}</ColorSchemeWrapper>
</View>
</Animated.View>
</FocusTrapForModal>
</ModalContent>
</ReactNativeModal>
Expand Down
Loading