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