|
1 | 1 | import * as React from "react"; |
2 | | -import { Animated, Pressable, PressableProps, View } from "react-native"; |
3 | | -const AnimatedView: any = Animated.createAnimatedComponent(View); |
| 2 | +import { |
| 3 | + Animated, |
| 4 | + Pressable, |
| 5 | + PressableProps, |
| 6 | + PressableStateCallbackType, |
| 7 | + StyleProp, |
| 8 | + ViewStyle, |
| 9 | +} from "react-native"; |
| 10 | + |
| 11 | +const AnimatedPressable = Animated.createAnimatedComponent( |
| 12 | + Pressable as unknown as React.ComponentClass<PressableProps>, |
| 13 | +) as React.ComponentClass<PressableProps>; |
4 | 14 |
|
5 | 15 | export interface IRNBounceableProps extends PressableProps { |
6 | 16 | bounceEffectIn?: number; |
@@ -70,20 +80,36 @@ const RNBounceableInner = ( |
70 | 80 | [disabled, bounceAnimation, bounceEffectOut, bounceVelocityOut, bouncinessOut, onPressOut], |
71 | 81 | ); |
72 | 82 |
|
| 83 | + const animatedTransformStyle = React.useMemo<StyleProp<ViewStyle>>( |
| 84 | + () => ({ |
| 85 | + transform: [{ scale: bounceValue }], |
| 86 | + }), |
| 87 | + [bounceValue], |
| 88 | + ); |
| 89 | + |
| 90 | + const composedStyle = React.useMemo<PressableProps["style"]>(() => { |
| 91 | + if (typeof style === "function") { |
| 92 | + return (pressState: PressableStateCallbackType) => [ |
| 93 | + animatedTransformStyle, |
| 94 | + style(pressState), |
| 95 | + ]; |
| 96 | + } |
| 97 | + |
| 98 | + return [animatedTransformStyle, style]; |
| 99 | + }, [style, animatedTransformStyle]); |
| 100 | + |
73 | 101 | return ( |
74 | | - <AnimatedView style={{ transform: [{ scale: bounceValue }] }}> |
75 | | - <Pressable |
76 | | - ref={ref as any} |
77 | | - {...rest} |
78 | | - style={style} |
79 | | - onPressIn={handlePressIn} |
80 | | - onPressOut={handlePressOut} |
81 | | - onPress={onPress} |
82 | | - disabled={disabled} |
83 | | - > |
84 | | - {children} |
85 | | - </Pressable> |
86 | | - </AnimatedView> |
| 102 | + <AnimatedPressable |
| 103 | + ref={ref as any} |
| 104 | + {...rest} |
| 105 | + style={composedStyle} |
| 106 | + onPressIn={handlePressIn} |
| 107 | + onPressOut={handlePressOut} |
| 108 | + onPress={onPress} |
| 109 | + disabled={disabled} |
| 110 | + > |
| 111 | + {children} |
| 112 | + </AnimatedPressable> |
87 | 113 | ); |
88 | 114 | }; |
89 | 115 |
|
|
0 commit comments