Files
react/components/tooltip/helper.ts
witt de0c8fee97 feat: export all types related to components (#562)
* feat: export all types related to components

fix(tooltip): fix the vertical offset of the arrow

* refactor: optimize events of all popup related components

* test: append testcases for popup base component

* test: add testcase for visible events

* test: update snapshots
2021-08-13 17:10:59 +08:00

34 lines
862 B
TypeScript

import { MutableRefObject } from 'react'
export interface ReactiveDomReact {
top: number
bottom: number
left: number
right: number
width: number
height: number
}
const defaultRect: ReactiveDomReact = {
top: -1000,
left: -1000,
right: -1000,
bottom: -1000,
width: 0,
height: 0,
}
export const getRect = (ref: MutableRefObject<HTMLElement | null>): ReactiveDomReact => {
if (!ref || !ref.current) return defaultRect
const rect = ref.current.getBoundingClientRect()
return {
...rect,
width: rect.width || rect.right - rect.left,
height: rect.height || rect.bottom - rect.top,
top: rect.top + document.documentElement.scrollTop,
bottom: rect.bottom + document.documentElement.scrollTop,
left: rect.left + document.documentElement.scrollLeft,
right: rect.right + document.documentElement.scrollLeft,
}
}