|
| 1 | +import Animated from 'react-native-reanimated'; |
| 2 | +import { CardInterpolationProps, CardInterpolatedStyle } from '../types'; |
| 3 | + |
| 4 | +const { cond, multiply, interpolate } = Animated; |
| 5 | + |
| 6 | +/** |
| 7 | + * Standard iOS-style slide in from the right. |
| 8 | + */ |
| 9 | +export function forHorizontalIOS({ |
| 10 | + positions: { current, next }, |
| 11 | + layout, |
| 12 | +}: CardInterpolationProps): CardInterpolatedStyle { |
| 13 | + const translateFocused = interpolate(current, { |
| 14 | + inputRange: [0, 1], |
| 15 | + outputRange: [layout.width, 0], |
| 16 | + }); |
| 17 | + const translateUnfocused = next |
| 18 | + ? interpolate(next, { |
| 19 | + inputRange: [0, 1], |
| 20 | + outputRange: [0, multiply(layout.width, -0.3)], |
| 21 | + }) |
| 22 | + : 0; |
| 23 | + |
| 24 | + const opacity = interpolate(current, { |
| 25 | + inputRange: [0, 1], |
| 26 | + outputRange: [0, 0.07], |
| 27 | + }); |
| 28 | + |
| 29 | + const shadowOpacity = interpolate(current, { |
| 30 | + inputRange: [0, 1], |
| 31 | + outputRange: [0, 0.3], |
| 32 | + }); |
| 33 | + |
| 34 | + return { |
| 35 | + cardStyle: { |
| 36 | + backgroundColor: '#eee', |
| 37 | + transform: [ |
| 38 | + // Translation for the animation of the current card |
| 39 | + { translateX: translateFocused }, |
| 40 | + // Translation for the animation of the card on top of this |
| 41 | + { translateX: translateUnfocused }, |
| 42 | + ], |
| 43 | + shadowOpacity, |
| 44 | + }, |
| 45 | + overlayStyle: { opacity }, |
| 46 | + }; |
| 47 | +} |
| 48 | + |
| 49 | +/** |
| 50 | + * Standard iOS-style slide in from the bottom (used for modals). |
| 51 | + */ |
| 52 | +export function forVerticalIOS({ |
| 53 | + positions: { current }, |
| 54 | + layout, |
| 55 | +}: CardInterpolationProps): CardInterpolatedStyle { |
| 56 | + const translateY = interpolate(current, { |
| 57 | + inputRange: [0, 1], |
| 58 | + outputRange: [layout.height, 0], |
| 59 | + }); |
| 60 | + |
| 61 | + return { |
| 62 | + cardStyle: { |
| 63 | + backgroundColor: '#eee', |
| 64 | + transform: [ |
| 65 | + // Translation for the animation of the current card |
| 66 | + { translateY }, |
| 67 | + ], |
| 68 | + }, |
| 69 | + }; |
| 70 | +} |
| 71 | + |
| 72 | +/** |
| 73 | + * Standard Android-style fade in from the bottom for Android Oreo. |
| 74 | + */ |
| 75 | +export function forFadeFromBottomAndroid({ |
| 76 | + positions: { current }, |
| 77 | + layout, |
| 78 | + closing, |
| 79 | +}: CardInterpolationProps): CardInterpolatedStyle { |
| 80 | + const translateY = interpolate(current, { |
| 81 | + inputRange: [0, 1], |
| 82 | + outputRange: [multiply(layout.height, 0.08), 0], |
| 83 | + }); |
| 84 | + |
| 85 | + const opacity = cond( |
| 86 | + closing, |
| 87 | + current, |
| 88 | + interpolate(current, { |
| 89 | + inputRange: [0, 0.5, 0.9, 1], |
| 90 | + outputRange: [0, 0.25, 0.7, 1], |
| 91 | + }) |
| 92 | + ); |
| 93 | + |
| 94 | + return { |
| 95 | + cardStyle: { |
| 96 | + opacity, |
| 97 | + transform: [{ translateY }], |
| 98 | + }, |
| 99 | + }; |
| 100 | +} |
| 101 | + |
| 102 | +/** |
| 103 | + * Standard Android-style wipe from the bottom for Android Pie. |
| 104 | + */ |
| 105 | +export function forWipeFromBottomAndroid({ |
| 106 | + positions: { current, next }, |
| 107 | + layout, |
| 108 | +}: CardInterpolationProps): CardInterpolatedStyle { |
| 109 | + const containerTranslateY = interpolate(current, { |
| 110 | + inputRange: [0, 1], |
| 111 | + outputRange: [layout.height, 0], |
| 112 | + }); |
| 113 | + const cardTranslateYFocused = interpolate(current, { |
| 114 | + inputRange: [0, 1], |
| 115 | + outputRange: [multiply(layout.height, 95.9 / 100, -1), 0], |
| 116 | + }); |
| 117 | + const cardTranslateYUnfocused = next |
| 118 | + ? interpolate(next, { |
| 119 | + inputRange: [0, 1], |
| 120 | + outputRange: [0, multiply(layout.height, 2 / 100, -1)], |
| 121 | + }) |
| 122 | + : 0; |
| 123 | + const overlayOpacity = interpolate(current, { |
| 124 | + inputRange: [0, 0.36, 1], |
| 125 | + outputRange: [0, 0.1, 0.1], |
| 126 | + }); |
| 127 | + |
| 128 | + return { |
| 129 | + containerStyle: { |
| 130 | + transform: [{ translateY: containerTranslateY }], |
| 131 | + }, |
| 132 | + cardStyle: { |
| 133 | + transform: [ |
| 134 | + { translateY: cardTranslateYFocused }, |
| 135 | + { translateY: cardTranslateYUnfocused }, |
| 136 | + ], |
| 137 | + }, |
| 138 | + overlayStyle: { opacity: overlayOpacity }, |
| 139 | + }; |
| 140 | +} |
0 commit comments