forked from mrzachnugent/react-native-reusables
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbottom-sheet.native.tsx
356 lines (328 loc) · 9.31 KB
/
bottom-sheet.native.tsx
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
import type {
BottomSheetBackdropProps,
BottomSheetFooterProps as GBottomSheetFooterProps,
} from '@gorhom/bottom-sheet';
import {
BottomSheetBackdrop,
BottomSheetModal,
BottomSheetFlatList as GBottomSheetFlatList,
BottomSheetFooter as GBottomSheetFooter,
BottomSheetTextInput as GBottomSheetTextInput,
BottomSheetView as GBottomSheetView,
useBottomSheetModal,
} from '@gorhom/bottom-sheet';
import type { BottomSheetModalMethods } from '@gorhom/bottom-sheet/lib/typescript/types';
import { X } from 'lucide-react-native';
import { useColorScheme } from 'nativewind';
import React, { useCallback, useImperativeHandle } from 'react';
import {
GestureResponderEvent,
Keyboard,
Pressable,
View,
ViewStyle,
} from 'react-native';
import { useSafeAreaInsets } from 'react-native-safe-area-context';
import { Button } from '~/components/ui/button';
import { NAV_THEME } from '~/lib/constants';
import * as Slot from '~/lib/rn-primitives/slot/slot-native';
import { cn } from '~/lib/utils';
type BottomSheetRef = React.ElementRef<typeof View>;
type BottomSheetProps = React.ComponentPropsWithoutRef<typeof View>;
interface BottomSheetContext {
sheetRef: React.RefObject<BottomSheetModal>;
}
const BottomSheetContext = React.createContext({} as BottomSheetContext);
const BottomSheet = React.forwardRef<BottomSheetRef, BottomSheetProps>(
({ ...props }, ref) => {
const sheetRef = React.useRef<BottomSheetModal>(null);
return (
<BottomSheetContext.Provider value={{ sheetRef: sheetRef }}>
<View ref={ref} {...props} />
</BottomSheetContext.Provider>
);
}
);
function useBottomSheetContext() {
const context = React.useContext(BottomSheetContext);
if (!context) {
throw new Error(
'BottomSheet compound components cannot be rendered outside the BottomSheet component'
);
}
return context;
}
const CLOSED_INDEX = -1;
type BottomSheetContentRef = React.ElementRef<typeof BottomSheetModal>;
type BottomSheetContentProps = Omit<
React.ComponentPropsWithoutRef<typeof BottomSheetModal>,
'backdropComponent'
> & {
backdropProps?: Partial<
React.ComponentPropsWithoutRef<typeof BottomSheetBackdrop>
>;
};
const BottomSheetContent = React.forwardRef<
BottomSheetContentRef,
BottomSheetContentProps
>(
(
{
enablePanDownToClose = true,
enableDynamicSizing = true,
index = 0,
backdropProps,
backgroundStyle,
android_keyboardInputMode = 'adjustResize',
...props
},
ref
) => {
const insets = useSafeAreaInsets();
const { colorScheme } = useColorScheme();
const { sheetRef } = useBottomSheetContext();
useImperativeHandle(
ref,
() => {
if (!sheetRef.current) {
return {} as BottomSheetModalMethods;
}
return sheetRef.current;
},
[sheetRef.current]
);
const renderBackdrop = useCallback(
(props: BottomSheetBackdropProps) => {
const {
pressBehavior = 'close',
opacity = colorScheme === 'dark' ? 0.3 : 0.7,
disappearsOnIndex = CLOSED_INDEX,
style,
onPress,
...rest
} = {
...props,
...backdropProps,
};
return (
<BottomSheetBackdrop
opacity={opacity}
disappearsOnIndex={disappearsOnIndex}
pressBehavior={pressBehavior}
style={[{ backgroundColor: NAV_THEME[colorScheme].border }, style]}
onPress={() => {
if (Keyboard.isVisible()) {
Keyboard.dismiss();
}
onPress?.();
}}
{...rest}
/>
);
},
[backdropProps, colorScheme]
);
return (
<BottomSheetModal
ref={sheetRef}
index={0}
enablePanDownToClose={enablePanDownToClose}
backdropComponent={renderBackdrop}
enableDynamicSizing={enableDynamicSizing}
backgroundStyle={[
{ backgroundColor: NAV_THEME[colorScheme].card },
backgroundStyle,
]}
handleIndicatorStyle={{
backgroundColor: NAV_THEME[colorScheme].text,
}}
topInset={insets.top}
android_keyboardInputMode={android_keyboardInputMode}
{...props}
/>
);
}
);
const BottomSheetOpenTrigger = React.forwardRef<
React.ElementRef<typeof Pressable>,
React.ComponentPropsWithoutRef<typeof Pressable> & {
asChild?: boolean;
}
>(({ onPress, asChild = false, ...props }, ref) => {
const { sheetRef } = useBottomSheetContext();
function handleOnPress(ev: GestureResponderEvent) {
sheetRef.current?.present();
onPress?.(ev);
}
const Trigger = asChild ? Slot.Pressable : Pressable;
return <Trigger ref={ref} onPress={handleOnPress} {...props} />;
});
const BottomSheetCloseTrigger = React.forwardRef<
React.ElementRef<typeof Pressable>,
React.ComponentPropsWithoutRef<typeof Pressable> & {
asChild?: boolean;
}
>(({ onPress, asChild = false, ...props }, ref) => {
const { dismiss } = useBottomSheetModal();
function handleOnPress(ev: GestureResponderEvent) {
dismiss();
if (Keyboard.isVisible()) {
Keyboard.dismiss();
}
onPress?.(ev);
}
const Trigger = asChild ? Slot.Pressable : Pressable;
return <Trigger ref={ref} onPress={handleOnPress} {...props} />;
});
const BOTTOM_SHEET_HEADER_HEIGHT = 60; // BottomSheetHeader height
type BottomSheetViewProps = Omit<
React.ComponentPropsWithoutRef<typeof GBottomSheetView>,
'style'
> & {
hadHeader?: boolean;
style?: ViewStyle;
};
function BottomSheetView({
className,
children,
hadHeader = true,
style,
...props
}: BottomSheetViewProps) {
const insets = useSafeAreaInsets();
return (
<GBottomSheetView
style={[
{
paddingBottom:
insets.bottom + (hadHeader ? BOTTOM_SHEET_HEADER_HEIGHT : 0),
},
style,
]}
className={cn(`px-4`, className)}
{...props}
>
{children}
</GBottomSheetView>
);
}
type BottomSheetTextInputRef = React.ElementRef<typeof GBottomSheetTextInput>;
type BottomSheetTextInputProps = React.ComponentPropsWithoutRef<
typeof GBottomSheetTextInput
>;
const BottomSheetTextInput = React.forwardRef<
BottomSheetTextInputRef,
BottomSheetTextInputProps
>(({ className, placeholderClassName, ...props }, ref) => {
return (
<GBottomSheetTextInput
ref={ref}
className={cn(
'rounded-md border border-input bg-background px-3 text-xl h-14 leading-[1.25] text-foreground items-center placeholder:text-muted-foreground disabled:opacity-50',
className
)}
placeholderClassName={cn('text-muted-foreground', placeholderClassName)}
{...props}
/>
);
});
type BottomSheetFlatListRef = React.ElementRef<typeof GBottomSheetFlatList>;
type BottomSheetFlatListProps = React.ComponentPropsWithoutRef<
typeof GBottomSheetFlatList
>;
const BottomSheetFlatList = React.forwardRef<
BottomSheetFlatListRef,
BottomSheetFlatListProps
>(({ className, ...props }, ref) => {
const insets = useSafeAreaInsets();
return (
<GBottomSheetFlatList
ref={ref}
contentContainerStyle={[{ paddingBottom: insets.bottom }]}
className={cn('py-4', className)}
keyboardShouldPersistTaps='handled'
{...props}
/>
);
});
type BottomSheetHeaderRef = React.ElementRef<typeof View>;
type BottomSheetHeaderProps = React.ComponentPropsWithoutRef<typeof View>;
const BottomSheetHeader = React.forwardRef<
BottomSheetHeaderRef,
BottomSheetHeaderProps
>(({ className, children, ...props }, ref) => {
const { dismiss } = useBottomSheetModal();
function close() {
if (Keyboard.isVisible()) {
Keyboard.dismiss();
}
dismiss();
}
return (
<View
ref={ref}
className={cn(
'border-b border-border flex-row items-center justify-between pl-4',
className
)}
{...props}
>
{children}
<Button onPress={close} variant='ghost' className='pr-4'>
<X className='text-muted-foreground' size={24} />
</Button>
</View>
);
});
type BottomSheetFooterRef = React.ElementRef<typeof View>;
type BottomSheetFooterProps = Omit<
React.ComponentPropsWithoutRef<typeof View>,
'style'
> & {
bottomSheetFooterProps: GBottomSheetFooterProps;
children?: React.ReactNode;
style?: ViewStyle;
};
/**
* To be used in a useCallback function as a props to BottomSheetContent
*/
const BottomSheetFooter = React.forwardRef<
BottomSheetFooterRef,
BottomSheetFooterProps
>(({ bottomSheetFooterProps, children, className, style, ...props }, ref) => {
const insets = useSafeAreaInsets();
return (
<GBottomSheetFooter {...bottomSheetFooterProps}>
<View
ref={ref}
style={[{ paddingBottom: insets.bottom + 6 }, style]}
className={cn('px-4 pt-1.5', className)}
{...props}
>
{children}
</View>
</GBottomSheetFooter>
);
});
function useBottomSheet() {
const ref = React.useRef<BottomSheetContentRef>(null);
const open = useCallback(() => {
ref.current?.present();
}, []);
const close = useCallback(() => {
ref.current?.dismiss();
}, []);
return { ref, open, close };
}
export {
BottomSheet,
BottomSheetCloseTrigger,
BottomSheetContent,
BottomSheetFlatList,
BottomSheetFooter,
BottomSheetHeader,
BottomSheetOpenTrigger,
BottomSheetTextInput,
BottomSheetView,
useBottomSheet,
};