diff options
Diffstat (limited to 'popperjs/package/lib/dom-utils/getViewportRect.js.flow')
-rw-r--r-- | popperjs/package/lib/dom-utils/getViewportRect.js.flow | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/popperjs/package/lib/dom-utils/getViewportRect.js.flow b/popperjs/package/lib/dom-utils/getViewportRect.js.flow new file mode 100644 index 0000000..44421f8 --- /dev/null +++ b/popperjs/package/lib/dom-utils/getViewportRect.js.flow @@ -0,0 +1,46 @@ +// @flow +import getWindow from './getWindow'; +import getDocumentElement from './getDocumentElement'; +import getWindowScrollBarX from './getWindowScrollBarX'; + +export default function getViewportRect(element: Element) { + const win = getWindow(element); + const html = getDocumentElement(element); + const visualViewport = win.visualViewport; + + let width = html.clientWidth; + let height = html.clientHeight; + let x = 0; + let y = 0; + + // NB: This isn't supported on iOS <= 12. If the keyboard is open, the popper + // can be obscured underneath it. + // Also, `html.clientHeight` adds the bottom bar height in Safari iOS, even + // if it isn't open, so if this isn't available, the popper will be detected + // to overflow the bottom of the screen too early. + if (visualViewport) { + width = visualViewport.width; + height = visualViewport.height; + + // Uses Layout Viewport (like Chrome; Safari does not currently) + // In Chrome, it returns a value very close to 0 (+/-) but contains rounding + // errors due to floating point numbers, so we need to check precision. + // Safari returns a number <= 0, usually < -1 when pinch-zoomed + + // Feature detection fails in mobile emulation mode in Chrome. + // Math.abs(win.innerWidth / visualViewport.scale - visualViewport.width) < + // 0.001 + // Fallback here: "Not Safari" userAgent + if (!/^((?!chrome|android).)*safari/i.test(navigator.userAgent)) { + x = visualViewport.offsetLeft; + y = visualViewport.offsetTop; + } + } + + return { + width, + height, + x: x + getWindowScrollBarX(element), + y, + }; +} |