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.

BottomSheet.js 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. // @flow
  2. import React, { PureComponent, type Node } from 'react';
  3. import { PanResponder, SafeAreaView, ScrollView, View } from 'react-native';
  4. import { ColorSchemeRegistry } from '../../../color-scheme';
  5. import { SlidingView } from '../../../react';
  6. import { connect } from '../../../redux';
  7. import { StyleType } from '../../../styles';
  8. import { bottomSheetStyles as styles } from './styles';
  9. /**
  10. * Minimal distance that needs to be moved by the finger to consider it a swipe.
  11. */
  12. const GESTURE_DISTANCE_THRESHOLD = 5;
  13. /**
  14. * The minimal speed needed to be achieved by the finger to consider it as a swipe.
  15. */
  16. const GESTURE_SPEED_THRESHOLD = 0.2;
  17. /**
  18. * The type of {@code BottomSheet}'s React {@code Component} prop types.
  19. */
  20. type Props = {
  21. /**
  22. * The height of the screen.
  23. */
  24. _height: number,
  25. /**
  26. * The color-schemed stylesheet of the feature.
  27. */
  28. _styles: StyleType,
  29. /**
  30. * Whether to add padding to scroll view.
  31. */
  32. addScrollViewPadding?: boolean,
  33. /**
  34. * The children to be displayed within this component.
  35. */
  36. children: Node,
  37. /**
  38. * Handler for the cancel event, which happens when the user dismisses
  39. * the sheet.
  40. */
  41. onCancel: ?Function,
  42. /**
  43. * Callback to be attached to the custom swipe event of the BottomSheet.
  44. */
  45. onSwipe?: Function,
  46. /**
  47. * Function to render a bottom sheet header element, if necessary.
  48. */
  49. renderHeader: ?Function,
  50. /**
  51. * Function to render a bottom sheet footer element, if necessary.
  52. */
  53. renderFooter: ?Function,
  54. /**
  55. * Whether to show sliding view or not.
  56. */
  57. showSlidingView?: boolean,
  58. /**
  59. * The component's external style
  60. */
  61. style: Object
  62. };
  63. /**
  64. * A component emulating Android's BottomSheet.
  65. */
  66. class BottomSheet extends PureComponent<Props> {
  67. panResponder: Object;
  68. /**
  69. * Default values for {@code BottomSheet} component's properties.
  70. *
  71. * @static
  72. */
  73. static defaultProps = {
  74. addScrollViewPadding: true,
  75. showSlidingView: true
  76. };
  77. /**
  78. * Instantiates a new component.
  79. *
  80. * @inheritdoc
  81. */
  82. constructor(props: Props) {
  83. super(props);
  84. this.panResponder = PanResponder.create({
  85. onStartShouldSetPanResponder: this._onShouldSetResponder.bind(this),
  86. onMoveShouldSetPanResponder: this._onShouldSetResponder.bind(this),
  87. onPanResponderRelease: this._onGestureEnd.bind(this)
  88. });
  89. }
  90. /**
  91. * Implements React's {@link Component#render()}.
  92. *
  93. * @inheritdoc
  94. * @returns {ReactElement}
  95. */
  96. render() {
  97. const {
  98. _height,
  99. _styles,
  100. addScrollViewPadding,
  101. renderHeader,
  102. renderFooter,
  103. showSlidingView,
  104. style
  105. } = this.props;
  106. return (
  107. <SlidingView
  108. accessibilityRole = 'menu'
  109. accessibilityViewIsModal = { true }
  110. onHide = { this.props.onCancel }
  111. position = 'bottom'
  112. show = { showSlidingView }>
  113. <View
  114. pointerEvents = 'box-none'
  115. style = { styles.sheetContainer }>
  116. <View
  117. pointerEvents = 'box-none'
  118. style = { styles.sheetAreaCover } />
  119. { renderHeader && renderHeader() }
  120. <SafeAreaView
  121. style = { [
  122. styles.sheetItemContainer,
  123. renderHeader
  124. ? _styles.sheetHeader
  125. : _styles.sheet,
  126. style,
  127. {
  128. maxHeight: _height - 100
  129. }
  130. ] }
  131. { ...this.panResponder.panHandlers }>
  132. <ScrollView
  133. bounces = { false }
  134. showsVerticalScrollIndicator = { false }
  135. style = { addScrollViewPadding && styles.scrollView } >
  136. { this.props.children }
  137. </ScrollView>
  138. { renderFooter && renderFooter() }
  139. </SafeAreaView>
  140. </View>
  141. </SlidingView>
  142. );
  143. }
  144. /**
  145. * Callback to handle a gesture end event.
  146. *
  147. * @param {Object} evt - The native gesture event.
  148. * @param {Object} gestureState - The gesture state.
  149. * @returns {void}
  150. */
  151. _onGestureEnd(evt, gestureState) {
  152. const verticalSwipe = Math.abs(gestureState.vy) > Math.abs(gestureState.vx)
  153. && Math.abs(gestureState.vy) > GESTURE_SPEED_THRESHOLD;
  154. if (verticalSwipe) {
  155. const direction = gestureState.vy > 0 ? 'down' : 'up';
  156. const { onCancel, onSwipe } = this.props;
  157. let isSwipeHandled = false;
  158. if (onSwipe) {
  159. isSwipeHandled = onSwipe(direction);
  160. }
  161. if (direction === 'down' && !isSwipeHandled) {
  162. // Swipe down is a special gesture that can be used to close the
  163. // BottomSheet, so if the swipe is not handled by the parent
  164. // component, we consider it as a request to close.
  165. onCancel && onCancel();
  166. }
  167. }
  168. }
  169. /**
  170. * Returns true if the pan responder should activate, false otherwise.
  171. *
  172. * @param {Object} evt - The native gesture event.
  173. * @param {Object} gestureState - The gesture state.
  174. * @returns {boolean}
  175. */
  176. _onShouldSetResponder({ nativeEvent }, gestureState) {
  177. return nativeEvent.touches.length === 1
  178. && Math.abs(gestureState.dx) > GESTURE_DISTANCE_THRESHOLD
  179. && Math.abs(gestureState.dy) > GESTURE_DISTANCE_THRESHOLD;
  180. }
  181. }
  182. /**
  183. * Maps part of the Redux state to the props of this component.
  184. *
  185. * @param {Object} state - The Redux state.
  186. * @returns {{
  187. * _styles: StyleType
  188. * }}
  189. */
  190. function _mapStateToProps(state) {
  191. return {
  192. _styles: ColorSchemeRegistry.get(state, 'BottomSheet'),
  193. _height: state['features/base/responsive-ui'].clientHeight
  194. };
  195. }
  196. export default connect(_mapStateToProps)(BottomSheet);