|
| 1 | +import React, { Children } from 'react'; |
| 2 | +import { StyleSheet, Dimensions, TouchableWithoutFeedback, View } from 'react-native'; |
| 3 | +import Animated, { Easing } from 'react-native-reanimated'; |
| 4 | + |
| 5 | +import { TransitionComponent } from './Transition'; |
| 6 | +import { Navigation } from '../Navigation'; |
| 7 | + |
| 8 | +const { width: SCREEN_WIDTH } = Dimensions.get('window'); |
| 9 | + |
| 10 | +const { |
| 11 | + block, |
| 12 | + call, |
| 13 | + Clock, |
| 14 | + clockRunning, |
| 15 | + cond, |
| 16 | + multiply, |
| 17 | + set, |
| 18 | + startClock, |
| 19 | + stopClock, |
| 20 | + sub, |
| 21 | + timing, |
| 22 | + Value, |
| 23 | +} = Animated; |
| 24 | + |
| 25 | +function runTiming( |
| 26 | + clock: Animated.Clock, |
| 27 | + value: Animated.Value<number>, |
| 28 | + dest: Animated.Value<number>, |
| 29 | + updateVisibility: () => any |
| 30 | +) { |
| 31 | + const state = { |
| 32 | + finished: new Value(0), |
| 33 | + frameTime: new Value(0), |
| 34 | + position: value, |
| 35 | + time: new Value(0), |
| 36 | + }; |
| 37 | + |
| 38 | + const config = { |
| 39 | + duration: 300, |
| 40 | + easing: Easing.inOut(Easing.ease), |
| 41 | + toValue: dest, |
| 42 | + }; |
| 43 | + |
| 44 | + return block([ |
| 45 | + cond( |
| 46 | + clockRunning(clock), |
| 47 | + [ |
| 48 | + // if the clock is already running we update the toValue, in case a new dest has been passed in |
| 49 | + set(config.toValue, dest), |
| 50 | + ], |
| 51 | + [ |
| 52 | + // If the clock isn't running we reset all the animation params and start the clock |
| 53 | + set(state.finished, 0), |
| 54 | + set(state.time, 0), |
| 55 | + set(state.position, value), |
| 56 | + set(state.frameTime, 0), |
| 57 | + set(config.toValue, dest), |
| 58 | + startClock(clock), |
| 59 | + ] |
| 60 | + ), |
| 61 | + // we run the step here that is going to update position |
| 62 | + timing(clock, state, config), |
| 63 | + // if the animation is over we stop the clock |
| 64 | + cond(state.finished, stopClock(clock)), |
| 65 | + cond(state.finished, [call([state.finished], updateVisibility), stopClock(clock)]), |
| 66 | + // we made the block return the updated position |
| 67 | + state.position, |
| 68 | + ]); |
| 69 | +} |
| 70 | + |
| 71 | +export class BurgerMenuLeft extends TransitionComponent { |
| 72 | + state = { |
| 73 | + hidden: !this.props.directionForward, |
| 74 | + }; |
| 75 | + |
| 76 | + clock = new Clock(); |
| 77 | + progress: Animated.Value<number> = new Value(this.props.directionForward ? 0 : 1); |
| 78 | + animation: Animated.Value<number> = new Value(this.props.directionForward ? 0 : 1); |
| 79 | + // istanbul ignore next |
| 80 | + trans = runTiming(this.clock, this.progress, this.animation, () => { |
| 81 | + // react-native-reanimated is not correctly mocked |
| 82 | + // istanbul ignore next |
| 83 | + // @ts-ignore |
| 84 | + if (process.env.NODE_ENV !== 'test' && this.state.hidden !== !this.props.directionForward) { |
| 85 | + this.setState({ hidden: !this.props.directionForward }); |
| 86 | + } |
| 87 | + }); |
| 88 | + |
| 89 | + componentDidUpdate() { |
| 90 | + this.animation.setValue(this.props.directionForward ? 0 : 1); |
| 91 | + if (this.state.hidden && this.props.directionForward) { |
| 92 | + this.setState({ hidden: false }); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + render() { |
| 97 | + return ( |
| 98 | + <View style={[StyleSheet.absoluteFill]} pointerEvents="box-none"> |
| 99 | + <Animated.View |
| 100 | + style={[StyleSheet.absoluteFill, { opacity: sub(new Value(1), this.trans) }]} |
| 101 | + pointerEvents={this.state.hidden ? 'none' : 'auto'} |
| 102 | + > |
| 103 | + <TouchableWithoutFeedback |
| 104 | + onPress={Navigation.instance.backHandlerDelegate.back} |
| 105 | + style={StyleSheet.absoluteFill} |
| 106 | + > |
| 107 | + <View style={[StyleSheet.absoluteFill, { backgroundColor: '#000000aa' }]}></View> |
| 108 | + </TouchableWithoutFeedback> |
| 109 | + </Animated.View> |
| 110 | + <View style={StyleSheet.absoluteFill} pointerEvents="box-none"> |
| 111 | + <Animated.View |
| 112 | + pointerEvents="box-none" |
| 113 | + style={{ |
| 114 | + flex: 1, |
| 115 | + width: (SCREEN_WIDTH / 3) * 2, |
| 116 | + transform: [{ translateX: multiply(this.trans, -(SCREEN_WIDTH / 3) * 2) }], |
| 117 | + }} |
| 118 | + > |
| 119 | + {!this.state.hidden && Children.only(this.props.children)} |
| 120 | + </Animated.View> |
| 121 | + </View> |
| 122 | + </View> |
| 123 | + ); |
| 124 | + } |
| 125 | +} |
0 commit comments