feat(@vant/use): add useRect

This commit is contained in:
chenjiahan
2020-10-05 11:13:22 +08:00
parent 5f2de02cc2
commit 8d4135a690
4 changed files with 86 additions and 0 deletions
+38
View File
@@ -0,0 +1,38 @@
import { Ref, unref } from 'vue';
function isWindow(val: unknown): val is Window {
return val === window;
}
export const useRect = (
elementRef: (Element | Window) | Ref<Element | Window>
) => {
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.getBoundingClientRect) {
return element.getBoundingClientRect();
}
return {
top: 0,
left: 0,
right: 0,
bottom: 0,
width: 0,
height: 0,
};
};