Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 2x 1x 1x 1x 1x 1x 1x 1x 1x 5x 5x 5x 5x 5x 5x 5x 5x 1x 5x 5x 3x 3x 3x 2x 2x 5x 2x 3x 1x | import { useCallback, useEffect, useMemo, useRef, ReactNode } from "react";
import BottomSheet, {
BottomSheetModal,
BottomSheetView,
} from "@gorhom/bottom-sheet";
import { StyleSheet, TouchableOpacity, View, ViewStyle } from "react-native";
interface BottomDrawerProps {
readonly visible: boolean;
readonly onClose?: () => void;
readonly children: ReactNode;
readonly snapPoints?: string[] | number[];
readonly initialSnapIndex?: number;
readonly backgroundColor?: string;
readonly handleColor?: string;
readonly enablePanDownToClose?: boolean;
readonly enableDynamicSizing?: boolean;
readonly contentContainerStyle?: ViewStyle;
readonly handleMode?: "dismiss" | "toggle";
readonly onPressAction?: () => void;
readonly onSnapIndexChange?: (index: number) => void;
readonly useModal?: boolean;
}
interface CustomHandleProps {
readonly onPress: () => void;
readonly backgroundColor: string;
readonly handleColor: string;
}
function CustomHandle({
onPress,
backgroundColor,
handleColor,
}: CustomHandleProps) {
return (
<TouchableOpacity
onPress={onPress}
activeOpacity={0.7}
style={[styles.handleWrapper, { backgroundColor }]}
>
<View
style={[styles.handleIndicator, { backgroundColor: handleColor }]}
/>
</TouchableOpacity>
);
}
function createHandleComponent(props: CustomHandleProps) {
return function HandleComponent() {
return (
<CustomHandle
onPress={props.onPress}
backgroundColor={props.backgroundColor}
handleColor={props.handleColor}
/>
);
};
}
export default function BottomDrawer({
visible,
onClose,
children,
snapPoints = ["10%", "50%", "75%"],
initialSnapIndex = 0,
backgroundColor = "#FFFFFF",
handleColor = "#00000040",
enablePanDownToClose = true,
enableDynamicSizing = false,
contentContainerStyle,
handleMode = "dismiss",
onPressAction,
onSnapIndexChange,
useModal = true,
}: BottomDrawerProps) {
const snapKey = JSON.stringify(snapPoints);
const memoizedSnapPoints = useMemo(() => snapPoints, [snapKey]);
const modalSheetRef = useRef<BottomSheetModal>(null);
const nonModalSheetRef = useRef<BottomSheet>(null);
const currentSnapIndex = useRef(initialSnapIndex);
const onPressActionRef = useRef(onPressAction);
onPressActionRef.current = onPressAction;
const onPress = useCallback(() => {
switch (handleMode) {
case "dismiss":
Iif (useModal) {
modalSheetRef.current?.dismiss();
} else {
nonModalSheetRef.current?.close();
}
break;
case "toggle": {
const nextIndex = currentSnapIndex.current === 0 ? 1 : 0;
if (useModal) {
modalSheetRef.current?.snapToIndex(nextIndex);
} else E{
nonModalSheetRef.current?.snapToIndex(nextIndex);
}
onPressActionRef.current?.();
break;
}
default:
break;
}
}, [handleMode, useModal]);
const handleComponent = useMemo(
() =>
createHandleComponent({
onPress,
backgroundColor,
handleColor,
}),
[onPress, backgroundColor, handleColor],
);
const onSnapIndexChangeRef = useRef(onSnapIndexChange);
onSnapIndexChangeRef.current = onSnapIndexChange;
const onCloseRef = useRef(onClose);
onCloseRef.current = onClose;
const handleChange = useCallback((index: number) => {
currentSnapIndex.current = index;
onSnapIndexChangeRef.current?.(index);
}, []);
const handleClose = useCallback(() => {
onCloseRef.current?.();
}, []);
useEffect(() => {
if (useModal) {
if (visible) {
modalSheetRef.current?.present();
} else E{
modalSheetRef.current?.dismiss();
}
return;
}
if (visible) {
nonModalSheetRef.current?.snapToIndex(initialSnapIndex);
} else E{
nonModalSheetRef.current?.close();
}
}, [visible, useModal, initialSnapIndex]);
if (!useModal) {
return (
<BottomSheet
ref={nonModalSheetRef}
snapPoints={memoizedSnapPoints}
index={visible ? initialSnapIndex : -1}
enableDynamicSizing={enableDynamicSizing}
enablePanDownToClose={enablePanDownToClose}
onClose={handleClose}
onChange={handleChange}
backgroundStyle={[styles.background, { backgroundColor }]}
handleComponent={handleComponent}
>
<BottomSheetView
style={[styles.contentContainer, contentContainerStyle]}
>
{children}
</BottomSheetView>
</BottomSheet>
);
}
return (
<BottomSheetModal
ref={modalSheetRef}
snapPoints={memoizedSnapPoints}
index={initialSnapIndex}
enableDynamicSizing={enableDynamicSizing}
enablePanDownToClose={enablePanDownToClose}
onDismiss={handleClose}
onChange={handleChange}
backgroundStyle={[styles.background, { backgroundColor }]}
handleComponent={handleComponent}
>
<BottomSheetView style={[styles.contentContainer, contentContainerStyle]}>
{children}
</BottomSheetView>
</BottomSheetModal>
);
}
const styles = StyleSheet.create({
background: {
shadowColor: "#000",
shadowOffset: {
width: 0,
height: -5, // Small negative value to show above the sheet
},
shadowOpacity: 0.99,
shadowRadius: 10,
elevation: 10,
},
contentContainer: {
paddingHorizontal: 24,
paddingBottom: 24,
paddingTop: 12,
alignItems: "center",
},
handleWrapper: {
alignItems: "center",
justifyContent: "center",
paddingVertical: 12,
borderTopLeftRadius: 15,
borderTopRightRadius: 15,
},
handleIndicator: {
width: 40,
height: 4,
borderRadius: 2,
},
});
|