Skip to content

Commit

Permalink
fix(Modal): don't add or remove multi body classes
Browse files Browse the repository at this point in the history
  • Loading branch information
TheSharpieOne committed Mar 23, 2018
1 parent 0a15fe7 commit 6ec3174
Showing 1 changed file with 14 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/Modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,13 @@ class Modal extends React.Component {

document.body.appendChild(this._element);

document.body.className = classNames(
document.body.className,
mapToCssModules('modal-open', this.props.cssModule)
);
if (!this.bodyClassAdded) {
document.body.className = classNames(
document.body.className,
mapToCssModules('modal-open', this.props.cssModule)
);
this.bodyClassAdded = true;
}
}

destroy() {
Expand All @@ -196,10 +199,13 @@ class Modal extends React.Component {
this._element = null;
}

const modalOpenClassName = mapToCssModules('modal-open', this.props.cssModule);
// 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();
if (this.bodyClassAdded) {
const modalOpenClassName = mapToCssModules('modal-open', this.props.cssModule);
// 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();
this.bodyClassAdded = false;
}

setScrollbarWidth(this._originalBodyPadding);
}
Expand Down

0 comments on commit 6ec3174

Please sign in to comment.