@procore/core-react
Version:
React library of Procore Design Guidelines
31 lines • 1.41 kB
JavaScript
import React from 'react';
import { isIE11 } from '../_utils/isIE11';
export function useScrollLock(shouldScrollLock) {
React.useEffect(function () {
var IE11 = isIE11();
var bodyRightPadding = document.body.style.paddingRight;
var bodyMaxWidth = document.body.style.maxWidth;
var scrollBarWidthBefore = window.innerWidth - document.documentElement.clientWidth;
if (!IE11 && shouldScrollLock) {
var bodyPosition = document.body.style.position;
var bodyTop = document.body.style.top;
document.body.style.maxWidth = '100vw';
if (scrollBarWidthBefore > 0) {
var scrollbarWidthAfter = document.documentElement.clientWidth - document.documentElement.offsetWidth;
var scrollbarWidth = scrollBarWidthBefore - scrollbarWidthAfter;
document.body.style.paddingRight = "".concat(scrollbarWidth, "px");
}
document.body.style.top = "-".concat(window.scrollY, "px");
document.body.style.position = 'fixed';
return function () {
var scrollY = document.body.style.top;
document.body.style.position = bodyPosition;
document.body.style.top = bodyTop;
document.body.style.paddingRight = bodyRightPadding;
document.body.style.maxWidth = bodyMaxWidth;
window.scrollTo(0, parseInt(scrollY || '0') * -1);
};
}
}, [shouldScrollLock]);
}
//# sourceMappingURL=ScrollLock.js.map