-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
165 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
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,105 @@ | ||
import React, { useCallback, useEffect, useRef } from 'react' | ||
import { StyleSheet, View, TouchableWithoutFeedback, BackHandler, Keyboard } from 'react-native' | ||
import Animated, { Value } from 'react-native-reanimated' | ||
import BottomSheet from 'reanimated-bottom-sheet' | ||
import { useSafeArea } from 'react-native-safe-area-context' | ||
import { useFocusEffect } from '@react-navigation/native' | ||
import { useTheme } from 'react-native-paper' | ||
import { AppTheme } from 'constants/theme' | ||
|
||
import styles from './styles' | ||
import { BottomSheetScreenProps } from './types' | ||
|
||
export const BottomSheetScreen: React.FC<BottomSheetScreenProps> = ({ navigation, route }) => { | ||
const { size = 300, middleSnapPoints = [], onClose, renderContent, renderHandle } = route.params | ||
const allPoints = [size, ...middleSnapPoints, 0] | ||
const safeArea = useSafeArea() | ||
const { colors } = useTheme() as AppTheme | ||
const snapPoints = useRef( | ||
allPoints.map( | ||
(point, index) => index === allPoints.length - 2 && typeof point === 'number' ? point + safeArea.bottom : point, | ||
), | ||
) | ||
const bsRef = useRef<BottomSheet>(null) | ||
const overlayOpacity = useRef(new Value(0)) | ||
|
||
// Animate on open | ||
useEffect(() => { | ||
if (bsRef.current) { | ||
bsRef.current.snapTo(snapPoints.current.length - 2) | ||
} | ||
}, [bsRef, snapPoints]) | ||
|
||
// Animate on close - onCloseEnd will be called after | ||
const onOverlayPress = useCallback(() => { | ||
if (bsRef.current) { | ||
bsRef.current.snapTo(snapPoints.current.length - 1) | ||
} | ||
}, [bsRef, snapPoints]) | ||
|
||
// pop position in the navigation stack so the screen behind become interactable | ||
const handleOnCloseEnd = useCallback(() => { | ||
onClose && onClose() | ||
navigation.goBack() | ||
}, [navigation, onClose]) | ||
|
||
// Dismiss keyboard & andle backpress on android | ||
useFocusEffect( | ||
useCallback(() => { | ||
Keyboard.dismiss() | ||
|
||
const onBackPress = () => { | ||
onOverlayPress() | ||
|
||
return true | ||
} | ||
|
||
BackHandler.addEventListener('hardwareBackPress', onBackPress) | ||
|
||
return () => BackHandler.removeEventListener('hardwareBackPress', onBackPress) | ||
}, [onOverlayPress]), | ||
) | ||
|
||
// header and handle on the top of the sheet | ||
const renderHeader = () => ( | ||
<View style={[styles.header, { backgroundColor: colors.surface }]}> | ||
<View style={styles.panelHeader}> | ||
{renderHandle ? renderHandle() : <View style={styles.panelHandle} />} | ||
</View> | ||
</View> | ||
) | ||
|
||
// sheet dynamic content render | ||
const renderBSContent = () => ( | ||
<View style={[styles.content, { paddingBottom: safeArea.bottom, backgroundColor: colors.surface }]}> | ||
{renderContent(onOverlayPress)} | ||
</View> | ||
) | ||
|
||
// sheet overlay animation | ||
const overlayStyles = { | ||
backgroundColor: '#000000', | ||
opacity: overlayOpacity.current.interpolate({ inputRange: [0, 1], outputRange: [0.5, 0] }), | ||
} | ||
|
||
return ( | ||
<View style={StyleSheet.absoluteFill}> | ||
<TouchableWithoutFeedback onPress={onOverlayPress}> | ||
<Animated.View | ||
style={[StyleSheet.absoluteFill, overlayStyles]} | ||
/> | ||
</TouchableWithoutFeedback> | ||
|
||
<BottomSheet | ||
ref={bsRef} | ||
callbackNode={overlayOpacity.current} | ||
snapPoints={snapPoints.current} | ||
initialSnap={snapPoints.current.length - 1} | ||
renderContent={renderBSContent} | ||
renderHeader={renderHeader} | ||
onCloseEnd={handleOnCloseEnd} | ||
enabledContentGestureInteraction={false} | ||
/> | ||
</View> | ||
) | ||
} |
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,25 @@ | ||
import { StyleSheet } from 'react-native' | ||
|
||
export default StyleSheet.create({ | ||
header: { | ||
shadowColor: '#000000', | ||
paddingTop: 20, | ||
borderTopLeftRadius: 20, | ||
borderTopRightRadius: 20, | ||
}, | ||
panelHeader: { | ||
alignItems: 'center', | ||
}, | ||
panelHandle: { | ||
width: 40, | ||
height: 8, | ||
borderRadius: 4, | ||
backgroundColor: '#00000040', | ||
marginBottom: 10, | ||
}, | ||
content: { | ||
height: '100%', | ||
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,9 @@ | ||
|
||
import { StackNavigationProp } from '@react-navigation/stack' | ||
import { MainNavigatorParamsList } from 'routes/MainNavigator/types' | ||
import { RouteProp } from '@react-navigation/native' | ||
|
||
export interface BottomSheetScreenProps { | ||
navigation: StackNavigationProp<MainNavigatorParamsList, 'BottomSheet'>, | ||
route: RouteProp<MainNavigatorParamsList, 'BottomSheet'>, | ||
} |
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