Advertisement
Guest User

Untitled

a guest
Sep 15th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.19 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import {
  3. ScrollView,
  4. StyleSheet,
  5. Text,
  6. Button,
  7. View,
  8. SafeAreaView,
  9. } from 'react-native';
  10. import Modal from 'react-native-modal';
  11.  
  12. export default class Example extends Component {
  13. state = {
  14. visibleModalId: null,
  15. };
  16.  
  17. renderModalContent = () => (
  18. <View style={styles.content}>
  19. <Text style={styles.contentTitle}>Hi 👋!</Text>
  20. <Button
  21. onPress={() => this.setState({ visibleModal: null })}
  22. title="Close"
  23. />
  24. </View>
  25. );
  26.  
  27. handleOnScroll = event => {
  28. this.setState({
  29. scrollOffset: event.nativeEvent.contentOffset.y,
  30. });
  31. };
  32.  
  33. handleScrollTo = p => {
  34. if (this.scrollViewRef) {
  35. this.scrollViewRef.scrollTo(p);
  36. }
  37. };
  38.  
  39. render() {
  40. return (
  41. <View style={styles.container}>
  42. <Button
  43. onPress={() => this.setState({ visibleModal: 'default' })}
  44. title="Default"
  45. />
  46. <Button
  47. onPress={() => this.setState({ visibleModal: 'sliding' })}
  48. title="Sliding from the sides"
  49. />
  50. <Button
  51. onPress={() => this.setState({ visibleModal: 'slow' })}
  52. title="Sloooow"
  53. />
  54. <Button
  55. onPress={() => this.setState({ visibleModal: 'fancy' })}
  56. title="Fancy!"
  57. />
  58. <Button
  59. onPress={() => this.setState({ visibleModal: 'bottom' })}
  60. title="Bottom half"
  61. />
  62. <Button
  63. onPress={() => this.setState({ visibleModal: 'backdropPress' })}
  64. title="Close on backdrop press"
  65. />
  66. <Button
  67. onPress={() => this.setState({ visibleModal: 'swipeable' })}
  68. title="Swipeable"
  69. />
  70. <Button
  71. onPress={() => this.setState({ visibleModal: 'scrollable' })}
  72. title="Scrollable"
  73. />
  74. <Button
  75. onPress={() => this.setState({ visibleModal: 'customBackdrop' })}
  76. title="Custom backdrop"
  77. />
  78. <Modal isVisible={this.state.visibleModal === 'default'}>
  79. {this.renderModalContent()}
  80. </Modal>
  81. <Modal
  82. isVisible={this.state.visibleModal === 'sliding'}
  83. animationIn="slideInLeft"
  84. animationOut="slideOutRight"
  85. >
  86. {this.renderModalContent()}
  87. </Modal>
  88. <Modal
  89. isVisible={this.state.visibleModal === 'slow'}
  90. animationInTiming={1000}
  91. animationOutTiming={1000}
  92. backdropTransitionInTiming={800}
  93. backdropTransitionOutTiming={800}
  94. >
  95. {this.renderModalContent()}
  96. </Modal>
  97. <Modal
  98. isVisible={this.state.visibleModal === 'fancy'}
  99. backdropColor="#B4B3DB"
  100. backdropOpacity={0.8}
  101. animationIn="zoomInDown"
  102. animationOut="zoomOutUp"
  103. animationInTiming={600}
  104. animationOutTiming={600}
  105. backdropTransitionInTiming={600}
  106. backdropTransitionOutTiming={600}
  107. >
  108. {this.renderModalContent()}
  109. </Modal>
  110. <Modal
  111. isVisible={this.state.visibleModal === 'bottom'}
  112. onSwipeComplete={() => this.setState({ visibleModal: null })}
  113. swipeDirection={['up', 'left', 'right', 'down']}
  114. style={styles.bottomModal}
  115. >
  116. {this.renderModalContent()}
  117. </Modal>
  118. <Modal
  119. isVisible={this.state.visibleModal === 'backdropPress'}
  120. onBackdropPress={() => this.setState({ visibleModal: null })}
  121. >
  122. {this.renderModalContent()}
  123. </Modal>
  124. <Modal
  125. isVisible={this.state.visibleModal === 'swipeable'}
  126. onSwipeComplete={() => this.setState({ visibleModal: null })}
  127. swipeDirection={['down']}
  128. >
  129. {this.renderModalContent()}
  130. </Modal>
  131. <Modal
  132. isVisible={this.state.visibleModal === 'scrollable'}
  133. onSwipeComplete={() => this.setState({ visibleModal: null })}
  134. swipeDirection="down"
  135. scrollTo={this.handleScrollTo}
  136. scrollOffset={this.state.scrollOffset}
  137. scrollOffsetMax={400 - 300} // content height - ScrollView height
  138. style={styles.bottomModal}
  139. >
  140. <View style={styles.scrollableModal}>
  141. <ScrollView
  142. ref={ref => (this.scrollViewRef = ref)}
  143. onScroll={this.handleOnScroll}
  144. scrollEventThrottle={16}
  145. >
  146. <View style={styles.scrollableModalContent1}>
  147. <Text style={styles.scrollableModalText1}>You can scroll me up! 👆</Text>
  148. </View>
  149. <View style={styles.scrollableModalContent2}>
  150. <Text style={styles.scrollableModalText2}>Same here as well! ☝</Text>
  151. </View>
  152. </ScrollView>
  153. </View>
  154. </Modal>
  155. <Modal
  156. isVisible={this.state.visibleModal === 'customBackdrop'}
  157. customBackdrop={
  158. <SafeAreaView style={styles.customBackdrop}>
  159. <Text style={styles.customBackdropText}>
  160. I'm in the backdrop! 👋
  161. </Text>
  162. </SafeAreaView>
  163. }
  164. >
  165. {this.renderModalContent()}
  166. </Modal>
  167. </View>
  168. );
  169. }
  170. }
  171. const styles = StyleSheet.create({
  172. container: {
  173. flex: 1,
  174. justifyContent: 'center',
  175. alignItems: 'center',
  176. backgroundColor: 'white',
  177. },
  178. content: {
  179. backgroundColor: 'white',
  180. padding: 22,
  181. justifyContent: 'center',
  182. alignItems: 'center',
  183. borderRadius: 4,
  184. borderColor: 'rgba(0, 0, 0, 0.1)',
  185. },
  186. contentTitle: {
  187. fontSize: 20,
  188. marginBottom: 12,
  189. },
  190. bottomModal: {
  191. justifyContent: 'flex-end',
  192. margin: 0,
  193. },
  194. scrollableModal: {
  195. height: 300,
  196. },
  197. scrollableModalContent1: {
  198. height: 200,
  199. backgroundColor: '#87BBE0',
  200. alignItems: 'center',
  201. justifyContent: 'center',
  202. },
  203. scrollableModalText1: {
  204. fontSize: 20,
  205. color: 'white',
  206. },
  207. scrollableModalContent2: {
  208. height: 200,
  209. backgroundColor: '#A9DCD3',
  210. alignItems: 'center',
  211. justifyContent: 'center',
  212. },
  213. scrollableModalText2: {
  214. fontSize: 20,
  215. color: 'white',
  216. },
  217. customBackdrop: {
  218. flex: 1,
  219. backgroundColor: '#87BBE0',
  220. alignItems: 'center',
  221. },
  222. customBackdropText: {
  223. marginTop: 10,
  224. fontSize: 17,
  225. },
  226. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement