|
| 1 | +import Animated, { |
| 2 | + useAnimatedStyle, |
| 3 | + useSharedValue, |
| 4 | + withSpring, |
| 5 | +} from 'react-native-reanimated'; |
| 6 | +import { |
| 7 | + Gesture, |
| 8 | + GestureDetector, |
| 9 | + enableExperimentalWebImplementation, |
| 10 | +} from 'react-native-gesture-handler'; |
| 11 | +import { StyleSheet, Text } from 'react-native'; |
| 12 | +import { useEffect, useState } from 'react'; |
| 13 | + |
| 14 | +enableExperimentalWebImplementation(true); |
| 15 | + |
| 16 | +export function WithoutBabelTest() { |
| 17 | + const isPressed = useSharedValue(false); |
| 18 | + const offset = useSharedValue({ x: 0, y: 0 }); |
| 19 | + |
| 20 | + const [stateObject, rerender] = useState({}); |
| 21 | + |
| 22 | + const stateNumber = Math.random(); |
| 23 | + const stateBoolean = stateNumber >= 0.5; |
| 24 | + |
| 25 | + console.log('[without-babel][render]'); |
| 26 | + |
| 27 | + useEffect(function updateState() { |
| 28 | + const interval = setInterval(() => { |
| 29 | + rerender({}); |
| 30 | + }, 1000); |
| 31 | + return () => clearInterval(interval); |
| 32 | + }, []); |
| 33 | + |
| 34 | + const animatedStyle = useAnimatedStyle(() => { |
| 35 | + return { |
| 36 | + transform: [ |
| 37 | + { translateX: offset.value.x }, |
| 38 | + { translateY: offset.value.y }, |
| 39 | + { scale: withSpring(isPressed.value ? 1.2 : 1) }, |
| 40 | + ], |
| 41 | + backgroundColor: isPressed.value ? 'cyan' : 'hotpink', |
| 42 | + cursor: isPressed.value ? 'grabbing' : 'grab', |
| 43 | + }; |
| 44 | + }, [isPressed, offset, stateObject, stateBoolean, stateNumber]); |
| 45 | + |
| 46 | + const gesture = Gesture.Pan() |
| 47 | + .manualActivation(true) |
| 48 | + .onBegin(() => { |
| 49 | + 'worklet'; |
| 50 | + isPressed.value = true; |
| 51 | + }) |
| 52 | + .onChange((e) => { |
| 53 | + 'worklet'; |
| 54 | + offset.value = { |
| 55 | + x: e.changeX + offset.value.x, |
| 56 | + y: e.changeY + offset.value.y, |
| 57 | + }; |
| 58 | + }) |
| 59 | + .onFinalize(() => { |
| 60 | + 'worklet'; |
| 61 | + isPressed.value = false; |
| 62 | + }) |
| 63 | + .onTouchesMove((_, state) => { |
| 64 | + state.activate(); |
| 65 | + }); |
| 66 | + |
| 67 | + return ( |
| 68 | + <GestureDetector gesture={gesture}> |
| 69 | + <Animated.View style={[styles.ball, animatedStyle]}> |
| 70 | + <Text style={styles.text}>I work without Babel</Text> |
| 71 | + </Animated.View> |
| 72 | + </GestureDetector> |
| 73 | + ); |
| 74 | +} |
| 75 | + |
| 76 | +const styles = StyleSheet.create({ |
| 77 | + ball: { |
| 78 | + width: 100, |
| 79 | + height: 100, |
| 80 | + borderRadius: 100, |
| 81 | + backgroundColor: 'hotpink', |
| 82 | + alignSelf: 'center', |
| 83 | + padding: 8, |
| 84 | + justifyContent: 'center', |
| 85 | + alignItems: 'center', |
| 86 | + }, |
| 87 | + text: { |
| 88 | + textAlign: 'center', |
| 89 | + }, |
| 90 | +}); |
0 commit comments