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.1KB

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