@granite-js/react-native 1.0.31 → 1.0.32
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.
- package/CHANGELOG.md +15 -0
- package/dist/app/AppRoot.d.ts +1 -1
- package/dist/app/Granite.d.ts +8 -1
- package/dist/router/Router.d.ts +1 -1
- package/dist/router/components/CanGoBackGuard.d.ts +13 -5
- package/dist/router/components/useRouterBackHandler.d.ts +4 -1
- package/dist/use-back-event/BackEventContext.d.ts +15 -0
- package/dist/use-back-event/index.d.ts +1 -0
- package/dist/use-back-event/useBackEvent.d.ts +8 -9
- package/dist/use-back-event/useBackEventActionsContext.d.ts +1 -0
- package/dist/use-back-event/useBackEventContext.d.ts +2 -0
- package/dist/use-back-event/useBackHandler.d.ts +53 -0
- package/package.json +12 -11
- package/src/app/AppRoot.tsx +3 -0
- package/src/app/Granite.tsx +8 -1
- package/src/router/Router.tsx +6 -2
- package/src/router/components/CanGoBackGuard.spec.tsx +85 -0
- package/src/router/components/CanGoBackGuard.tsx +37 -14
- package/src/router/components/useRouterBackHandler.spec.tsx +129 -0
- package/src/router/components/useRouterBackHandler.tsx +34 -13
- package/src/use-back-event/BackEventContext.ts +26 -0
- package/src/use-back-event/index.ts +1 -0
- package/src/use-back-event/useBackEvent.spec.tsx +107 -0
- package/src/use-back-event/useBackEvent.tsx +138 -48
- package/src/use-back-event/useBackEventActionsContext.ts +11 -0
- package/src/use-back-event/useBackEventContext.ts +28 -0
- package/src/use-back-event/useBackHandler.spec.tsx +113 -0
- package/src/use-back-event/useBackHandler.tsx +114 -0
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { useCallback, useEffect, useMemo, useRef } from 'react';
|
|
2
|
+
import { useVisibility } from '../visibility';
|
|
3
|
+
import type { BackEvent } from './useBackEvent';
|
|
4
|
+
import { useBackEventContext } from './useBackEventContext';
|
|
5
|
+
|
|
6
|
+
export type BackHandlerCallback = (event: BackEvent) => boolean | void;
|
|
7
|
+
|
|
8
|
+
export interface BackHandlerSubscription {
|
|
9
|
+
remove: () => void;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface BackHandlerControls {
|
|
13
|
+
addEventListener: (handler: BackHandlerCallback) => BackHandlerSubscription;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* @public
|
|
18
|
+
* @category Screen Control
|
|
19
|
+
* @name useBackHandler
|
|
20
|
+
* @description
|
|
21
|
+
* A Hook that registers back handlers. Back handlers run before handlers registered through `useBackEvent`.
|
|
22
|
+
* Return `true` to stop bubbling and prevent default back navigation. Return `false` or return nothing to continue bubbling to the next handler.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
*
|
|
26
|
+
* ### Example of Closing an Overlay First
|
|
27
|
+
*
|
|
28
|
+
* ```tsx
|
|
29
|
+
* import { useEffect, useState } from 'react';
|
|
30
|
+
* import { Button, View } from 'react-native';
|
|
31
|
+
* import { useBackHandler } from '@granite-js/react-native';
|
|
32
|
+
*
|
|
33
|
+
* export function OverlayExample() {
|
|
34
|
+
* const backHandler = useBackHandler();
|
|
35
|
+
* const [isOverlayOpen, setIsOverlayOpen] = useState(false);
|
|
36
|
+
*
|
|
37
|
+
* useEffect(() => {
|
|
38
|
+
* const subscription = backHandler.addEventListener(() => {
|
|
39
|
+
* if (isOverlayOpen) {
|
|
40
|
+
* setIsOverlayOpen(false);
|
|
41
|
+
* return true;
|
|
42
|
+
* }
|
|
43
|
+
*
|
|
44
|
+
* return undefined;
|
|
45
|
+
* });
|
|
46
|
+
*
|
|
47
|
+
* return () => {
|
|
48
|
+
* subscription.remove();
|
|
49
|
+
* };
|
|
50
|
+
* }, [backHandler, isOverlayOpen]);
|
|
51
|
+
*
|
|
52
|
+
* return (
|
|
53
|
+
* <View>
|
|
54
|
+
* <Button title="Open Overlay" onPress={() => setIsOverlayOpen(true)} />
|
|
55
|
+
* </View>
|
|
56
|
+
* );
|
|
57
|
+
* }
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
export function useBackHandler() {
|
|
61
|
+
const context = useBackEventContext();
|
|
62
|
+
const handlersRef = useRef<Set<BackHandlerCallback>>(new Set()).current;
|
|
63
|
+
|
|
64
|
+
const isVisible = useVisibility();
|
|
65
|
+
|
|
66
|
+
const contextAddBackHandler = context.addBackHandler;
|
|
67
|
+
const contextRemoveBackHandler = context.removeBackHandler;
|
|
68
|
+
|
|
69
|
+
const removeHandler = useCallback(
|
|
70
|
+
(...handlers: Array<BackHandlerCallback>) => {
|
|
71
|
+
for (const handler of handlers) {
|
|
72
|
+
handlersRef.delete(handler);
|
|
73
|
+
contextRemoveBackHandler(handler);
|
|
74
|
+
}
|
|
75
|
+
},
|
|
76
|
+
[contextRemoveBackHandler]
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
const addEventListener = useCallback(
|
|
80
|
+
(handler: BackHandlerCallback) => {
|
|
81
|
+
handlersRef.add(handler);
|
|
82
|
+
contextAddBackHandler(handler);
|
|
83
|
+
|
|
84
|
+
return {
|
|
85
|
+
remove: () => removeHandler(handler),
|
|
86
|
+
};
|
|
87
|
+
},
|
|
88
|
+
[contextAddBackHandler, removeHandler]
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
useEffect(() => {
|
|
92
|
+
if (!isVisible) {
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
for (const handler of handlersRef) {
|
|
97
|
+
contextAddBackHandler(handler);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return () => {
|
|
101
|
+
for (const handler of handlersRef) {
|
|
102
|
+
contextRemoveBackHandler(handler);
|
|
103
|
+
}
|
|
104
|
+
};
|
|
105
|
+
}, [contextAddBackHandler, contextRemoveBackHandler, isVisible]);
|
|
106
|
+
|
|
107
|
+
const backHandler = useMemo((): BackHandlerControls => {
|
|
108
|
+
return {
|
|
109
|
+
addEventListener,
|
|
110
|
+
};
|
|
111
|
+
}, [addEventListener]);
|
|
112
|
+
|
|
113
|
+
return backHandler;
|
|
114
|
+
}
|