Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

JitsiKeyboardAvoidingView.tsx 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import { useHeaderHeight } from '@react-navigation/elements';
  2. import React, { useCallback, useEffect, useState } from 'react';
  3. import {
  4. Keyboard,
  5. KeyboardAvoidingView,
  6. Platform,
  7. StatusBar
  8. } from 'react-native';
  9. import { useSafeAreaInsets } from 'react-native-safe-area-context';
  10. import { StyleType } from '../../styles/functions.any';
  11. interface IProps {
  12. /**
  13. * Adds bottom padding.
  14. */
  15. addBottomPadding?: boolean;
  16. /**
  17. * The children component(s) of the Modal, to be rendered.
  18. */
  19. children: React.ReactNode;
  20. /**
  21. * Additional style to be appended to the KeyboardAvoidingView content container.
  22. */
  23. contentContainerStyle?: StyleType;
  24. /**
  25. * Disable forced keyboard dismiss?
  26. */
  27. disableForcedKeyboardDismiss?: boolean;
  28. /**
  29. * Is a text input rendered at the bottom of the screen?
  30. */
  31. hasBottomTextInput: boolean;
  32. /**
  33. * Is the screen rendering a tab navigator?
  34. */
  35. hasTabNavigator: boolean;
  36. /**
  37. * Additional style to be appended to the KeyboardAvoidingView.
  38. */
  39. style?: StyleType;
  40. }
  41. const JitsiKeyboardAvoidingView = (
  42. {
  43. addBottomPadding = true,
  44. children,
  45. contentContainerStyle,
  46. disableForcedKeyboardDismiss,
  47. hasTabNavigator,
  48. hasBottomTextInput,
  49. style
  50. }: IProps) => {
  51. const headerHeight = useHeaderHeight();
  52. const insets = useSafeAreaInsets();
  53. const [ bottomPadding, setBottomPadding ] = useState(insets.bottom);
  54. useEffect(() => {
  55. // This useEffect is needed because insets are undefined at first for some reason
  56. // https://github.com/th3rdwave/react-native-safe-area-context/issues/54
  57. setBottomPadding(insets.bottom);
  58. }, [ insets.bottom ]);
  59. const tabNavigatorPadding
  60. = hasTabNavigator ? headerHeight : 0;
  61. const extraBottomPadding
  62. = addBottomPadding ? bottomPadding : 0;
  63. const noNotchDevicePadding = extraBottomPadding || 10;
  64. const iosVerticalOffset
  65. = headerHeight + noNotchDevicePadding + tabNavigatorPadding;
  66. const androidVerticalOffset = hasBottomTextInput
  67. ? headerHeight + Number(StatusBar.currentHeight) : headerHeight;
  68. // Tells the view what to do with taps
  69. const shouldSetResponse = useCallback(() => !disableForcedKeyboardDismiss, []);
  70. const onRelease = useCallback(() => Keyboard.dismiss(), []);
  71. return (
  72. <KeyboardAvoidingView
  73. behavior = { Platform.OS === 'ios' ? 'padding' : 'height' }
  74. contentContainerStyle = { contentContainerStyle }
  75. enabled = { true }
  76. keyboardVerticalOffset = {
  77. Platform.OS === 'ios'
  78. ? iosVerticalOffset
  79. : androidVerticalOffset
  80. }
  81. onResponderRelease = { onRelease }
  82. onStartShouldSetResponder = { shouldSetResponse }
  83. style = { style }>
  84. { children }
  85. </KeyboardAvoidingView>
  86. );
  87. };
  88. export default JitsiKeyboardAvoidingView;