From 069fca0473f4f801a71c71c5026a56dda587769a Mon Sep 17 00:00:00 2001 From: Mo Gorhom Date: Tue, 11 Aug 2020 16:06:15 +0200 Subject: [PATCH] chore: added useBottomSheet hook & backgroundComponent --- src/components/bottomSheet/BottomSheet.tsx | 52 ++++++++++++++-------- src/components/bottomSheet/styles.ts | 1 - src/components/bottomSheet/types.d.ts | 6 +++ src/context.ts | 10 +++++ src/hooks.ts | 6 ++- src/index.ts | 2 + 6 files changed, 57 insertions(+), 20 deletions(-) diff --git a/src/components/bottomSheet/BottomSheet.tsx b/src/components/bottomSheet/BottomSheet.tsx index 21bbb62b0..c8615fe9a 100644 --- a/src/components/bottomSheet/BottomSheet.tsx +++ b/src/components/bottomSheet/BottomSheet.tsx @@ -44,7 +44,10 @@ import { useStableCallback, useScrollable, } from '../../utilities'; -import { BottomSheetInternalProvider } from '../../context'; +import { + BottomSheetInternalProvider, + BottomSheetProvider, +} from '../../context'; import { DEFAULT_ANIMATION_EASING, DEFAULT_ANIMATION_DURATION, @@ -79,6 +82,7 @@ const BottomSheetComponent = forwardRef( onChange: _onChange, // components handleComponent: HandleComponent = Handle, + backgroundComponent: BackgroundComponent = null, children, }, ref @@ -245,6 +249,13 @@ const BottomSheetComponent = forwardRef( // eslint-disable-next-line react-hooks/exhaustive-deps [] ); + const contextVariables = useMemo( + () => ({ + snapTo: handleOnSnapTo, + close: handleClose, + }), + [handleOnSnapTo, handleClose] + ); //#endregion //#region effects @@ -309,24 +320,29 @@ const BottomSheetComponent = forwardRef( {...tapGestureHandler} > - - - - - + {BackgroundComponent && ( + + )} + + + + + + - - - {children} - - + + + {children} + + + diff --git a/src/components/bottomSheet/styles.ts b/src/components/bottomSheet/styles.ts index 08def398d..4c1601094 100644 --- a/src/components/bottomSheet/styles.ts +++ b/src/components/bottomSheet/styles.ts @@ -9,7 +9,6 @@ export const styles = StyleSheet.create({ }, contentContainer: { flex: 1, - backgroundColor: 'black', }, debug: { position: 'absolute', diff --git a/src/components/bottomSheet/types.d.ts b/src/components/bottomSheet/types.d.ts index e8b7a7972..a80cf46ca 100644 --- a/src/components/bottomSheet/types.d.ts +++ b/src/components/bottomSheet/types.d.ts @@ -1,6 +1,7 @@ import type React from 'react'; import type Animated from 'react-native-reanimated'; import type { BottomSheetHandleProps } from '../handle'; +import type { ViewProps } from 'react-native'; export interface BottomSheetProps extends BottomSheetAnimationConfigs { /** @@ -38,6 +39,11 @@ export interface BottomSheetProps extends BottomSheetAnimationConfigs { * @type React.FC\ */ handleComponent?: React.FC; + /** + * Component to be placed as a background. + * @type React.FC + */ + backgroundComponent?: React.FC; /** * Callback when sheet position changed to a provided point. * @type (index: number) => void; diff --git a/src/context.ts b/src/context.ts index 6dd5235ac..df6ed191b 100644 --- a/src/context.ts +++ b/src/context.ts @@ -21,3 +21,13 @@ export const BottomSheetInternalContext = createContext< >(); export const BottomSheetInternalProvider = BottomSheetInternalContext.Provider; + +export interface BottomSheetContextType { + snapTo: (index: number) => void; + close: () => void; +} + +// @ts-ignore +export const BottomSheetContext = createContext(); + +export const BottomSheetProvider = BottomSheetContext.Provider; diff --git a/src/hooks.ts b/src/hooks.ts index 794f092bc..187d4ae0c 100644 --- a/src/hooks.ts +++ b/src/hooks.ts @@ -1,6 +1,10 @@ import { useContext } from 'react'; -import { BottomSheetInternalContext } from './context'; +import { BottomSheetInternalContext, BottomSheetContext } from './context'; export const useBottomSheetInternal = () => { return useContext(BottomSheetInternalContext); }; + +export const useBottomSheet = () => { + return useContext(BottomSheetContext); +}; diff --git a/src/index.ts b/src/index.ts index a7aaf3c70..3c6ddf7e5 100644 --- a/src/index.ts +++ b/src/index.ts @@ -13,3 +13,5 @@ export const { } = BottomSheetTouchable; export type { BottomSheetHandleProps } from './components/handle'; + +export { useBottomSheet } from './hooks';