-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathCarousel.tsx
95 lines (89 loc) · 2.11 KB
/
Carousel.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import React, { useState } from 'react';
import {
View,
Text,
Button,
StyleSheet,
Image,
ImageSourcePropType,
} from 'react-native';
import Animated, { SlideInLeft, SlideOutRight } from 'react-native-reanimated';
const AnimatedImage = Animated.createAnimatedComponent(Image);
interface Pokemon {
pokemonName: string;
firstType: string;
secondType: string;
img: ImageSourcePropType;
}
const DATA: Pokemon[] = [
{
pokemonName: 'Bulbasaur',
firstType: 'poison',
secondType: 'grass',
img: require('./Bulbasaur.png'),
},
{
pokemonName: 'Charizard',
firstType: 'Fire',
secondType: 'flying',
img: require('./Charizard.png'),
},
{
pokemonName: 'Butterfree',
firstType: 'Bug',
secondType: 'flying',
img: require('./Butterfree.png'),
},
];
function AnimatedView({ pokemon }: { pokemon: Pokemon }) {
return (
<Animated.View
entering={SlideInLeft}
exiting={SlideOutRight}
style={[styles.animatedView]}>
<AnimatedImage
entering={SlideInLeft.delay(300).springify()}
source={pokemon.img}
/>
<Animated.View
entering={SlideInLeft.delay(500).springify()}
exiting={SlideOutRight}>
<Text> {pokemon.firstType} </Text>
<Text> {pokemon.secondType}</Text>
</Animated.View>
</Animated.View>
);
}
export function Carousel(): React.ReactElement {
const [currentIndex, incrementIndex] = useState(0);
return (
<View style={{ flexDirection: 'column-reverse' }}>
<Button
title="toggle"
onPress={() => {
incrementIndex((prev) => (prev + 1) % DATA.length);
}}
/>
<View
style={{
height: 400,
alignItems: 'center',
justifyContent: 'center',
borderWidth: 1,
}}>
<AnimatedView key={currentIndex} pokemon={DATA[currentIndex]} />
</View>
</View>
);
}
const styles = StyleSheet.create({
animatedView: {
height: 300,
width: 200,
borderWidth: 1,
borderColor: 'black',
alignItems: 'center',
justifyContent: 'center',
backgroundColor: 'red',
},
});