react-native-bottom-sheet-stack 1.9.2 → 1.11.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. package/README.md +14 -0
  2. package/lib/commonjs/BottomSheetBackdrop.js +15 -17
  3. package/lib/commonjs/BottomSheetBackdrop.js.map +1 -1
  4. package/lib/commonjs/QueueItem.js +32 -28
  5. package/lib/commonjs/QueueItem.js.map +1 -1
  6. package/lib/commonjs/adapters/actions-sheet/ActionsSheetAdapter.js +17 -9
  7. package/lib/commonjs/adapters/actions-sheet/ActionsSheetAdapter.js.map +1 -1
  8. package/lib/commonjs/adapters/gorhom-sheet/GorhomSheetAdapter.js +12 -9
  9. package/lib/commonjs/adapters/gorhom-sheet/GorhomSheetAdapter.js.map +1 -1
  10. package/lib/commonjs/adapters/react-native-modal/ReactNativeModalAdapter.js +14 -8
  11. package/lib/commonjs/adapters/react-native-modal/ReactNativeModalAdapter.js.map +1 -1
  12. package/lib/commonjs/bottomSheetCoordinator.js +103 -0
  13. package/lib/commonjs/bottomSheetCoordinator.js.map +1 -1
  14. package/lib/commonjs/index.js +38 -0
  15. package/lib/commonjs/index.js.map +1 -1
  16. package/lib/commonjs/onBeforeCloseRegistry.js +42 -0
  17. package/lib/commonjs/onBeforeCloseRegistry.js.map +1 -0
  18. package/lib/commonjs/store/hooks.js +37 -4
  19. package/lib/commonjs/store/hooks.js.map +1 -1
  20. package/lib/commonjs/store/store.js +9 -0
  21. package/lib/commonjs/store/store.js.map +1 -1
  22. package/lib/commonjs/useBottomSheetContext.js +31 -17
  23. package/lib/commonjs/useBottomSheetContext.js.map +1 -1
  24. package/lib/commonjs/useBottomSheetControl.js +47 -33
  25. package/lib/commonjs/useBottomSheetControl.js.map +1 -1
  26. package/lib/commonjs/useBottomSheetManager.js +18 -11
  27. package/lib/commonjs/useBottomSheetManager.js.map +1 -1
  28. package/lib/commonjs/useOnBeforeClose.js +107 -0
  29. package/lib/commonjs/useOnBeforeClose.js.map +1 -0
  30. package/lib/typescript/example/src/screens/HomeScreen.d.ts.map +1 -1
  31. package/lib/typescript/example/src/sheets/CloseInterceptionSheets.d.ts +14 -0
  32. package/lib/typescript/example/src/sheets/CloseInterceptionSheets.d.ts.map +1 -0
  33. package/lib/typescript/example/src/sheets/index.d.ts +1 -0
  34. package/lib/typescript/example/src/sheets/index.d.ts.map +1 -1
  35. package/lib/typescript/src/BottomSheetBackdrop.d.ts.map +1 -1
  36. package/lib/typescript/src/QueueItem.d.ts.map +1 -1
  37. package/lib/typescript/src/adapters/actions-sheet/ActionsSheetAdapter.d.ts.map +1 -1
  38. package/lib/typescript/src/adapters/gorhom-sheet/GorhomSheetAdapter.d.ts.map +1 -1
  39. package/lib/typescript/src/adapters/react-native-modal/ReactNativeModalAdapter.d.ts.map +1 -1
  40. package/lib/typescript/src/bottomSheetCoordinator.d.ts +25 -0
  41. package/lib/typescript/src/bottomSheetCoordinator.d.ts.map +1 -1
  42. package/lib/typescript/src/index.d.ts +6 -2
  43. package/lib/typescript/src/index.d.ts.map +1 -1
  44. package/lib/typescript/src/onBeforeCloseRegistry.d.ts +26 -0
  45. package/lib/typescript/src/onBeforeCloseRegistry.d.ts.map +1 -0
  46. package/lib/typescript/src/store/hooks.d.ts +3 -0
  47. package/lib/typescript/src/store/hooks.d.ts.map +1 -1
  48. package/lib/typescript/src/store/store.d.ts.map +1 -1
  49. package/lib/typescript/src/store/types.d.ts +9 -0
  50. package/lib/typescript/src/store/types.d.ts.map +1 -1
  51. package/lib/typescript/src/useBottomSheetContext.d.ts +5 -0
  52. package/lib/typescript/src/useBottomSheetContext.d.ts.map +1 -1
  53. package/lib/typescript/src/useBottomSheetControl.d.ts +3 -0
  54. package/lib/typescript/src/useBottomSheetControl.d.ts.map +1 -1
  55. package/lib/typescript/src/useBottomSheetManager.d.ts +7 -0
  56. package/lib/typescript/src/useBottomSheetManager.d.ts.map +1 -1
  57. package/lib/typescript/src/useOnBeforeClose.d.ts +65 -0
  58. package/lib/typescript/src/useOnBeforeClose.d.ts.map +1 -0
  59. package/package.json +1 -1
  60. package/src/BottomSheetBackdrop.tsx +2 -3
  61. package/src/QueueItem.tsx +5 -1
  62. package/src/adapters/actions-sheet/ActionsSheetAdapter.tsx +5 -3
  63. package/src/adapters/gorhom-sheet/GorhomSheetAdapter.tsx +3 -1
  64. package/src/adapters/react-native-modal/ReactNativeModalAdapter.tsx +4 -2
  65. package/src/bottomSheetCoordinator.ts +128 -0
  66. package/src/index.tsx +15 -2
  67. package/src/onBeforeCloseRegistry.ts +44 -0
  68. package/src/store/hooks.ts +12 -0
  69. package/src/store/store.ts +11 -0
  70. package/src/store/types.ts +9 -0
  71. package/src/useBottomSheetContext.ts +11 -1
  72. package/src/useBottomSheetControl.ts +13 -8
  73. package/src/useBottomSheetManager.tsx +16 -8
  74. package/src/useOnBeforeClose.ts +92 -0
