Skip to content

Commit 3900765

Browse files
committed
[material-ui] Adjust getScrollbarSize to account for browser zoom level
1 parent bbb595a commit 3900765

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed
Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,24 @@
11
// A change of the browser zoom change the scrollbar size.
2-
// Credit https://github.com/twbs/bootstrap/blob/488fd8afc535ca3a6ad4dc581f5e89217b6a36ac/js/src/util/scrollbar.js#L14-L18
2+
// Credit https://github.com/twbs/bootstrap/blob/0907244256d923807c3a4e55f4ea606b9558d0ca/js/modal.js#L214-L221
3+
// and https://github.com/twbs/bootstrap/blob/0907244256d923807c3a4e55f4ea606b9558d0ca/less/modals.less#L122-L128
34
export default function getScrollbarSize(win: Window = window): number {
4-
// https://developer.mozilla.org/en-US/docs/Web/API/Window/innerWidth#usage_notes
5-
const documentWidth = win.document.documentElement.clientWidth;
6-
return win.innerWidth - documentWidth;
5+
const scrollDiv = win.document.createElement('div');
6+
7+
scrollDiv.style.setProperty('position', 'absolute');
8+
scrollDiv.style.setProperty('top', '-9999px');
9+
scrollDiv.style.setProperty('width', '50px');
10+
scrollDiv.style.setProperty('height', '50px');
11+
scrollDiv.style.setProperty('overflow', 'scroll');
12+
// Invert the zoom level to get 100% sized scrollbars for the element
13+
scrollDiv.style.setProperty('zoom', `${1 / win.devicePixelRatio}`);
14+
15+
win.document.body.append(scrollDiv);
16+
17+
const unZoomedScrollbarWidth = scrollDiv.offsetWidth - scrollDiv.clientWidth;
18+
19+
win.document.body.removeChild(scrollDiv);
20+
21+
const zoomedScrollbarWidth = unZoomedScrollbarWidth / win.devicePixelRatio;
22+
23+
return zoomedScrollbarWidth;
724
}

0 commit comments

Comments
 (0)