Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix(#2411): remove scroll from body when modal open
  • Loading branch information
illiteratewriter authored and davidacevedo committed Jun 7, 2022
1 parent 2a96fba commit 9d71faa
Showing 1 changed file with 4 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/Modal.js
Expand Up @@ -101,6 +101,7 @@ class Modal extends React.Component {

this._element = null;
this._originalBodyPadding = null;
this._originalBodyOverflow = null;
this.getFocusableChildren = this.getFocusableChildren.bind(this);
this.handleBackdropClick = this.handleBackdropClick.bind(this);
this.handleBackdropMouseDown = this.handleBackdropMouseDown.bind(this);
Expand Down Expand Up @@ -339,13 +340,15 @@ class Modal extends React.Component {
}

this._originalBodyPadding = getOriginalBodyPadding();
this._originalBodyOverflow = window.getComputedStyle(document.body).overflow;
conditionallyUpdateScrollbar();

if (Modal.openCount === 0) {
document.body.className = classNames(
document.body.className,
mapToCssModules('modal-open', this.props.cssModule)
);
document.body.style.overflow = 'hidden';
}

this.modalIndex = Modal.openCount;
Expand Down Expand Up @@ -375,6 +378,7 @@ class Modal extends React.Component {
// Use regex to prevent matching `modal-open` as part of a different class, e.g. `my-modal-opened`
const modalOpenClassNameRegex = new RegExp(`(^| )${modalOpenClassName}( |$)`);
document.body.className = document.body.className.replace(modalOpenClassNameRegex, ' ').trim();
document.body.style.overflow = this._originalBodyOverflow;
}
this.manageFocusAfterClose();
Modal.openCount = Math.max(0, Modal.openCount - 1);
Expand Down

0 comments on commit 9d71faa

Please sign in to comment.