Skip to main content

Flip Card

Flip card allows you to display different content depending on whether the card is flipped or not. It can be especially useful when you do not want to display some data immediately after entering the screen (e.g. secure data) and only after fulfilling a certain condition or performing an action.

Loading...

For storing information about whether the card is flipped or not we use shared value with the useSharedValue hook. Using shared values helps to prevent unnecessary re-renders.

  const isFlipped = useSharedValue(false);

This allows us to interpolate values between 0-180 and 180-360 degrees, depending on whether the card is flipped or not. In addition, we use withTiming util which makes our animation smooth.

    const spinValue = interpolate(Number(isFlipped.value), [0, 1], [0, 180]);
const rotateValue = withTiming(`${spinValue}deg`, { duration });

The FlipCard component accepts several props: duration allows you to change the duration of the animation, setting direction to the value x allows you to change the direction of our animation, RegularContent and FlippedContent give ability to display different content for flipped and non flipped variants.

Flip Card
const FlipCard = ({
isFlipped,
cardStyle,
direction = 'y',
duration = 500,
RegularContent,
FlippedContent,
}) => {
const isDirectionX = direction === 'x';

const regularCardAnimatedStyle = useAnimatedStyle(() => {
const spinValue = interpolate(Number(isFlipped.value), [0, 1], [0, 180]);
const rotateValue = withTiming(`${spinValue}deg`, { duration });

return {
transform: [
isDirectionX ? { rotateX: rotateValue } : { rotateY: rotateValue },
],
};
});

const flippedCardAnimatedStyle = useAnimatedStyle(() => {
const spinValue = interpolate(Number(isFlipped.value), [0, 1], [180, 360]);
const rotateValue = withTiming(`${spinValue}deg`, { duration });

return {
transform: [
isDirectionX ? { rotateX: rotateValue } : { rotateY: rotateValue },
],
};
});

return (
<View>
<Animated.View
style={[
flipCardStyles.regularCard,
cardStyle,
regularCardAnimatedStyle,
]}>
{RegularContent}
</Animated.View>
<Animated.View
style={[
flipCardStyles.flippedCard,
cardStyle,
flippedCardAnimatedStyle,
]}>
{FlippedContent}
</Animated.View>
</View>
);
};