chenjiahan a746ded63e Revert "fix(@vant/use): useRect should always return DOMRect (#8147)"
This reverts commit 0d0310d34e230115a0cd3389835a0fdd2109c66d.
2021-02-13 20:59:27 +08:00

39 lines
681 B
TypeScript

import { Ref, unref } from 'vue';
function isWindow(val: unknown): val is Window {
return val === window;
}
export const useRect = (
elementRef: (Element | Window) | Ref<Element | Window | undefined>
) => {
const element = unref(elementRef);
if (isWindow(element)) {
const width = element.innerWidth;
const height = element.innerHeight;
return {
top: 0,
left: 0,
right: width,
bottom: height,
width,
height,
};
}
if (element && element.getBoundingClientRect) {
return element.getBoundingClientRect();
}
return {
top: 0,
left: 0,
right: 0,
bottom: 0,
width: 0,
height: 0,
};
};