Skip to content

Commit f7cd9b1

Browse files
committed
awesome FAB animation
1 parent 69f0ab0 commit f7cd9b1

File tree

3 files changed

+50
-22
lines changed

3 files changed

+50
-22
lines changed

hackathon/spacecraft/src/components/StarshipCard.tsx

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,10 @@ import Animated, {
1212
import type { StarshipProps } from "../../api/types";
1313
import { useImage } from "../hooks/useImage";
1414
import { Routes } from "../navigation/Routes";
15+
import { withAnimated } from "../utils/withAnimated";
1516

1617
import { Image } from "~/components/Image";
1718

18-
export function withAnimated<T extends object>(
19-
WrappedComponent: React.ComponentType<T>
20-
): React.ComponentClass<AnimateProps<T>, any> {
21-
const displayName =
22-
WrappedComponent.displayName || WrappedComponent.name || "Component";
23-
24-
class WithAnimated extends React.Component<T, any> {
25-
static displayName = `WithAnimated(${displayName})`;
26-
27-
render(): React.ReactNode {
28-
return <WrappedComponent {...this.props} />;
29-
}
30-
}
31-
return Animated.createAnimatedComponent(WithAnimated);
32-
}
33-
3419
const AnimatedCard = withAnimated(Card);
3520

3621
export interface StarshipCardProps {

hackathon/spacecraft/src/screens/StarshipDetailsScreen.tsx

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,18 @@ import { FontAwesome5 } from "@expo/vector-icons";
22
import { useNavigation } from "@react-navigation/native";
33
import React from "react";
44
import {
5+
Pressable,
56
View,
67
StyleSheet,
78
TouchableOpacity,
89
ScrollView,
910
Alert,
1011
} from "react-native";
1112
import { Text, DataTable, List, FAB, Chip } from "react-native-paper";
13+
import { withAnimated } from "~/utils/withAnimated";
14+
15+
const AnimatedFAB = withAnimated(FAB);
16+
import { useSharedValue, withSpring } from "react-native-reanimated";
1217

1318
import type { StarshipProps } from "../../api/types";
1419

@@ -46,9 +51,11 @@ export const StarshipDetailsScreen = ({
4651
};
4752

4853
const handleBuy = () => {
49-
Alert.alert("Give me the money!");
54+
// Alert.alert("Give me the money!");
5055
};
5156

57+
const scale = useSharedValue(1);
58+
5259
return (
5360
<View>
5461
<ScrollView>
@@ -109,12 +116,30 @@ export const StarshipDetailsScreen = ({
109116
</View>
110117
</ScrollView>
111118

112-
<FAB
113-
style={styles.fab}
114-
label="Buy this ship"
115-
icon="cart"
119+
<Pressable
120+
onPressIn={() => {
121+
scale.value = withSpring(0.9);
122+
}}
123+
onPressOut={() => {
124+
scale.value = withSpring(1);
125+
}}
116126
onPress={handleBuy}
117-
/>
127+
>
128+
<AnimatedFAB
129+
style={[
130+
styles.fab,
131+
{
132+
transform: [
133+
{
134+
scale,
135+
},
136+
],
137+
},
138+
]}
139+
label="Buy this ship"
140+
icon="cart"
141+
/>
142+
</Pressable>
118143
</View>
119144
);
120145
};
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import * as React from "react";
2+
import Animated, { AnimateProps } from "react-native-reanimated";
3+
4+
export function withAnimated<T extends object>(
5+
WrappedComponent: React.ComponentType<T>
6+
): React.ComponentClass<AnimateProps<T>, any> {
7+
const displayName =
8+
WrappedComponent.displayName || WrappedComponent.name || "Component";
9+
10+
class WithAnimated extends React.Component<T, any> {
11+
static displayName = `WithAnimated(${displayName})`;
12+
13+
render(): React.ReactNode {
14+
return <WrappedComponent {...this.props} />;
15+
}
16+
}
17+
return Animated.createAnimatedComponent(WithAnimated);
18+
}

0 commit comments

Comments
 (0)