Guest User

Untitled

a guest
Apr 23rd, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.82 KB | None | 0 0
  1. import React from 'react'
  2. import { Dimensions, Animated, View, Easing, Platform } from 'react-native'
  3. import PropTypes from 'prop-types'
  4.  
  5. /**
  6. * DismissableScrollview
  7. * Will dismiss element when the user scrolls from the top and there is no more vertical
  8. * scrolling available.
  9. */
  10. export default class DismissableScrollview extends React.Component {
  11. static propTypes = {
  12. onSwipeDownThreshold: PropTypes.number,
  13. onSwipeAway: PropTypes.func,
  14. children: PropTypes.node
  15. }
  16.  
  17. static defaultProps = {
  18. onBeforeSwipeAway: () => { },
  19. onSwipeAway: () => { },
  20.  
  21. // Account for lack of overscroll in Android
  22. onSwipeDownThreshold: Platform.OS === 'android' ? 0 : -10
  23. }
  24.  
  25. constructor() {
  26. super()
  27.  
  28. this.state = {
  29. cardLocation: new Animated.Value(Dimensions.get('screen').height),
  30. isTransparentBackground: true
  31. }
  32.  
  33. Animated.timing(this.state.cardLocation, {
  34. duration: 250,
  35. easing: Easing.inOut(Easing.quad),
  36. toValue: 0,
  37. delay: 0,
  38. useNativeDriver: true
  39. }).start()
  40.  
  41. this._startDragValue = 0
  42. }
  43.  
  44. animateAway(onCompleteCb = () => { }) {
  45. this.props.onBeforeSwipeAway()
  46. Animated.timing(this.state.cardLocation, {
  47. toValue: Dimensions.get('screen').height,
  48. duration: 300,
  49. delay: 0,
  50. useNativeDriver: true
  51. }).start(onCompleteCb)
  52. }
  53.  
  54. _onScroll(e) {
  55. if (e.nativeEvent.contentOffset.y > 5 && this.state.isTransparentBackground) {
  56. this.setState({
  57. isTransparentBackground: false
  58. })
  59. }
  60.  
  61. if (e.nativeEvent.contentOffset.y < 5 && !this.state.isTransparentBackground) {
  62. this.setState({
  63. isTransparentBackground: true
  64. })
  65. }
  66.  
  67. if (this.props.onScroll) {
  68. this.props.onScroll(e)
  69. }
  70. }
  71.  
  72. _onScrollBeginDrag(e) {
  73. this._startDragValue = e.nativeEvent.contentOffset.y
  74. }
  75.  
  76. _onScrollEndDrag(e) {
  77. if (Platform.OS === 'android' && this._startDragValue >= 5) return
  78.  
  79. if (e.nativeEvent.contentOffset.y <= this.props.onSwipeDownThreshold) {
  80. this.animateAway(() => {
  81. this.props.onSwipeAway()
  82. })
  83. }
  84. }
  85.  
  86. render() {
  87. return (
  88. <Animated.ScrollView
  89. scrollEventThrottle={16}
  90. onScroll={this._onScroll.bind(this)}
  91. onScrollBeginDrag={this._onScrollBeginDrag.bind(this)}
  92. onScrollEndDrag={this._onScrollEndDrag.bind(this)}
  93. contentContainerStyle={this.props.contentContainerStyle}
  94. alwaysBounceVertical={true}
  95. style={{
  96. top: 0,
  97. left: 0,
  98. right: 0,
  99. position: 'absolute',
  100. height: Dimensions.get('window').height,
  101. backgroundColor: this.state.isTransparentBackground ? 'transparent' : '#fff',
  102. transform: [
  103. {
  104. translateY: this.state.cardLocation
  105. }
  106. ]
  107. }}
  108. >
  109. <View
  110. style={{
  111. minHeight: Dimensions.get('window').height,
  112. shadowColor: this.state.isTransparentBackground ? '#333' : '#fff',
  113. shadowOffset: {
  114. height: 0
  115. },
  116. shadowOpacity: .3,
  117. shadowRadius: 10,
  118. backgroundColor: '#fff'
  119. }}
  120. >
  121. {this.props.children}
  122. </View>
  123. </Animated.ScrollView>
  124. )
  125. }
  126. }
Add Comment
Please, Sign In to add comment