@navikt/ds-react
Version:
React components from the Norwegian Labour and Welfare Administration.
61 lines • 2.57 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.BODY_CLASS = exports.BODY_CLASS_LEGACY = exports.coordsAreInside = void 0;
exports.getCloseHandler = getCloseHandler;
exports.useBodyScrollLock = useBodyScrollLock;
const react_1 = __importDefault(require("react"));
const coordsAreInside = ({ clientX, clientY }, { left, top, right, bottom }) => {
if (clientX < left || clientY < top)
return false;
if (clientX > right || clientY > bottom)
return false;
return true;
};
exports.coordsAreInside = coordsAreInside;
function getCloseHandler(modalRef, header, onBeforeClose) {
if (header && header.closeButton === false)
return undefined;
if (onBeforeClose) {
return () => { var _a; return onBeforeClose() !== false && ((_a = modalRef.current) === null || _a === void 0 ? void 0 : _a.close()); };
}
return () => { var _a; return (_a = modalRef.current) === null || _a === void 0 ? void 0 : _a.close(); };
}
exports.BODY_CLASS_LEGACY = "navds-modal__document-body";
exports.BODY_CLASS = "aksel-modal__document-body";
function useBodyScrollLock(modalRef, portalNode, isNested) {
react_1.default.useEffect(() => {
if (isNested) {
return;
}
// We check both to avoid running this twice when not using portal
if (!modalRef.current || !portalNode) {
return;
}
// In case `open` is true initially
if (modalRef.current.open) {
document.body.classList.add(exports.BODY_CLASS, exports.BODY_CLASS_LEGACY);
}
const observer = new MutationObserver(() => {
var _a;
if ((_a = modalRef.current) === null || _a === void 0 ? void 0 : _a.open) {
document.body.classList.add(exports.BODY_CLASS, exports.BODY_CLASS_LEGACY);
}
else {
document.body.classList.remove(exports.BODY_CLASS, exports.BODY_CLASS_LEGACY);
}
});
observer.observe(modalRef.current, {
attributes: true,
attributeFilter: ["open"],
});
return () => {
observer.disconnect();
// In case modal is unmounted before it's closed
document.body.classList.remove(exports.BODY_CLASS, exports.BODY_CLASS_LEGACY);
};
}, [modalRef, portalNode, isNested]);
}
//# sourceMappingURL=ModalUtils.js.map