Skip to content

Commit 202c426

Browse files
committed
#20 fix: window is not defined
1 parent 33ff3ec commit 202c426

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/utils/getScrollbarWidth.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
import css from 'dom-css';
22

33
let scrollbarWidth: number | undefined = undefined;
4-
let pxRatio: number = window.screen.availWidth / document.documentElement.clientWidth;
4+
let pxRatio: number = getPxRatio();
55

66
export default function getScrollbarWidth() {
77
/**
88
* Check zoom ratio. If it was changed, then it would update scrollbatWidth
99
*/
10-
const newPxRatio = window.screen.availWidth / document.documentElement.clientWidth;
10+
const newPxRatio = getPxRatio();
11+
1112
if (pxRatio !== newPxRatio) {
1213
scrollbarWidth = getScrollbarWidthFromDom();
1314
}
@@ -42,3 +43,8 @@ function getScrollbarWidthFromDom() {
4243

4344
return result;
4445
}
46+
47+
function getPxRatio() {
48+
if (typeof window === 'undefined') return 1;
49+
return window.screen.availWidth / document.documentElement.clientWidth;
50+
}

0 commit comments

Comments
 (0)