[change] Fix the Dimensions.window value

Fixes issues in Safari from using window.innerWidth. Using
documentElement.clientWidth doesn't have issues and provides a more practical
value for the canvas width that excludes the width of scrollbars.

Fix #1369
Fix #1905
This commit is contained in:
Nicolas Gallagher
2021-04-08 18:20:19 -07:00
parent c4e2d7a919
commit 89cf78d529
2 changed files with 42 additions and 20 deletions
+30 -19
View File
@@ -27,19 +27,20 @@ type DimensionKey = 'window' | 'screen';
type DimensionEventListenerType = 'change';
const win = canUseDOM
? window
: {
devicePixelRatio: undefined,
innerHeight: (undefined: any),
innerWidth: (undefined: any),
screen: {
height: (undefined: any),
width: (undefined: any)
}
};
const dimensions = {};
const dimensions = {
window: {
fontScale: 1,
height: 0,
scale: 1,
width: 0
},
screen: {
fontScale: 1,
height: 0,
scale: 1,
width: 0
}
};
const listeners = {};
export default class Dimensions {
@@ -53,18 +54,29 @@ export default class Dimensions {
if (canUseDOM) {
invariant(false, 'Dimensions cannot be set in the browser');
} else {
dimensions.screen = initialDimensions.screen;
dimensions.window = initialDimensions.window;
if (initialDimensions.screen != null) {
dimensions.screen = initialDimensions.screen;
}
if (initialDimensions.window != null) {
dimensions.window = initialDimensions.window;
}
}
}
}
static _update() {
if (!canUseDOM) {
return;
}
const win = window;
const docEl = win.document.documentElement;
dimensions.window = {
fontScale: 1,
height: win.innerHeight,
height: docEl.clientHeight,
scale: win.devicePixelRatio || 1,
width: win.innerWidth
width: docEl.clientWidth
};
dimensions.screen = {
@@ -97,8 +109,7 @@ export default class Dimensions {
}
}
Dimensions._update();
if (canUseDOM) {
Dimensions._update();
window.addEventListener('resize', Dimensions._update, false);
}
+12 -1
View File
@@ -6,5 +6,16 @@ class ResizeObserver {
observe() {}
unobserve() {}
}
window.ResizeObserver = ResizeObserver;
// JSDOM doesn't provide values for 'clientWidth' etc
Object.defineProperty(window.document.documentElement, 'clientHeight', {
get: function () {
return this._jsdomClientWidth || window.innerHeight;
}
});
Object.defineProperty(window.document.documentElement, 'clientWidth', {
get: function () {
return this._jsdomClientWidth || window.innerWidth;
}
});