Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow dynamic snap points #81

Merged
merged 12 commits into from
Nov 27, 2020
Prev Previous commit
chore: added dynamic snap point example
  • Loading branch information
gorhom committed Nov 22, 2020
commit 57ce7598ff7d690add13b453bcdf622c20a64ee6
9 changes: 9 additions & 0 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,15 @@ const App = () => {
require('./screens/advanced/MapExample').default
}
/>
<Stack.Screen
name="Advanced/DynamicSnapPointExample"
options={{
title: 'Dynamic Snap Point',
}}
getComponent={() =>
require('./screens/advanced/DynamicSnapPointExample').default
}
/>
</Stack.Navigator>
</NavigationContainer>
</AppearanceProvider>
Expand Down
4 changes: 4 additions & 0 deletions example/src/screens/Root.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ const data = [
name: 'Map',
slug: 'Advanced/MapExample',
},
{
name: 'Dynamic Snap Point',
slug: 'Advanced/DynamicSnapPointExample',
},
],
},
].reverse();
Expand Down
147 changes: 147 additions & 0 deletions example/src/screens/advanced/DynamicSnapPointExample.tsx
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;
1 change: 1 addition & 0 deletions example/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ export type AppStackParamsList = {
['Advanced/CustomHandleExample']: undefined;
['Advanced/OverlayExample']: undefined;
['Advanced/MapExample']: undefined;
['Advanced/DynamicSnapPointExample']: undefined;
};