@niibase/bottom-sheet-manager 1.4.5 → 1.4.6

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.
@@ -16,7 +16,6 @@ import {
16
16
  import { interpolate } from "react-native-reanimated";
17
17
  import { useSafeAreaInsets } from "react-native-safe-area-context";
18
18
 
19
- import { eventManager } from "../events";
20
19
  import { PrivateManager } from "../manager";
21
20
  import {
22
21
  useProviderContext,
@@ -24,6 +23,7 @@ import {
24
23
  useSheetRef,
25
24
  useSheetSharedContext,
26
25
  } from "../provider";
26
+ import { useSheetManager, useTeardownSheet } from "./shared";
27
27
  import { BottomSheetInstance, BottomSheetProps, SheetIds, StackBehavior } from "../types";
28
28
 
29
29
  type OmittedTrueSheetProps =
@@ -60,52 +60,6 @@ interface BottomSheetFC
60
60
 
61
61
  const FULL_SCREEN_DETENT = 0.9;
62
62
 
63
- const useSheetManager = ({
64
- id,
65
- onHide,
66
- onBeforeShow,
67
- onContextUpdate,
68
- }: {
69
- id?: string;
70
- onHide: (data?: unknown, dismiss?: boolean, behavior?: StackBehavior) => void;
71
- onBeforeShow?: (data?: unknown, behavior?: StackBehavior) => void;
72
- onContextUpdate: () => void;
73
- }) => {
74
- const currentContext = useProviderContext();
75
- const hasShownRef = React.useRef(false);
76
-
77
- React.useEffect(() => {
78
- if (!id) return undefined;
79
-
80
- const subscriptions = [
81
- eventManager.subscribe(
82
- `show_${id}`,
83
- (data: unknown, context?: string, behavior?: StackBehavior) => {
84
- if (currentContext !== context) return;
85
- if (!hasShownRef.current) {
86
- hasShownRef.current = true;
87
- onContextUpdate?.();
88
- onBeforeShow?.(data, behavior);
89
- }
90
- },
91
- ),
92
- eventManager.subscribe(
93
- `hide_${id}`,
94
- (data: unknown, context: string, dismiss?: boolean, behavior?: StackBehavior) => {
95
- if (currentContext !== context) return;
96
- hasShownRef.current = false;
97
- onHide?.(data, dismiss, behavior);
98
- },
99
- ),
100
- ];
101
-
102
- return () => {
103
- hasShownRef.current = false;
104
- subscriptions.forEach((s) => s?.unsubscribe?.());
105
- };
106
- }, [id, onHide, onBeforeShow, onContextUpdate, currentContext]);
107
- };
108
-
109
63
  const BottomSheetComponent = React.forwardRef<
110
64
  BottomSheetInstance,
111
65
  TrueSheetComponentProps
@@ -139,12 +93,19 @@ const BottomSheetComponent = React.forwardRef<
139
93
 
140
94
  const { fullScreenValues } = useSheetSharedContext();
141
95
  const [detents, fullScreenIndex] = React.useMemo(() => {
142
- const resolved = defaultDetents ?? [0.5, 1];
96
+ let resolved = defaultDetents ?? [0.5, 1];
97
+
98
+ if (Platform.OS === "android" && iosModalSheetTypeOfAnimation) {
99
+ resolved = resolved.map((detent) =>
100
+ detent === 1 || detent === FULL_SCREEN_DETENT ? FULL_SCREEN_DETENT : detent,
101
+ );
102
+ }
103
+
143
104
  const fullScreenIdx = resolved.findIndex(
144
105
  (p) => p === FULL_SCREEN_DETENT || p === 1,
145
106
  );
146
107
  return [resolved, fullScreenIdx] as const;
147
- }, [defaultDetents]);
108
+ }, [defaultDetents, iosModalSheetTypeOfAnimation]);
148
109
 
149
110
  const valueRef = React.useRef<unknown>(null);
150
111
  const trueSheetRef = React.useRef<TrueSheet>(null);
@@ -156,6 +117,7 @@ const BottomSheetComponent = React.forwardRef<
156
117
  behavior?: StackBehavior;
157
118
  }>({});
158
119
  const hasPresentedRef = React.useRef(false);
120
+ const isDismissingRef = React.useRef(false);
159
121
 
160
122
  const id = useSheetIDContext();
161
123
  const sheetId = props.id || id;
@@ -222,52 +184,7 @@ const BottomSheetComponent = React.forwardRef<
222
184
  };
223
185
  }, [iosModalSheetTypeOfAnimation, sheetId, fullScreenValues]);
224
186
 
225
- const teardownSheet = React.useCallback(
226
- (value: unknown, dismiss: boolean | undefined, behavior: StackBehavior) => {
227
- if (!sheetId) return;
228
-
229
- const hasHistory = PrivateManager.history.length > 0;
230
- const shouldRestorePrevious = behavior !== "replace";
231
-
232
- eventManager.publish(
233
- `onclose_${sheetId}`,
234
- value,
235
- currentCtx,
236
- hasHistory || !!dismiss,
237
- behavior,
238
- );
239
-
240
- if (shouldRestorePrevious) {
241
- if (dismiss) {
242
- if (behavior !== "push") {
243
- PrivateManager.history.push({
244
- id: sheetId,
245
- context: currentCtx,
246
- behavior: behavior,
247
- });
248
- }
249
- } else if (hasHistory) {
250
- const otherSheetsStillOpen = PrivateManager.stack().some(
251
- (s) => !(s.id === sheetId && s.context === currentCtx),
252
- );
253
- if (!otherSheetsStillOpen) {
254
- const prev = PrivateManager.history.pop()!;
255
- eventManager.publish(
256
- `show_wrap_${prev.id}`,
257
- undefined,
258
- prev.context,
259
- true,
260
- prev.behavior,
261
- );
262
- }
263
- }
264
- }
265
-
266
- PrivateManager.remove(sheetId, currentCtx);
267
- },
268
- [sheetId, currentCtx],
269
- );
270
-
187
+ const teardownSheet = useTeardownSheet({ sheetId, currentCtx });
271
188
  const hideSheet = React.useCallback(
272
189
  (
273
190
  data?: unknown,
@@ -289,6 +206,8 @@ const BottomSheetComponent = React.forwardRef<
289
206
  const shouldClose = activeBehavior !== "replace" || !dismiss;
290
207
 
291
208
  if (fromManager && shouldClose) {
209
+ if (isDismissingRef.current) return;
210
+ isDismissingRef.current = true;
292
211
  valueRef.current = value;
293
212
  teardownDataRef.current = { dismiss, behavior: activeBehavior };
294
213
  trueSheetRef.current?.dismiss();
@@ -306,6 +225,7 @@ const BottomSheetComponent = React.forwardRef<
306
225
  if (closeValue !== undefined) value = closeValue;
307
226
 
308
227
  teardownSheet(value, finalDismiss, finalBehavior);
228
+ isDismissingRef.current = false;
309
229
  if (fromManager) valueRef.current = data;
310
230
  },
311
231
  [currentStackBehavior, onClose, teardownSheet],