Skip to content

Commit

Permalink
[fix] iOS not firing resize event when keyboard opens/closes
Browse files Browse the repository at this point in the history
The workaround is listening to window.visualViewport 'resize' event.

Fix #2430
Close #2438
  • Loading branch information
rawalyogendra authored and necolas committed Jan 25, 2023
1 parent 21b5d44 commit ccfd936
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions packages/react-native-web/src/exports/Dimensions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,29 @@ function update() {
}

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 = {
fontScale: 1,
height: docEl.clientHeight,
height,
scale: win.devicePixelRatio || 1,
width: docEl.clientWidth
width
};

dimensions.screen = {
Expand Down Expand Up @@ -128,5 +144,9 @@ export default class Dimensions {
}

if (canUseDOM) {
window.addEventListener('resize', handleResize, false);
if (window.visualViewport) {
window.visualViewport.addEventListener('resize', handleResize, false);
} else {
window.addEventListener('resize', handleResize, false);
}
}

0 comments on commit ccfd936

Please sign in to comment.