@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.
@@ -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
+ }