$ git show star-rating
Star rating
Tap a star and every star up to it fills in, each one popping with a short stagger.
★★★★★
// Reanimated 4: the color is a CSS transition, and the pop keyframes run when a star turns on
const pop = {
'0%': { transform: [{ scale: 1 }] },
'35%': { transform: [{ scale: 1.35 }] },
'70%': { transform: [{ scale: 0.92 }] },
'100%': { transform: [{ scale: 1 }] },
};
function Star({ index, rating, onPress }) {
const filled = index < rating;
return (
<Pressable onPress={() => onPress(index + 1)} hitSlop={6}>
<Animated.Text style={[styles.star, {
color: filled ? '#f5b301' : '#d4d4d8',
transitionProperty: 'color',
transitionDuration: '150ms',
// Adding animationName starts the keyframes; removing it resets them
...(filled && {
animationName: pop,
animationDuration: '380ms',
animationDelay: index * 40 + 'ms',
animationTimingFunction: 'ease-out',
}),
}]}>★</Animated.Text>
</Pressable>
);
}
const [rating, setRating] = useState(0);
<View style={{ flexDirection: 'row', gap: 8 }}>
{[0, 1, 2, 3, 4].map((i) => <Star key={i} index={i} rating={rating} onPress={setRating} />)}
</View>