component.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.RemoveScrollBar = exports.lockAttribute = void 0;
  4. var tslib_1 = require("tslib");
  5. var React = tslib_1.__importStar(require("react"));
  6. var react_style_singleton_1 = require("react-style-singleton");
  7. var constants_1 = require("./constants");
  8. var utils_1 = require("./utils");
  9. var Style = (0, react_style_singleton_1.styleSingleton)();
  10. exports.lockAttribute = 'data-scroll-locked';
  11. // important tip - once we measure scrollBar width and remove them
  12. // we could not repeat this operation
  13. // thus we are using style-singleton - only the first "yet correct" style will be applied.
  14. var getStyles = function (_a, allowRelative, gapMode, important) {
  15. var left = _a.left, top = _a.top, right = _a.right, gap = _a.gap;
  16. if (gapMode === void 0) { gapMode = 'margin'; }
  17. return "\n .".concat(constants_1.noScrollbarsClassName, " {\n overflow: hidden ").concat(important, ";\n padding-right: ").concat(gap, "px ").concat(important, ";\n }\n body[").concat(exports.lockAttribute, "] {\n overflow: hidden ").concat(important, ";\n overscroll-behavior: contain;\n ").concat([
  18. allowRelative && "position: relative ".concat(important, ";"),
  19. gapMode === 'margin' &&
  20. "\n padding-left: ".concat(left, "px;\n padding-top: ").concat(top, "px;\n padding-right: ").concat(right, "px;\n margin-left:0;\n margin-top:0;\n margin-right: ").concat(gap, "px ").concat(important, ";\n "),
  21. gapMode === 'padding' && "padding-right: ".concat(gap, "px ").concat(important, ";"),
  22. ]
  23. .filter(Boolean)
  24. .join(''), "\n }\n \n .").concat(constants_1.zeroRightClassName, " {\n right: ").concat(gap, "px ").concat(important, ";\n }\n \n .").concat(constants_1.fullWidthClassName, " {\n margin-right: ").concat(gap, "px ").concat(important, ";\n }\n \n .").concat(constants_1.zeroRightClassName, " .").concat(constants_1.zeroRightClassName, " {\n right: 0 ").concat(important, ";\n }\n \n .").concat(constants_1.fullWidthClassName, " .").concat(constants_1.fullWidthClassName, " {\n margin-right: 0 ").concat(important, ";\n }\n \n body[").concat(exports.lockAttribute, "] {\n ").concat(constants_1.removedBarSizeVariable, ": ").concat(gap, "px;\n }\n");
  25. };
  26. /**
  27. * Removes page scrollbar and blocks page scroll when mounted
  28. */
  29. var RemoveScrollBar = function (props) {
  30. var noRelative = props.noRelative, noImportant = props.noImportant, _a = props.gapMode, gapMode = _a === void 0 ? 'margin' : _a;
  31. /*
  32. gap will be measured on every component mount
  33. however it will be used only by the "first" invocation
  34. due to singleton nature of <Style
  35. */
  36. var gap = React.useMemo(function () { return (0, utils_1.getGapWidth)(gapMode); }, [gapMode]);
  37. React.useEffect(function () {
  38. document.body.setAttribute(exports.lockAttribute, '');
  39. return function () {
  40. document.body.removeAttribute(exports.lockAttribute);
  41. };
  42. }, []);
  43. return React.createElement(Style, { styles: getStyles(gap, !noRelative, gapMode, !noImportant ? '!important' : '') });
  44. };
  45. exports.RemoveScrollBar = RemoveScrollBar;