UNPKG

@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.

59 lines (44 loc) 1.94 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray")); var _react = require("react"); var _bodyScrollLock = require("body-scroll-lock"); var _useTheme2 = _interopRequireDefault(require("../useTheme")); function lockScrolling(el) { (0, _bodyScrollLock.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 = ""; } (0, _bodyScrollLock.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 = (0, _useTheme2.default)(), _useTheme$lockScrolli = _useTheme.lockScrolling, themeLockScrolling = _useTheme$lockScrolli === void 0 ? true : _useTheme$lockScrolli; (0, _react.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((0, _toConsumableArray2.default)(dependencies))); }; var _default = useLockScrolling; exports.default = _default;