Skip to content

Commit 3a78701

Browse files
committed
refactor: better bounceable library code and android crash fix
1 parent 75d7832 commit 3a78701

4 files changed

Lines changed: 97 additions & 31 deletions

File tree

example/lib/RNBounceable.tsx

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
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>;
414

515
export interface IRNBounceableProps extends PressableProps {
616
bounceEffectIn?: number;
@@ -70,20 +80,36 @@ const RNBounceableInner = (
7080
[disabled, bounceAnimation, bounceEffectOut, bounceVelocityOut, bouncinessOut, onPressOut],
7181
);
7282

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+
73101
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>
87113
);
88114
};
89115

example/package-lock.json

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/RNBounceable.tsx

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
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>;
414

515
export interface IRNBounceableProps extends PressableProps {
616
bounceEffectIn?: number;
@@ -70,20 +80,36 @@ const RNBounceableInner = (
7080
[disabled, bounceAnimation, bounceEffectOut, bounceVelocityOut, bouncinessOut, onPressOut],
7181
);
7282

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+
73101
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>
87113
);
88114
};
89115

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@freakycoder/react-native-bounceable",
3-
"version": "2.0.0",
3+
"version": "2.1.0",
44
"description": "Animate and bounce any component with RNBounceable for React Native",
55
"main": "./build/dist/RNBounceable.js",
66
"repository": "git@github.com:WrathChaos/react-native-bounceable.git",

0 commit comments

Comments
 (0)