-
-
Notifications
You must be signed in to change notification settings - Fork 800
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: added BottomSheetBackdrop * chore: updated BottomSheet implementation to handle backdrops * chore: updated examples
- Loading branch information
Showing
16 changed files
with
281 additions
and
43 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
126 changes: 126 additions & 0 deletions
126
src/components/bottomSheetBackdrop/BottomSheetBackdrop.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,126 @@ | ||
import React, { memo, useCallback, useMemo, useRef } from 'react'; | ||
import { TouchableWithoutFeedback } from 'react-native'; | ||
import Animated, { | ||
interpolate, | ||
Extrapolate, | ||
useAnimatedStyle, | ||
useAnimatedProps, | ||
useSharedValue, | ||
useAnimatedReaction, | ||
} from 'react-native-reanimated'; | ||
import isEqual from 'lodash.isequal'; | ||
import { useBottomSheet } from '../../hooks/useBottomSheet'; | ||
import { | ||
DEFAULT_OPACITY, | ||
DEFAULT_APPEARS_ON_INDEX, | ||
DEFAULT_DISAPPEARS_ON_INDEX, | ||
DEFAULT_ENABLE_TOUCH_THROUGH, | ||
DEFAULT_CLOSE_ON_PRESS, | ||
} from './constants'; | ||
import { WINDOW_HEIGHT } from '../../constants'; | ||
import type { BottomSheetDefaultBackdropProps } from './types'; | ||
import { styles } from './styles'; | ||
|
||
const AnimatedTouchableWithoutFeedback = Animated.createAnimatedComponent( | ||
TouchableWithoutFeedback | ||
); | ||
|
||
const BottomSheetBackdropComponent = ({ | ||
animatedIndex, | ||
opacity = DEFAULT_OPACITY, | ||
appearsOnIndex = DEFAULT_APPEARS_ON_INDEX, | ||
disappearsOnIndex = DEFAULT_DISAPPEARS_ON_INDEX, | ||
enableTouchThrough = DEFAULT_ENABLE_TOUCH_THROUGH, | ||
closeOnPress = DEFAULT_CLOSE_ON_PRESS, | ||
style, | ||
}: BottomSheetDefaultBackdropProps) => { | ||
//#region hooks | ||
const { close } = useBottomSheet(); | ||
//#endregion | ||
|
||
//#region variables | ||
const containerRef = useRef<Animated.View>(null); | ||
const pointerEvents = useMemo(() => (enableTouchThrough ? 'none' : 'auto'), [ | ||
enableTouchThrough, | ||
]); | ||
//#endregion | ||
|
||
//#region callbacks | ||
const handleOnPress = useCallback(() => { | ||
close(); | ||
}, [close]); | ||
//#endregion | ||
|
||
//#region animated props | ||
const isContainerTouchable = useSharedValue<boolean>(closeOnPress, true); | ||
const containerAnimatedProps = useAnimatedProps(() => ({ | ||
pointerEvents: isContainerTouchable.value ? 'auto' : 'none', | ||
})); | ||
//#endregion | ||
|
||
//#region styles | ||
const buttonAnimatedStyle = useAnimatedStyle( | ||
() => ({ | ||
top: animatedIndex.value === disappearsOnIndex ? WINDOW_HEIGHT : 0, | ||
}), | ||
[disappearsOnIndex] | ||
); | ||
const buttonStyle = useMemo(() => [style, buttonAnimatedStyle], [ | ||
style, | ||
buttonAnimatedStyle, | ||
]); | ||
const containerAnimatedStyle = useAnimatedStyle( | ||
() => ({ | ||
opacity: interpolate( | ||
animatedIndex.value, | ||
[disappearsOnIndex, appearsOnIndex], | ||
[0, opacity], | ||
Extrapolate.CLAMP | ||
), | ||
}), | ||
[] | ||
); | ||
const containerStyle = useMemo( | ||
() => [style, styles.container, containerAnimatedStyle], | ||
[style, containerAnimatedStyle] | ||
); | ||
//#endregion | ||
|
||
//#region effects | ||
useAnimatedReaction( | ||
() => animatedIndex.value === disappearsOnIndex, | ||
shouldDisableTouchability => { | ||
if (shouldDisableTouchability) { | ||
isContainerTouchable.value = false; | ||
} else { | ||
isContainerTouchable.value = true; | ||
} | ||
}, | ||
[disappearsOnIndex] | ||
); | ||
//#endregion | ||
|
||
return closeOnPress ? ( | ||
<AnimatedTouchableWithoutFeedback | ||
accessible={true} | ||
accessibilityRole="button" | ||
accessibilityLabel="Bottom Sheet backdrop" | ||
accessibilityHint="Tap to close the Bottom Sheet" | ||
onPress={handleOnPress} | ||
style={buttonStyle} | ||
> | ||
<Animated.View | ||
ref={containerRef} | ||
style={containerStyle} | ||
// @ts-ignore | ||
animatedProps={containerAnimatedProps} | ||
/> | ||
</AnimatedTouchableWithoutFeedback> | ||
) : ( | ||
<Animated.View pointerEvents={pointerEvents} style={containerStyle} /> | ||
); | ||
}; | ||
|
||
const BottomSheetBackdrop = memo(BottomSheetBackdropComponent, isEqual); | ||
|
||
export default BottomSheetBackdrop; |
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,13 @@ | ||
const DEFAULT_OPACITY = 0.5; | ||
const DEFAULT_APPEARS_ON_INDEX = 1; | ||
const DEFAULT_DISAPPEARS_ON_INDEX = 0; | ||
const DEFAULT_ENABLE_TOUCH_THROUGH = false; | ||
const DEFAULT_CLOSE_ON_PRESS = true; | ||
|
||
export { | ||
DEFAULT_OPACITY, | ||
DEFAULT_APPEARS_ON_INDEX, | ||
DEFAULT_DISAPPEARS_ON_INDEX, | ||
DEFAULT_ENABLE_TOUCH_THROUGH, | ||
DEFAULT_CLOSE_ON_PRESS, | ||
}; |
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,2 @@ | ||
export { default } from './BottomSheetBackdrop'; | ||
export type { BottomSheetBackdropProps } from './types'; |
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,7 @@ | ||
import { StyleSheet } from 'react-native'; | ||
|
||
export const styles = StyleSheet.create({ | ||
container: { | ||
backgroundColor: 'black', | ||
}, | ||
}); |
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,49 @@ | ||
import type { ViewProps } from 'react-native'; | ||
import type Animated from 'react-native-reanimated'; | ||
|
||
export interface BottomSheetBackdropProps extends Pick<ViewProps, 'style'> { | ||
/** | ||
* Current sheet position index. | ||
* @type Animated.SharedValue<number> | ||
*/ | ||
animatedIndex: Animated.SharedValue<number>; | ||
/** | ||
* Current sheet position. | ||
* @type Animated.SharedValue<number> | ||
*/ | ||
animatedPosition: Animated.SharedValue<number>; | ||
} | ||
|
||
export interface BottomSheetDefaultBackdropProps | ||
extends BottomSheetBackdropProps { | ||
/** | ||
* Backdrop opacity. | ||
* @type number | ||
* @default 0.5 | ||
*/ | ||
opacity?: number; | ||
/** | ||
* Snap point index when backdrop will appears on. | ||
* @type number | ||
* @default 1 | ||
*/ | ||
appearsOnIndex?: number; | ||
/** | ||
* Snap point index when backdrop will disappears on. | ||
* @type number | ||
* @default 0 | ||
*/ | ||
disappearsOnIndex?: number; | ||
/** | ||
* Enable touch through backdrop component. | ||
* @type boolean | ||
* @default false | ||
*/ | ||
enableTouchThrough?: boolean; | ||
/** | ||
* Close sheet when user press on backdrop. | ||
* @type boolean | ||
* @default true | ||
*/ | ||
closeOnPress?: boolean; | ||
} |
Oops, something went wrong.