-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStickyList
More file actions
139 lines (127 loc) · 5.23 KB
/
StickyList
File metadata and controls
139 lines (127 loc) · 5.23 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
import React, { useRef, useState } from 'react';
import { View, Animated, SectionList as NativeSectionList } from 'react-native';
import { MD3LightTheme } from '@jmsstudiosinc/react-native-paper';
import { itemSeparator } from '../utils';
import * as Tabs from '../Tabs/Tabs';
import sectionListGetItemLayout from './utils';
import { moderateScale } from 'react-native-size-matters';
const AnimatedSectionList = Animated.createAnimatedComponent(NativeSectionList);
const StickyList = ({
title,
sections,
listHeaderComponent,
onItemPress,
onContentOffsetYScroll,
contentOffsetY,
...props
}) => {
const animatedValue = new Animated.Value(0);
const scrollY = useRef(animatedValue).current;
const blockUpdateIndexRef = useRef(false);
const sectionListRef = useRef();
const contentOffsetYRangeRef = useRef(false);
const [currentIndex, setCurrentIdex] = useState(0);
const [layoutHeight, setLayoutHeight] = useState(0);
const maxHeight = layoutHeight + 1;
const tabBarOpacity = scrollY.interpolate({
inputRange: [layoutHeight, maxHeight],
outputRange: [0, 100],
extrapolate: 'clamp',
});
const getItemLayout = sectionListGetItemLayout({
getSeparatorHeight: () => 0,
getSectionHeaderHeight: () => moderateScale(50),
getSectionFooterHeight: () => 0,
listHeaderHeight: layoutHeight,
});
const renderTab = (
<Tabs.Scrollable title={title} currentIndex={currentIndex}>
{sections.map((item, index) => (
<Tabs.Item
key={`sticky-section-${item.id}`}
title={item.title}
isSelected={currentIndex === index}
style={{ backgroundColor: itemSeparator(index, sections.length) ? MD3LightTheme.spacing.x4 : null }}
onPress={() => {
setCurrentIdex(index);
blockUpdateIndexRef.current = true;
const sectionList = sectionListRef.current;
if (sectionList && sectionList.scrollToLocation) {
sectionList.scrollToLocation({
animated: true,
itemIndex: 0,
viewOffset: 0,
sectionIndex: index,
});
}
}}
/>
))}
</Tabs.Scrollable>
);
return (
<>
<AnimatedSectionList
{...props}
ref={(ref) => (sectionListRef.current = ref)}
scrollEventThrottle={16}
stickySectionHeadersEnabled={false}
sections={sections}
onScroll={Animated.event([{ nativeEvent: { contentOffset: { y: scrollY } } }], {
useNativeDriver: true,
listener: onContentOffsetYScroll
? (event) => {
if (
event.nativeEvent.contentOffset.y > contentOffsetY &&
contentOffsetYRangeRef.current === false
) {
onContentOffsetYScroll(event.nativeEvent.contentOffset.y);
contentOffsetYRangeRef.current = true;
} else if (
event.nativeEvent.contentOffset.y < contentOffsetY &&
contentOffsetYRangeRef.current === true
) {
onContentOffsetYScroll(event.nativeEvent.contentOffset.y);
contentOffsetYRangeRef.current = false;
}
}
: null,
})}
onMomentumScrollEnd={() => (blockUpdateIndexRef.current = false)}
showsVerticalScrollIndicator={true}
onViewableItemsChanged={({ viewableItems }) => {
if (!blockUpdateIndexRef.current && viewableItems[0]) {
const { index } = viewableItems[0].section;
if (currentIndex !== index) {
setCurrentIdex(index);
}
}
}}
viewabilityConfig={{
minimumViewTime: 10,
itemVisiblePercentThreshold: 10,
}}
ListHeaderComponent={
<>
{listHeaderComponent}
<View onLayout={(ev) => setLayoutHeight(ev.nativeEvent.layout.y)}></View>
</>
}
showsHorizontalScrollIndicator={false}
getItemLayout={getItemLayout}
/>
<Animated.View
style={{
position: 'absolute',
top: 0,
left: 0,
right: 0,
opacity: tabBarOpacity,
}}
>
{renderTab}
</Animated.View>
</>
);
};
export default StickyList;