r/reactnative • • 2d ago

Could you recreate this CSS animation in React Native?

I've never used react native.

0 Upvotes

2 comments sorted by

2

u/Intrepid-Anywhere255 2d ago

If you are using Reanimated, this maps to one shared progress value and an animated style for translateX, translateY, and rotateZ through 0, 5, 0, -5, then back to 0. Run it with withSequence and withTiming on the UI thread, triggered by the button press, so React does not rerender on every frame.

1

u/davidbalbino 1d ago

yes, pretty much what the other comment said. something like this should get you close:

const r = useSharedValue(0);
const style = useAnimatedStyle(() => ({
  transform: [{ rotateZ: `${r.value}deg` }],
}));

const wiggle = () => {
  r.value = withSequence(
    withTiming(5, { duration: 80 }),
    withTiming(-5, { duration: 80 }),
    withTiming(0, { duration: 80 })
  );
};

then put style on an Animated.View and call wiggle() on press. if the CSS one also moves on X/Y just add translateX/translateY to the same transform. tweak the durations until it looks the same, depends of the easing they used