Skip to content

Instantly share code, notes, and snippets.

@hirbod
Forked from vcapretz/instagram-like-button.jsx
Created October 20, 2022 17:26
Show Gist options
  • Save hirbod/5eac799d96b8cfd1be8a64f959a11928 to your computer and use it in GitHub Desktop.
Save hirbod/5eac799d96b8cfd1be8a64f959a11928 to your computer and use it in GitHub Desktop.
A simple Like button with a nice animation using react-native-reanimated v2, support for my post in https://vcapretz.com/2021/instagram-button-react-native
import React from "react";
import Animated, {
useSharedValue,
withSpring,
useAnimatedStyle,
Extrapolate,
interpolate,
} from "react-native-reanimated";
import { Pressable, View, Button, StyleSheet } from "react-native";
import { MaterialCommunityIcons } from "@expo/vector-icons";
const LikeButton = () => {
const liked = useSharedValue(0);
const outlineStyle = useAnimatedStyle(() => {
return {
transform: [
{
scale: interpolate(liked.value, [0, 1], [1, 0], Extrapolate.CLAMP),
},
],
};
});
const fillStyle = useAnimatedStyle(() => {
return {
transform: [
{
scale: liked.value,
},
],
opacity: liked.value,
};
});
return (
<Pressable onPress={() => (liked.value = withSpring(liked.value ? 0 : 1))}>
<Animated.View style={[StyleSheet.absoluteFillObject, outlineStyle]}>
<MaterialCommunityIcons
name={"heart-outline"}
size={32}
color={"black"}
/>
</Animated.View>
<Animated.View style={fillStyle}>
<MaterialCommunityIcons name={"heart"} size={32} color={"red"} />
</Animated.View>
</Pressable>
);
};
export default function AnimatedStyleUpdateExample(props) {
return (
<View
style={{
flex: 1,
alignItems: "center",
justifyContent: "center",
flexDirection: "column",
}}
>
<LikeButton />
</View>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment