-
-
Notifications
You must be signed in to change notification settings - Fork 767
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow dynamic snap points (#81)
* chore: updated normalized snap points implementation * chore: reverse snap points transitions * chore: updated content wrapper * chore: added bottom sheet container * chore: updated bottom sheet to handle dynamic snap points * chore: updated examples * chore: updated examples * chore: added handleHeight prop * chore: rename internal components * chore: added default background * chore: updated handling nonflex views * chore: added dynamic snap point example
- Loading branch information
Showing
48 changed files
with
698 additions
and
203 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
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
147 changes: 147 additions & 0 deletions
147
example/src/screens/advanced/DynamicSnapPointExample.tsx
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,147 @@ | ||
import React, { useCallback, useMemo, useRef, useState } from 'react'; | ||
import { View, StyleSheet, Text } from 'react-native'; | ||
import BottomSheet, { BottomSheetView } from '@gorhom/bottom-sheet'; | ||
import { useSafeArea } from 'react-native-safe-area-context'; | ||
import { Easing } from 'react-native-reanimated'; | ||
import Button from '../../components/button'; | ||
|
||
const DynamicSnapPointExample = () => { | ||
// state | ||
const [count, setCount] = useState(0); | ||
const [contentHeight, setContentHeight] = useState(0); | ||
|
||
// hooks | ||
const bottomSheetRef = useRef<BottomSheet>(null); | ||
const { bottom: safeBottomArea } = useSafeArea(); | ||
|
||
// variables | ||
const snapPoints = useMemo(() => [0, contentHeight], [contentHeight]); | ||
|
||
// callbacks | ||
const handleIncreaseContentPress = useCallback(() => { | ||
setCount(state => state + 1); | ||
}, []); | ||
const handleDecreaseContentPress = useCallback(() => { | ||
setCount(state => Math.max(state - 1, 0)); | ||
}, []); | ||
const handleExpandPress = useCallback(() => { | ||
bottomSheetRef.current?.expand(); | ||
}, []); | ||
const handleClosePress = useCallback(() => { | ||
bottomSheetRef.current?.close(); | ||
}, []); | ||
const handleOnLayout = useCallback( | ||
({ | ||
nativeEvent: { | ||
layout: { height }, | ||
}, | ||
}) => { | ||
// console.log('SCREEN \t\t', 'handleOnLayout', height); | ||
setContentHeight(height); | ||
}, | ||
[] | ||
); | ||
|
||
// styles | ||
const contentContainerStyle = useMemo( | ||
() => ({ | ||
...styles.contentContainerStyle, | ||
paddingBottom: safeBottomArea, | ||
}), | ||
[safeBottomArea] | ||
); | ||
const emojiContainerStyle = useMemo( | ||
() => ({ | ||
...styles.emojiContainer, | ||
height: 50 * count, | ||
}), | ||
[count] | ||
); | ||
|
||
// renders | ||
const renderBackground = useCallback( | ||
() => <View style={styles.background} />, | ||
[] | ||
); | ||
|
||
// console.log('SCREEN \t\t', 'render \t'); | ||
return ( | ||
<View style={styles.container}> | ||
<Button | ||
label="Expand" | ||
style={styles.buttonContainer} | ||
onPress={handleExpandPress} | ||
/> | ||
<Button | ||
label="Close" | ||
style={styles.buttonContainer} | ||
onPress={handleClosePress} | ||
/> | ||
<BottomSheet | ||
ref={bottomSheetRef} | ||
snapPoints={snapPoints} | ||
initialSnapIndex={1} | ||
animateOnMount={true} | ||
animationEasing={Easing.out(Easing.quad)} | ||
animationDuration={250} | ||
backgroundComponent={renderBackground} | ||
> | ||
<BottomSheetView | ||
style={contentContainerStyle} | ||
onLayout={handleOnLayout} | ||
> | ||
<Text style={styles.message}> | ||
Could this sheet resize to its content height ? | ||
</Text> | ||
<View style={emojiContainerStyle}> | ||
<Text style={styles.emoji}>😍</Text> | ||
</View> | ||
<Button | ||
label="Yes" | ||
style={styles.buttonContainer} | ||
onPress={handleIncreaseContentPress} | ||
/> | ||
<Button | ||
label="Mayby" | ||
style={styles.buttonContainer} | ||
onPress={handleDecreaseContentPress} | ||
/> | ||
</BottomSheetView> | ||
</BottomSheet> | ||
</View> | ||
); | ||
}; | ||
|
||
const styles = StyleSheet.create({ | ||
container: { | ||
flex: 1, | ||
padding: 24, | ||
}, | ||
background: { | ||
...StyleSheet.absoluteFillObject, | ||
backgroundColor: 'white', | ||
}, | ||
buttonContainer: { | ||
marginBottom: 6, | ||
}, | ||
contentContainerStyle: { | ||
paddingTop: 12, | ||
paddingHorizontal: 24, | ||
backgroundColor: 'white', | ||
}, | ||
message: { | ||
fontSize: 24, | ||
fontWeight: '600', | ||
marginBottom: 12, | ||
}, | ||
emoji: { | ||
fontSize: 156, | ||
textAlign: 'center', | ||
alignSelf: 'center', | ||
}, | ||
emojiContainer: { | ||
justifyContent: 'center', | ||
}, | ||
}); | ||
|
||
export default DynamicSnapPointExample; |
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
Oops, something went wrong.