File tree Expand file tree Collapse file tree 1 file changed +21
-4
lines changed
packages/mui-utils/src/getScrollbarSize Expand file tree Collapse file tree 1 file changed +21
-4
lines changed Original file line number Diff line number Diff line change 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
34export 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}
You can’t perform that action at this time.
0 commit comments