Skip to content

Instantly share code, notes, and snippets.

@IslamRustamov
Created June 10, 2023 19:12
Show Gist options
  • Save IslamRustamov/7dbb5ff7636015d49a1b47dcaf303857 to your computer and use it in GitHub Desktop.
Save IslamRustamov/7dbb5ff7636015d49a1b47dcaf303857 to your computer and use it in GitHub Desktop.
const height = Dimensions.get('window').height;
const blockSize = 70;
function ItemSeparator() {
return <View style={{height: 10}} />;
}
function ListSpacer() {
return <View style={{height: height / 2 - blockSize}} />;
}
function AnimatedFlatList() {
const [centeredIndex, setCenteredIndex] = useState(0);
function momentumScrollEndHandler(
e: NativeSyntheticEvent<NativeScrollEvent>,
) {
setCenteredIndex(Math.round(e.nativeEvent.contentOffset.y / blockSize));
}
function renderItem({item, index}: ListRenderItemInfo<Article>) {
return (
<ArticleBlock
key={item.title}
title={item.title}
date={item.date}
isCenter={index === centeredIndex}
/>
);
}
return (
<FlatList
snapToInterval={blockSize}
onMomentumScrollEnd={momentumScrollEndHandler}
contentContainerStyle={{alignItems: 'center'}}
data={mockedData}
renderItem={renderItem}
ItemSeparatorComponent={ItemSeparator}
ListHeaderComponent={ListSpacer}
ListFooterComponent={ListSpacer}
/>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment