Skip to content

Commit b364706

Browse files
authored
Merge pull request #10 from rawalyogendra/exp-safari-viewport-fix
Fix: Safari Keyboard open/close viewport height update
2 parents bd1f7b8 + 2e0c6b8 commit b364706

File tree

1 file changed

+29
-4
lines changed
  • packages/react-native-web/src/exports/Dimensions

1 file changed

+29
-4
lines changed

packages/react-native-web/src/exports/Dimensions/index.js

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,30 @@ export default class Dimensions {
7777
}
7878

7979
const win = window;
80-
const docEl = win.document.documentElement;
80+
let height;
81+
let width;
82+
83+
/*
84+
* iOS does not update viewport dimensions on keyboard open/close
85+
* So, we are using window.visualViewport(https://developer.mozilla.org/en-US/docs/Web/API/VisualViewport)
86+
* instead of document.documentElement.clientHeight
87+
* document.documentElement.clientWidth is used as a fallback
88+
*/
89+
if (win.visualViewport) {
90+
const visualViewport = win.visualViewport;
91+
height = Math.round(visualViewport.height);
92+
width = Math.round(visualViewport.width);
93+
} else {
94+
const docEl = win.document.documentElement;
95+
height = docEl.clientHeight;
96+
width = docEl.clientWidth;
97+
}
8198

8299
dimensions.window = {
83100
fontScale: 1,
84-
height: docEl.clientHeight,
101+
height,
85102
scale: win.devicePixelRatio || 1,
86-
width: docEl.clientWidth
103+
width
87104
};
88105

89106
dimensions.screen = {
@@ -125,5 +142,13 @@ export default class Dimensions {
125142
}
126143

127144
if (canUseDOM) {
128-
window.addEventListener('resize', Dimensions._update, false);
145+
/*
146+
* Same as in _update method, we are
147+
* keeping the previous implementation as a fallback
148+
*/
149+
if (window.visualViewport) {
150+
window.visualViewport.addEventListener('resize', Dimensions._update, false);
151+
} else {
152+
window.addEventListener('resize', Dimensions._update, false);
153+
}
129154
}

0 commit comments

Comments
 (0)