From 1225e8448524633466379d5ac00a78b53e1a9a51 Mon Sep 17 00:00:00 2001 From: Hailey Date: Mon, 2 Sep 2024 01:37:24 -0700 Subject: Improve animations for like button (#5074) --- src/lib/custom-animations/CountWheel.tsx | 177 +++++++++++++++++++++++++++++++ 1 file changed, 177 insertions(+) create mode 100644 src/lib/custom-animations/CountWheel.tsx (limited to 'src/lib/custom-animations/CountWheel.tsx') diff --git a/src/lib/custom-animations/CountWheel.tsx b/src/lib/custom-animations/CountWheel.tsx new file mode 100644 index 000000000..dfa697911 --- /dev/null +++ b/src/lib/custom-animations/CountWheel.tsx @@ -0,0 +1,177 @@ +import React from 'react' +import {View} from 'react-native' +import Animated, { + Easing, + LayoutAnimationConfig, + useReducedMotion, + withTiming, +} from 'react-native-reanimated' +import {i18n} from '@lingui/core' + +import {decideShouldRoll} from 'lib/custom-animations/util' +import {s} from 'lib/styles' +import {formatCount} from 'view/com/util/numeric/format' +import {Text} from 'view/com/util/text/Text' +import {atoms as a, useTheme} from '#/alf' + +const animationConfig = { + duration: 400, + easing: Easing.out(Easing.cubic), +} + +function EnteringUp() { + 'worklet' + const animations = { + opacity: withTiming(1, animationConfig), + transform: [{translateY: withTiming(0, animationConfig)}], + } + const initialValues = { + opacity: 0, + transform: [{translateY: 18}], + } + return { + animations, + initialValues, + } +} + +function EnteringDown() { + 'worklet' + const animations = { + opacity: withTiming(1, animationConfig), + transform: [{translateY: withTiming(0, animationConfig)}], + } + const initialValues = { + opacity: 0, + transform: [{translateY: -18}], + } + return { + animations, + initialValues, + } +} + +function ExitingUp() { + 'worklet' + const animations = { + opacity: withTiming(0, animationConfig), + transform: [ + { + translateY: withTiming(-18, animationConfig), + }, + ], + } + const initialValues = { + opacity: 1, + transform: [{translateY: 0}], + } + return { + animations, + initialValues, + } +} + +function ExitingDown() { + 'worklet' + const animations = { + opacity: withTiming(0, animationConfig), + transform: [{translateY: withTiming(18, animationConfig)}], + } + const initialValues = { + opacity: 1, + transform: [{translateY: 0}], + } + return { + animations, + initialValues, + } +} + +export function CountWheel({ + likeCount, + big, + isLiked, +}: { + likeCount: number + big?: boolean + isLiked: boolean +}) { + const t = useTheme() + const shouldAnimate = !useReducedMotion() + const shouldRoll = decideShouldRoll(isLiked, likeCount) + + // Incrementing the key will cause the `Animated.View` to re-render, with the newly selected entering/exiting + // animation + // The initial entering/exiting animations will get skipped, since these will happen on screen mounts and would + // be unnecessary + const [key, setKey] = React.useState(0) + const [prevCount, setPrevCount] = React.useState(likeCount) + const prevIsLiked = React.useRef(isLiked) + const formattedCount = formatCount(i18n, likeCount) + const formattedPrevCount = formatCount(i18n, prevCount) + + React.useEffect(() => { + if (isLiked === prevIsLiked.current) { + return + } + + const newPrevCount = isLiked ? likeCount - 1 : likeCount + 1 + setKey(prev => prev + 1) + setPrevCount(newPrevCount) + prevIsLiked.current = isLiked + }, [isLiked, likeCount]) + + const enteringAnimation = + shouldAnimate && shouldRoll + ? isLiked + ? EnteringUp + : EnteringDown + : undefined + const exitingAnimation = + shouldAnimate && shouldRoll + ? isLiked + ? ExitingUp + : ExitingDown + : undefined + + return ( + + {likeCount > 0 ? ( + + + + {formattedCount} + + + {shouldAnimate ? ( + + + {formattedPrevCount} + + + ) : null} + + ) : null} + + ) +} -- cgit 1.4.1