@kiwicom/orbit-components
Version:
Orbit-components is a React component library which provides developers with the easiest possible way of building Kiwi.com’s products.
46 lines (37 loc) • 1.62 kB
JavaScript
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
import { useLayoutEffect } from "react";
import { disableBodyScroll, clearAllBodyScrollLocks } from "body-scroll-lock";
import useTheme from "../useTheme";
function lockScrolling(el) {
disableBodyScroll(el); // body-scroll-lock sets fixed position on requestAnimationFrame
// so we need to use it as well
window.requestAnimationFrame(function () {
if (document.body && document.body.style.position === "fixed") {
// avoid a bug on iOS Safari where body doesn't take up full width
document.body.style.right = "0";
}
});
}
function unlockScrolling() {
if (document.body && document.body.style.position === "fixed") {
document.body.style.right = "";
}
clearAllBodyScrollLocks();
}
var useLockScrolling = function useLockScrolling(ref) {
var lock = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
var dependencies = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
var _useTheme = useTheme(),
_useTheme$lockScrolli = _useTheme.lockScrolling,
themeLockScrolling = _useTheme$lockScrolli === void 0 ? true : _useTheme$lockScrolli;
useLayoutEffect(function () {
if (ref.current && lock && themeLockScrolling) {
lockScrolling(ref.current);
}
return function () {
unlockScrolling();
}; // the rule doesn't know that "ref" is a ref object
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [lock, themeLockScrolling].concat(_toConsumableArray(dependencies)));
};
export default useLockScrolling;