diff --git a/src/components/Modal/Modal.jsx b/src/components/Modal/Modal.jsx
index f3128328..dfcd993f 100644
--- a/src/components/Modal/Modal.jsx
+++ b/src/components/Modal/Modal.jsx
@@ -98,7 +98,7 @@ Modal.defaultProps = {
closeButtonRef: null,
portalId: null,
position: 'center',
- preventScrollUnderneath: 'default',
+ preventScrollUnderneath: window.document.body,
primaryButtonRef: null,
size: 'medium',
};
@@ -137,15 +137,15 @@ Modal.propTypes = {
position: PropTypes.oneOf(['top', 'center']),
/**
* Mode in which Modal prevents scroll of elements bellow:
- * * `default` - Modal prevents scroll on the `body` element
* * `off` - Modal does not prevent any scroll
+ * * [HTMLElement](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement) - Modal prevents scroll on this HTML element
* * object
- * * * `reset` - method called on Modal's unmount to reset scroll prevention
- * * * `start` - method called on Modal's mount to custom scroll prevention
+ * * `reset` - method called on Modal's unmount to reset scroll prevention
+ * * `start` - method called on Modal's mount to custom scroll prevention
*/
preventScrollUnderneath: PropTypes.oneOfType([
PropTypes.oneOf([
- 'default',
+ HTMLElement,
'off',
]),
PropTypes.shape({
diff --git a/src/components/Modal/README.md b/src/components/Modal/README.md
index edf738bd..50e56f9e 100644
--- a/src/components/Modal/README.md
+++ b/src/components/Modal/README.md
@@ -26,7 +26,13 @@ React.createElement(() => {
const modalPrimaryButtonRef = React.useRef();
const modalCloseButtonRef = React.useRef();
return (
- <>
+ {/*
+ The `preventScrollUnderneath` feature is necessary for Modals to work in
+ React UI docs. You may not need it in your application.
+ */}
+
);
});
```
@@ -116,7 +122,13 @@ React.createElement(() => {
const modalPrimaryButtonRef = React.useRef();
const modalCloseButtonRef = React.useRef();
return (
- <>
+ {/*
+ The `preventScrollUnderneath` feature is necessary for Modals to work in
+ React UI docs. You may not need it in your application.
+ */}
+
);
});
```
@@ -265,7 +277,13 @@ React.createElement(() => {
const modalPrimaryButtonRef = React.useRef();
const modalCloseButtonRef = React.useRef();
return (
- <>
+ {/*
+ The `preventScrollUnderneath` feature is necessary for Modals to work in
+ React UI docs. You may not need it in your application.
+ */}
+
);
});
```
@@ -393,7 +411,13 @@ React.createElement(() => {
const modalPrimaryButtonRef = React.useRef();
const modalCloseButtonRef = React.useRef();
return (
- <>
+ {/*
+ The `preventScrollUnderneath` feature is necessary for Modals to work in
+ React UI docs. You may not need it in your application.
+ */}
+
);
});
```
@@ -510,7 +534,13 @@ React.createElement(() => {
const modalPrimaryButtonRef = React.useRef();
const modalCloseButtonRef = React.useRef();
return (
- <>
+ {/*
+ The `preventScrollUnderneath` feature is necessary for Modals to work in
+ React UI docs. You may not need it in your application.
+ */}
+
);
});
```
@@ -588,7 +618,13 @@ React.createElement(() => {
const modalPrimaryButtonRef = React.useRef();
const modalCloseButtonRef = React.useRef();
return (
- <>
+ {/*
+ The `preventScrollUnderneath` feature is necessary for Modals to work in
+ React UI docs. You may not need it in your application.
+ */}
+
);
});
```
@@ -650,7 +686,13 @@ React.createElement(() => {
const modalPrimaryButtonRef = React.useRef();
const modalCloseButtonRef = React.useRef();
return (
- <>
+ {/*
+ The `preventScrollUnderneath` feature is necessary for Modals to work in
+ React UI docs. You may not need it in your application.
+ */}
+
);
});
```
@@ -708,7 +750,13 @@ React.createElement(() => {
const modalPrimaryButtonRef = React.useRef();
const modalCloseButtonRef = React.useRef();
return (
- <>
+ {/*
+ The `preventScrollUnderneath` feature is necessary for Modals to work in
+ React UI docs. You may not need it in your application.
+ */}
+
);
});
```
@@ -884,7 +932,13 @@ React.createElement(() => {
)
return (
- <>
+ {/*
+ The `preventScrollUnderneath` feature is necessary for Modals to work in
+ React UI docs. You may not need it in your application.
+ */}
+
);
});
```
diff --git a/src/components/Modal/_hooks/useModalScrollPrevention.js b/src/components/Modal/_hooks/useModalScrollPrevention.js
index 4b1ca54c..5cccde6b 100644
--- a/src/components/Modal/_hooks/useModalScrollPrevention.js
+++ b/src/components/Modal/_hooks/useModalScrollPrevention.js
@@ -7,22 +7,24 @@ export const useModalScrollPrevention = (preventScrollUnderneath) => {
return () => {};
}
- if (preventScrollUnderneath === 'default') {
+ if (preventScrollUnderneath instanceof HTMLElement) {
+ const scrollableElement = preventScrollUnderneath;
+
const scrollbarWidth = Math.abs(window.innerWidth - window.document.documentElement.clientWidth);
- const prevOverflow = window.document.body.style.overflow;
- const prevPaddingRight = window.document.body.style.paddingRight;
+ const prevOverflow = scrollableElement.style.overflow;
+ const prevPaddingRight = scrollableElement.style.paddingRight;
- window.document.body.style.overflow = 'hidden';
+ scrollableElement.style.overflow = 'hidden';
if (Number.isNaN(parseInt(prevPaddingRight, 10))) {
- window.document.body.style.paddingRight = `${scrollbarWidth}px`;
+ scrollableElement.style.paddingRight = `${scrollbarWidth}px`;
} else {
- window.document.body.style.paddingRight = `calc(${prevPaddingRight} + ${scrollbarWidth}px)`;
+ scrollableElement.style.paddingRight = `calc(${prevPaddingRight} + ${scrollbarWidth}px)`;
}
return () => {
- window.document.body.style.overflow = prevOverflow;
- window.document.body.style.paddingRight = prevPaddingRight;
+ scrollableElement.style.overflow = prevOverflow;
+ scrollableElement.style.paddingRight = prevPaddingRight;
};
}