-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathuseIsFocused.ts
More file actions
41 lines (37 loc) · 1.36 KB
/
useIsFocused.ts
File metadata and controls
41 lines (37 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/**
* Safe wrapper around `@react-navigation/native`'s `useIsFocused` hook.
*
* If `@react-navigation/native` is not installed (i.e., the consumer does not
* use React Navigation), the hook falls back to always returning `true` —
* meaning the component behaves as if it is always focused.
*
* This makes `@react-navigation/native` an optional peer dependency so that
* consumers who don't use the Inbox UI (or who use a different navigation
* library) are not forced to install it.
*
* @see https://github.com/Iterable/react-native-sdk/issues/770
*/
// Use globalThis.require to avoid needing @types/node in the tsconfig while
// still performing a synchronous optional require at module-load time.
declare const globalThis: { require?: (id: string) => unknown };
let useIsFocusedFromNav: (() => boolean) | undefined;
try {
const reactNavigation = globalThis.require?.('@react-navigation/native') as
| { useIsFocused?: () => boolean }
| undefined;
useIsFocusedFromNav = reactNavigation?.useIsFocused;
} catch {
// @react-navigation/native is not installed — that's fine.
}
/**
* Returns whether the screen is currently focused.
*
* Uses React Navigation's `useIsFocused` when available, otherwise defaults
* to `true`.
*/
export function useIsFocused(): boolean {
if (useIsFocusedFromNav) {
return useIsFocusedFromNav();
}
return true;
}