forked from Expensify/App
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
142 lines (121 loc) · 6.62 KB
/
index.ts
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import {useContext, useEffect, useRef} from 'react';
// eslint-disable-next-line no-restricted-imports
import {Dimensions, useWindowDimensions} from 'react-native';
import type {ResponsiveLayoutProperties} from '@components/VideoPlayerContexts/FullScreenContext';
import {FullScreenContext} from '@components/VideoPlayerContexts/FullScreenContext';
import useDebouncedState from '@hooks/useDebouncedState';
import * as Browser from '@libs/Browser';
import variables from '@styles/variables';
import CONST from '@src/CONST';
import type WindowDimensions from './types';
const initalViewportHeight = window.visualViewport?.height ?? window.innerHeight;
const tagNamesOpenKeyboard = ['INPUT', 'TEXTAREA'];
const isMobile = Browser.isMobile();
/**
* A wrapper around React Native's useWindowDimensions hook.
*/
export default function (useCachedViewportHeight = false): WindowDimensions {
const {isFullScreenRef, lockedWindowDimensionsRef, lockWindowDimensions, unlockWindowDimensions} = useContext(FullScreenContext) ?? {
isFullScreenRef: useRef(false),
lockedWindowDimensionsRef: useRef<ResponsiveLayoutProperties | null>(null),
lockWindowDimensions: () => {},
unlockWindowDimensions: () => {},
};
const isCachedViewportHeight = useCachedViewportHeight && Browser.isMobileWebKit();
const cachedViewportHeightWithKeyboardRef = useRef(initalViewportHeight);
const {width: windowWidth, height: windowHeight} = useWindowDimensions();
// These are the same as the ones in useResponsiveLayout, but we need to redefine them here to avoid cyclic dependency.
// When the soft keyboard opens on mWeb, the window height changes. Use static screen height instead to get real screenHeight.
const screenHeight = Dimensions.get('screen').height;
const isExtraSmallScreenHeight = screenHeight <= variables.extraSmallMobileResponsiveHeightBreakpoint;
const isSmallScreenWidth = windowWidth <= variables.mobileResponsiveWidthBreakpoint;
const isMediumScreenWidth = windowWidth > variables.mobileResponsiveWidthBreakpoint && windowWidth <= variables.tabletResponsiveWidthBreakpoint;
const isLargeScreenWidth = windowWidth > variables.tabletResponsiveWidthBreakpoint;
const isExtraSmallScreenWidth = windowWidth <= variables.extraSmallMobileResponsiveWidthBreakpoint;
const lowerScreenDimmension = Math.min(windowWidth, windowHeight);
const isSmallScreen = lowerScreenDimmension <= variables.mobileResponsiveWidthBreakpoint;
const responsiveLayoutResults = {
isSmallScreenWidth,
isExtraSmallScreenHeight,
isExtraSmallScreenWidth,
isMediumScreenWidth,
isLargeScreenWidth,
isSmallScreen,
};
const [, cachedViewportHeight, setCachedViewportHeight] = useDebouncedState(windowHeight, CONST.TIMING.RESIZE_DEBOUNCE_TIME);
const handleFocusIn = useRef((event: FocusEvent) => {
const targetElement = event.target as HTMLElement;
if (tagNamesOpenKeyboard.includes(targetElement.tagName)) {
setCachedViewportHeight(cachedViewportHeightWithKeyboardRef.current);
}
});
useEffect(() => {
if (!isCachedViewportHeight) {
return;
}
const handleFocusInValue = handleFocusIn.current;
window.addEventListener('focusin', handleFocusInValue);
return () => {
window.removeEventListener('focusin', handleFocusInValue);
};
}, [isCachedViewportHeight]);
const handleFocusOut = useRef((event: FocusEvent) => {
const targetElement = event.target as HTMLElement;
if (tagNamesOpenKeyboard.includes(targetElement.tagName)) {
setCachedViewportHeight(initalViewportHeight);
}
});
useEffect(() => {
if (!isCachedViewportHeight) {
return;
}
const handleFocusOutValue = handleFocusOut.current;
window.addEventListener('focusout', handleFocusOutValue);
return () => {
window.removeEventListener('focusout', handleFocusOutValue);
};
}, [isCachedViewportHeight]);
useEffect(() => {
if (!isCachedViewportHeight && windowHeight >= cachedViewportHeightWithKeyboardRef.current) {
return;
}
setCachedViewportHeight(windowHeight);
// eslint-disable-next-line react-compiler/react-compiler, react-hooks/exhaustive-deps
}, [windowHeight, isCachedViewportHeight]);
useEffect(() => {
if (!isCachedViewportHeight || !window.matchMedia('(orientation: portrait)').matches || windowHeight >= initalViewportHeight) {
return;
}
cachedViewportHeightWithKeyboardRef.current = windowHeight;
}, [isCachedViewportHeight, windowHeight]);
const windowDimensions = {
windowWidth,
windowHeight: isCachedViewportHeight ? cachedViewportHeight : windowHeight,
responsiveLayoutResults,
};
if (!lockedWindowDimensionsRef.current && !isFullScreenRef.current) {
return windowDimensions;
}
const didScreenChangeOrientation =
isMobile &&
lockedWindowDimensionsRef.current &&
isExtraSmallScreenWidth === lockedWindowDimensionsRef.current.responsiveLayoutResults.isExtraSmallScreenHeight &&
isSmallScreenWidth === lockedWindowDimensionsRef.current.responsiveLayoutResults.isSmallScreen &&
isMediumScreenWidth === lockedWindowDimensionsRef.current.responsiveLayoutResults.isMediumScreenWidth &&
isLargeScreenWidth === lockedWindowDimensionsRef.current.responsiveLayoutResults.isLargeScreenWidth &&
lockedWindowDimensionsRef.current.windowWidth !== windowWidth &&
lockedWindowDimensionsRef.current.windowHeight !== windowHeight;
// if video is in fullscreen mode, lock the window dimensions since they can change and casue whole app to re-render
if (!lockedWindowDimensionsRef.current || didScreenChangeOrientation) {
lockWindowDimensions(windowDimensions);
return windowDimensions;
}
const didScreenReturnToOriginalSize = lockedWindowDimensionsRef.current.windowWidth === windowWidth && lockedWindowDimensionsRef.current.windowHeight === windowHeight;
// if video exits fullscreen mode, unlock the window dimensions
if (lockedWindowDimensionsRef.current && !isFullScreenRef.current && didScreenReturnToOriginalSize) {
const lastLockedWindowDimensions = {...lockedWindowDimensionsRef.current};
unlockWindowDimensions();
return {windowWidth: lastLockedWindowDimensions.windowWidth, windowHeight: lastLockedWindowDimensions.windowHeight};
}
return {windowWidth: lockedWindowDimensionsRef.current.windowWidth, windowHeight: lockedWindowDimensionsRef.current.windowHeight};
}