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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. /**
  44. * A component emulating Android's BottomSheet.
  45. */
  46. class BottomSheet extends PureComponent<Props> {
  47. panResponder: Object;
  48. /**
  49. * Instantiates a new component.
  50. *
  51. * @inheritdoc
  52. */
  53. constructor(props: Props) {
  54. super(props);
  55. this.panResponder = PanResponder.create({
  56. onStartShouldSetPanResponder: this._onShouldSetResponder.bind(this),
  57. onMoveShouldSetPanResponder: this._onShouldSetResponder.bind(this),
  58. onPanResponderRelease: this._onGestureEnd.bind(this)
  59. });
  60. }
  61. /**
  62. * Implements React's {@link Component#render()}.
  63. *
  64. * @inheritdoc
  65. * @returns {ReactElement}
  66. */
  67. render() {
  68. const { _styles, renderHeader } = this.props;
  69. return (
  70. <SlidingView
  71. onHide = { this.props.onCancel }
  72. position = 'bottom'
  73. show = { true }>
  74. <View
  75. pointerEvents = 'box-none'
  76. style = { styles.sheetContainer }>
  77. <View
  78. pointerEvents = 'box-none'
  79. style = { styles.sheetAreaCover } />
  80. { renderHeader && renderHeader() }
  81. <SafeAreaView
  82. style = { [
  83. styles.sheetItemContainer,
  84. _styles.sheet
  85. ] }
  86. { ...this.panResponder.panHandlers }>
  87. <ScrollView
  88. bounces = { false }
  89. showsVerticalScrollIndicator = { false }
  90. style = { styles.scrollView } >
  91. { this.props.children }
  92. </ScrollView>
  93. </SafeAreaView>
  94. </View>
  95. </SlidingView>
  96. );
  97. }
  98. /**
  99. * Callback to handle a gesture end event.
  100. *
  101. * @param {Object} evt - The native gesture event.
  102. * @param {Object} gestureState - The gesture state.
  103. * @returns {void}
  104. */
  105. _onGestureEnd(evt, gestureState) {
  106. const verticalSwipe = Math.abs(gestureState.vy) > Math.abs(gestureState.vx)
  107. && Math.abs(gestureState.vy) > GESTURE_SPEED_THRESHOLD;
  108. if (verticalSwipe) {
  109. const direction = gestureState.vy > 0 ? 'down' : 'up';
  110. const { onCancel, onSwipe } = this.props;
  111. let isSwipeHandled = false;
  112. if (onSwipe) {
  113. isSwipeHandled = onSwipe(direction);
  114. }
  115. if (direction === 'down' && !isSwipeHandled) {
  116. // Swipe down is a special gesture that can be used to close the
  117. // BottomSheet, so if the swipe is not handled by the parent
  118. // component, we consider it as a request to close.
  119. onCancel && onCancel();
  120. }
  121. }
  122. }
  123. /**
  124. * Returns true if the pan responder should activate, false otherwise.
  125. *
  126. * @param {Object} evt - The native gesture event.
  127. * @param {Object} gestureState - The gesture state.
  128. * @returns {boolean}
  129. */
  130. _onShouldSetResponder({ nativeEvent }, gestureState) {
  131. return nativeEvent.touches.length === 1
  132. && Math.abs(gestureState.dx) > GESTURE_DISTANCE_THRESHOLD
  133. && Math.abs(gestureState.dy) > GESTURE_DISTANCE_THRESHOLD;
  134. }
  135. }
  136. /**
  137. * Maps part of the Redux state to the props of this component.
  138. *
  139. * @param {Object} state - The Redux state.
  140. * @returns {{
  141. * _styles: StyleType
  142. * }}
  143. */
  144. function _mapStateToProps(state) {
  145. return {
  146. _styles: ColorSchemeRegistry.get(state, 'BottomSheet')
  147. };
  148. }
  149. export default connect(_mapStateToProps)(BottomSheet);