@workday/canvas-kit-react
Version:
The parent module that contains all Workday Canvas Kit React components
37 lines (36 loc) • 1.55 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.useDisableBodyScroll = void 0;
const react_1 = __importDefault(require("react"));
const common_1 = require("@workday/canvas-kit-react/common");
const usePopupModel_1 = require("./usePopupModel");
/**
* Disables body scroll by adding `overflow: hidden` to the body element. This effectively prevents
* page scrolling while the popup is visible.
*
* This should be used with popup elements that hide all other content and force the user to accept
* or dismiss the popup before continuing (i.e. Modals).
*/
exports.useDisableBodyScroll = (0, common_1.createElemPropsHook)(usePopupModel_1.usePopupModel)(model => {
const visible = model.state.visibility !== 'hidden';
react_1.default.useLayoutEffect(() => {
if (!visible) {
return;
}
const overflowY = document.body.style.overflowY;
const overflowX = document.body.style.overflowX;
const overflow = document.body.style.overflow;
document.body.style.overflow = 'hidden';
document.body.style.overflowY = 'hidden';
document.body.style.overflowX = 'hidden';
return () => {
document.body.style.overflow = overflow;
document.body.style.overflowY = overflowY;
document.body.style.overflowX = overflowX;
};
}, [visible]);
return {};
});