Files
react/components/tooltip/helper.ts
witt e8a7a17d56 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-06-23 23:05: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,
}
}