geist-ui
geist-ui copied to clipboard
The position calculated by the getRectFromDOMWithContainer method needs to be subtracted from the offset of the body
trafficstars
Bug report 🐞
Version & Environment
- Version of browser: Chrome latest version
- Version of
geist-ui/core: latest version
Expected Behaviour
The position calculated by the getRectFromDOMWithContainer method needs to be subtracted from the offset of the body
Actual results (or Errors)
If body or root element is offset(for example: body or root element has a margin), getRectFromDOMWithContainer can’t get the right position
I think the correct implementation should be:
const getRectFromDOMWithContainer = (
domRect?: DOMRect,
getContainer?: () => HTMLElement | null,
): ReactiveDomReact => {
if (!domRect) return defaultRect
const bodyRect = document.body.getBoundingClientRect()
const container = getContainer ? getContainer() : null
const { top: offsetTop, left: offsetLeft } = getElementOffset(container)
return {
...domRect,
width: domRect.width || domRect.right - domRect.left,
height: domRect.height || domRect.top - domRect.bottom,
top: container ? domRect.bottom + container.scrollTop - offsetTop : domRect.bottom - bodyRect.top,
left: container ? domRect.left + container.scrollLeft - offsetLeft : domRect.left - bodyRect.left,
elementTop: container ? domRect.top - container.top - offsetTop : domRect.top - bodyRect.top,
}
}