Something went wrong. Try again.
Mirror — see github.com/blacksky-algorithms/blacksky.community
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263import { Pressable, type PressableProps, type StyleProp, type ViewStyle,} from 'react-native'import Animated, { cancelAnimation, useAnimatedStyle, useReducedMotion, useSharedValue, withTiming,} from 'react-native-reanimated'
import {IS_NATIVE, IS_WEB_TOUCH_DEVICE} from '#/env'
const DEFAULT_TARGET_SCALE = IS_NATIVE || IS_WEB_TOUCH_DEVICE ? 0.98 : 1
const AnimatedPressable = Animated.createAnimatedComponent(Pressable)
export function PressableScale({ targetScale = DEFAULT_TARGET_SCALE, children, style, onPressIn, onPressOut, ...rest}: { targetScale?: number style?: StyleProp<ViewStyle>} & Exclude<PressableProps, 'onPressIn' | 'onPressOut' | 'style'>) { const reducedMotion = useReducedMotion()
const scale = useSharedValue(1)
const animatedStyle = useAnimatedStyle(() => ({ transform: [{scale: scale.get()}], }))
return ( <AnimatedPressable accessibilityRole="button" onPressIn={e => { if (onPressIn) { onPressIn(e) } cancelAnimation(scale) scale.set(() => withTiming(targetScale, {duration: 100})) }} onPressOut={e => { if (onPressOut) { onPressOut(e) } cancelAnimation(scale) scale.set(() => withTiming(1, {duration: 100})) }} style={[!reducedMotion && animatedStyle, style]} {...rest}> {children} </AnimatedPressable> )}