Files
vant/packages/vant-use/src/useRect/index.ts
T
neverlandandGitHub 3dcc92a5c0 chore: bump prettier v3 and format all code (#12111)
* chore: bump prettier v3 and format all code

* chore: mjs config

* chore: revert

* chore: revert

* chore: update lock
2023-07-22 14:14:14 +08:00

32 lines
677 B
TypeScript

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