You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
63 lines
1.6 KiB
63 lines
1.6 KiB
3 years ago
|
/* eslint-disable no-param-reassign */
|
||
|
|
||
|
let cached: number;
|
||
|
|
||
|
export default function getScrollBarSize(fresh?: boolean) {
|
||
|
if (typeof document === 'undefined') {
|
||
|
return 0;
|
||
|
}
|
||
7 years ago
|
|
||
|
if (fresh || cached === undefined) {
|
||
6 years ago
|
const inner = document.createElement('div');
|
||
|
inner.style.width = '100%';
|
||
|
inner.style.height = '200px';
|
||
7 years ago
|
|
||
6 years ago
|
const outer = document.createElement('div');
|
||
|
const outerStyle = outer.style;
|
||
7 years ago
|
|
||
6 years ago
|
outerStyle.position = 'absolute';
|
||
3 years ago
|
outerStyle.top = '0';
|
||
|
outerStyle.left = '0';
|
||
6 years ago
|
outerStyle.pointerEvents = 'none';
|
||
|
outerStyle.visibility = 'hidden';
|
||
|
outerStyle.width = '200px';
|
||
|
outerStyle.height = '150px';
|
||
|
outerStyle.overflow = 'hidden';
|
||
7 years ago
|
|
||
6 years ago
|
outer.appendChild(inner);
|
||
7 years ago
|
|
||
6 years ago
|
document.body.appendChild(outer);
|
||
7 years ago
|
|
||
6 years ago
|
const widthContained = inner.offsetWidth;
|
||
|
outer.style.overflow = 'scroll';
|
||
|
let widthScroll = inner.offsetWidth;
|
||
7 years ago
|
|
||
|
if (widthContained === widthScroll) {
|
||
6 years ago
|
widthScroll = outer.clientWidth;
|
||
7 years ago
|
}
|
||
|
|
||
6 years ago
|
document.body.removeChild(outer);
|
||
7 years ago
|
|
||
6 years ago
|
cached = widthContained - widthScroll;
|
||
7 years ago
|
}
|
||
6 years ago
|
return cached;
|
||
7 years ago
|
}
|
||
3 years ago
|
|
||
|
function ensureSize(str: string) {
|
||
|
const match = str.match(/^(.*)px$/);
|
||
|
const value = Number(match?.[1]);
|
||
|
return Number.isNaN(value) ? getScrollBarSize() : value;
|
||
|
}
|
||
|
|
||
|
export function getTargetScrollBarSize(target: HTMLElement) {
|
||
|
if (typeof document === 'undefined' || !target || !(target instanceof Element)) {
|
||
|
return { width: 0, height: 0 };
|
||
|
}
|
||
|
|
||
|
const { width, height } = getComputedStyle(target, '::-webkit-scrollbar');
|
||
|
return {
|
||
|
width: ensureSize(width),
|
||
|
height: ensureSize(height),
|
||
|
};
|
||
|
}
|