react-native-bottom-sheet-stack 0.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 (56) hide show
  1. package/LICENSE +20 -0
  2. package/README.md +82 -0
  3. package/lib/commonjs/BottomSheet.context.js +14 -0
  4. package/lib/commonjs/BottomSheet.context.js.map +1 -0
  5. package/lib/commonjs/BottomSheetHost.js +73 -0
  6. package/lib/commonjs/BottomSheetHost.js.map +1 -0
  7. package/lib/commonjs/BottomSheetManaged.js +72 -0
  8. package/lib/commonjs/BottomSheetManaged.js.map +1 -0
  9. package/lib/commonjs/BottomSheetManager.provider.js +39 -0
  10. package/lib/commonjs/BottomSheetManager.provider.js.map +1 -0
  11. package/lib/commonjs/bottomSheet.store.js +101 -0
  12. package/lib/commonjs/bottomSheet.store.js.map +1 -0
  13. package/lib/commonjs/bottomSheetCoordinator.js +43 -0
  14. package/lib/commonjs/bottomSheetCoordinator.js.map +1 -0
  15. package/lib/commonjs/index.js +41 -0
  16. package/lib/commonjs/index.js.map +1 -0
  17. package/lib/commonjs/package.json +1 -0
  18. package/lib/commonjs/refsMap.js +8 -0
  19. package/lib/commonjs/refsMap.js.map +1 -0
  20. package/lib/commonjs/useBottomSheetManager.js +78 -0
  21. package/lib/commonjs/useBottomSheetManager.js.map +1 -0
  22. package/lib/commonjs/useBottomSheetState.js +27 -0
  23. package/lib/commonjs/useBottomSheetState.js.map +1 -0
  24. package/lib/typescript/example/src/App.d.ts +4 -0
  25. package/lib/typescript/example/src/App.d.ts.map +1 -0
  26. package/lib/typescript/src/BottomSheet.context.d.ts +7 -0
  27. package/lib/typescript/src/BottomSheet.context.d.ts.map +1 -0
  28. package/lib/typescript/src/BottomSheetHost.d.ts +5 -0
  29. package/lib/typescript/src/BottomSheetHost.d.ts.map +1 -0
  30. package/lib/typescript/src/BottomSheetManaged.d.ts +10 -0
  31. package/lib/typescript/src/BottomSheetManaged.d.ts.map +1 -0
  32. package/lib/typescript/src/BottomSheetManager.provider.d.ts +14 -0
  33. package/lib/typescript/src/BottomSheetManager.provider.d.ts.map +1 -0
  34. package/lib/typescript/src/bottomSheet.store.d.ts +32 -0
  35. package/lib/typescript/src/bottomSheet.store.d.ts.map +1 -0
  36. package/lib/typescript/src/bottomSheetCoordinator.d.ts +2 -0
  37. package/lib/typescript/src/bottomSheetCoordinator.d.ts.map +1 -0
  38. package/lib/typescript/src/index.d.ts +6 -0
  39. package/lib/typescript/src/index.d.ts.map +1 -0
  40. package/lib/typescript/src/refsMap.d.ts +3 -0
  41. package/lib/typescript/src/refsMap.d.ts.map +1 -0
  42. package/lib/typescript/src/useBottomSheetManager.d.ts +22 -0
  43. package/lib/typescript/src/useBottomSheetManager.d.ts.map +1 -0
  44. package/lib/typescript/src/useBottomSheetState.d.ts +6 -0
  45. package/lib/typescript/src/useBottomSheetState.d.ts.map +1 -0
  46. package/package.json +130 -0
  47. package/src/BottomSheet.context.ts +15 -0
  48. package/src/BottomSheetHost.tsx +70 -0
  49. package/src/BottomSheetManaged.tsx +89 -0
  50. package/src/BottomSheetManager.provider.tsx +39 -0
  51. package/src/bottomSheet.store.ts +107 -0
  52. package/src/bottomSheetCoordinator.ts +34 -0
  53. package/src/index.tsx +5 -0
  54. package/src/refsMap.ts +6 -0
  55. package/src/useBottomSheetManager.tsx +87 -0
  56. package/src/useBottomSheetState.ts +31 -0
