react-native-bottom-sheet-stack 1.0.2 → 1.1.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 (86) hide show
  1. package/README.md +262 -50
  2. package/lib/commonjs/BottomSheet.context.js.map +1 -1
  3. package/lib/commonjs/BottomSheetBackdrop.js +76 -0
  4. package/lib/commonjs/BottomSheetBackdrop.js.map +1 -0
  5. package/lib/commonjs/BottomSheetHost.js +280 -38
  6. package/lib/commonjs/BottomSheetHost.js.map +1 -1
  7. package/lib/commonjs/BottomSheetManaged.js +128 -38
  8. package/lib/commonjs/BottomSheetManaged.js.map +1 -1
  9. package/lib/commonjs/BottomSheetManager.context.js.map +1 -1
  10. package/lib/commonjs/BottomSheetManager.provider.js +41 -13
  11. package/lib/commonjs/BottomSheetManager.provider.js.map +1 -1
  12. package/lib/commonjs/BottomSheetPortal.js +46 -0
  13. package/lib/commonjs/BottomSheetPortal.js.map +1 -0
  14. package/lib/commonjs/BottomSheetScaleView.js +67 -0
  15. package/lib/commonjs/BottomSheetScaleView.js.map +1 -0
  16. package/lib/commonjs/animatedRegistry.js +25 -0
  17. package/lib/commonjs/animatedRegistry.js.map +1 -0
  18. package/lib/commonjs/bottomSheet.store.js +19 -0
  19. package/lib/commonjs/bottomSheet.store.js.map +1 -1
  20. package/lib/commonjs/bottomSheetCoordinator.js +5 -6
  21. package/lib/commonjs/bottomSheetCoordinator.js.map +1 -1
  22. package/lib/commonjs/index.js +17 -3
  23. package/lib/commonjs/index.js.map +1 -1
  24. package/lib/commonjs/portal.types.js +2 -0
  25. package/lib/commonjs/portal.types.js.map +1 -0
  26. package/lib/commonjs/useBottomSheetControl.js +81 -0
  27. package/lib/commonjs/useBottomSheetControl.js.map +1 -0
  28. package/lib/commonjs/useBottomSheetManager.js +88 -44
  29. package/lib/commonjs/useBottomSheetManager.js.map +1 -1
  30. package/lib/commonjs/useBottomSheetState.js +40 -10
  31. package/lib/commonjs/useBottomSheetState.js.map +1 -1
  32. package/lib/commonjs/useScaleAnimation.js +108 -0
  33. package/lib/commonjs/useScaleAnimation.js.map +1 -0
  34. package/lib/typescript/example/src/App.d.ts +0 -2
  35. package/lib/typescript/example/src/App.d.ts.map +1 -1
  36. package/lib/typescript/src/BottomSheetBackdrop.d.ts +12 -0
  37. package/lib/typescript/src/BottomSheetBackdrop.d.ts.map +1 -0
  38. package/lib/typescript/src/BottomSheetHost.d.ts +1 -2
  39. package/lib/typescript/src/BottomSheetHost.d.ts.map +1 -1
  40. package/lib/typescript/src/BottomSheetManaged.d.ts.map +1 -1
  41. package/lib/typescript/src/BottomSheetManager.context.d.ts +2 -0
  42. package/lib/typescript/src/BottomSheetManager.context.d.ts.map +1 -1
  43. package/lib/typescript/src/BottomSheetManager.provider.d.ts +4 -3
  44. package/lib/typescript/src/BottomSheetManager.provider.d.ts.map +1 -1
  45. package/lib/typescript/src/BottomSheetPortal.d.ts +9 -0
  46. package/lib/typescript/src/BottomSheetPortal.d.ts.map +1 -0
  47. package/lib/typescript/src/BottomSheetScaleView.d.ts +18 -0
  48. package/lib/typescript/src/BottomSheetScaleView.d.ts.map +1 -0
  49. package/lib/typescript/src/animatedRegistry.d.ts +4 -0
  50. package/lib/typescript/src/animatedRegistry.d.ts.map +1 -0
  51. package/lib/typescript/src/bottomSheet.store.d.ts +9 -3
  52. package/lib/typescript/src/bottomSheet.store.d.ts.map +1 -1
  53. package/lib/typescript/src/bottomSheetCoordinator.d.ts.map +1 -1
  54. package/lib/typescript/src/index.d.ts +5 -1
  55. package/lib/typescript/src/index.d.ts.map +1 -1
  56. package/lib/typescript/src/portal.types.d.ts +24 -0
  57. package/lib/typescript/src/portal.types.d.ts.map +1 -0
  58. package/lib/typescript/src/useBottomSheetControl.d.ts +10 -0
  59. package/lib/typescript/src/useBottomSheetControl.d.ts.map +1 -0
  60. package/lib/typescript/src/useBottomSheetManager.d.ts +2 -0
  61. package/lib/typescript/src/useBottomSheetManager.d.ts.map +1 -1
  62. package/lib/typescript/src/useBottomSheetState.d.ts.map +1 -1
  63. package/lib/typescript/src/useScaleAnimation.d.ts +43 -0
  64. package/lib/typescript/src/useScaleAnimation.d.ts.map +1 -0
  65. package/package.json +11 -2
  66. package/src/BottomSheetBackdrop.tsx +61 -0
  67. package/src/BottomSheetHost.tsx +74 -15
  68. package/src/BottomSheetManaged.tsx +26 -33
  69. package/src/BottomSheetManager.context.tsx +2 -0
  70. package/src/BottomSheetManager.provider.tsx +15 -8
  71. package/src/BottomSheetPortal.tsx +39 -0
  72. package/src/BottomSheetScaleView.tsx +41 -0
  73. package/src/animatedRegistry.ts +22 -0
  74. package/src/bottomSheet.store.ts +150 -123
  75. package/src/bottomSheetCoordinator.ts +5 -6
  76. package/src/index.tsx +8 -4
  77. package/src/portal.types.ts +25 -0
  78. package/src/useBottomSheetControl.ts +52 -0
  79. package/src/useBottomSheetManager.tsx +37 -48
  80. package/src/useBottomSheetState.ts +2 -6
  81. package/src/useScaleAnimation.ts +114 -0
  82. package/lib/commonjs/ScaleBackgroundWrapper.js +0 -71
  83. package/lib/commonjs/ScaleBackgroundWrapper.js.map +0 -1
  84. package/lib/typescript/src/ScaleBackgroundWrapper.d.ts +0 -32
  85. package/lib/typescript/src/ScaleBackgroundWrapper.d.ts.map +0 -1
  86. package/src/ScaleBackgroundWrapper.tsx +0 -97
