|
| 1 | +import { StyleSheet } from 'react-native'; |
| 2 | +import type { SharedValue } from 'react-native-reanimated'; |
| 3 | +import Animated, { |
| 4 | + Easing, |
| 5 | + FadeIn, |
| 6 | + FadeOut, |
| 7 | + FlipInXDown, |
| 8 | + FlipOutXDown, |
| 9 | + useAnimatedStyle, |
| 10 | + withTiming, |
| 11 | +} from 'react-native-reanimated'; |
| 12 | + |
| 13 | +export type StatusType = 'inProgress' | 'correct' | 'wrong'; |
| 14 | + |
| 15 | +export type AnimatedCodeNumberProps = { |
| 16 | + code?: string; |
| 17 | + highlighted: boolean; |
| 18 | + status: SharedValue<StatusType>; |
| 19 | +}; |
| 20 | + |
| 21 | +export const AnimatedCodeNumber: React.FC<AnimatedCodeNumberProps> = ({ |
| 22 | + code, |
| 23 | + highlighted, |
| 24 | + status, |
| 25 | +}) => { |
| 26 | + const correctColor = 'hsl(151, 40.2%, 54.1%)'; // green-600 |
| 27 | + const defaultColor = 'hsl(0, 0%, 89.5%)'; // gray-300 |
| 28 | + |
| 29 | + const rBoxStyle = useAnimatedStyle(() => { |
| 30 | + return { |
| 31 | + // Only show green border for correct status, default border for all other states |
| 32 | + borderColor: withTiming(status.value === 'correct' ? correctColor : defaultColor), |
| 33 | + }; |
| 34 | + }, [correctColor, defaultColor]); |
| 35 | + |
| 36 | + return ( |
| 37 | + <Animated.View style={[styles.container, rBoxStyle]}> |
| 38 | + {code != null && ( |
| 39 | + <Animated.View entering={FadeIn.duration(250)} exiting={FadeOut.duration(250)}> |
| 40 | + <Animated.Text |
| 41 | + entering={FlipInXDown.duration(500) |
| 42 | + // Go to this website and you'll see the curve I used: |
| 43 | + // https://cubic-bezier.com/#0,0.75,0.5,0.9 |
| 44 | + // Basically, I want the animation to start slow, then accelerate at the end |
| 45 | + // Do we really need to use a curve? Every detail matters :) |
| 46 | + .easing(Easing.bezier(0, 0.75, 0.5, 0.9).factory()) |
| 47 | + .build()} |
| 48 | + exiting={FlipOutXDown.duration(500) |
| 49 | + // https://cubic-bezier.com/#0.6,0.1,0.4,0.8 |
| 50 | + // I want the animation to start fast, then decelerate at the end (opposite of the previous one) |
| 51 | + .easing(Easing.bezier(0.6, 0.1, 0.4, 0.8).factory()) |
| 52 | + .build()} |
| 53 | + style={[styles.text, { color: 'hsl(0, 0%, 39.3%)' }]}> |
| 54 | + {code} |
| 55 | + </Animated.Text> |
| 56 | + </Animated.View> |
| 57 | + )} |
| 58 | + </Animated.View> |
| 59 | + ); |
| 60 | +}; |
| 61 | + |
| 62 | +const styles = StyleSheet.create({ |
| 63 | + container: { |
| 64 | + height: '90%', |
| 65 | + width: '80%', |
| 66 | + borderWidth: 2, |
| 67 | + borderRadius: 12, |
| 68 | + borderCurve: 'continuous', |
| 69 | + justifyContent: 'center', |
| 70 | + alignItems: 'center', |
| 71 | + }, |
| 72 | + text: { |
| 73 | + fontSize: 40, |
| 74 | + fontFamily: 'Inter-500-24', |
| 75 | + }, |
| 76 | +}); |
0 commit comments