|
| 1 | +import React, { Component } from 'react'; |
| 2 | +import { Animated, Dimensions, Easing, PanResponder, StyleSheet, TouchableWithoutFeedback, View } from 'react-native'; |
| 3 | +import { ComponentEvent, Navigation } from 'react-native-navigation'; |
| 4 | +import { Colors } from '../../styles/colors'; |
| 5 | + |
| 6 | +const deviceHeight = Dimensions.get('window').height; |
| 7 | +const ANIM_DURATION = 200; |
| 8 | + |
| 9 | +interface ISlideBottomOverlayProps extends ComponentEvent { |
| 10 | + renderOverlayComponent: (dismissOverlay: () => void) => React.ReactElement<{}>; |
| 11 | +} |
| 12 | + |
| 13 | +interface IState { |
| 14 | + overlayHeight: number; |
| 15 | + overlaySlide: Animated.Value; |
| 16 | + backgroundOpacity: Animated.Value; |
| 17 | +} |
| 18 | + |
| 19 | +export default class SlideBottomOverlay extends Component<ISlideBottomOverlayProps, IState> { |
| 20 | + constructor(props: ISlideBottomOverlayProps) { |
| 21 | + super(props); |
| 22 | + |
| 23 | + this.state = { |
| 24 | + overlayHeight: 0, |
| 25 | + overlaySlide: new Animated.Value(deviceHeight), |
| 26 | + backgroundOpacity: new Animated.Value(0) |
| 27 | + }; |
| 28 | + } |
| 29 | + |
| 30 | + private dismissOverlay = () => { |
| 31 | + Animated.parallel([ |
| 32 | + Animated.timing(this.state.overlaySlide, { |
| 33 | + toValue: this.state.overlayHeight, |
| 34 | + duration: ANIM_DURATION, |
| 35 | + easing: Easing.ease, |
| 36 | + useNativeDriver: true |
| 37 | + }), |
| 38 | + Animated.timing(this.state.backgroundOpacity, { |
| 39 | + toValue: 0, |
| 40 | + duration: ANIM_DURATION, |
| 41 | + useNativeDriver: false |
| 42 | + }) |
| 43 | + ]).start(() => { |
| 44 | + Navigation.dismissOverlay(this.props.componentId); |
| 45 | + }); |
| 46 | + }; |
| 47 | + |
| 48 | + render() { |
| 49 | + return ( |
| 50 | + <TouchableWithoutFeedback style={{ flex: 1 }} onPress={this.dismissOverlay}> |
| 51 | + <Animated.View |
| 52 | + style={[ |
| 53 | + styles.backgroundContainer, |
| 54 | + { |
| 55 | + backgroundColor: this.state.backgroundOpacity.interpolate({ |
| 56 | + inputRange: [0, 1], |
| 57 | + outputRange: ['transparent', Colors.OVERLAY_OPACITY] |
| 58 | + }) |
| 59 | + } |
| 60 | + ]} |
| 61 | + > |
| 62 | + <Animated.View |
| 63 | + style={{ |
| 64 | + transform: [{ translateY: this.state.overlaySlide }] |
| 65 | + }} |
| 66 | + {...PanResponder.create({ |
| 67 | + onStartShouldSetPanResponder: () => true |
| 68 | + }).panHandlers} |
| 69 | + > |
| 70 | + <TouchableWithoutFeedback |
| 71 | + onLayout={({ nativeEvent }) => { |
| 72 | + if (this.state.overlayHeight === 0) { |
| 73 | + this.setState({ overlayHeight: nativeEvent.layout.height }); |
| 74 | + this.state.overlaySlide.setValue(nativeEvent.layout.height); |
| 75 | + |
| 76 | + Animated.parallel([ |
| 77 | + Animated.timing(this.state.overlaySlide, { |
| 78 | + toValue: 0, |
| 79 | + duration: ANIM_DURATION, |
| 80 | + easing: Easing.ease, |
| 81 | + useNativeDriver: true |
| 82 | + }), |
| 83 | + Animated.timing(this.state.backgroundOpacity, { |
| 84 | + toValue: 1, |
| 85 | + duration: ANIM_DURATION, |
| 86 | + useNativeDriver: false |
| 87 | + }) |
| 88 | + ]).start(); |
| 89 | + } |
| 90 | + }} |
| 91 | + > |
| 92 | + <View style={styles.overlayContainer}>{this.props.renderOverlayComponent(this.dismissOverlay)}</View> |
| 93 | + </TouchableWithoutFeedback> |
| 94 | + </Animated.View> |
| 95 | + </Animated.View> |
| 96 | + </TouchableWithoutFeedback> |
| 97 | + ); |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +const styles = StyleSheet.create({ |
| 102 | + backgroundContainer: { |
| 103 | + flex: 1, |
| 104 | + justifyContent: 'flex-end', |
| 105 | + alignItems: 'stretch', |
| 106 | + backgroundColor: 'transparent' |
| 107 | + }, |
| 108 | + overlayContainer: { |
| 109 | + overflow: 'hidden', |
| 110 | + maxHeight: deviceHeight * 0.8, |
| 111 | + borderTopRightRadius: 8, |
| 112 | + borderTopLeftRadius: 8 |
| 113 | + } |
| 114 | +}); |
0 commit comments