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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // @flow
  2. import React, { PureComponent, type Node } from 'react';
  3. import { Platform, 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. * The type of {@code BottomSheet}'s React {@code Component} prop types.
  11. */
  12. type Props = {
  13. /**
  14. * The color-schemed stylesheet of the feature.
  15. */
  16. _styles: StyleType,
  17. /**
  18. * The children to be displayed within this component.
  19. */
  20. children: Node,
  21. /**
  22. * Handler for the cancel event, which happens when the user dismisses
  23. * the sheet.
  24. */
  25. onCancel: ?Function
  26. };
  27. /**
  28. * A component emulating Android's BottomSheet.
  29. */
  30. class BottomSheet extends PureComponent<Props> {
  31. /**
  32. * Implements React's {@link Component#render()}.
  33. *
  34. * @inheritdoc
  35. * @returns {ReactElement}
  36. */
  37. render() {
  38. const { _styles } = this.props;
  39. return (
  40. <SlidingView
  41. onHide = { this.props.onCancel }
  42. position = 'bottom'
  43. show = { true }>
  44. <View
  45. pointerEvents = 'box-none'
  46. style = { styles.sheetContainer }>
  47. <View
  48. pointerEvents = 'box-none'
  49. style = { styles.sheetAreaCover } />
  50. <View
  51. style = { [
  52. styles.sheetItemContainer,
  53. _styles.sheet
  54. ] }>
  55. <ScrollView
  56. bounces = { false }
  57. showsVerticalScrollIndicator = { false }>
  58. { this._getWrappedContent() }
  59. </ScrollView>
  60. </View>
  61. </View>
  62. </SlidingView>
  63. );
  64. }
  65. /**
  66. * Wraps the content when needed (iOS 11 and above), or just returns the original children.
  67. *
  68. * @returns {React$Element}
  69. */
  70. _getWrappedContent() {
  71. if (Platform.OS === 'ios') {
  72. const majorVersionIOS = parseInt(Platform.Version, 10);
  73. if (majorVersionIOS > 10) {
  74. return (
  75. <SafeAreaView>
  76. { this.props.children }
  77. </SafeAreaView>
  78. );
  79. }
  80. }
  81. return this.props.children;
  82. }
  83. }
  84. /**
  85. * Maps part of the Redux state to the props of this component.
  86. *
  87. * @param {Object} state - The Redux state.
  88. * @returns {{
  89. * _styles: StyleType
  90. * }}
  91. */
  92. function _mapStateToProps(state) {
  93. return {
  94. _styles: ColorSchemeRegistry.get(state, 'BottomSheet')
  95. };
  96. }
  97. export default connect(_mapStateToProps)(BottomSheet);