mirror of
https://github.com/zoriya/react-native-web.git
synced 2026-05-30 09:19:21 +00:00
[fix] iOS not firing resize event when keyboard opens/closes
The workaround is listening to window.visualViewport 'resize' event. Fix #2430 Close #2438
This commit is contained in:
committed by
Nicolas Gallagher
parent
21b5d44c74
commit
ccfd936f27
+24
-4
@@ -52,13 +52,29 @@ function update() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const win = window;
|
const win = window;
|
||||||
const docEl = win.document.documentElement;
|
let height;
|
||||||
|
let width;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* iOS does not update viewport dimensions on keyboard open/close.
|
||||||
|
* window.visualViewport(https://developer.mozilla.org/en-US/docs/Web/API/VisualViewport)
|
||||||
|
* is used instead of document.documentElement.clientHeight (which remains as a fallback)
|
||||||
|
*/
|
||||||
|
if (win.visualViewport) {
|
||||||
|
const visualViewport = win.visualViewport;
|
||||||
|
height = Math.round(visualViewport.height);
|
||||||
|
width = Math.round(visualViewport.width);
|
||||||
|
} else {
|
||||||
|
const docEl = win.document.documentElement;
|
||||||
|
height = docEl.clientHeight;
|
||||||
|
width = docEl.clientWidth;
|
||||||
|
}
|
||||||
|
|
||||||
dimensions.window = {
|
dimensions.window = {
|
||||||
fontScale: 1,
|
fontScale: 1,
|
||||||
height: docEl.clientHeight,
|
height,
|
||||||
scale: win.devicePixelRatio || 1,
|
scale: win.devicePixelRatio || 1,
|
||||||
width: docEl.clientWidth
|
width
|
||||||
};
|
};
|
||||||
|
|
||||||
dimensions.screen = {
|
dimensions.screen = {
|
||||||
@@ -128,5 +144,9 @@ export default class Dimensions {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (canUseDOM) {
|
if (canUseDOM) {
|
||||||
window.addEventListener('resize', handleResize, false);
|
if (window.visualViewport) {
|
||||||
|
window.visualViewport.addEventListener('resize', handleResize, false);
|
||||||
|
} else {
|
||||||
|
window.addEventListener('resize', handleResize, false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user