@@ -1,5 +1,6 @@
1
1
  import { useMaybeBottomSheetContext } from './BottomSheet.context';
2
2
  import { useSheetParams, useStartClosing } from './bottomSheet.store';
3
+ import { requestClose } from './bottomSheetCoordinator';
3
4
  import type {
4
5
  BottomSheetPortalId,
5
6
  BottomSheetPortalParams,
@@ -9,6 +10,11 @@ export interface UseBottomSheetContextReturn<TParams> {
9
10
  id: string;
10
11
  params: TParams;
11
12
  close: () => void;
13
+ /**
14
+ * Close the sheet, bypassing any onBeforeClose interceptor.
15
+ * Useful for force-closing from within onBeforeClose confirmation flows.
16
+ */
17
+ forceClose: () => void;
12
18
  /** @deprecated Use `close` instead */
13
19
  closeBottomSheet: () => void;
14
20
  }
@@ -32,12 +38,16 @@ export function useBottomSheetContext<
32
38
  );
33
39
  }
34
40
 
35
- const close = () => startClosing(context.id);
41
+ const close = () => {
42
+ requestClose(context.id);
43
+ };
44
+ const forceClose = () => startClosing(context.id);
36
45
 
37
46
  return {
38
47
  id: context.id,
39
48
  params: params as BottomSheetPortalParams<T>,
40
49
  close,
50
+ forceClose,
41
51
  closeBottomSheet: close,
42
52
  };
43
53
  }
@@ -1,23 +1,21 @@
1
1
  import React from 'react';
2
2
  import type { SheetAdapterRef } from './adapter.types';
3
3
 
4
- import {
5
- useOpen,
6
- useStartClosing,
7
- useUpdateParams,
8
- type OpenMode,
9
- } from './bottomSheet.store';
4
+ import { useOpen, useUpdateParams, type OpenMode } from './bottomSheet.store';
10
5
  import { useMaybeBottomSheetManagerContext } from './BottomSheetManager.provider';
6
+ import { closeAllAnimated, requestClose } from './bottomSheetCoordinator';
11
7
  import type {
12
8
  BottomSheetPortalId,
13
9
  BottomSheetPortalParams,
14
10
  HasParams,
15
11
  } from './portal.types';
16
12
  import { getSheetRef, setSheetRef } from './refsMap';
13
+ import type { CloseAllOptions } from './useBottomSheetManager';
17
14
 
18
15
  interface BaseOpenOptions<TParams> {
19
16
  mode?: OpenMode;
20
17
  scaleBackground?: boolean;
18
+ backdrop?: boolean;
21
19
  params?: TParams;
22
20
  }
