-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: update example with LazyFlatList
- Loading branch information
Showing
8 changed files
with
254 additions
and
0 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import React, { useCallback, useRef } from 'react'; | ||
import { | ||
ImageSourcePropType, | ||
ListRenderItemInfo, | ||
StyleSheet, | ||
Text, | ||
TouchableOpacity, | ||
View, | ||
} from 'react-native'; | ||
import { | ||
LazyFlatList, | ||
LazyFlatListMethods, | ||
} from 'react-native-lazy-scrollview'; | ||
import { ALBUMS, PADDING_VERTICAL } from '../../constants'; | ||
import shuffle from 'lodash/shuffle'; | ||
import { FireOnceBlock } from '../../components/blocks/FireOnceBlock'; | ||
import { ImageBlock } from '../../components/blocks/ImageBlock'; | ||
import { NoLazyChild } from '../../components/blocks/NoLazyChild'; | ||
import { Header } from '../../components/blocks/Header'; | ||
|
||
const OFFSET = -50; | ||
|
||
type Block = ImageSourcePropType | 'no-lazy' | 'fire-once'; | ||
|
||
export default function FlatList() { | ||
const ref = useRef<LazyFlatListMethods>(null); | ||
|
||
const data: Block[] = shuffle( | ||
ALBUMS.concat(shuffle(ALBUMS)).concat(shuffle(ALBUMS)) | ||
); | ||
|
||
const renderItem = useCallback( | ||
({ item: source, index }: ListRenderItemInfo<Block>) => { | ||
if (source === 'no-lazy') { | ||
return <NoLazyChild key={`no-lazy-child-${index}`} />; | ||
} | ||
|
||
if (source === 'fire-once') { | ||
return ( | ||
<FireOnceBlock | ||
key={`fire-once-child-${index}`} | ||
percentVisibleThreshold={1} | ||
debug | ||
/> | ||
); | ||
} | ||
|
||
return ( | ||
<ImageBlock | ||
key={`${source.toString()}-${index}`} | ||
source={source} | ||
percentVisibleThreshold={1} | ||
debug | ||
/> | ||
); | ||
}, | ||
[] | ||
); | ||
|
||
return ( | ||
<View style={styles.scrollviewContainer}> | ||
<LazyFlatList | ||
ref={ref} | ||
offset={OFFSET} | ||
showsVerticalScrollIndicator={false} | ||
numColumns={2} | ||
ListHeaderComponent={Header} | ||
data={data} | ||
renderItem={renderItem} | ||
debug | ||
/> | ||
<View style={styles.arrowsContainer}> | ||
<TouchableOpacity | ||
style={styles.arrowButton} | ||
activeOpacity={0.7} | ||
onPress={() => ref.current?.scrollToStart({ animated: true })} | ||
> | ||
<Text style={styles.arrow}>⬆️</Text> | ||
</TouchableOpacity> | ||
<TouchableOpacity | ||
activeOpacity={0.7} | ||
onPress={() => ref.current?.scrollToEnd({ animated: true })} | ||
> | ||
<Text style={styles.arrow}>⬇️</Text> | ||
</TouchableOpacity> | ||
</View> | ||
</View> | ||
); | ||
} | ||
|
||
const styles = StyleSheet.create({ | ||
scrollviewContainer: { | ||
flex: 1, | ||
backgroundColor: '#ecf0f1', | ||
}, | ||
offsetBar: { | ||
position: 'absolute', | ||
bottom: OFFSET * -1 + PADDING_VERTICAL, | ||
borderBottomWidth: 1, | ||
borderBottomColor: 'black', | ||
left: 0, | ||
right: 0, | ||
opacity: 0.7, | ||
height: 50, | ||
justifyContent: 'flex-end', | ||
}, | ||
offsetText: { | ||
color: 'white', | ||
fontSize: 18, | ||
fontWeight: '600', | ||
backgroundColor: '#000', | ||
padding: 8, | ||
alignSelf: 'flex-start', | ||
}, | ||
arrowsContainer: { | ||
top: 8, | ||
right: 8, | ||
position: 'absolute', | ||
justifyContent: 'center', | ||
flexDirection: 'row', | ||
}, | ||
arrowButton: { marginBottom: 8 }, | ||
arrow: { fontSize: 32 }, | ||
}); |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import React, { ComponentProps, useState } from 'react'; | ||
import { | ||
Dimensions, | ||
Image, | ||
ImageSourcePropType, | ||
StyleSheet, | ||
View, | ||
} from 'react-native'; | ||
import { LazyChild } from 'react-native-lazy-scrollview'; | ||
import { SQUARE_SIZE } from '../../constants'; | ||
import { Skeleton } from '../loaders/Skeleton'; | ||
|
||
const SCREEN_WIDTH = Dimensions.get('window').width; | ||
|
||
export function Header() { | ||
const [triggered, setTriggered] = useState(false); | ||
const [isVisible, setIsVisible] = useState(false); | ||
|
||
const onEnterThresholdPass = () => { | ||
setTriggered(true); | ||
}; | ||
|
||
const onExitThresholdPass = () => { | ||
setTriggered(false); | ||
}; | ||
|
||
const onVisibilityEnter = () => { | ||
setIsVisible(true); | ||
}; | ||
|
||
const onVisibilityExit = () => { | ||
setIsVisible(false); | ||
}; | ||
|
||
return ( | ||
<View style={styles.container}> | ||
<LazyChild | ||
onEnterThresholdPass={onEnterThresholdPass} | ||
onExitThresholdPass={onExitThresholdPass} | ||
onVisibilityEnter={onVisibilityEnter} | ||
onVisibilityExit={onVisibilityExit} | ||
percentVisibleThreshold={0.5} | ||
> | ||
<View style={styles.contentContainer}> | ||
<Skeleton show={!triggered}> | ||
<Image | ||
source={require('../../assets/header.jpg')} | ||
style={styles.image} | ||
/> | ||
</Skeleton> | ||
{!isVisible ? <View style={styles.percentTextWrapper} /> : null} | ||
</View> | ||
</LazyChild> | ||
</View> | ||
); | ||
} | ||
|
||
const styles = StyleSheet.create({ | ||
container: { | ||
justifyContent: 'center', | ||
alignItems: 'center', | ||
alignSelf: 'center', | ||
width: SCREEN_WIDTH, | ||
}, | ||
contentContainer: { | ||
width: SCREEN_WIDTH, | ||
height: SCREEN_WIDTH * 1.25, | ||
justifyContent: 'center', | ||
alignItems: 'center', | ||
}, | ||
image: { | ||
width: SCREEN_WIDTH, | ||
height: SCREEN_WIDTH * 1.25, | ||
resizeMode: 'cover', | ||
}, | ||
percentTextWrapper: { | ||
position: 'absolute', | ||
top: 0, | ||
right: 0, | ||
bottom: 0, | ||
left: 0, | ||
backgroundColor: 'rgba(255, 255, 255, 0.5)', | ||
justifyContent: 'center', | ||
alignItems: 'center', | ||
}, | ||
}); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import React, { useEffect } from 'react'; | ||
import { | ||
interpolate, | ||
useAnimatedStyle, | ||
useSharedValue, | ||
withRepeat, | ||
withTiming, | ||
} from 'react-native-reanimated'; | ||
import { FLATLIST } from '../../constants'; | ||
import { Card } from './Card'; | ||
|
||
export function FlatListCard() { | ||
const animation = useSharedValue(0); | ||
|
||
useEffect(() => { | ||
animation.value = withRepeat(withTiming(1, { duration: 1000 }), -1, true); | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, []); | ||
const animatedStyle = useAnimatedStyle(() => { | ||
return { | ||
marginRight: 16, | ||
transform: [ | ||
{ translateY: interpolate(animation.value, [0, 1], [-20, 20]) }, | ||
], | ||
}; | ||
}); | ||
|
||
return <Card scrollView={FLATLIST} animatedStyle={animatedStyle} />; | ||
} |
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