Skip to content

Instantly share code, notes, and snippets.

@Karthik-B-06
Created August 4, 2025 17:18
Show Gist options
  • Save Karthik-B-06/8d90cba0275b414a454431832773af3c to your computer and use it in GitHub Desktop.
Save Karthik-B-06/8d90cba0275b414a454431832773af3c to your computer and use it in GitHub Desktop.
import { useCallback, useMemo } from 'react';
import {
useAnimatedStyle,
useSharedValue,
withSpring,
WithSpringConfig,
withTiming,
WithTimingConfig,
} from 'react-native-reanimated';
type AnimationValue = {
/**
* The scale value to which the component should resize onPressIn
* @default 0.95
*/
value: number;
};
type SpringAnimation = AnimationValue & {
/**
* The animation type
* @default "spring"
*/
type: 'spring';
config: WithSpringConfig;
};
type TimingAnimation = AnimationValue & {
/**
* The animation type
* @default "spring"
*/
type: 'timing';
config: WithTimingConfig;
};
type AnimationTypes = SpringAnimation | TimingAnimation;
const DefaultSpringConfig: WithSpringConfig = {
mass: 1,
damping: 17,
stiffness: 250,
overshootClamping: false,
restSpeedThreshold: 0.001,
restDisplacementThreshold: 0.001,
};
const DefaultAnimationType: AnimationTypes = {
type: 'spring',
config: DefaultSpringConfig,
value: 0.96,
};
export const useScaleAnimation = (scaleAnimationConfig: AnimationTypes = DefaultAnimationType) => {
const scale = useSharedValue(1);
const { type, config, value } = scaleAnimationConfig;
const getAnimation = useCallback((animationValue: number) => {
if (type === 'spring') {
return withSpring(animationValue, config);
} else {
return withTiming(animationValue, config);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
const onPressIn = useCallback(() => {
scale.value = getAnimation(value);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
const onPressOut = useCallback(() => {
scale.value = getAnimation(1);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
const animatedStyle = useAnimatedStyle(() => ({
transform: [{ scale: scale.value }],
}));
const scaleAnimationHandler = useMemo(() => {
return {
handlers: { onPressIn, onPressOut },
animatedStyle,
};
}, [animatedStyle, onPressIn, onPressOut]);
return scaleAnimationHandler;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment