| 1 |
import getOffsetRectRelativeToArbitraryNode from './getOffsetRectRelativeToArbitraryNode'; |
| 2 |
import getScroll from './getScroll'; |
| 3 |
import getClientRect from './getClientRect'; |
| 4 |
|
| 5 |
export default function getViewportOffsetRectRelativeToArtbitraryNode(element, excludeScroll = false) { |
| 6 |
const html = element.ownerDocument.documentElement; |
| 7 |
const relativeOffset = getOffsetRectRelativeToArbitraryNode(element, html); |
| 8 |
const width = Math.max(html.clientWidth, window.innerWidth || 0); |
| 9 |
const height = Math.max(html.clientHeight, window.innerHeight || 0); |
| 10 |
|
| 11 |
const scrollTop = !excludeScroll ? getScroll(html) : 0; |
| 12 |
const scrollLeft = !excludeScroll ? getScroll(html, 'left') : 0; |
| 13 |
|
| 14 |
const offset = { |
| 15 |
top: scrollTop - relativeOffset.top + relativeOffset.marginTop, |
| 16 |
left: scrollLeft - relativeOffset.left + relativeOffset.marginLeft, |
| 17 |
width, |
| 18 |
height, |
| 19 |
}; |
| 20 |
|
| 21 |
return getClientRect(offset); |
| 22 |
} |