-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathApp.tsx
108 lines (98 loc) · 3.02 KB
/
App.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
import React, { useCallback, useState, useRef } from "react";
import { SafeAreaView, Dimensions, View, FlatList } from "react-native";
import Animated from "react-native-reanimated";
import { TabView, TabBar } from "react-native-tab-view";
import ExampleHeader from "./ExampleHeader";
import ExampleTab from "./ExampleTab";
const initialLayout = { width: Dimensions.get("window").width };
const HEADER_HEIGHT = 50;
const TABBAR_HEIGHT = 48;
const TOTAL_HEADER_HEIGHT = HEADER_HEIGHT + TABBAR_HEIGHT;
const App: React.FC = () => {
const scrollY = useRef(new Animated.Value(0)).current;
const headerTranslateY = useRef(
scrollY.interpolate({
inputRange: [0, HEADER_HEIGHT],
outputRange: [0, -HEADER_HEIGHT],
extrapolate: Animated.Extrapolate.CLAMP
})
).current;
const headerOpacity = useRef(
scrollY.interpolate({
inputRange: [0, HEADER_HEIGHT],
outputRange: [1, 0],
extrapolate: Animated.Extrapolate.CLAMP
})
).current;
const [index, setIndex] = useState(0);
const routes = [
{
key: "Tab 1",
title: "Tab 1"
},
{ key: "Tab 2", title: "Tab 2" }
];
const [listRefs] = useState(() => {
const refs: { [key: string]: React.RefObject<FlatList<any>> } = {};
for (const route of routes) {
refs[route.key] = React.createRef();
}
return refs;
});
const renderScene = useCallback(({ route }) => {
return (
<ExampleTab
ref={listRefs[route.key]}
scrollContentContainerStyle={{ paddingTop: TOTAL_HEADER_HEIGHT }}
onScroll={Animated.event([
{ nativeEvent: { contentOffset: { y: scrollY } } }
])}
/>
);
}, []);
const handleTabChange = () => {
listRefs["Tab 2"].current.getNode().scrollToOffset({
offset: 50,
animated: false
});
};
return (
<SafeAreaView style={{ flex: 1, backgroundColor: "white" }}>
<View style={{ flex: 1, backgroundColor: "rgb(239, 243, 252)" }}>
<ExampleHeader
style={{
position: "absolute",
height: HEADER_HEIGHT,
opacity: headerOpacity,
transform: [{ translateY: headerTranslateY }],
zIndex: 1
}}
/>
<TabView
onSwipeStart={() => console.log(`swipe start (index = ${index})`)}
navigationState={{ index, routes }}
renderScene={renderScene}
renderTabBar={props => (
<Animated.View
style={{
position: "absolute",
top: HEADER_HEIGHT,
left: 0,
right: 0,
zIndex: 1,
transform: [{ translateY: headerTranslateY }],
backgroundColor: "rgb(239, 243, 252)"
}}
>
<TabBar {...props} onTabPress={() => handleTabChange()} />
</Animated.View>
)}
initialLayout={initialLayout}
onIndexChange={setIndex}
style={{ overflow: "visible" }}
/>
</View>
</SafeAreaView>
);
};
export default App;