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 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 color-schemed stylesheet of the feature.
  23. */
  24. _styles: StyleType,
  25. /**
  26. * The children to be displayed within this component.
  27. */
  28. children: Node,
  29. /**
  30. * Handler for the cancel event, which happens when the user dismisses
  31. * the sheet.
  32. */
  33. onCancel: ?Function,
  34. /**
  35. * Callback to be attached to the custom swipe event of the BottomSheet.
  36. */
  37. onSwipe?: Function,
  38. /**
  39. * Function to render a bottom sheet header element, if necessary.
  40. */
  41. renderHeader: ?Function,
  42. /**
  43. * Function to render a bottom sheet footer element, if necessary.
  44. */
  45. renderFooter: ?Function,
  46. /**
  47. * The height of the screen.
  48. */
  49. _height: number
  50. };
  51. /**
  52. * A component emulating Android's BottomSheet.
  53. */
  54. class BottomSheet extends PureComponent<Props> {
  55. panResponder: Object;
  56. /**
  57. * Instantiates a new component.
  58. *
  59. * @inheritdoc
  60. */
  61. constructor(props: Props) {
  62. super(props);
  63. this.panResponder = PanResponder.create({
  64. onStartShouldSetPanResponder: this._onShouldSetResponder.bind(this),
  65. onMoveShouldSetPanResponder: this._onShouldSetResponder.bind(this),
  66. onPanResponderRelease: this._onGestureEnd.bind(this)
  67. });
  68. }
  69. /**
  70. * Implements React's {@link Component#render()}.
  71. *
  72. * @inheritdoc
  73. * @returns {ReactElement}
  74. */
  75. render() {
  76. const { _styles, renderHeader, renderFooter, _height } = this.props;
  77. return (
  78. <SlidingView
  79. accessibilityRole = 'menu'
  80. accessibilityViewIsModal = { true }
  81. onHide = { this.props.onCancel }
  82. position = 'bottom'
  83. show = { true }>
  84. <View
  85. pointerEvents = 'box-none'
  86. style = { styles.sheetContainer }>
  87. <View
  88. pointerEvents = 'box-none'
  89. style = { styles.sheetAreaCover } />
  90. { renderHeader && renderHeader() }
  91. <SafeAreaView
  92. style = { [
  93. styles.sheetItemContainer,
  94. _styles.sheet,
  95. {
  96. maxHeight: _height - 100
  97. }
  98. ] }
  99. { ...this.panResponder.panHandlers }>
  100. <ScrollView
  101. bounces = { false }
  102. showsVerticalScrollIndicator = { false }
  103. style = { styles.scrollView } >
  104. { this.props.children }
  105. </ScrollView>
  106. { renderFooter && renderFooter() }
  107. </SafeAreaView>
  108. </View>
  109. </SlidingView>
  110. );
  111. }
  112. /**
  113. * Callback to handle a gesture end event.
  114. *
  115. * @param {Object} evt - The native gesture event.
  116. * @param {Object} gestureState - The gesture state.
  117. * @returns {void}
  118. */
  119. _onGestureEnd(evt, gestureState) {
  120. const verticalSwipe = Math.abs(gestureState.vy) > Math.abs(gestureState.vx)
  121. && Math.abs(gestureState.vy) > GESTURE_SPEED_THRESHOLD;
  122. if (verticalSwipe) {
  123. const direction = gestureState.vy > 0 ? 'down' : 'up';
  124. const { onCancel, onSwipe } = this.props;
  125. let isSwipeHandled = false;
  126. if (onSwipe) {
  127. isSwipeHandled = onSwipe(direction);
  128. }
  129. if (direction === 'down' && !isSwipeHandled) {
  130. // Swipe down is a special gesture that can be used to close the
  131. // BottomSheet, so if the swipe is not handled by the parent
  132. // component, we consider it as a request to close.
  133. onCancel && onCancel();
  134. }
  135. }
  136. }
  137. /**
  138. * Returns true if the pan responder should activate, false otherwise.
  139. *
  140. * @param {Object} evt - The native gesture event.
  141. * @param {Object} gestureState - The gesture state.
  142. * @returns {boolean}
  143. */
  144. _onShouldSetResponder({ nativeEvent }, gestureState) {
  145. return nativeEvent.touches.length === 1
  146. && Math.abs(gestureState.dx) > GESTURE_DISTANCE_THRESHOLD
  147. && Math.abs(gestureState.dy) > GESTURE_DISTANCE_THRESHOLD;
  148. }
  149. }
  150. /**
  151. * Maps part of the Redux state to the props of this component.
  152. *
  153. * @param {Object} state - The Redux state.
  154. * @returns {{
  155. * _styles: StyleType
  156. * }}
  157. */
  158. function _mapStateToProps(state) {
  159. return {
  160. _styles: ColorSchemeRegistry.get(state, 'BottomSheet'),
  161. _height: state['features/base/responsive-ui'].clientHeight
  162. };
  163. }
  164. export default connect(_mapStateToProps)(BottomSheet);