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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // @flow
  2. import React, { PureComponent, type Node } from 'react';
  3. import { Platform, SafeAreaView, 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. { this._getWrappedContent() }
  56. </View>
  57. </View>
  58. </SlidingView>
  59. );
  60. }
  61. /**
  62. * Wraps the content when needed (iOS 11 and above), or just returns the original children.
  63. *
  64. * @returns {React$Element}
  65. */
  66. _getWrappedContent() {
  67. if (Platform.OS === 'ios') {
  68. const majorVersionIOS = parseInt(Platform.Version, 10);
  69. if (majorVersionIOS > 10) {
  70. return (
  71. <SafeAreaView>
  72. { this.props.children }
  73. </SafeAreaView>
  74. );
  75. }
  76. }
  77. return this.props.children;
  78. }
  79. }
  80. /**
  81. * Maps part of the Redux state to the props of this component.
  82. *
  83. * @param {Object} state - The Redux state.
  84. * @returns {{
  85. * _styles: StyleType
  86. * }}
  87. */
  88. function _mapStateToProps(state) {
  89. return {
  90. _styles: ColorSchemeRegistry.get(state, 'BottomSheet')
  91. };
  92. }
  93. export default connect(_mapStateToProps)(BottomSheet);