@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.
41 lines (33 loc) • 1.39 kB
JavaScript
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
import { useEffect } from "react";
import useTheme from "../useTheme";
import useIsMounted from "../useIsMounted";
import { disableBodyScroll, enableBodyScroll } from "./lock-scrolling";
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;
var isMounted = useIsMounted();
useEffect(function () {
var el = ref.current;
if (el) {
if (lock && themeLockScrolling) {
disableBodyScroll(el);
}
if (!lock || !themeLockScrolling) {
enableBodyScroll(el);
}
}
return function () {
// disabling this because we're interested in precisely that changed value
// eslint-disable-next-line react-hooks/exhaustive-deps
var didRefChange = el !== ref.current;
if (el && (!isMounted() || didRefChange)) {
enableBodyScroll(el);
}
};
}, [ref, lock, themeLockScrolling, isMounted].concat(_toConsumableArray(dependencies)));
};
export default useLockScrolling;