| 1 |
import getStyleComputedProperty from './getStyleComputedProperty'; |
| 2 |
import getBordersSize from './getBordersSize'; |
| 3 |
import getWindowSizes from './getWindowSizes'; |
| 4 |
import getScroll from './getScroll'; |
| 5 |
import getClientRect from './getClientRect'; |
| 6 |
import isIE from './isIE'; |
| 7 |
|
| 8 |
/** |
| 9 |
* Get bounding client rect of given element |
| 10 |
* @method |
| 11 |
* @memberof Popper.Utils |
| 12 |
* @param {HTMLElement} element |
| 13 |
* @return {Object} client rect |
| 14 |
*/ |
| 15 |
export default function getBoundingClientRect(element) { |
| 16 |
let rect = {}; |
| 17 |
|
| 18 |
// IE10 10 FIX: Please, don't ask, the element isn't |
| 19 |
// considered in DOM in some circumstances... |
| 20 |
// This isn't reproducible in IE10 compatibility mode of IE11 |
| 21 |
try { |
| 22 |
if (isIE(10)) { |
| 23 |
rect = element.getBoundingClientRect(); |
| 24 |
const scrollTop = getScroll(element, 'top'); |
| 25 |
const scrollLeft = getScroll(element, 'left'); |
| 26 |
rect.top += scrollTop; |
| 27 |
rect.left += scrollLeft; |
| 28 |
rect.bottom += scrollTop; |
| 29 |
rect.right += scrollLeft; |
| 30 |
} |
| 31 |
else { |
| 32 |
rect = element.getBoundingClientRect(); |
| 33 |
} |
| 34 |
} |
| 35 |
catch(e){} |
| 36 |
|
| 37 |
const result = { |
| 38 |
left: rect.left, |
| 39 |
top: rect.top, |
| 40 |
width: rect.right - rect.left, |
| 41 |
height: rect.bottom - rect.top, |
| 42 |
}; |
| 43 |
|
| 44 |
// subtract scrollbar size from sizes |
| 45 |
const sizes = element.nodeName === 'HTML' ? getWindowSizes(element.ownerDocument) : {}; |
| 46 |
const width = |
| 47 |
sizes.width || element.clientWidth || result.width; |
| 48 |
const height = |
| 49 |
sizes.height || element.clientHeight || result.height; |
| 50 |
|
| 51 |
let horizScrollbar = element.offsetWidth - width; |
| 52 |
let vertScrollbar = element.offsetHeight - height; |
| 53 |
|
| 54 |
// if an hypothetical scrollbar is detected, we must be sure it's not a `border` |
| 55 |
// we make this check conditional for performance reasons |
| 56 |
if (horizScrollbar || vertScrollbar) { |
| 57 |
const styles = getStyleComputedProperty(element); |
| 58 |
horizScrollbar -= getBordersSize(styles, 'x'); |
| 59 |
vertScrollbar -= getBordersSize(styles, 'y'); |
| 60 |
|
| 61 |
result.width -= horizScrollbar; |
| 62 |
result.height -= vertScrollbar; |
| 63 |
} |
| 64 |
|
| 65 |
return getClientRect(result); |
| 66 |
} |