-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.tsx
160 lines (147 loc) · 4.62 KB
/
index.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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// Inspiration: https://dribbble.com/shots/3731362-Event-cards-iOS-interaction
import React, { useCallback, useEffect, useRef, useState } from 'react';
import {
TouchableWithoutFeedback,
FlatList,
View,
Animated,
} from 'react-native';
import {
Directions,
FlingGestureHandler,
State,
} from 'react-native-gesture-handler';
import { SharedElement } from 'react-navigation-shared-element';
import {
Container,
PlaceContainer,
PlaceImage,
PlaceName,
ITEM_WIDTH,
ITEM_HEIGHT,
} from './styles';
import { places } from './data';
// navigation.navigate('Shared Transition Detail')
const VISIBLE_ITEMS = 4;
const SharedTransition: React.FC = ({ navigation }) => {
const [activeIndex, setActiveIndex] = useState(0);
const animatedValue = useRef(new Animated.Value(0)).current;
const reactiveAnimated = useRef(new Animated.Value(0)).current;
useEffect(() => {
Animated.timing(animatedValue, {
toValue: reactiveAnimated,
duration: 280,
useNativeDriver: true,
}).start();
}, [animatedValue, reactiveAnimated]);
const setActiveSlide = useCallback(
(newIndex) => {
setActiveIndex(newIndex);
reactiveAnimated.setValue(newIndex);
},
[reactiveAnimated],
);
return (
<FlingGestureHandler
key="UP"
direction={Directions.UP}
onHandlerStateChange={(ev) => {
if (ev.nativeEvent.state === State.END && activeIndex !== 0) {
setActiveSlide(activeIndex - 1);
}
}}>
<FlingGestureHandler
key="DOWN"
direction={Directions.DOWN}
onHandlerStateChange={(ev) => {
if (
ev.nativeEvent.state === State.END &&
activeIndex < places.length - 1
) {
setActiveSlide(activeIndex + 1);
}
}}>
<Container>
<FlatList
data={places}
keyExtractor={({ name }) => name}
scrollEnabled={false}
contentContainerStyle={{
flex: 1,
alignItems: 'center',
justifyContent: 'center',
}}
CellRendererComponent={({
index,
item,
children,
style,
...props
}) => {
return (
<View style={style} {...props}>
{children}
</View>
);
}}
renderItem={({ item, index }) => {
const inputRange = [index - 1, index, index + 1];
const translateY = animatedValue.interpolate({
inputRange,
outputRange: [-38, 0, 38],
});
const opacity = animatedValue.interpolate({
inputRange,
outputRange: [1 - 1 / VISIBLE_ITEMS, 1, 0],
});
const scale = animatedValue.interpolate({
inputRange,
outputRange: [0.9, 1, 1.1],
});
const { poster, name } = item;
return (
<TouchableWithoutFeedback
// activeOpacity={1}
onPress={() =>
navigation.navigate('Places Transition Detail', {
item: places[activeIndex],
})
}>
<PlaceContainer
as={Animated.View}
style={{
transform: [{ translateY }, { scale }],
opacity,
elevation: places.length - index,
zIndex: places.length - index,
}}>
<SharedElement
id={`item.${name}.image`}
style={{
width: ITEM_WIDTH,
height: ITEM_HEIGHT,
borderRadius: 20,
}}>
<PlaceImage source={poster} resizeMode="cover" />
</SharedElement>
<SharedElement id={`item.${name}.name`}>
<PlaceName
style={{
textShadowColor: '#000',
textShadowOffset: { width: 0, height: 3 },
textShadowRadius: 15,
}}>
{name}
</PlaceName>
</SharedElement>
</PlaceContainer>
</TouchableWithoutFeedback>
);
}}
/>
</Container>
</FlingGestureHandler>
</FlingGestureHandler>
);
};
export default SharedTransition;