-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
150 lines (140 loc) · 4.16 KB
/
App.js
File metadata and controls
150 lines (140 loc) · 4.16 KB
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
import { StatusBar } from 'expo-status-bar';
import React, { useRef } from 'react';
import { StyleSheet, Text, View, Dimensions, Animated, Image, findNodeHandle, TouchableOpacity } from 'react-native';
const { width, height } = Dimensions.get('screen');
const images = {
man:
'https://images.pexels.com/photos/3147528/pexels-photo-3147528.jpeg?auto=compress&cs=tinysrgb&dpr=2&w=500',
women:
'https://images.pexels.com/photos/2552130/pexels-photo-2552130.jpeg?auto=compress&cs=tinysrgb&dpr=2&w=500',
kids:
'https://images.pexels.com/photos/5080167/pexels-photo-5080167.jpeg?auto=compress&cs=tinysrgb&dpr=2&w=500',
skullcandy:
'https://images.pexels.com/photos/5602879/pexels-photo-5602879.jpeg?auto=compress&cs=tinysrgb&dpr=2&w=500',
help:
'https://images.pexels.com/photos/2552130/pexels-photo-2552130.jpeg?auto=compress&cs=tinysrgb&dpr=2&w=500',
};
const data = Object.keys(images).map((i) => ({
key: i,
title: i,
image: images[i],
ref: React.createRef()
}));
const Tab = React.forwardRef(({ item, onItemPress }, ref) => {
return (
<TouchableOpacity onPress={() => onItemPress(item.key)}>
<View ref={ref}>
<Text style={{ color: 'white', fontSize: 17, fontWeight: '800', textTransform: 'uppercase' }}>
{item.title}
</Text>
</View>
</TouchableOpacity>
);
});
const Indicator = ({ measures, scrollX }) => {
const inputRange = data.map((_, i) => i * width);
const indicatorWidth = scrollX.interpolate({
inputRange,
outputRange: measures.map(measure => measure.width)
});
const translateX = scrollX.interpolate({
inputRange,
outputRange: measures.map(measure => measure.x)
});
return (
<Animated.View
style={{
position: 'absolute',
height: 4,
backgroundColor: 'white',
bottom: -10,
left: 0,
width: indicatorWidth,
transform: [{ translateX }]
}}
/>
);
};
const Tabs = ({ data, scrollX, onItemPress }) => {
const [measures, setMeasures] = React.useState([]);
const containerRef = React.useRef();
React.useEffect(() => {
const m = [];
data.forEach((item) => {
item.ref.current.measureLayout(
containerRef.current,
(x, y, width, height) => {
m.push({
x,
y,
width,
height
});
if (m.length === data.length) {
setMeasures(m);
}
},
() => console.error('measureLayout failed')
);
});
}, []);
return (
<View style={{ position: 'absolute', top: 100, width }}>
<View ref={containerRef} style={{ justifyContent: 'space-evenly', flex: 1, flexDirection: 'row' }}>
{data.map((item, index) => (
<Tab key={item.key} item={item} ref={item.ref} onItemPress={() => onItemPress(index)} />
))}
</View>
{measures.length > 0 && <Indicator measures={measures} scrollX={scrollX} />}
</View>
);
};
export default function App() {
const scrollX = useRef(new Animated.Value(0)).current;
const ref = React.useRef();
const onItemPress = React.useCallback((itemIndex) => {
ref?.current?.scrollToOffset({ offset: itemIndex * width });
}, []);
return (
<View style={styles.container}>
<StatusBar hidden />
<Animated.FlatList
ref={ref}
horizontal
pagingEnabled
showsHorizontalScrollIndicator={false}
bounces={false}
onScroll={Animated.event(
[{ nativeEvent: { contentOffset: { x: scrollX } } }],
{ useNativeDriver: true }
)}
data={data}
keyExtractor={(item) => item.key}
renderItem={({ item }) => (
<View style={styles.slide}>
<Image source={{ uri: item.image }} style={styles.image} />
</View>
)}
/>
<Tabs scrollX={scrollX} data={data} onItemPress={onItemPress} />
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
},
slide: {
width,
height: height * 1,
justifyContent: 'center',
alignItems: 'center',
},
image: {
width: width * 1,
height: '100%',
resizeMode: 'cover',
borderRadius: 10,
},
});