@@ -11,6 +11,7 @@ export interface BottomSheetState {
11
11
  content: ReactNode;
12
12
  status: BottomSheetStatus;
13
13
  scaleBackground?: boolean;
14
+ usePortal?: boolean;
14
15
  }
15
16
 
16
17
  type TriggerState = Omit<BottomSheetState, 'status'>;
@@ -20,146 +21,172 @@ interface BottomSheetStoreState {
20
21
  stackOrder: string[];
21
22
  }
22
23
 
24
+ export interface PortalOpenOptions {
25
+ scaleBackground?: boolean;
26
+ }
27
+
23
28
  interface BottomSheetStoreActions {
24
29
  push(sheet: TriggerState): void;
25
30
  switch(sheet: TriggerState): void;
26
31
  replace(sheet: TriggerState): void;
32
+ openPortal(id: string, groupId: string, options?: PortalOpenOptions): void;
27
33
  markOpen(id: string): void;
28
34
  startClosing(id: string): void;
29
35
  finishClosing(id: string): void;
30
36
  clearAll(): void;
31
37
  }
32
38
 
33
- export const useBottomSheetStore = create(
34
- subscribeWithSelector<BottomSheetStoreState & BottomSheetStoreActions>(
35
- (set) => ({
36
- sheetsById: {},
37
- stackOrder: [],
38
-
39
- push: (sheet) =>
40
- set((state) => {
41
- if (state.sheetsById[sheet.id]) {
42
- return state;
43
- }
44
- return {
45
- sheetsById: {
46
- ...state.sheetsById,
47
- [sheet.id]: { ...sheet, status: 'opening' },
48
- },
49
- stackOrder: [...state.stackOrder, sheet.id],
50
- };
51
- }),
52
-
53
- switch: (sheet) =>
54
- set((state) => {
55
- if (state.sheetsById[sheet.id]) {
56
- return state;
57
- }
39
+ export type BottomSheetStore = BottomSheetStoreState & BottomSheetStoreActions;
58
40
 
59
- const newSheetsById = { ...state.sheetsById };
60
- const topId = state.stackOrder[state.stackOrder.length - 1];
61
-
62
- if (topId && newSheetsById[topId]) {
63
- newSheetsById[topId] = {
64
- ...newSheetsById[topId],
65
- status: 'hidden',
66
- };
67
- }
68
-
69
- newSheetsById[sheet.id] = { ...sheet, status: 'opening' };
70
-
71
- return {
72
- sheetsById: newSheetsById,
73
- stackOrder: [...state.stackOrder, sheet.id],
41
+ export const useBottomSheetStore = create(
42
+ subscribeWithSelector<BottomSheetStore>((set) => ({
43
+ sheetsById: {},
44
+ stackOrder: [],
45
+
46
+ push: (sheet) =>
47
+ set((state) => {
48
+ if (state.sheetsById[sheet.id]) {
49
+ return state;
50
+ }
51
+ return {
52
+ sheetsById: {
53
+ ...state.sheetsById,
54
+ [sheet.id]: { ...sheet, status: 'opening' },
55
+ },
56
+ stackOrder: [...state.stackOrder, sheet.id],
57
+ };
58
+ }),
59
+
60
+ switch: (sheet) =>
61
+ set((state) => {
62
+ if (state.sheetsById[sheet.id]) {
63
+ return state;
64
+ }
65
+
66
+ const newSheetsById = { ...state.sheetsById };
67
+ const topId = state.stackOrder[state.stackOrder.length - 1];
68
+
69
+ if (topId && newSheetsById[topId]) {
70
+ newSheetsById[topId] = {
71
+ ...newSheetsById[topId],
72
+ status: 'hidden',
74
73
  };
75
- }),
74
+ }
76
75
 
77
- replace: (sheet) =>
78
- set((state) => {
79
- if (state.sheetsById[sheet.id]) {
80
- return state;
81
- }
76
+ newSheetsById[sheet.id] = { ...sheet, status: 'opening' };
82
77
 
83
- const newSheetsById = { ...state.sheetsById };
84
- const topId = state.stackOrder[state.stackOrder.length - 1];
78
+ return {
79
+ sheetsById: newSheetsById,
80
+ stackOrder: [...state.stackOrder, sheet.id],
81
+ };
82
+ }),
85
83
 
86
- if (topId && newSheetsById[topId]) {
87
- newSheetsById[topId] = {
88
- ...newSheetsById[topId],
89
- status: 'closing',
90
- };
91
- }
84
+ replace: (sheet) =>
85
+ set((state) => {
86
+ if (state.sheetsById[sheet.id]) {
87
+ return state;
88
+ }
92
89
 
93
- newSheetsById[sheet.id] = { ...sheet, status: 'opening' };
90
+ const newSheetsById = { ...state.sheetsById };
91
+ const topId = state.stackOrder[state.stackOrder.length - 1];
94
92
 
95
- return {
96
- sheetsById: newSheetsById,
97
- stackOrder: [...state.stackOrder, sheet.id],
93
+ if (topId && newSheetsById[topId]) {
94
+ newSheetsById[topId] = {
95
+ ...newSheetsById[topId],
96
+ status: 'closing',
98
97
  };
99
- }),
100
-
101
- markOpen: (id) =>
102
- set((state) => {
103
- const sheet = state.sheetsById[id];
104
- if (!sheet) {
105
- return state;
106
- }
107
- return {
108
- sheetsById: {
109
- ...state.sheetsById,
110
- [id]: { ...sheet, status: 'open' },
98
+ }
99
+
100
+ newSheetsById[sheet.id] = { ...sheet, status: 'opening' };
101
+
102
+ return {
103
+ sheetsById: newSheetsById,
104
+ stackOrder: [...state.stackOrder, sheet.id],
105
+ };
106
+ }),
107
+
108
+ openPortal: (id, groupId, options) =>
109
+ set((state) => {
110
+ if (state.sheetsById[id]) {
111
+ return state;
112
+ }
113
+ return {
114
+ sheetsById: {
115
+ ...state.sheetsById,
116
+ [id]: {
117
+ id,
118
+ groupId,
119
+ content: null,
120
+ status: 'opening',
121
+ usePortal: true,
122
+ scaleBackground: options?.scaleBackground,
111
123
  },
112
- };
113
- }),
114
-
115
- startClosing: (id) =>
116
- set((state) => {
117
- const sheet = state.sheetsById[id];
118
- if (!sheet || sheet.status === 'hidden') {
119
- return state;
120
- }
121
-
122
- const newSheetsById = { ...state.sheetsById };
123
- newSheetsById[id] = { ...sheet, status: 'closing' };
124
-
125
- const index = state.stackOrder.indexOf(id);
126
- const belowId = state.stackOrder[index - 1];
127
- const belowSheet = belowId ? newSheetsById[belowId] : undefined;
128
-
129
- if (belowId && belowSheet && belowSheet.status === 'hidden') {
130
- newSheetsById[belowId] = { ...belowSheet, status: 'opening' };
131
- }
132
-
133
- return { sheetsById: newSheetsById };
134
- }),
135
-
136
- finishClosing: (id) =>
137
- set((state) => {
138
- if (!state.sheetsById[id]) {
139
- return state;
140
- }
141
-
142
- const newSheetsById = { ...state.sheetsById };
143
- delete newSheetsById[id];
144
-
145
- const newStackOrder = state.stackOrder.filter(
146
- (sheetId) => sheetId !== id
147
- );
148
-
149
- const topId = newStackOrder[newStackOrder.length - 1];
150
- const topSheet = topId ? newSheetsById[topId] : undefined;
151
-
152
- if (topId && topSheet && topSheet.status === 'hidden') {
153
- newSheetsById[topId] = { ...topSheet, status: 'opening' };
154
- }
155
-
156
- return {
157
- sheetsById: newSheetsById,
158
- stackOrder: newStackOrder,
159
- };
160
- }),
161
-
162
- clearAll: () => set(() => ({ sheetsById: {}, stackOrder: [] })),
163
- })
164
- )
124
+ },
125
+ stackOrder: [...state.stackOrder, id],
126
+ };
127
+ }),
128
+
129
+ markOpen: (id) =>
130
+ set((state) => {
131
+ const sheet = state.sheetsById[id];
132
+ if (!sheet) {
133
+ return state;
134
+ }
135
+ return {
136
+ sheetsById: {
137
+ ...state.sheetsById,
138
+ [id]: { ...sheet, status: 'open' },
139
+ },
140
+ };
141
+ }),
142
+
143
+ startClosing: (id) =>
144
+ set((state) => {
145
+ const sheet = state.sheetsById[id];
146
+ if (!sheet || sheet.status === 'hidden') {
147
+ return state;
148
+ }
149
+
150
+ const newSheetsById = { ...state.sheetsById };
151
+ newSheetsById[id] = { ...sheet, status: 'closing' };
152
+
153
+ const index = state.stackOrder.indexOf(id);
154
+ const belowId = state.stackOrder[index - 1];
155
+ const belowSheet = belowId ? newSheetsById[belowId] : undefined;
156
+
157
+ if (belowId && belowSheet && belowSheet.status === 'hidden') {
158
+ newSheetsById[belowId] = { ...belowSheet, status: 'opening' };
159
+ }
160
+
161
+ return { sheetsById: newSheetsById };
162
+ }),
163
+
164
+ finishClosing: (id) =>
165
+ set((state) => {
166
+ if (!state.sheetsById[id]) {
167
+ return state;
168
+ }
169
+
170
+ const newSheetsById = { ...state.sheetsById };
171
+ delete newSheetsById[id];
172
+
173
+ const newStackOrder = state.stackOrder.filter(
174
+ (sheetId) => sheetId !== id
175
+ );
176
+
177
+ const topId = newStackOrder[newStackOrder.length - 1];
178
+ const topSheet = topId ? newSheetsById[topId] : undefined;
179
+
180
+ if (topId && topSheet && topSheet.status === 'hidden') {
181
+ newSheetsById[topId] = { ...topSheet, status: 'opening' };
182
+ }
183
+
184
+ return {
185
+ sheetsById: newSheetsById,
186
+ stackOrder: newStackOrder,
187
+ };
188
+ }),
189
+
190
+ clearAll: () => set(() => ({ sheetsById: {}, stackOrder: [] })),
191
+ }))
165
192
  );
@@ -16,19 +16,18 @@ export function initBottomSheetCoordinator(groupId: string) {
16
16
  }
17
17
 
18
18
  const ref = sheetRefs[id]?.current;
19
- if (!ref) {
20
- return;
21
- }
22
19
 
23
20
  switch (status) {
24
21
  case 'opening':
25
- ref.expand();
22
+ requestAnimationFrame(() => {
23
+ sheetRefs[id]?.current?.expand();
24
+ });
26
25
  break;
27
26
  case 'hidden':
28
- ref.close();
27
+ if (ref) ref.close();
29
28
  break;
30
29
  case 'closing':
31
- ref.close();
30
+ if (ref) ref.close();
32
31
  break;
33
32
  }
34
33
  });
package/src/index.tsx CHANGED
@@ -3,7 +3,11 @@ export { BottomSheetManagerProvider } from './BottomSheetManager.provider';
3
3
  export { useBottomSheetManager } from './useBottomSheetManager';
4
4
  export { useBottomSheetState } from './useBottomSheetState';
5
5
  export { BottomSheetManaged } from './BottomSheetManaged';
6
- export {
7
- ScaleBackgroundWrapper,
8
- type ScaleBackgroundConfig,
9
- } from './ScaleBackgroundWrapper';
6
+ export { BottomSheetScaleView } from './BottomSheetScaleView';
7
+ export { type ScaleConfig } from './useScaleAnimation';
8
+ export { BottomSheetPortal } from './BottomSheetPortal';
9
+ export { useBottomSheetControl } from './useBottomSheetControl';
10
+ export type {
11
+ BottomSheetPortalRegistry,
12
+ BottomSheetPortalId,
13
+ } from './portal.types';
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Registry for portal-based bottom sheets.
3
+ * Augment this interface in your app to get type-safe sheet IDs:
4
+ *
5
+ * @example
6
+ * ```tsx
7
+ * declare module 'react-native-bottom-sheet-stack' {
8
+ * interface BottomSheetPortalRegistry {
9
+ * 'my-sheet': true;
10
+ * 'settings-sheet': true;
11
+ * 'profile-sheet': true;
12
+ * }
13
+ * }
14
+ * ```
15
+ */
16
+ export interface BottomSheetPortalRegistry {}
17
+
18
+ /**
19
+ * Type-safe portal sheet ID.
20
+ * If BottomSheetPortalRegistry is augmented, this will be a union of the registered keys.
21
+ * Otherwise, it falls back to `string` for flexibility.
22
+ */
23
+ export type BottomSheetPortalId = keyof BottomSheetPortalRegistry extends never
24
+ ? string
25
+ : Extract<keyof BottomSheetPortalRegistry, string>;
@@ -0,0 +1,52 @@
1
+ import React from 'react';
2
+ import type { BottomSheetMethods } from '@gorhom/bottom-sheet/lib/typescript/types';
3
+
4
+ import {
5
+ useBottomSheetStore,
6
+ type BottomSheetStatus,
7
+ type PortalOpenOptions,
8
+ } from './bottomSheet.store';
9
+ import { useMaybeBottomSheetManagerContext } from './BottomSheetManager.provider';
10
+ import type { BottomSheetPortalId } from './portal.types';
11
+ import { sheetRefs } from './refsMap';
12
+
13
+ export interface UseBottomSheetControlReturn {
14
+ open: (options?: PortalOpenOptions) => void;
15
+ close: () => void;
16
+ isOpen: boolean;
17
+ status: BottomSheetStatus | null;
18
+ }
19
+
20
+ export function useBottomSheetControl(
21
+ id: BottomSheetPortalId
22
+ ): UseBottomSheetControlReturn {
23
+ const bottomSheetManagerContext = useMaybeBottomSheetManagerContext();
24
+
25
+ const openPortal = useBottomSheetStore((state) => state.openPortal);
26
+ const startClosing = useBottomSheetStore((state) => state.startClosing);
27
+ const sheetState = useBottomSheetStore((state) => state.sheetsById[id]);
28
+
29
+ const open = (options?: PortalOpenOptions) => {
30
+ const groupId = bottomSheetManagerContext?.groupId || 'default';
31
+
32
+ // Create ref when opening (same pattern as useBottomSheetManager)
33
+ const ref = React.createRef<BottomSheetMethods>();
34
+ sheetRefs[id] = ref;
35
+
36
+ openPortal(id, groupId, options);
37
+ };
38
+
39
+ const close = () => {
40
+ startClosing(id);
41
+ };
42
+
43
+ const status = sheetState?.status ?? null;
44
+ const isOpen = status === 'open' || status === 'opening';
45
+
46
+ return {
47
+ open,
48
+ close,
49
+ isOpen,
50
+ status,
51
+ };
52
+ }
@@ -1,4 +1,4 @@
1
- import React, { useCallback } from 'react';
1
+ import React from 'react';
2
2
 
3
3
  import { useBottomSheetStore, type OpenMode } from './bottomSheet.store';
4
4
  import { useMaybeBottomSheetManagerContext } from './BottomSheetManager.provider';
@@ -26,62 +26,51 @@ export const useBottomSheetManager = () => {
26
26
  shallow
27
27
  );
28
28
 
29
- const openBottomSheet = useCallback(
30
- (
31
- content: React.ReactElement,
32
- options: {
33
- id?: string;
34
- groupId?: string;
35
- mode?: OpenMode;
36
- scaleBackground?: boolean;
37
- } = {}
38
- ) => {
39
- const groupId =
40
- options.groupId || bottomSheetManagerContext?.groupId || 'default';
29
+ const openBottomSheet = (
30
+ content: React.ReactElement,
31
+ options: {
32
+ id?: string;
33
+ groupId?: string;
34
+ mode?: OpenMode;
35
+ scaleBackground?: boolean;
36
+ } = {}
37
+ ) => {
38
+ const groupId =
39
+ options.groupId || bottomSheetManagerContext?.groupId || 'default';
41
40
 
42
- const id = options.id || Math.random().toString(36);
43
- const ref = React.createRef<BottomSheetMethods>();
41
+ const id = options.id || Math.random().toString(36);
42
+ const ref = React.createRef<BottomSheetMethods>();
44
43
 
45
- sheetRefs[id] = ref;
44
+ sheetRefs[id] = ref;
46
45
 
47
- // @ts-ignore
48
- const contentWithRef = React.cloneElement(content, { ref });
46
+ // @ts-ignore
47
+ const contentWithRef = React.cloneElement(content, { ref });
49
48
 
50
- const sheetData = {
51
- id,
52
- groupId,
53
- content: contentWithRef,
54
- scaleBackground: options.scaleBackground,
55
- };
49
+ const sheetData = {
50
+ id,
51
+ groupId,
52
+ content: contentWithRef,
53
+ scaleBackground: options.scaleBackground,
54
+ };
56
55
 
57
- if (options.mode === 'replace') {
58
- replaceBottomSheet(sheetData);
59
- } else if (options.mode === 'switch') {
60
- switchBottomSheet(sheetData);
61
- } else {
62
- pushBottomSheet(sheetData);
63
- }
56
+ if (options.mode === 'replace') {
57
+ replaceBottomSheet(sheetData);
58
+ } else if (options.mode === 'switch') {
59
+ switchBottomSheet(sheetData);
60
+ } else {
61
+ pushBottomSheet(sheetData);
62
+ }
64
63
 
65
- return id;
66
- },
67
- [
68
- bottomSheetManagerContext?.groupId,
69
- pushBottomSheet,
70
- replaceBottomSheet,
71
- switchBottomSheet,
72
- ]
73
- );
64
+ return id;
65
+ };
74
66
 
75
- const close = useCallback(
76
- (id: string) => {
77
- startClosing(id);
78
- },
79
- [startClosing]
80
- );
67
+ const close = (id: string) => {
68
+ startClosing(id);
69
+ };
81
70
 
82
- const clearAll = useCallback(() => {
71
+ const clearAll = () => {
83
72
  storeClearAll();
84
- }, [storeClearAll]);
73
+ };
85
74
 
86
75
  return {
87
76
  clearAll,
@@ -1,4 +1,3 @@
1
- import { useCallback } from 'react';
2
1
  import { useMaybeBottomSheetContext } from './BottomSheet.context';
3
2
  import {
4
3
  useBottomSheetStore,
@@ -13,7 +12,7 @@ export function useBottomSheetState(): {
13
12
  const context = useMaybeBottomSheetContext();
14
13
 
15
14
  const bottomSheetState = useBottomSheetStore(
16
- useCallback((state) => state.sheetsById[context?.id!], [context?.id])
15
+ (state) => state.sheetsById[context?.id!]
17
16
  );
18
17
 
19
18
  const startClosing = useBottomSheetStore((state) => state.startClosing);
@@ -24,10 +23,7 @@ export function useBottomSheetState(): {
24
23
  );
25
24
  }
26
25
 
27
- const close = useCallback(
28
- () => startClosing(bottomSheetState.id),
29
- [bottomSheetState.id, startClosing]
30
- );
26
+ const close = () => startClosing(bottomSheetState.id);
31
27
 
32
28
  return {
33
29
  bottomSheetState,
@@ -0,0 +1,114 @@
1
+ import { useRef } from 'react';
2
+ import {
3
+ useAnimatedStyle,
4
+ useDerivedValue,
5
+ withTiming,
6
+ } from 'react-native-reanimated';
7
+ import {
8
+ useBottomSheetStore,
9
+ type BottomSheetStore,
10
+ } from './bottomSheet.store';
11
+
12
+ export interface ScaleConfig {
13
+ /** Scale factor when sheet is open (default: 0.92) */
14
+ scale?: number;
15
+ /** Vertical translation when sheet is open (default: 10) */
16
+ translateY?: number;
17
+ /** Border radius when sheet is open (default: 12) */
18
+ borderRadius?: number;
19
+ /** Animation duration in ms (default: 300) */
20
+ duration?: number;
21
+ }
22
+
23
+ const DEFAULT_CONFIG: Required<ScaleConfig> = {
24
+ scale: 0.92,
25
+ translateY: 10,
26
+ borderRadius: 12,
27
+ duration: 300,
28
+ };
29
+
30
+ /**
31
+ * Returns the number of sheets with scaleBackground above a given element.
32
+ * For background wrapper, pass undefined as sheetId - returns 0 or 1 (binary).
33
+ * For sheets, returns the count of scaleBackground sheets above it.
34
+ * Uses shallow comparison internally for optimal re-renders.
35
+ */
36
+ export function useScaleDepth(groupId: string, sheetId?: string): number {
37
+ const prevDepthRef = useRef(0);
38
+
39
+ const scaleDepthSelector = (state: BottomSheetStore) => {
40
+ const { stackOrder, sheetsById } = state;
41
+
42
+ // For background: check if ANY scaleBackground sheet is active (binary 0/1)
43
+ // For a sheet: count scaleBackground sheets above it in the stack
44
+ const startIndex = sheetId ? stackOrder.indexOf(sheetId) + 1 : 0;
45
+
46
+ if (sheetId && startIndex === 0) {
47
+ // Sheet not found in stack, return previous value to avoid flicker
48
+ return prevDepthRef.current;
49
+ }
50
+
51
+ let depth = 0;
52
+ for (let i = startIndex; i < stackOrder.length; i++) {
53
+ const id = stackOrder[i]!;
54
+ const sheet = sheetsById[id];
55
+ if (
56
+ sheet &&
57
+ sheet.groupId === groupId &&
58
+ sheet.scaleBackground &&
59
+ sheet.status !== 'closing'
60
+ ) {
61
+ depth++;
62
+ // For background wrapper (no sheetId), we only need to know if there's at least one
63
+ // Don't accumulate - background scales once, sheets below scale cumulatively
64
+ if (!sheetId) {
65
+ break;
66
+ }
67
+ }
68
+ }
69
+
70
+ prevDepthRef.current = depth;
71
+ return depth;
72
+ };
73
+
74
+ return useBottomSheetStore(scaleDepthSelector);
75
+ }
76
+
77
+ /**
78
+ * Returns animated style for scale effect based on depth.
79
+ * Uses power scaling: scale^depth for cascading effect.
80
+ */
81
+ export function useScaleAnimatedStyle(depth: number, config?: ScaleConfig) {
82
+ const {
83
+ scale = DEFAULT_CONFIG.scale,
84
+ translateY = DEFAULT_CONFIG.translateY,
85
+ borderRadius = DEFAULT_CONFIG.borderRadius,
86
+ duration = DEFAULT_CONFIG.duration,
87
+ } = config ?? {};
88
+
89
+ const progress = useDerivedValue(() => {
90
+ return withTiming(depth, { duration });
91
+ });
92
+
93
+ return useAnimatedStyle(() => {
94
+ const p = progress.value;
95
+
96
+ if (p === 0) {
97
+ return {
98
+ transform: [{ scale: 1 }, { translateY: 0 }],
99
+ borderRadius: 0,
100
+ overflow: 'visible',
101
+ };
102
+ }
103
+
104
+ const currentScale = Math.pow(scale, p);
105
+ const currentTranslateY = translateY * p;
106
+ const currentBorderRadius = Math.min(borderRadius * p, borderRadius);
107
+
108
+ return {
109
+ transform: [{ scale: currentScale }, { translateY: currentTranslateY }],
110
+ borderRadius: currentBorderRadius,
111
+ overflow: 'hidden',
112
+ };
113
+ });
114
+ }