@@ -0,0 +1,34 @@
1
+ import { useBottomSheetStore } from './bottomSheet.store';
2
+ import { sheetRefs } from './refsMap';
3
+
4
+ export function initBottomSheetCoordinator() {
5
+ useBottomSheetStore.subscribe(
6
+ s => s.stack.map(({ id, status }) => ({ id, status })),
7
+ (next, prev) => {
8
+ next.forEach(({ id, status }) => {
9
+ const prevStatus = prev.find(p => p.id === id)?.status;
10
+
11
+ if (prevStatus === status) {
12
+ return;
13
+ }
14
+
15
+ const ref = sheetRefs[id]?.current;
16
+ if (!ref) {
17
+ return;
18
+ }
19
+
20
+ switch (status) {
21
+ case 'opening':
22
+ ref.expand();
23
+ break;
24
+ case 'hidden':
25
+ ref.close();
26
+ break;
27
+ case 'closing':
28
+ ref.close();
29
+ break;
30
+ }
31
+ });
32
+ },
33
+ );
34
+ }
package/src/index.tsx ADDED
@@ -0,0 +1,5 @@
1
+ export { BottomSheetHost } from './BottomSheetHost';
2
+ export { BottomSheetManagerProvider } from './BottomSheetManager.provider';
3
+ export { useBottomSheetManager } from './useBottomSheetManager';
4
+ export { useBottomSheetState } from './useBottomSheetState';
5
+ export { BottomSheetManaged } from './BottomSheetManaged';
package/src/refsMap.ts ADDED
@@ -0,0 +1,6 @@
1
+ import type { BottomSheetMethods } from '@gorhom/bottom-sheet/lib/typescript/types';
2
+
3
+ export const sheetRefs: Record<
4
+ string,
5
+ React.RefObject<BottomSheetMethods | null>
6
+ > = {};
@@ -0,0 +1,87 @@
1
+ import React from 'react';
2
+
3
+ import { useBottomSheetStore, type OpenMode } from './bottomSheet.store';
4
+ import { useMaybeBottomSheetManagerContext } from './BottomSheetManager.provider';
5
+ import { sheetRefs } from './refsMap';
6
+ import type { BottomSheetMethods } from '@gorhom/bottom-sheet/lib/typescript/types';
7
+
8
+ export const useBottomSheetManager = () => {
9
+ const bottomSheetManagerContext = useMaybeBottomSheetManagerContext();
10
+
11
+ const {
12
+ pushBottomSheet,
13
+ replaceBottomSheet,
14
+ switchBottomSheet,
15
+ stack,
16
+ startClosing,
17
+ storeClearAll,
18
+ } = useBottomSheetStore((store) => ({
19
+ storeClearAll: store.clearAll,
20
+ replaceBottomSheet: store.replace,
21
+ pushBottomSheet: store.push,
22
+ switchBottomSheet: store.switch,
23
+ stack: store.stack,
24
+ startClosing: store.startClosing,
25
+ }));
26
+
27
+ const openBottomSheet = (
28
+ content: React.ReactElement,
29
+ options: {
30
+ id?: string;
31
+ groupId?: string;
32
+ mode?: OpenMode;
33
+ } = {}
34
+ ) => {
35
+ const groupId =
36
+ options.groupId || bottomSheetManagerContext?.groupId || 'default';
37
+
38
+ const id = Math.random().toString(36);
39
+ const ref = React.createRef<BottomSheetMethods>();
40
+
41
+ sheetRefs[id] = ref;
42
+
43
+ // @ts-ignore
44
+ const contentWithRef = React.cloneElement(content, { ref });
45
+
46
+ if (options.mode === 'replace') {
47
+ replaceBottomSheet({
48
+ id,
49
+ groupId,
50
+ content: contentWithRef,
51
+ });
52
+ } else if (options.mode === 'switch') {
53
+ switchBottomSheet({
54
+ id,
55
+ groupId,
56
+ content: contentWithRef,
57
+ });
58
+ } else {
59
+ pushBottomSheet({
60
+ id,
61
+ groupId,
62
+ content: contentWithRef,
63
+ });
64
+ }
65
+
66
+ return id;
67
+ };
68
+
69
+ const closeTop = () => {
70
+ const top = stack.at(-1);
71
+ if (top) {
72
+ startClosing(top.id);
73
+ }
74
+ };
75
+
76
+ const clearAll = () => {
77
+ storeClearAll();
78
+ };
79
+
80
+ return {
81
+ clearAll,
82
+ closeTop,
83
+ openBottomSheet,
84
+ pushBottomSheet,
85
+ replaceBottomSheet,
86
+ };
87
+ };
@@ -0,0 +1,31 @@
1
+ import { useMaybeBottomSheetContext } from './BottomSheet.context';
2
+ import {
3
+ useBottomSheetStore,
4
+ type BottomSheetState,
5
+ } from './bottomSheet.store';
6
+
7
+ export function useBottomSheetState(): {
8
+ bottomSheetState: BottomSheetState;
9
+ close: () => void;
10
+ } {
11
+ const context = useMaybeBottomSheetContext();
12
+ const { bottomSheetsStack, startClosing } = useBottomSheetStore((store) => ({
13
+ bottomSheetsStack: store.stack,
14
+ startClosing: store.startClosing,
15
+ }));
16
+
17
+ const bottomSheetState = bottomSheetsStack.find(
18
+ (bottomSheet) => bottomSheet.id === context?.id
19
+ );
20
+
21
+ if (!bottomSheetState) {
22
+ throw new Error(
23
+ 'useBottomSheetState must be used within a BottomSheetProvider'
24
+ );
25
+ }
26
+
27
+ return {
28
+ bottomSheetState,
29
+ close: () => startClosing(bottomSheetState.id),
30
+ };
31
+ }