@yandex/ui
Version:
Yandex UI components
59 lines (58 loc) • 1.89 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.unlock = exports.lock = void 0;
var utils_1 = require("./utils");
var randomKey = Math.random()
.toString(36)
.slice(2);
var SCROLL_LOCK_STATE_KEY = "__scrollLockState$" + randomKey;
function getLockState(node) {
return node[SCROLL_LOCK_STATE_KEY];
}
function setLockState(node, state) {
node[SCROLL_LOCK_STATE_KEY] = state;
}
function clearLockState(node) {
delete node[SCROLL_LOCK_STATE_KEY];
}
function hasStaticVerticalScroll(node) {
var overflowY = getComputedStyle(node).overflowY;
return /scroll/.test(overflowY);
}
function getScrollbarSize(node) {
var hasScrollbarRoot = utils_1.isRootHTMLElement(node) && window.innerWidth - document.documentElement.clientWidth > 0;
var hasScrollbarNode = node.scrollHeight > node.clientHeight;
if (hasScrollbarRoot || hasScrollbarNode || hasStaticVerticalScroll(node)) {
return utils_1.getScrollBarGap();
}
return 0;
}
function lock(container) {
var state = getLockState(container);
if (state) {
state.count++;
return;
}
var scrollBarSize = getScrollbarSize(container);
var computedPaddingRight = parseInt(getComputedStyle(container).getPropertyValue('padding-right'), 10);
var initialStyle = utils_1.setStyle(container, {
paddingRight: computedPaddingRight + scrollBarSize + "px",
overflow: 'hidden',
overflowX: 'hidden',
overflowY: 'hidden',
});
setLockState(container, { initialStyle: initialStyle, count: 1 });
}
exports.lock = lock;
function unlock(container) {
var state = getLockState(container);
if (!state) {
return;
}
state.count--;
if (state.count === 0) {
utils_1.setStyle(container, state.initialStyle);
clearLockState(container);
}
}
exports.unlock = unlock;