2023-11-28 12:47:55 -05:00
|
|
|
import { isMobile } from '../is_mobile';
|
|
|
|
|
|
|
|
let cachedScrollbarWidth: number | null = null;
|
2019-09-29 08:30:58 -04:00
|
|
|
|
|
|
|
const getActualScrollbarWidth = () => {
|
|
|
|
const outer = document.createElement('div');
|
|
|
|
outer.style.visibility = 'hidden';
|
|
|
|
outer.style.overflow = 'scroll';
|
|
|
|
document.body.appendChild(outer);
|
|
|
|
|
|
|
|
const inner = document.createElement('div');
|
|
|
|
outer.appendChild(inner);
|
|
|
|
|
|
|
|
const scrollbarWidth = outer.offsetWidth - inner.offsetWidth;
|
2023-11-28 12:47:55 -05:00
|
|
|
outer.remove();
|
2019-09-29 08:30:58 -04:00
|
|
|
|
|
|
|
return scrollbarWidth;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const getScrollbarWidth = () => {
|
|
|
|
if (cachedScrollbarWidth !== null) {
|
|
|
|
return cachedScrollbarWidth;
|
|
|
|
}
|
|
|
|
|
2023-11-28 12:47:55 -05:00
|
|
|
const scrollbarWidth = isMobile(window.innerWidth)
|
|
|
|
? 0
|
|
|
|
: getActualScrollbarWidth();
|
2019-09-29 08:30:58 -04:00
|
|
|
cachedScrollbarWidth = scrollbarWidth;
|
|
|
|
|
|
|
|
return scrollbarWidth;
|
|
|
|
};
|