@navikt/ds-react
Version:
React components from the Norwegian Labour and Welfare Administration.
52 lines • 2.07 kB
JavaScript
import React from "react";
export const coordsAreInside = ({ clientX, clientY }, { left, top, right, bottom }) => {
if (clientX < left || clientY < top)
return false;
if (clientX > right || clientY > bottom)
return false;
return true;
};
export 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(); };
}
export const BODY_CLASS_LEGACY = "navds-modal__document-body";
export const BODY_CLASS = "aksel-modal__document-body";
export function useBodyScrollLock(modalRef, portalNode, isNested) {
React.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(BODY_CLASS, 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(BODY_CLASS, BODY_CLASS_LEGACY);
}
else {
document.body.classList.remove(BODY_CLASS, 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(BODY_CLASS, BODY_CLASS_LEGACY);
};
}, [modalRef, portalNode, isNested]);
}
//# sourceMappingURL=ModalUtils.js.map