diff --git a/packages/vant-use/src/useRect/index.ts b/packages/vant-use/src/useRect/index.ts index e02b96af4..16494cdbc 100644 --- a/packages/vant-use/src/useRect/index.ts +++ b/packages/vant-use/src/useRect/index.ts @@ -4,6 +4,17 @@ function isWindow(val: unknown): val is Window { return val === window; } +function makeDOMRect(width: number, height: number) { + return { + top: 0, + left: 0, + right: width, + bottom: height, + width, + height, + } as DOMRect; +} + export const useRect = ( elementRef: (Element | Window) | Ref ) => { @@ -12,27 +23,12 @@ export const useRect = ( if (isWindow(element)) { const width = element.innerWidth; const height = element.innerHeight; - - return { - top: 0, - left: 0, - right: width, - bottom: height, - width, - height, - }; + return makeDOMRect(width, height); } if (element && element.getBoundingClientRect) { return element.getBoundingClientRect(); } - return { - top: 0, - left: 0, - right: 0, - bottom: 0, - width: 0, - height: 0, - }; + return makeDOMRect(0, 0); };