You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

JitsiKeyboardAvoidingView.js 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // @flow
  2. import { useHeaderHeight } from '@react-navigation/stack';
  3. import React, { useEffect, useState } from 'react';
  4. import {
  5. Keyboard,
  6. KeyboardAvoidingView,
  7. Platform,
  8. TouchableWithoutFeedback
  9. } from 'react-native';
  10. import { useSafeAreaInsets } from 'react-native-safe-area-context';
  11. import { StyleType } from '../../styles';
  12. type Props = {
  13. /**
  14. * The children component(s) of the Modal, to be rendered.
  15. */
  16. children: React$Node,
  17. /**
  18. * Additional style to be appended to the KeyboardAvoidingView content container.
  19. */
  20. contentContainerStyle?: StyleType,
  21. /**
  22. * Is the screen rendering a tab navigator?
  23. */
  24. hasTabNavigator: boolean,
  25. /**
  26. * Additional style to be appended to the KeyboardAvoidingView.
  27. */
  28. style?: StyleType
  29. }
  30. const JitsiKeyboardAvoidingView = (
  31. {
  32. children,
  33. contentContainerStyle,
  34. hasTabNavigator,
  35. style
  36. }: Props) => {
  37. const headerHeight = useHeaderHeight();
  38. const insets = useSafeAreaInsets();
  39. const [ bottomPadding, setBottomPadding ] = useState(insets.bottom);
  40. useEffect(() => {
  41. // This useEffect is needed because insets are undefined at first for some reason
  42. // https://github.com/th3rdwave/react-native-safe-area-context/issues/54
  43. setBottomPadding(insets.bottom);
  44. }, [ insets.bottom ]);
  45. const tabNavigatorPadding
  46. = hasTabNavigator ? headerHeight : 0;
  47. const noNotchDevicePadding = bottomPadding || 10;
  48. const iosVerticalOffset = headerHeight + noNotchDevicePadding + tabNavigatorPadding;
  49. const androidVerticalOffset = headerHeight;
  50. return (
  51. <TouchableWithoutFeedback
  52. /* eslint-disable-next-line react/jsx-handler-names */
  53. onPress = { Keyboard.dismiss }>
  54. <KeyboardAvoidingView
  55. behavior = { Platform.OS === 'ios' ? 'padding' : 'height' }
  56. contentContainerStyle = { contentContainerStyle }
  57. enabled = { true }
  58. keyboardVerticalOffset = {
  59. Platform.OS === 'ios'
  60. ? iosVerticalOffset
  61. : androidVerticalOffset
  62. }
  63. style = { style }>
  64. { children }
  65. </KeyboardAvoidingView>
  66. </TouchableWithoutFeedback>
  67. );
  68. };
  69. export default JitsiKeyboardAvoidingView;