-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.tsx
More file actions
102 lines (94 loc) · 2.4 KB
/
App.tsx
File metadata and controls
102 lines (94 loc) · 2.4 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
import React, { useMemo, useRef, useState } from 'react';
import {
StyleSheet,
View,
useWindowDimensions,
TouchableWithoutFeedback,
TouchableWithoutFeedbackProps,
} from 'react-native';
import ScrollableTabs, {
ScrollIndicator,
useScrollableTabs,
} from '@matejpekar/react-native-scrollable-tabs';
import { impactAsync, ImpactFeedbackStyle } from 'expo-haptics';
import Animated, {
interpolateColor,
useAnimatedStyle,
} from 'react-native-reanimated';
export default () => {
const { width } = useWindowDimensions();
const ref = useRef<ScrollableTabs>(null);
const [haptics, setHaptics] = useState(false);
const hapticFeedback = () => impactAsync(ImpactFeedbackStyle.Light);
const indexes = useMemo(
() =>
Array(15)
.fill(0)
.map((_, index) => index + 1),
[]
);
return (
<View style={styles.container}>
<ScrollableTabs
ref={ref}
width={width}
contentContainerStyle={styles.contentContainer}
scrollIndicator={<ScrollIndicator />}
onScrollBeginDrag={() => setHaptics(true)}
onMomentumScrollEnd={() => setHaptics(false)}
onChange={() => haptics && hapticFeedback()}
decelerationRate="fast"
>
{indexes.map((item, i) => (
<Tab
index={item}
key={i}
onPress={() => {
ref.current?.snapToIndex(i);
hapticFeedback();
}}
/>
))}
</ScrollableTabs>
</View>
);
};
const Tab = ({
index,
...props
}: TouchableWithoutFeedbackProps & { index: number }) => {
const { animatedIndex } = useScrollableTabs();
const animatedStyle = useAnimatedStyle(() => ({
color: interpolateColor(
animatedIndex.value + 1,
[index - 0.5, index, index + 0.5],
['gray', 'black', 'gray']
),
}));
return (
<TouchableWithoutFeedback {...props}>
<View style={[styles.tab]}>
{index % 2 ? (
<Animated.Text style={animatedStyle}>Tab long {index}</Animated.Text>
) : (
<Animated.Text style={animatedStyle}>Tab {index}</Animated.Text>
)}
</View>
</TouchableWithoutFeedback>
);
};
const styles = StyleSheet.create({
container: {
marginTop: 100,
height: 100,
},
contentContainer: {
gap: 20,
paddingHorizontal: 20,
},
tab: {
justifyContent: 'center',
alignItems: 'center',
height: 100,
},
});