Skip to content

Commit cbdfc80

Browse files
committed
Added sticky header support
1 parent 270d1e5 commit cbdfc80

File tree

1 file changed

+61
-47
lines changed

1 file changed

+61
-47
lines changed

components/Swiper.js

Lines changed: 61 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ import {usePrevious} from "../helpers/usePrevious";
66
const isJSX = element => typeof element !== 'function' && typeof element?.type === 'object';
77
const isMemo = element => typeof element !== 'function' && typeof element?.type === 'function';
88

9-
export default function Swiper({style, data, isStaticPills, initialScrollIndex, ...rest}) {
9+
export default function Swiper({style, data, isStaticPills, initialScrollIndex, stickyHeaderEnabled, children, ...rest}) {
1010
const width = useWindowDimensions().width;
1111
const flatList = useRef(null);
1212
const scrollViewRef = useRef(null);
1313
const [currentIndex, setCurrentIndex] = useState(initialScrollIndex || 0);
14+
const [pillContainerHeight, setPillContainerHeight] = useState(0);
1415
const prevIndex = usePrevious(currentIndex);
1516
const buttonsCoords = React.useRef([]).current;
1617
const [x, setX] = useState(0);
@@ -56,51 +57,64 @@ export default function Swiper({style, data, isStaticPills, initialScrollIndex,
5657
);
5758

5859
return (
59-
<>
60-
<View style={[
61-
styles.pillContainer,
62-
isStaticPills && styles.staticPillContainer,
63-
style?.pillContainer,
64-
]}>
65-
{!!isStaticPills && (
66-
<StaticPills
67-
data={data}
68-
currentIndex={currentIndex}
69-
x={x}
70-
style={style}
71-
onPillPress={onButtonPress}
72-
/>
73-
)}
74-
{!isStaticPills && (
75-
<ScrollView
76-
ref={scrollViewRef}
77-
showsHorizontalScrollIndicator={false}
78-
horizontal={true}
79-
>
80-
{!!data?.length && data.map((item, index) => (
81-
<TouchableOpacity
82-
key={index}
83-
onLayout={onButtonLayout(index)}
84-
style={[
85-
styles.pillButton,
86-
style?.pillButton,
87-
index === currentIndex && styles.pillActive,
88-
index === currentIndex && style?.pillActive,
89-
]}
90-
onPress={onButtonPress(index)}
91-
>
92-
{item.icon}
93-
<Text style={[
94-
styles.pillLabel,
95-
style?.pillLabel,
96-
index === currentIndex && (style?.activeLabel || styles.pillLabelActive),
97-
]}>
98-
{item.tabLabel}
99-
</Text>
100-
</TouchableOpacity>
101-
))}
102-
</ScrollView>
103-
)}
60+
<ScrollView
61+
stickyHeaderIndices={stickyHeaderEnabled ? [1] : null}
62+
>
63+
{children}
64+
<View style={{
65+
overflow: 'hidden',
66+
height: pillContainerHeight + 10,
67+
}}>
68+
<View
69+
onLayout={(event) => {
70+
let {x, y, width, height} = event.nativeEvent.layout;
71+
setPillContainerHeight(height)
72+
}}
73+
style={[
74+
styles.pillContainer,
75+
isStaticPills && styles.staticPillContainer,
76+
style?.pillContainer,
77+
]}>
78+
{!!isStaticPills && (
79+
<StaticPills
80+
data={data}
81+
currentIndex={currentIndex}
82+
x={x}
83+
style={style}
84+
onPillPress={onButtonPress}
85+
/>
86+
)}
87+
{!isStaticPills && (
88+
<ScrollView
89+
ref={scrollViewRef}
90+
showsHorizontalScrollIndicator={false}
91+
horizontal={true}
92+
>
93+
{!!data?.length && data.map((item, index) => (
94+
<TouchableOpacity
95+
key={index}
96+
onLayout={onButtonLayout(index)}
97+
style={[
98+
styles.pillButton,
99+
style?.pillButton,
100+
index === currentIndex && styles.pillActive,
101+
index === currentIndex && style?.pillActive,
102+
]}
103+
onPress={onButtonPress(index)}
104+
>
105+
{item.icon}
106+
<Text style={[
107+
styles.pillLabel,
108+
style?.pillLabel,
109+
index === currentIndex && (style?.activeLabel || styles.pillLabelActive),
110+
]}>
111+
{item.tabLabel}
112+
</Text>
113+
</TouchableOpacity>
114+
))}
115+
</ScrollView>
116+
)}
117+
</View>
104118
</View>
105119
<FlatList
106120
ref={flatList}
@@ -118,7 +132,7 @@ export default function Swiper({style, data, isStaticPills, initialScrollIndex,
118132
initialScrollIndex={initialScrollIndex}
119133
{...rest}
120134
/>
121-
</>
135+
</ScrollView>
122136
);
123137
};
124138

0 commit comments

Comments
 (0)