-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathindex.web.tsx
76 lines (68 loc) · 3.15 KB
/
index.web.tsx
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import {useIsFocused, useRoute} from '@react-navigation/native';
import FocusTrap from 'focus-trap-react';
import React, {useMemo} from 'react';
import BOTTOM_TAB_SCREENS from '@components/FocusTrap/BOTTOM_TAB_SCREENS';
import sharedTrapStack from '@components/FocusTrap/sharedTrapStack';
import TOP_TAB_SCREENS from '@components/FocusTrap/TOP_TAB_SCREENS';
import WIDE_LAYOUT_INACTIVE_SCREENS from '@components/FocusTrap/WIDE_LAYOUT_INACTIVE_SCREENS';
import useWindowDimensions from '@hooks/useWindowDimensions';
import canFocusInputOnScreenFocus from '@libs/canFocusInputOnScreenFocus';
import CONST from '@src/CONST';
import type FocusTrapProps from './FocusTrapProps';
function FocusTrapForScreen({children, focusTrapSettings}: FocusTrapProps) {
const isFocused = useIsFocused();
const route = useRoute();
const {isSmallScreenWidth} = useWindowDimensions();
const isActive = useMemo(() => {
if (typeof focusTrapSettings?.active !== 'undefined') {
return focusTrapSettings.active;
}
// Focus trap can't be active on bottom tab screens because it would block access to the tab bar.
if (BOTTOM_TAB_SCREENS.find((screen) => screen === route.name)) {
return false;
}
// in top tabs only focus trap for currently shown tab should be active
if (TOP_TAB_SCREENS.find((screen) => screen === route.name)) {
return isFocused;
}
// Focus trap can't be active on these screens if the layout is wide because they may be displayed side by side.
if (WIDE_LAYOUT_INACTIVE_SCREENS.includes(route.name) && !isSmallScreenWidth) {
return false;
}
return true;
}, [isFocused, isSmallScreenWidth, route.name, focusTrapSettings?.active]);
return (
<FocusTrap
active={isActive}
paused={!isFocused}
containerElements={focusTrapSettings?.containerElements?.length ? focusTrapSettings.containerElements : undefined}
focusTrapOptions={{
trapStack: sharedTrapStack,
allowOutsideClick: true,
fallbackFocus: document.body,
delayInitialFocus: CONST.ANIMATED_TRANSITION,
initialFocus: (focusTrapContainers) => {
if (!canFocusInputOnScreenFocus()) {
return false;
}
const isFocusedElementInsideContainer = focusTrapContainers?.some((container) => container.contains(document.activeElement));
if (isFocusedElementInsideContainer) {
return false;
}
return undefined;
},
setReturnFocus: (element) => {
if (document.activeElement && document.activeElement !== document.body) {
return false;
}
return element;
},
...(focusTrapSettings?.focusTrapOptions ?? {}),
}}
>
{children}
</FocusTrap>
);
}
FocusTrapForScreen.displayName = 'FocusTrapForScreen';
export default FocusTrapForScreen;