2022-07-07 15:05:58 +00:00
|
|
|
import { useHeaderHeight } from '@react-navigation/elements';
|
2022-11-10 14:48:53 +00:00
|
|
|
import React, { ReactElement, useCallback, useEffect, useState } from 'react';
|
2021-10-20 19:29:21 +00:00
|
|
|
import {
|
2022-06-21 14:16:38 +00:00
|
|
|
Keyboard,
|
2021-10-20 19:29:21 +00:00
|
|
|
KeyboardAvoidingView,
|
2021-12-08 14:35:20 +00:00
|
|
|
Platform,
|
|
|
|
StatusBar
|
2021-10-20 19:29:21 +00:00
|
|
|
} from 'react-native';
|
2022-07-07 15:05:58 +00:00
|
|
|
import { useSafeAreaInsets } from 'react-native-safe-area-context';
|
2021-10-20 19:29:21 +00:00
|
|
|
|
|
|
|
import { StyleType } from '../../styles';
|
|
|
|
|
|
|
|
type Props = {
|
|
|
|
|
2022-11-10 14:48:53 +00:00
|
|
|
/**
|
|
|
|
* Adds bottom padding.
|
|
|
|
*/
|
|
|
|
addBottomPadding?: boolean,
|
|
|
|
|
2021-10-20 19:29:21 +00:00
|
|
|
/**
|
|
|
|
* The children component(s) of the Modal, to be rendered.
|
|
|
|
*/
|
2022-11-10 14:48:53 +00:00
|
|
|
children: ReactElement,
|
2021-10-20 19:29:21 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Additional style to be appended to the KeyboardAvoidingView content container.
|
|
|
|
*/
|
|
|
|
contentContainerStyle?: StyleType,
|
|
|
|
|
2022-08-01 09:16:13 +00:00
|
|
|
/**
|
|
|
|
* Disable forced keyboard dismiss?
|
|
|
|
*/
|
|
|
|
disableForcedKeyboardDismiss?: boolean,
|
|
|
|
|
2021-12-08 14:35:20 +00:00
|
|
|
/**
|
|
|
|
* Is a text input rendered at the bottom of the screen?
|
|
|
|
*/
|
|
|
|
hasBottomTextInput: boolean,
|
|
|
|
|
2021-10-20 19:29:21 +00:00
|
|
|
/**
|
|
|
|
* Is the screen rendering a tab navigator?
|
|
|
|
*/
|
|
|
|
hasTabNavigator: boolean,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Additional style to be appended to the KeyboardAvoidingView.
|
|
|
|
*/
|
|
|
|
style?: StyleType
|
|
|
|
}
|
|
|
|
|
|
|
|
const JitsiKeyboardAvoidingView = (
|
|
|
|
{
|
2022-11-10 14:48:53 +00:00
|
|
|
addBottomPadding = true,
|
2021-10-20 19:29:21 +00:00
|
|
|
children,
|
|
|
|
contentContainerStyle,
|
2022-11-22 16:13:36 +00:00
|
|
|
disableForcedKeyboardDismiss,
|
2021-10-20 19:29:21 +00:00
|
|
|
hasTabNavigator,
|
2021-12-08 14:35:20 +00:00
|
|
|
hasBottomTextInput,
|
2021-10-20 19:29:21 +00:00
|
|
|
style
|
|
|
|
}: Props) => {
|
2022-07-07 15:05:58 +00:00
|
|
|
const headerHeight = useHeaderHeight();
|
2021-10-20 19:29:21 +00:00
|
|
|
const insets = useSafeAreaInsets();
|
|
|
|
const [ bottomPadding, setBottomPadding ] = useState(insets.bottom);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
// This useEffect is needed because insets are undefined at first for some reason
|
|
|
|
// https://github.com/th3rdwave/react-native-safe-area-context/issues/54
|
|
|
|
setBottomPadding(insets.bottom);
|
2022-07-07 15:05:58 +00:00
|
|
|
}, [ insets.bottom ]);
|
2021-10-20 19:29:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
const tabNavigatorPadding
|
2022-07-07 15:05:58 +00:00
|
|
|
= hasTabNavigator ? headerHeight : 0;
|
2022-11-10 14:48:53 +00:00
|
|
|
const extraBottomPadding
|
|
|
|
= addBottomPadding ? bottomPadding : 0;
|
|
|
|
const noNotchDevicePadding = extraBottomPadding || 10;
|
2021-12-08 14:35:20 +00:00
|
|
|
const iosVerticalOffset
|
2022-07-07 15:05:58 +00:00
|
|
|
= headerHeight + noNotchDevicePadding + tabNavigatorPadding;
|
2021-12-08 14:35:20 +00:00
|
|
|
const androidVerticalOffset = hasBottomTextInput
|
2022-07-07 15:05:58 +00:00
|
|
|
? headerHeight + StatusBar.currentHeight : headerHeight;
|
2021-10-20 19:29:21 +00:00
|
|
|
|
2022-06-21 14:16:38 +00:00
|
|
|
// Tells the view what to do with taps
|
2022-08-01 09:16:13 +00:00
|
|
|
const shouldSetResponse = useCallback(() => !disableForcedKeyboardDismiss);
|
2022-06-21 14:16:38 +00:00
|
|
|
const onRelease = useCallback(() => Keyboard.dismiss());
|
|
|
|
|
2021-10-20 19:29:21 +00:00
|
|
|
return (
|
2021-11-16 15:01:43 +00:00
|
|
|
<KeyboardAvoidingView
|
|
|
|
behavior = { Platform.OS === 'ios' ? 'padding' : 'height' }
|
|
|
|
contentContainerStyle = { contentContainerStyle }
|
|
|
|
enabled = { true }
|
|
|
|
keyboardVerticalOffset = {
|
|
|
|
Platform.OS === 'ios'
|
|
|
|
? iosVerticalOffset
|
|
|
|
: androidVerticalOffset
|
|
|
|
}
|
2022-06-21 14:16:38 +00:00
|
|
|
onResponderRelease = { onRelease }
|
|
|
|
onStartShouldSetResponder = { shouldSetResponse }
|
2021-11-16 15:01:43 +00:00
|
|
|
style = { style }>
|
|
|
|
{ children }
|
|
|
|
</KeyboardAvoidingView>
|
2021-10-20 19:29:21 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
export default JitsiKeyboardAvoidingView;
|