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.

JitsiModal.js 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. // @flow
  2. import React, { PureComponent } from 'react';
  3. import { KeyboardAvoidingView, Platform, SafeAreaView } from 'react-native';
  4. import { ColorSchemeRegistry } from '../../color-scheme';
  5. import { HeaderWithNavigation, SlidingView } from '../../react';
  6. import { connect } from '../../redux';
  7. import { StyleType } from '../../styles';
  8. import { setActiveModalId } from '../actions';
  9. import styles from './styles';
  10. type Props = {
  11. /**
  12. * The color schemed style of the common header component.
  13. */
  14. _headerStyles: StyleType,
  15. /**
  16. * True if the modal should be shown, false otherwise.
  17. */
  18. _show: boolean,
  19. /**
  20. * The color schemed style of the modal.
  21. */
  22. _styles: StyleType,
  23. /**
  24. * The children component(s) of the Modal, to be rendered.
  25. */
  26. children: React$Node,
  27. /**
  28. * The Redux Dispatch function.
  29. */
  30. dispatch: Function,
  31. /**
  32. * Optional function that renders a footer component, if needed.
  33. */
  34. footerComponent?: Function,
  35. /**
  36. * Props to be passed over to the header.
  37. *
  38. * See {@code HeaderWithNavigation} for more details.
  39. */
  40. headerProps: Object,
  41. /**
  42. * True if the header with navigation should be hidden, false otherwise.
  43. */
  44. hideHeaderWithNavigation?: boolean,
  45. /**
  46. * The ID of the modal that is being rendered. This is used to show/hide the modal.
  47. */
  48. modalId: string,
  49. /**
  50. * Callback to be invoked when the modal closes.
  51. */
  52. onClose?: Function,
  53. /**
  54. * The position from where the modal should be opened. This is derived from the
  55. * props of the {@code SlidingView} with the same name.
  56. */
  57. position?: string,
  58. /**
  59. * Additional style to be appended to the View containing the content of the modal.
  60. */
  61. style?: StyleType
  62. };
  63. /**
  64. * Implements a custom Jitsi Modal that doesn't use the built in native
  65. * Modal component of React Native.
  66. */
  67. class JitsiModal extends PureComponent<Props> {
  68. static defaultProps = {
  69. position: 'bottom',
  70. hideHeaderWithNavigation: false
  71. };
  72. /**
  73. * Instantiates a new component.
  74. *
  75. * @inheritdoc
  76. */
  77. constructor(props: Props) {
  78. super(props);
  79. this._onRequestClose = this._onRequestClose.bind(this);
  80. }
  81. /**
  82. * Implements {@code PureComponent#render}.
  83. *
  84. * @inheritdoc
  85. */
  86. render() {
  87. const {
  88. _headerStyles,
  89. _show,
  90. _styles,
  91. children,
  92. footerComponent,
  93. headerProps,
  94. position,
  95. hideHeaderWithNavigation,
  96. style
  97. } = this.props;
  98. return (
  99. <SlidingView
  100. onHide = { this._onRequestClose }
  101. position = { position }
  102. show = { _show }>
  103. <KeyboardAvoidingView
  104. behavior =
  105. {
  106. Platform.OS === 'ios'
  107. ? 'padding' : 'height'
  108. }
  109. enabled = { true }
  110. style = { [
  111. _headerStyles.page,
  112. _styles.page,
  113. style
  114. ] }>
  115. <HeaderWithNavigation
  116. { ...headerProps }
  117. hideHeaderWithNavigation = { hideHeaderWithNavigation }
  118. onPressBack = { this._onRequestClose } />
  119. <SafeAreaView style = { styles.safeArea }>
  120. { children }
  121. </SafeAreaView>
  122. { footerComponent && footerComponent() }
  123. </KeyboardAvoidingView>
  124. </SlidingView>
  125. );
  126. }
  127. _onRequestClose: () => boolean;
  128. /**
  129. * Callback to be invoked when the SlidingView requests closing.
  130. *
  131. * @returns {boolean}
  132. */
  133. _onRequestClose() {
  134. const { _show, dispatch, onClose } = this.props;
  135. let shouldCloseModal = true;
  136. if (_show) {
  137. if (typeof onClose === 'function') {
  138. shouldCloseModal = onClose();
  139. }
  140. shouldCloseModal && dispatch(setActiveModalId());
  141. return shouldCloseModal;
  142. }
  143. return false;
  144. }
  145. }
  146. /**
  147. * Maps part of the Redix state to the props of this component.
  148. *
  149. * @param {Object} state - The Redux state.
  150. * @param {Props} ownProps - The own props of the component.
  151. * @returns {Props}
  152. */
  153. function _mapStateToProps(state, ownProps): $Shape<Props> {
  154. return {
  155. _headerStyles: ColorSchemeRegistry.get(state, 'Header'),
  156. _show: state['features/base/modal'].activeModalId === ownProps.modalId,
  157. _styles: ColorSchemeRegistry.get(state, 'Modal')
  158. };
  159. }
  160. export default connect(_mapStateToProps)(JitsiModal);