Skip to content

Commit

Permalink
feat: added enabled prop (#35)
Browse files Browse the repository at this point in the history
* chore: added enabled prop implementation

* chore: updated examples

* docs: updated readme file to include enabled prop
  • Loading branch information
gorhom committed Sep 24, 2020
1 parent fea90a9 commit 7e14c2b
Show file tree
Hide file tree
Showing 12 changed files with 106 additions and 20 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,12 @@ Top inset value helps to calculate percentage snap points values. usually comes

> `required:` NO | `type:` number | `default:` 0
#### `enabled`

To enable or disable user interaction with the sheet.

> `required:` NO | `type:` boolean | `default:` true
#### `animationDuration`

Snapping animation duration.
Expand Down
12 changes: 11 additions & 1 deletion example/src/screens/BasicExample.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useMemo, useRef } from 'react';
import React, { useCallback, useMemo, useRef, useState } from 'react';
import { View, StyleSheet } from 'react-native';
import { useHeaderHeight } from '@react-navigation/stack';
import Animated, {
Expand All @@ -13,6 +13,9 @@ import Button from '../components/button';
import { ReText } from 'react-native-redash';

const BasicExample = () => {
// state
const [enabled, setEnabled] = useState(true);

// hooks
const bottomSheetRef = useRef<BottomSheet>(null);
const headerHeight = useHeaderHeight();
Expand Down Expand Up @@ -78,10 +81,17 @@ const BasicExample = () => {
style={styles.buttonContainer}
onPress={() => handleClosePress()}
/>

<Button
label={`${enabled ? 'Disable' : 'Enable'}`}
style={styles.buttonContainer}
onPress={() => setEnabled(state => !state)}
/>
<ReText text={concat('Position from bottom: ', position)} />
<Animated.View pointerEvents="none" style={shadowOverlayStyle} />
<BottomSheet
ref={bottomSheetRef}
enabled={enabled}
snapPoints={snapPoints}
initialSnapIndex={1}
handleComponent={Handle}
Expand Down
23 changes: 19 additions & 4 deletions example/src/screens/BasicExamples.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, memo, useRef, useMemo } from 'react';
import React, { useCallback, memo, useRef, useMemo, useState } from 'react';
import { StyleSheet, View } from 'react-native';
import BottomSheet from '@gorhom/bottom-sheet';
import ContactList from '../components/contactList';
Expand All @@ -13,12 +13,18 @@ interface ExampleScreenProps {

const createExampleScreen = ({ type, count = 50 }: ExampleScreenProps) =>
memo(() => {
// state
const [enabled, setEnabled] = useState(true);

// hooks
const bottomSheetRef = useRef<BottomSheet>(null);
const headerHeight = useHeaderHeight();

// variables
const snapPoints = useMemo(() => ['25%', '50%', '90%'], []);
const enableButtonText = useMemo(() => (enabled ? 'Disable' : 'Enable'), [
enabled,
]);

// callbacks
const handleSheetChange = useCallback(index => {
Expand All @@ -36,6 +42,9 @@ const createExampleScreen = ({ type, count = 50 }: ExampleScreenProps) =>
const handleClosePress = useCallback(() => {
bottomSheetRef.current?.close();
}, []);
const handleEnablePress = useCallback(() => {
setEnabled(state => !state);
}, []);

return (
<View style={styles.container}>
Expand All @@ -57,20 +66,26 @@ const createExampleScreen = ({ type, count = 50 }: ExampleScreenProps) =>
<Button
label="Expand"
style={styles.buttonContainer}
onPress={() => handleExpandPress()}
onPress={handleExpandPress}
/>
<Button
label="Collapse"
style={styles.buttonContainer}
onPress={() => handleCollapsePress()}
onPress={handleCollapsePress}
/>
<Button
label="Close"
style={styles.buttonContainer}
onPress={() => handleClosePress()}
onPress={handleClosePress}
/>
<Button
label={enableButtonText}
style={styles.buttonContainer}
onPress={handleEnablePress}
/>
<BottomSheet
ref={bottomSheetRef}
enabled={enabled}
snapPoints={snapPoints}
initialSnapIndex={1}
topInset={headerHeight}
Expand Down
25 changes: 19 additions & 6 deletions example/src/screens/CustomHandleExample.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useMemo, useRef } from 'react';
import React, { useCallback, useMemo, useRef, useState } from 'react';
import { View, StyleSheet, Text } from 'react-native';
import { useHeaderHeight } from '@react-navigation/stack';
import BottomSheet from '@gorhom/bottom-sheet';
Expand All @@ -7,14 +7,18 @@ import Button from '../components/button';
import ContactList from '../components/contactList';

const CustomHandleExample = () => {
// state
const [enabled, setEnabled] = useState(true);

// hooks
const bottomSheetRef = useRef<BottomSheet>(null);
const headerHeight = useHeaderHeight();

// variables
const snapPoints = useMemo(() => [150, 300, 450], []);

// styles
const enableButtonText = useMemo(() => (enabled ? 'Disable' : 'Enable'), [
enabled,
]);

// callbacks
const handleSnapPress = useCallback(index => {
Expand All @@ -29,6 +33,9 @@ const CustomHandleExample = () => {
const handleClosePress = useCallback(() => {
bottomSheetRef.current?.close();
}, []);
const handleEnablePress = useCallback(() => {
setEnabled(state => !state);
}, []);

// renders
const renderHeader = useCallback(() => {
Expand Down Expand Up @@ -59,20 +66,26 @@ const CustomHandleExample = () => {
<Button
label="Expand"
style={styles.buttonContainer}
onPress={() => handleExpandPress()}
onPress={handleExpandPress}
/>
<Button
label="Collapse"
style={styles.buttonContainer}
onPress={() => handleCollapsePress()}
onPress={handleCollapsePress}
/>
<Button
label="Close"
style={styles.buttonContainer}
onPress={() => handleClosePress()}
onPress={handleClosePress}
/>
<Button
label={enableButtonText}
style={styles.buttonContainer}
onPress={handleEnablePress}
/>
<BottomSheet
ref={bottomSheetRef}
enabled={enabled}
snapPoints={snapPoints}
initialSnapIndex={1}
topInset={headerHeight}
Expand Down
23 changes: 19 additions & 4 deletions example/src/screens/NavigatorExample.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useCallback, useMemo, useRef } from 'react';
import React, { useCallback, useMemo, useRef, useState } from 'react';
import { View, StyleSheet } from 'react-native';
import {
useHeaderHeight,
Expand Down Expand Up @@ -70,12 +70,18 @@ const Navigator = () => {
};

const NavigatorExample = () => {
// state
const [enabled, setEnabled] = useState(true);

// hooks
const bottomSheetRef = useRef<BottomSheet>(null);
const headerHeight = useHeaderHeight();

// variables
const snapPoints = useMemo(() => ['25%', '50%', '90%'], []);
const enableButtonText = useMemo(() => (enabled ? 'Disable' : 'Enable'), [
enabled,
]);

// callbacks
const handleSheetChange = useCallback(index => {
Expand All @@ -93,6 +99,9 @@ const NavigatorExample = () => {
const handleClosePress = useCallback(() => {
bottomSheetRef.current?.close();
}, []);
const handleEnablePress = useCallback(() => {
setEnabled(state => !state);
}, []);

// renders
return (
Expand All @@ -115,20 +124,26 @@ const NavigatorExample = () => {
<Button
label="Expand"
style={styles.buttonContainer}
onPress={() => handleExpandPress()}
onPress={handleExpandPress}
/>
<Button
label="Collapse"
style={styles.buttonContainer}
onPress={() => handleCollapsePress()}
onPress={handleCollapsePress}
/>
<Button
label="Close"
style={styles.buttonContainer}
onPress={() => handleClosePress()}
onPress={handleClosePress}
/>
<Button
label={enableButtonText}
style={styles.buttonContainer}
onPress={handleEnablePress}
/>
<BottomSheet
ref={bottomSheetRef}
enabled={enabled}
snapPoints={snapPoints}
initialSnapIndex={1}
topInset={headerHeight}
Expand Down
5 changes: 4 additions & 1 deletion src/components/bottomSheet/BottomSheet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
initialSnapIndex = 0,
snapPoints: _snapPoints,
topInset = 0,
enabled = true,
// animated nodes callback
animatedPosition: _animatedPosition,
animatedPositionIndex: _animatedPositionIndex,
Expand Down Expand Up @@ -300,6 +301,7 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
//#region
const internalContextVariables = useMemo(
() => ({
enabled,
rootTapGestureRef,
handlePanGestureState,
handlePanGestureTranslationY,
Expand All @@ -313,7 +315,7 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
removeScrollableRef,
}),
// eslint-disable-next-line react-hooks/exhaustive-deps
[]
[enabled]
);
const externalContextVariables = useMemo(
() => ({
Expand Down Expand Up @@ -399,6 +401,7 @@ const BottomSheetComponent = forwardRef<BottomSheet, BottomSheetProps>(
)}
<BottomSheetProvider value={externalContextVariables}>
<PanGestureHandler
enabled={enabled}
ref={handlePanGestureRef}
simultaneousHandlers={rootTapGestureRef}
shouldCancelWhenOutside={false}
Expand Down
7 changes: 7 additions & 0 deletions src/components/bottomSheet/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,15 @@ export interface BottomSheetProps extends BottomSheetAnimationConfigs {
/**
* Top inset value helps to calculate percentage snap points values. usually comes from `@react-navigation/stack` hook `useHeaderHeight` or from `react-native-safe-area-context` hook `useSafeArea`.
* @type number
* @default 0
*/
topInset?: number;
/**
* To enable or disable user interaction with the sheet.
* @type boolean
* @default true
*/
enabled?: boolean;
/**
* Animated value to be used as a callback of the position node internally.
* @type Animated.Value<number>
Expand Down
3 changes: 2 additions & 1 deletion src/components/draggableView/DraggableView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const BottomSheetDraggableViewComponent = ({

// hooks
const {
enabled,
rootTapGestureRef,
handlePanGestureState,
handlePanGestureTranslationY,
Expand Down Expand Up @@ -69,10 +70,10 @@ const BottomSheetDraggableViewComponent = ({
);

// effects

return (
<PanGestureHandler
ref={panGestureRef}
enabled={enabled}
simultaneousHandlers={simultaneousHandlers}
shouldCancelWhenOutside={false}
onGestureEvent={handleGestureEvent}
Expand Down
7 changes: 6 additions & 1 deletion src/components/flatList/FlatList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ const BottomSheetFlatListComponent = forwardRef(
handleScrollEvent,
handleSettingScrollable,
} = useScrollableInternal('FlatList');
const { rootTapGestureRef, decelerationRate } = useBottomSheetInternal();
const {
enabled,
rootTapGestureRef,
decelerationRate,
} = useBottomSheetInternal();

// effects
// @ts-ignore
Expand All @@ -59,6 +63,7 @@ const BottomSheetFlatListComponent = forwardRef(
>
<NativeViewGestureHandler
ref={nativeGestureRef}
enabled={enabled}
waitFor={rootTapGestureRef}
>
<AnimatedFlatList
Expand Down
7 changes: 6 additions & 1 deletion src/components/scrollView/ScrollView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ const BottomSheetScrollViewComponent = forwardRef(
handleScrollEvent,
handleSettingScrollable,
} = useScrollableInternal('ScrollView');
const { rootTapGestureRef, decelerationRate } = useBottomSheetInternal();
const {
enabled,
rootTapGestureRef,
decelerationRate,
} = useBottomSheetInternal();

// effects
// @ts-ignore
Expand All @@ -63,6 +67,7 @@ const BottomSheetScrollViewComponent = forwardRef(
>
<NativeViewGestureHandler
ref={nativeGestureRef}
enabled={enabled}
waitFor={rootTapGestureRef}
>
<AnimatedScrollView
Expand Down
7 changes: 6 additions & 1 deletion src/components/sectionList/SectionList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ const BottomSheetSectionListComponent = forwardRef(
handleScrollEvent,
handleSettingScrollable,
} = useScrollableInternal('SectionList');
const { rootTapGestureRef, decelerationRate } = useBottomSheetInternal();
const {
enabled,
rootTapGestureRef,
decelerationRate,
} = useBottomSheetInternal();
// effects
// @ts-ignore
useImperativeHandle(ref, () => scrollableRef.current!.getNode());
Expand All @@ -58,6 +62,7 @@ const BottomSheetSectionListComponent = forwardRef(
>
<NativeViewGestureHandler
ref={nativeGestureRef}
enabled={enabled}
waitFor={rootTapGestureRef}
>
<AnimatedSectionList
Expand Down
1 change: 1 addition & 0 deletions src/contexts/internal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type Animated from 'react-native-reanimated';
import { Scrollable, ScrollableRef } from '../types';

export type BottomSheetInternalContextType = {
enabled: boolean;
rootTapGestureRef: Ref<TapGestureHandler>;
contentPanGestureState: Animated.Value<State>;
contentPanGestureTranslationY: Animated.Value<number>;
Expand Down

0 comments on commit 7e14c2b

Please sign in to comment.