2018-05-07 20:55:17 +00:00
|
|
|
// @flow
|
|
|
|
|
2019-04-24 11:05:14 +00:00
|
|
|
import React, { PureComponent, type Node } from 'react';
|
2019-12-12 13:52:41 +00:00
|
|
|
import { PanResponder, SafeAreaView, ScrollView, View } from 'react-native';
|
2019-01-09 16:58:29 +00:00
|
|
|
|
2019-01-22 10:35:28 +00:00
|
|
|
import { ColorSchemeRegistry } from '../../../color-scheme';
|
2019-04-24 11:05:14 +00:00
|
|
|
import { SlidingView } from '../../../react';
|
2019-03-21 16:38:29 +00:00
|
|
|
import { connect } from '../../../redux';
|
2019-01-22 10:35:28 +00:00
|
|
|
import { StyleType } from '../../../styles';
|
2018-05-07 20:55:17 +00:00
|
|
|
|
|
|
|
import { bottomSheetStyles as styles } from './styles';
|
|
|
|
|
2019-12-12 13:52:41 +00:00
|
|
|
/**
|
|
|
|
* Minimal distance that needs to be moved by the finger to consider it a swipe.
|
|
|
|
*/
|
|
|
|
const GESTURE_DISTANCE_THRESHOLD = 5;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The minimal speed needed to be achieved by the finger to consider it as a swipe.
|
|
|
|
*/
|
|
|
|
const GESTURE_SPEED_THRESHOLD = 0.2;
|
|
|
|
|
2018-05-07 20:55:17 +00:00
|
|
|
/**
|
|
|
|
* The type of {@code BottomSheet}'s React {@code Component} prop types.
|
|
|
|
*/
|
|
|
|
type Props = {
|
|
|
|
|
2021-06-03 16:23:18 +00:00
|
|
|
/**
|
|
|
|
* The height of the screen.
|
|
|
|
*/
|
|
|
|
_height: number,
|
|
|
|
|
2019-01-22 10:35:28 +00:00
|
|
|
/**
|
|
|
|
* The color-schemed stylesheet of the feature.
|
|
|
|
*/
|
|
|
|
_styles: StyleType,
|
|
|
|
|
2021-06-03 16:23:18 +00:00
|
|
|
/**
|
|
|
|
* Whether to add padding to scroll view.
|
|
|
|
*/
|
|
|
|
addScrollViewPadding?: boolean,
|
|
|
|
|
2018-05-07 20:55:17 +00:00
|
|
|
/**
|
|
|
|
* The children to be displayed within this component.
|
|
|
|
*/
|
|
|
|
children: Node,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handler for the cancel event, which happens when the user dismisses
|
|
|
|
* the sheet.
|
|
|
|
*/
|
2019-11-25 12:01:54 +00:00
|
|
|
onCancel: ?Function,
|
|
|
|
|
2019-12-12 13:52:41 +00:00
|
|
|
/**
|
|
|
|
* Callback to be attached to the custom swipe event of the BottomSheet.
|
|
|
|
*/
|
2020-03-23 08:37:35 +00:00
|
|
|
onSwipe?: Function,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function to render a bottom sheet header element, if necessary.
|
|
|
|
*/
|
2021-07-13 06:50:08 +00:00
|
|
|
renderHeader: ?Function,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function to render a bottom sheet footer element, if necessary.
|
|
|
|
*/
|
|
|
|
renderFooter: ?Function,
|
|
|
|
|
|
|
|
/**
|
2021-06-03 16:23:18 +00:00
|
|
|
* Whether to show sliding view or not.
|
|
|
|
*/
|
|
|
|
showSlidingView?: boolean,
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The component's external style
|
|
|
|
*/
|
|
|
|
style: Object
|
2018-05-07 20:55:17 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2019-04-24 11:05:14 +00:00
|
|
|
* A component emulating Android's BottomSheet.
|
2018-05-07 20:55:17 +00:00
|
|
|
*/
|
2019-04-24 11:05:14 +00:00
|
|
|
class BottomSheet extends PureComponent<Props> {
|
2019-12-12 13:52:41 +00:00
|
|
|
panResponder: Object;
|
|
|
|
|
2021-06-03 16:23:18 +00:00
|
|
|
/**
|
|
|
|
* Default values for {@code BottomSheet} component's properties.
|
|
|
|
*
|
|
|
|
* @static
|
|
|
|
*/
|
|
|
|
static defaultProps = {
|
|
|
|
addScrollViewPadding: true,
|
|
|
|
showSlidingView: true
|
|
|
|
};
|
|
|
|
|
2019-12-12 13:52:41 +00:00
|
|
|
/**
|
|
|
|
* Instantiates a new component.
|
|
|
|
*
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
constructor(props: Props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.panResponder = PanResponder.create({
|
|
|
|
onStartShouldSetPanResponder: this._onShouldSetResponder.bind(this),
|
|
|
|
onMoveShouldSetPanResponder: this._onShouldSetResponder.bind(this),
|
|
|
|
onPanResponderRelease: this._onGestureEnd.bind(this)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-05-07 20:55:17 +00:00
|
|
|
/**
|
|
|
|
* Implements React's {@link Component#render()}.
|
|
|
|
*
|
|
|
|
* @inheritdoc
|
|
|
|
* @returns {ReactElement}
|
|
|
|
*/
|
|
|
|
render() {
|
2021-06-03 16:23:18 +00:00
|
|
|
const {
|
|
|
|
_height,
|
|
|
|
_styles,
|
|
|
|
addScrollViewPadding,
|
|
|
|
renderHeader,
|
|
|
|
renderFooter,
|
|
|
|
showSlidingView,
|
|
|
|
style
|
|
|
|
} = this.props;
|
2019-01-22 10:35:28 +00:00
|
|
|
|
2019-04-24 11:05:14 +00:00
|
|
|
return (
|
|
|
|
<SlidingView
|
2020-04-10 13:07:48 +00:00
|
|
|
accessibilityRole = 'menu'
|
|
|
|
accessibilityViewIsModal = { true }
|
2019-04-24 11:05:14 +00:00
|
|
|
onHide = { this.props.onCancel }
|
|
|
|
position = 'bottom'
|
2021-06-03 16:23:18 +00:00
|
|
|
show = { showSlidingView }>
|
2019-05-24 07:24:47 +00:00
|
|
|
<View
|
|
|
|
pointerEvents = 'box-none'
|
|
|
|
style = { styles.sheetContainer }>
|
|
|
|
<View
|
|
|
|
pointerEvents = 'box-none'
|
|
|
|
style = { styles.sheetAreaCover } />
|
2020-03-23 08:37:35 +00:00
|
|
|
{ renderHeader && renderHeader() }
|
2019-11-25 12:01:54 +00:00
|
|
|
<SafeAreaView
|
2019-05-24 07:24:47 +00:00
|
|
|
style = { [
|
|
|
|
styles.sheetItemContainer,
|
2021-08-04 13:11:32 +00:00
|
|
|
renderHeader
|
|
|
|
? _styles.sheetHeader
|
|
|
|
: _styles.sheet,
|
2021-06-03 16:23:18 +00:00
|
|
|
style,
|
2021-07-13 06:50:08 +00:00
|
|
|
{
|
|
|
|
maxHeight: _height - 100
|
|
|
|
}
|
2019-12-12 13:52:41 +00:00
|
|
|
] }
|
|
|
|
{ ...this.panResponder.panHandlers }>
|
2019-11-25 12:01:54 +00:00
|
|
|
<ScrollView
|
|
|
|
bounces = { false }
|
|
|
|
showsVerticalScrollIndicator = { false }
|
2021-06-03 16:23:18 +00:00
|
|
|
style = { addScrollViewPadding && styles.scrollView } >
|
2019-11-25 12:01:54 +00:00
|
|
|
{ this.props.children }
|
|
|
|
</ScrollView>
|
2021-07-13 06:50:08 +00:00
|
|
|
{ renderFooter && renderFooter() }
|
2019-11-25 12:01:54 +00:00
|
|
|
</SafeAreaView>
|
2019-05-24 07:24:47 +00:00
|
|
|
</View>
|
2019-04-24 11:05:14 +00:00
|
|
|
</SlidingView>
|
|
|
|
);
|
2018-05-07 20:55:17 +00:00
|
|
|
}
|
2019-12-12 13:52:41 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback to handle a gesture end event.
|
|
|
|
*
|
|
|
|
* @param {Object} evt - The native gesture event.
|
|
|
|
* @param {Object} gestureState - The gesture state.
|
|
|
|
* @returns {void}
|
|
|
|
*/
|
|
|
|
_onGestureEnd(evt, gestureState) {
|
|
|
|
const verticalSwipe = Math.abs(gestureState.vy) > Math.abs(gestureState.vx)
|
|
|
|
&& Math.abs(gestureState.vy) > GESTURE_SPEED_THRESHOLD;
|
|
|
|
|
|
|
|
if (verticalSwipe) {
|
|
|
|
const direction = gestureState.vy > 0 ? 'down' : 'up';
|
|
|
|
const { onCancel, onSwipe } = this.props;
|
|
|
|
let isSwipeHandled = false;
|
|
|
|
|
|
|
|
if (onSwipe) {
|
|
|
|
isSwipeHandled = onSwipe(direction);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (direction === 'down' && !isSwipeHandled) {
|
|
|
|
// Swipe down is a special gesture that can be used to close the
|
|
|
|
// BottomSheet, so if the swipe is not handled by the parent
|
|
|
|
// component, we consider it as a request to close.
|
|
|
|
onCancel && onCancel();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if the pan responder should activate, false otherwise.
|
|
|
|
*
|
|
|
|
* @param {Object} evt - The native gesture event.
|
|
|
|
* @param {Object} gestureState - The gesture state.
|
|
|
|
* @returns {boolean}
|
|
|
|
*/
|
|
|
|
_onShouldSetResponder({ nativeEvent }, gestureState) {
|
|
|
|
return nativeEvent.touches.length === 1
|
|
|
|
&& Math.abs(gestureState.dx) > GESTURE_DISTANCE_THRESHOLD
|
|
|
|
&& Math.abs(gestureState.dy) > GESTURE_DISTANCE_THRESHOLD;
|
|
|
|
}
|
2018-05-07 20:55:17 +00:00
|
|
|
}
|
2019-01-22 10:35:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Maps part of the Redux state to the props of this component.
|
|
|
|
*
|
|
|
|
* @param {Object} state - The Redux state.
|
|
|
|
* @returns {{
|
|
|
|
* _styles: StyleType
|
|
|
|
* }}
|
|
|
|
*/
|
|
|
|
function _mapStateToProps(state) {
|
|
|
|
return {
|
2021-07-13 06:50:08 +00:00
|
|
|
_styles: ColorSchemeRegistry.get(state, 'BottomSheet'),
|
|
|
|
_height: state['features/base/responsive-ui'].clientHeight
|
2019-01-22 10:35:28 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export default connect(_mapStateToProps)(BottomSheet);
|