2022-07-07 18:05:58 +03:00
|
|
|
import { useHeaderHeight } from '@react-navigation/elements';
|
2023-04-24 14:09:50 +03:00
|
|
|
import React, { useCallback, useEffect, useState } from 'react';
|
2021-10-20 22:29:21 +03:00
|
|
|
import {
|
2022-06-21 17:16:38 +03:00
|
|
|
Keyboard,
|
2021-10-20 22:29:21 +03:00
|
|
|
KeyboardAvoidingView,
|
2021-12-08 16:35:20 +02:00
|
|
|
Platform,
|
2023-11-08 11:44:10 +02:00
|
|
|
StatusBar,
|
|
|
|
|
ViewStyle
|
2021-10-20 22:29:21 +03:00
|
|
|
} from 'react-native';
|
2022-07-07 18:05:58 +03:00
|
|
|
import { useSafeAreaInsets } from 'react-native-safe-area-context';
|
2021-10-20 22:29:21 +03:00
|
|
|
|
2023-04-24 14:09:50 +03:00
|
|
|
import { StyleType } from '../../styles/functions.any';
|
2021-10-20 22:29:21 +03:00
|
|
|
|
2023-04-24 14:09:50 +03:00
|
|
|
interface IProps {
|
2021-10-20 22:29:21 +03:00
|
|
|
|
2022-11-10 16:48:53 +02:00
|
|
|
/**
|
|
|
|
|
* Adds bottom padding.
|
|
|
|
|
*/
|
2023-04-24 14:09:50 +03:00
|
|
|
addBottomPadding?: boolean;
|
2022-11-10 16:48:53 +02:00
|
|
|
|
2021-10-20 22:29:21 +03:00
|
|
|
/**
|
|
|
|
|
* The children component(s) of the Modal, to be rendered.
|
|
|
|
|
*/
|
2023-04-24 14:09:50 +03:00
|
|
|
children: React.ReactNode;
|
2021-10-20 22:29:21 +03:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Additional style to be appended to the KeyboardAvoidingView content container.
|
|
|
|
|
*/
|
2023-04-24 14:09:50 +03:00
|
|
|
contentContainerStyle?: StyleType;
|
2021-10-20 22:29:21 +03:00
|
|
|
|
2022-08-01 12:16:13 +03:00
|
|
|
/**
|
|
|
|
|
* Disable forced keyboard dismiss?
|
|
|
|
|
*/
|
2023-04-24 14:09:50 +03:00
|
|
|
disableForcedKeyboardDismiss?: boolean;
|
2022-08-01 12:16:13 +03:00
|
|
|
|
2021-12-08 16:35:20 +02:00
|
|
|
/**
|
|
|
|
|
* Is a text input rendered at the bottom of the screen?
|
|
|
|
|
*/
|
2023-04-24 14:09:50 +03:00
|
|
|
hasBottomTextInput: boolean;
|
2021-12-08 16:35:20 +02:00
|
|
|
|
2021-10-20 22:29:21 +03:00
|
|
|
/**
|
2023-11-08 11:44:10 +02:00
|
|
|
* Is the screen header having an extra height?
|
2021-10-20 22:29:21 +03:00
|
|
|
*/
|
2023-11-08 11:44:10 +02:00
|
|
|
hasExtraHeaderHeight?: boolean;
|
2021-10-20 22:29:21 +03:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Additional style to be appended to the KeyboardAvoidingView.
|
|
|
|
|
*/
|
2023-04-24 14:09:50 +03:00
|
|
|
style?: StyleType;
|
2021-10-20 22:29:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const JitsiKeyboardAvoidingView = (
|
|
|
|
|
{
|
2022-11-10 16:48:53 +02:00
|
|
|
addBottomPadding = true,
|
2021-10-20 22:29:21 +03:00
|
|
|
children,
|
|
|
|
|
contentContainerStyle,
|
2022-11-22 18:13:36 +02:00
|
|
|
disableForcedKeyboardDismiss,
|
2021-12-08 16:35:20 +02:00
|
|
|
hasBottomTextInput,
|
2023-11-08 11:44:10 +02:00
|
|
|
hasExtraHeaderHeight,
|
2021-10-20 22:29:21 +03:00
|
|
|
style
|
2023-04-24 14:09:50 +03:00
|
|
|
}: IProps) => {
|
2022-07-07 18:05:58 +03:00
|
|
|
const headerHeight = useHeaderHeight();
|
2021-10-20 22:29:21 +03: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 18:05:58 +03:00
|
|
|
}, [ insets.bottom ]);
|
2021-10-20 22:29:21 +03:00
|
|
|
|
|
|
|
|
|
2023-11-08 11:44:10 +02:00
|
|
|
const extraHeaderHeight
|
|
|
|
|
= hasExtraHeaderHeight ? headerHeight : 0;
|
2022-11-10 16:48:53 +02:00
|
|
|
const extraBottomPadding
|
|
|
|
|
= addBottomPadding ? bottomPadding : 0;
|
|
|
|
|
const noNotchDevicePadding = extraBottomPadding || 10;
|
2021-12-08 16:35:20 +02:00
|
|
|
const iosVerticalOffset
|
2023-11-08 11:44:10 +02:00
|
|
|
= headerHeight + noNotchDevicePadding + extraHeaderHeight;
|
2021-12-08 16:35:20 +02:00
|
|
|
const androidVerticalOffset = hasBottomTextInput
|
2023-04-24 14:09:50 +03:00
|
|
|
? headerHeight + Number(StatusBar.currentHeight) : headerHeight;
|
2021-10-20 22:29:21 +03:00
|
|
|
|
2022-06-21 17:16:38 +03:00
|
|
|
// Tells the view what to do with taps
|
2023-04-24 14:09:50 +03:00
|
|
|
const shouldSetResponse = useCallback(() => !disableForcedKeyboardDismiss, []);
|
|
|
|
|
const onRelease = useCallback(() => Keyboard.dismiss(), []);
|
2022-06-21 17:16:38 +03:00
|
|
|
|
2021-10-20 22:29:21 +03:00
|
|
|
return (
|
2021-11-16 17:01:43 +02:00
|
|
|
<KeyboardAvoidingView
|
|
|
|
|
behavior = { Platform.OS === 'ios' ? 'padding' : 'height' }
|
2023-11-08 11:44:10 +02:00
|
|
|
contentContainerStyle = { contentContainerStyle as ViewStyle }
|
2021-11-16 17:01:43 +02:00
|
|
|
enabled = { true }
|
|
|
|
|
keyboardVerticalOffset = {
|
|
|
|
|
Platform.OS === 'ios'
|
|
|
|
|
? iosVerticalOffset
|
|
|
|
|
: androidVerticalOffset
|
|
|
|
|
}
|
2022-06-21 17:16:38 +03:00
|
|
|
onResponderRelease = { onRelease }
|
|
|
|
|
onStartShouldSetResponder = { shouldSetResponse }
|
2023-11-08 11:44:10 +02:00
|
|
|
style = { style as ViewStyle }>
|
2021-11-16 17:01:43 +02:00
|
|
|
{ children }
|
|
|
|
|
</KeyboardAvoidingView>
|
2021-10-20 22:29:21 +03:00
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export default JitsiKeyboardAvoidingView;
|