|
| 1 | +import { balloonsImage } from '@/apps/css/assets'; |
| 2 | +import React, { useCallback } from 'react'; |
| 3 | +import { Button, Platform, StyleSheet, Text, View } from 'react-native'; |
| 4 | +import Animated, { |
| 5 | + DynamicColorIOS, |
| 6 | + PlatformColor, |
| 7 | + useAnimatedStyle, |
| 8 | + useSharedValue, |
| 9 | + withSpring, |
| 10 | +} from 'react-native-reanimated'; |
| 11 | + |
| 12 | +const instructions = [ |
| 13 | + '1. Press "Toggle shared value" button', |
| 14 | + '2. Wait until the animated styles are synced back to React (about 3 seconds)', |
| 15 | + '3. Press "Increase counter" button', |
| 16 | + '4. The width and colors should not change, similar to when the feature flag is disabled', |
| 17 | +].join('\n'); |
| 18 | + |
| 19 | +interface CustomButtonProps { |
| 20 | + title: string; |
| 21 | + onPress: () => void; |
| 22 | +} |
| 23 | + |
| 24 | +// We use a custom button component on native platforms |
| 25 | +// because the one from React Native triggers additional renders when pressed. |
| 26 | +function CustomButton({ title, onPress }: CustomButtonProps) { |
| 27 | + if (Platform.OS === 'web') { |
| 28 | + return <Button title={title} onPress={onPress} />; |
| 29 | + } |
| 30 | + |
| 31 | + return ( |
| 32 | + <View onTouchEnd={onPress} style={styles.buttonView}> |
| 33 | + <Text style={styles.buttonText}>{title}</Text> |
| 34 | + </View> |
| 35 | + ); |
| 36 | +} |
| 37 | + |
| 38 | +export default function SyncBackToReactExample() { |
| 39 | + const [count, setCount] = React.useState(0); |
| 40 | + |
| 41 | + const ref = React.useRef(false); |
| 42 | + |
| 43 | + const sv = useSharedValue(false); |
| 44 | + |
| 45 | + const animatedStyle1 = useAnimatedStyle(() => { |
| 46 | + return { |
| 47 | + width: withSpring(sv.value ? 150 : 50), |
| 48 | + }; |
| 49 | + }); |
| 50 | + |
| 51 | + const animatedStyle2 = useAnimatedStyle(() => { |
| 52 | + return { |
| 53 | + backgroundColor: sv.value ? 'blue' : 'green', |
| 54 | + }; |
| 55 | + }); |
| 56 | + |
| 57 | + const animatedStyle3 = useAnimatedStyle(() => { |
| 58 | + return { |
| 59 | + backgroundColor: sv.value ? 'transparent' : 'green', |
| 60 | + }; |
| 61 | + }); |
| 62 | + |
| 63 | + const animatedStyle4 = useAnimatedStyle(() => { |
| 64 | + return { |
| 65 | + backgroundColor: |
| 66 | + Platform.OS === 'android' |
| 67 | + ? PlatformColor( |
| 68 | + sv.value |
| 69 | + ? '@android:color/holo_blue_bright' |
| 70 | + : '@android:color/holo_green_light' |
| 71 | + ) |
| 72 | + : Platform.OS === 'ios' |
| 73 | + ? PlatformColor(sv.value ? 'systemBlue' : 'systemGreen') |
| 74 | + : 'gray', |
| 75 | + }; |
| 76 | + }); |
| 77 | + |
| 78 | + const animatedStyle5 = useAnimatedStyle(() => { |
| 79 | + return { |
| 80 | + backgroundColor: |
| 81 | + Platform.OS === 'ios' |
| 82 | + ? sv.value |
| 83 | + ? DynamicColorIOS({ light: 'blue', dark: 'orange' }) |
| 84 | + : DynamicColorIOS({ light: 'green', dark: 'red' }) |
| 85 | + : 'gray', |
| 86 | + }; |
| 87 | + }); |
| 88 | + |
| 89 | + const animatedStyle6 = useAnimatedStyle(() => { |
| 90 | + return { |
| 91 | + boxShadow: [ |
| 92 | + { |
| 93 | + blurRadius: 10, |
| 94 | + offsetX: 10, |
| 95 | + offsetY: 10, |
| 96 | + color: sv.value ? 'blue' : 'green', |
| 97 | + }, |
| 98 | + ], |
| 99 | + }; |
| 100 | + }); |
| 101 | + |
| 102 | + const animatedStyle7 = useAnimatedStyle(() => { |
| 103 | + return { |
| 104 | + transform: `rotate(${sv.value ? 30 : 0}deg)`, |
| 105 | + }; |
| 106 | + }); |
| 107 | + |
| 108 | + const animatedStyle8 = useAnimatedStyle(() => { |
| 109 | + return { |
| 110 | + filter: `brightness(${sv.value ? 0.5 : 1})`, |
| 111 | + }; |
| 112 | + }); |
| 113 | + |
| 114 | + const handleToggle = useCallback(() => { |
| 115 | + sv.value = ref.current = !ref.current; |
| 116 | + }, [sv]); |
| 117 | + |
| 118 | + const handleIncreaseCounter = useCallback(() => { |
| 119 | + setCount((c) => c + 1); |
| 120 | + }, []); |
| 121 | + |
| 122 | + return ( |
| 123 | + <View style={styles.container}> |
| 124 | + <Animated.View style={[styles.box, animatedStyle1]} /> |
| 125 | + <Animated.View style={[styles.box, animatedStyle2]} /> |
| 126 | + <Animated.View style={[styles.box, animatedStyle3]} /> |
| 127 | + <Animated.View style={[styles.box, animatedStyle4]} /> |
| 128 | + <Animated.View style={[styles.box, animatedStyle5]} /> |
| 129 | + <Animated.View style={[styles.box, animatedStyle6]} /> |
| 130 | + <Animated.View style={[styles.box, animatedStyle7]} /> |
| 131 | + <Animated.Image |
| 132 | + source={balloonsImage} |
| 133 | + // @ts-ignore |
| 134 | + style={[styles.box, animatedStyle8]} |
| 135 | + /> |
| 136 | + <CustomButton title="Toggle shared value" onPress={handleToggle} /> |
| 137 | + <Text>Counter: {count}</Text> |
| 138 | + <CustomButton title="Increase counter" onPress={handleIncreaseCounter} /> |
| 139 | + <Text style={styles.instructions}>{instructions}</Text> |
| 140 | + </View> |
| 141 | + ); |
| 142 | +} |
| 143 | + |
| 144 | +const styles = StyleSheet.create({ |
| 145 | + container: { |
| 146 | + flex: 1, |
| 147 | + alignItems: 'center', |
| 148 | + justifyContent: 'center', |
| 149 | + }, |
| 150 | + box: { |
| 151 | + width: 50, |
| 152 | + height: 50, |
| 153 | + backgroundColor: 'black', |
| 154 | + }, |
| 155 | + buttonView: { |
| 156 | + margin: 20, |
| 157 | + }, |
| 158 | + buttonText: { |
| 159 | + fontSize: 20, |
| 160 | + color: 'dodgerblue', |
| 161 | + }, |
| 162 | + instructions: { |
| 163 | + marginHorizontal: 20, |
| 164 | + }, |
| 165 | +}); |
0 commit comments