geist-ui icon indicating copy to clipboard operation
geist-ui copied to clipboard

The position calculated by the getRectFromDOMWithContainer method needs to be subtracted from the offset of the body

Open shervinchen opened this issue 2 years ago • 0 comments
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,
  }
}

shervinchen avatar Apr 14 '23 14:04 shervinchen