-
-
Notifications
You must be signed in to change notification settings - Fork 720
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: added BottomSheetFooter * fix: added bottom inset to slide behavior * chore: added footer example
- Loading branch information
Showing
13 changed files
with
352 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import React, { useCallback, useMemo, useRef, useState } from 'react'; | ||
import { View, StyleSheet, Text } from 'react-native'; | ||
import BottomSheet, { BottomSheetFooter } from '@gorhom/bottom-sheet'; | ||
import SearchHandle from '../../components/searchHandle'; | ||
import Button from '../../components/button'; | ||
import ContactList from '../../components/contactList'; | ||
import { useSafeAreaInsets } from 'react-native-safe-area-context'; | ||
|
||
const FooterExample = () => { | ||
// state | ||
const [fadeBehavior, setFadeBehavior] = useState<'none' | 'fade'>('none'); | ||
const [slideBehavior, setSlideBehavior] = useState<'none' | 'slide'>('none'); | ||
const [scaleBehavior, setScaleBehavior] = useState<'none' | 'scale'>('none'); | ||
|
||
// hooks | ||
const bottomSheetRef = useRef<BottomSheet>(null); | ||
const { bottom: bottomSafeArea } = useSafeAreaInsets(); | ||
|
||
// variables | ||
const snapPoints = useMemo(() => [80, 250], []); | ||
const appearanceBehavior = useMemo( | ||
() => [fadeBehavior, slideBehavior, scaleBehavior], | ||
[fadeBehavior, slideBehavior, scaleBehavior] | ||
); | ||
|
||
// callbacks | ||
const handleFadeBehavior = useCallback(() => { | ||
setFadeBehavior(state => (state === 'none' ? 'fade' : 'none')); | ||
}, []); | ||
const handleScaleBehavior = useCallback(() => { | ||
setScaleBehavior(state => (state === 'none' ? 'scale' : 'none')); | ||
}, []); | ||
const handleSlideBehavior = useCallback(() => { | ||
setSlideBehavior(state => (state === 'none' ? 'slide' : 'none')); | ||
}, []); | ||
const handleExpandPress = useCallback(() => { | ||
bottomSheetRef.current?.expand(); | ||
}, []); | ||
const handleCollapsePress = useCallback(() => { | ||
bottomSheetRef.current?.collapse(); | ||
}, []); | ||
const handleClosePress = useCallback(() => { | ||
bottomSheetRef.current?.close(); | ||
}, []); | ||
|
||
// renders | ||
return ( | ||
<View style={styles.container}> | ||
<Button | ||
label={`Toggle Fade Behavior: ${fadeBehavior}`} | ||
onPress={handleFadeBehavior} | ||
/> | ||
<Button | ||
label={`Toggle Scale Behavior: ${scaleBehavior}`} | ||
onPress={handleScaleBehavior} | ||
/> | ||
<Button | ||
label={`Toggle Slide Behavior: ${slideBehavior}`} | ||
onPress={handleSlideBehavior} | ||
/> | ||
<Button label="Expand" onPress={handleExpandPress} /> | ||
<Button label="Collapse" onPress={handleCollapsePress} /> | ||
<Button label="Close" onPress={handleClosePress} /> | ||
<BottomSheet | ||
ref={bottomSheetRef} | ||
snapPoints={snapPoints} | ||
keyboardBehavior="interactive" | ||
keyboardBlurBehavior="restore" | ||
handleComponent={SearchHandle} | ||
> | ||
<ContactList count={10} type="FlatList" /> | ||
<BottomSheetFooter | ||
bottomInset={bottomSafeArea} | ||
appearanceBehavior={appearanceBehavior} | ||
> | ||
<View style={styles.footer}> | ||
<Text style={styles.footerText}>this is a footer!</Text> | ||
</View> | ||
</BottomSheetFooter> | ||
</BottomSheet> | ||
</View> | ||
); | ||
}; | ||
|
||
const styles = StyleSheet.create({ | ||
container: { | ||
flex: 1, | ||
padding: 24, | ||
}, | ||
footer: { | ||
justifyContent: 'center', | ||
alignItems: 'center', | ||
marginHorizontal: 12, | ||
padding: 12, | ||
marginBottom: 12, | ||
borderRadius: 24, | ||
backgroundColor: '#80f', | ||
}, | ||
footerText: { | ||
fontSize: 16, | ||
fontWeight: '600', | ||
color: '#fff', | ||
}, | ||
}); | ||
|
||
export default FooterExample; |
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,130 @@ | ||
import React, { memo, useCallback, useMemo } from 'react'; | ||
import { LayoutChangeEvent, StyleSheet } from 'react-native'; | ||
import Animated, { | ||
Extrapolate, | ||
interpolate, | ||
useAnimatedStyle, | ||
} from 'react-native-reanimated'; | ||
import { KEYBOARD_STATE } from '../../constants'; | ||
import { useBottomSheetInternal } from '../../hooks'; | ||
import { APPEARANCE_BEHAVIOR } from './constants'; | ||
import type { BottomSheetFooterProps } from './types'; | ||
|
||
function BottomSheetFooterComponent({ | ||
children, | ||
appearanceBehavior = APPEARANCE_BEHAVIOR.fade, | ||
bottomInset = 0, | ||
}: BottomSheetFooterProps) { | ||
//#region hooks | ||
const { | ||
animatedContainerHeight, | ||
animatedHandleHeight, | ||
animatedFooterHeight, | ||
animatedPosition, | ||
animatedKeyboardState, | ||
getKeyboardHeightInContainer, | ||
} = useBottomSheetInternal(); | ||
//#endregion | ||
|
||
//#region styles | ||
const containerAnimatedStyle = useAnimatedStyle(() => { | ||
const keyboardHeight = getKeyboardHeightInContainer(); | ||
let footerTranslateY = Math.max( | ||
0, | ||
animatedContainerHeight.value - animatedPosition.value | ||
); | ||
|
||
if (animatedKeyboardState.value === KEYBOARD_STATE.SHOWN) { | ||
footerTranslateY = footerTranslateY - keyboardHeight; | ||
} else { | ||
footerTranslateY = footerTranslateY - bottomInset; | ||
} | ||
|
||
footerTranslateY = | ||
footerTranslateY - | ||
animatedFooterHeight.value - | ||
animatedHandleHeight.value; | ||
|
||
const style: any = { | ||
transform: [ | ||
{ | ||
translateY: footerTranslateY, | ||
}, | ||
], | ||
}; | ||
|
||
// merge appearance behavior styles | ||
(typeof appearanceBehavior === 'string' | ||
? [appearanceBehavior] | ||
: appearanceBehavior | ||
).map(behavior => { | ||
if (behavior === APPEARANCE_BEHAVIOR.fade) { | ||
style.opacity = interpolate( | ||
footerTranslateY, | ||
[5, 0], | ||
[1, 0], | ||
Extrapolate.CLAMP | ||
); | ||
} else if (behavior === APPEARANCE_BEHAVIOR.scale) { | ||
style.transform.push({ | ||
scale: interpolate( | ||
footerTranslateY, | ||
[5, 0], | ||
[1, 0], | ||
Extrapolate.CLAMP | ||
), | ||
}); | ||
} else if (behavior === APPEARANCE_BEHAVIOR.slide) { | ||
style.transform.push({ | ||
translateY: interpolate( | ||
footerTranslateY, | ||
[5, 0], | ||
[0, animatedFooterHeight.value + bottomInset], | ||
Extrapolate.CLAMP | ||
), | ||
}); | ||
} | ||
}); | ||
|
||
return style; | ||
}, [appearanceBehavior, bottomInset]); | ||
const containerStyle = useMemo( | ||
() => [styles.container, containerAnimatedStyle], | ||
[containerAnimatedStyle] | ||
); | ||
//#endregion | ||
|
||
//#region callbacks | ||
const handleContainerLayout = useCallback( | ||
({ | ||
nativeEvent: { | ||
layout: { height }, | ||
}, | ||
}: LayoutChangeEvent) => { | ||
animatedFooterHeight.value = height; | ||
}, | ||
[animatedFooterHeight] | ||
); | ||
//#endregion | ||
|
||
return children !== null ? ( | ||
<Animated.View onLayout={handleContainerLayout} style={containerStyle}> | ||
{typeof children === 'function' ? children() : children} | ||
</Animated.View> | ||
) : null; | ||
} | ||
|
||
const styles = StyleSheet.create({ | ||
container: { | ||
position: 'absolute', | ||
top: 0, | ||
left: 0, | ||
right: 0, | ||
zIndex: 9999, | ||
}, | ||
}); | ||
|
||
const BottomSheetFooter = memo(BottomSheetFooterComponent); | ||
BottomSheetFooter.displayName = 'BottomSheetFooter'; | ||
|
||
export default BottomSheetFooter; |
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,8 @@ | ||
enum APPEARANCE_BEHAVIOR { | ||
none = 'none', | ||
fade = 'fade', | ||
scale = 'scale', | ||
slide = 'slide', | ||
} | ||
|
||
export { APPEARANCE_BEHAVIOR }; |
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 @@ | ||
export { default } from './BottomSheetFooter'; |
Oops, something went wrong.