Created
June 10, 2023 19:12
-
-
Save IslamRustamov/7dbb5ff7636015d49a1b47dcaf303857 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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