23
21
 
@@ -37,6 +35,7 @@ type OpenFunction<T extends BottomSheetPortalId> =
37
35
  export interface UseBottomSheetControlReturn<T extends BottomSheetPortalId> {
38
36
  open: OpenFunction<T>;
39
37
  close: () => void;
38
+ closeAll: (options?: CloseAllOptions) => Promise<void>;
40
39
  updateParams: (params: BottomSheetPortalParams<T>) => void;
41
40
  resetParams: () => void;
42
41
  }
@@ -47,7 +46,6 @@ export function useBottomSheetControl<T extends BottomSheetPortalId>(
47
46
  const bottomSheetManagerContext = useMaybeBottomSheetManagerContext();
48
47
 
49
48
  const storeOpen = useOpen();
50
- const startClosing = useStartClosing();
51
49
  const storeUpdateParams = useUpdateParams();
52
50
 
53
51
  const open = (options?: OpenOptions<T>) => {
@@ -67,6 +65,7 @@ export function useBottomSheetControl<T extends BottomSheetPortalId>(
67
65
  content: null,
68
66
  usePortal: true,
69
67
  scaleBackground: options?.scaleBackground,
68
+ backdrop: options?.backdrop,
70
69
  params: options?.params as Record<string, unknown>,
71
70
  },
72
71
  options?.mode
@@ -74,7 +73,12 @@ export function useBottomSheetControl<T extends BottomSheetPortalId>(
74
73
  };
75
74
 
76
75
  const close = () => {
77
- startClosing(id);
76
+ requestClose(id);
77
+ };
78
+
79
+ const closeAll = (options?: CloseAllOptions) => {
80
+ const groupId = bottomSheetManagerContext?.groupId || 'default';
81
+ return closeAllAnimated(groupId, options);
78
82
  };
79
83
 
80
84
  const updateParams = (params: BottomSheetPortalParams<T>) => {
@@ -88,6 +92,7 @@ export function useBottomSheetControl<T extends BottomSheetPortalId>(
88
92
  return {
89
93
  open: open as OpenFunction<T>,
90
94
  close,
95
+ closeAll,
91
96
  updateParams,
92
97
  resetParams,
93
98
  };
@@ -1,20 +1,20 @@
1
1
  import React from 'react';
2
2
 
3
- import {
4
- useOpen,
5
- useStartClosing,
6
- useClearGroup,
7
- type OpenMode,
8
- } from './bottomSheet.store';
3
+ import { useOpen, useClearGroup, type OpenMode } from './bottomSheet.store';
9
4
  import { useMaybeBottomSheetManagerContext } from './BottomSheetManager.provider';
10
5
  import type { SheetAdapterRef } from './adapter.types';
6
+ import { closeAllAnimated, requestClose } from './bottomSheetCoordinator';
11
7
  import { setSheetRef } from './refsMap';
12
8
 
9
+ export interface CloseAllOptions {
10
+ /** Delay in ms between each cascading close animation. Default: 100 */
11
+ stagger?: number;
12
+ }
13
+
13
14
  export const useBottomSheetManager = () => {
14
15
  const bottomSheetManagerContext = useMaybeBottomSheetManagerContext();
15
16
 
16
17
  const storeOpen = useOpen();
17
- const startClosing = useStartClosing();
18
18
  const storeClearGroup = useClearGroup();
19
19
 
20
20
  const openBottomSheet = (
@@ -24,6 +24,7 @@ export const useBottomSheetManager = () => {
24
24
  groupId?: string;
25
25
  mode?: OpenMode;
26
26
  scaleBackground?: boolean;
27
+ backdrop?: boolean;
27
28
  } = {}
28
29
  ) => {
29
30
  const groupId =
@@ -44,6 +45,7 @@ export const useBottomSheetManager = () => {
44
45
  groupId,
45
46
  content: contentWithRef,
46
47
  scaleBackground: options.scaleBackground,
48
+ backdrop: options.backdrop,
47
49
  },
48
50
  options.mode
49
51
  );
@@ -52,7 +54,12 @@ export const useBottomSheetManager = () => {
52
54
  };
53
55
 
54
56
  const close = (id: string) => {
55
- startClosing(id);
57
+ requestClose(id);
58
+ };
59
+
60
+ const closeAll = (options?: CloseAllOptions) => {
61
+ const groupId = bottomSheetManagerContext?.groupId || 'default';
62
+ return closeAllAnimated(groupId, options);
56
63
  };
57
64
 
58
65
  const clear = () => {
@@ -63,6 +70,7 @@ export const useBottomSheetManager = () => {
63
70
  return {
64
71
  open: openBottomSheet,
65
72
  close,
73
+ closeAll,
66
74
  clear,
67
75
  /** @deprecated Use `open` instead */
68
76
  openBottomSheet,
@@ -0,0 +1,92 @@
1
+ import { useEffect } from 'react';
2
+
3
+ import { useMaybeBottomSheetContext } from './BottomSheet.context';
4
+ import { useSetPreventDismiss } from './bottomSheet.store';
5
+ import type { OnBeforeCloseCallback } from './onBeforeCloseRegistry';
6
+ import { removeOnBeforeClose, setOnBeforeClose } from './onBeforeCloseRegistry';
7
+ import { useEvent } from './useEvent';
8
+
9
+ /**
10
+ * Registers an interceptor that is called before the sheet closes.
11
+ *
12
+ * When active, this hook:
13
+ * 1. Sets `preventDismiss` on the sheet so adapters block user-initiated
14
+ * gestures (swipe down, pan-to-close) at the native level.
15
+ * 2. Intercepts all programmatic close paths (backdrop tap, back button,
16
+ * `close()`, `closeAll()`) and calls the callback first.
17
+ *
18
+ * The interceptor receives `onConfirm` and `onCancel` callbacks. Call these
19
+ * when the user makes a decision. This works seamlessly with `Alert.alert`:
20
+ *
21
+ * ```tsx
22
+ * useOnBeforeClose(({ onConfirm, onCancel }) => {
23
+ * if (dirty) {
24
+ * Alert.alert('Discard changes?', '', [
25
+ * { text: 'Cancel', onPress: onCancel },
26
+ * { text: 'Discard', onPress: onConfirm },
27
+ * ]);
28
+ * } else {
29
+ * onConfirm(); // Allow close immediately
30
+ * }
31
+ * });
32
+ * ```
33
+ *
34
+ * For backward compatibility, you can still return `boolean` or `Promise<boolean>`:
35
+ * - Return `false` (or resolve to `false`) to prevent closing
36
+ * - Return `true` (or resolve to `true`) to allow closing
37
+ *
38
+ * Use `forceClose()` from `useBottomSheetContext` to bypass the interceptor entirely.
39
+ *
40
+ * Must be used inside a sheet component (within BottomSheetContext).
41
+ *
42
+ * @example Callback pattern (recommended)
43
+ * ```tsx
44
+ * function MySheet() {
45
+ * const [dirty, setDirty] = useState(false);
46
+ *
47
+ * useOnBeforeClose(({ onConfirm, onCancel }) => {
48
+ * if (dirty) {
49
+ * Alert.alert('Discard changes?', '', [
50
+ * { text: 'Cancel', style: 'cancel', onPress: onCancel },
51
+ * { text: 'Discard', onPress: onConfirm },
52
+ * ]);
53
+ * } else {
54
+ * onConfirm();
55
+ * }
56
+ * });
57
+ * }
58
+ * ```
59
+ *
60
+ * @example Boolean return (backward compatible)
61
+ * ```tsx
62
+ * function MySheet() {
63
+ * const [dirty, setDirty] = useState(false);
64
+ *
65
+ * useOnBeforeClose(() => {
66
+ * return !dirty; // false blocks, true allows
67
+ * });
68
+ * }
69
+ * ```
70
+ */
71
+ export function useOnBeforeClose(callback: OnBeforeCloseCallback): void {
72
+ const context = useMaybeBottomSheetContext();
73
+ const setPreventDismiss = useSetPreventDismiss();
74
+
75
+ if (!context?.id) {
76
+ throw new Error(
77
+ 'useOnBeforeClose must be used within a BottomSheet component'
78
+ );
79
+ }
80
+
81
+ const id = context.id;
82
+ const stableCallback = useEvent(callback);
83
+
84
+ useEffect(() => {
85
+ setOnBeforeClose(id, stableCallback);
86
+ setPreventDismiss(id, true);
87
+ return () => {
88
+ removeOnBeforeClose(id);
89
+ setPreventDismiss(id, false);
90
+ };
91
+ }, [id, stableCallback, setPreventDismiss]);
92
+ }