Skip to content

Commit

Permalink
[change] allow to customize the react-modal document.body open class.
Browse files Browse the repository at this point in the history
This is a backport of @cassln's feature to version 1.8.x.
  • Loading branch information
diasbruno committed Jun 12, 2017
1 parent 5cf1867 commit e5bb415
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
5 changes: 5 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ import ReactModal from 'react-modal';
See the `Styles` section for more details.
*/
className="ReactModal__Content"
/*
String className to be applied to the document.body.
See the `Styles` section for more details.
*/
bodyOpenClassName="ReactModal__Body--open"
/*
Boolean indicating if the appElement should be hidden
*/
Expand Down
1 change: 1 addition & 0 deletions docs/styles/classes.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
### CSS Classes

Sometimes it may be preferable to use CSS classes rather than inline styles. You can use the `className` and `overlayClassName` props to specify a given CSS class for each of those.
You can override the default class that is added to `document.body` when the modal is open by defining a property `bodyOpenClassName`.
Note: If you provide those props all default styles will not be applied, leaving all styles under control of the CSS class.
The `portalClassName` can also be used however there are no styles by default applied
9 changes: 6 additions & 3 deletions lib/components/Modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ var Modal = createReactClass({
overlay: PropTypes.object
}),
portalClassName: PropTypes.string,
bodyOpenClassName: React.PropTypes.string,
appElement: PropTypes.instanceOf(SafeHTMLElement),
onAfterOpen: PropTypes.func,
onRequestClose: PropTypes.func,
Expand All @@ -53,6 +54,7 @@ var Modal = createReactClass({
return {
isOpen: false,
portalClassName: 'ReactModalPortal',
bodyOpenClassName: 'ReactModal__Body--open',
ariaHideApp: true,
closeTimeoutMS: 0,
shouldCloseOnOverlayClick: true,
Expand Down Expand Up @@ -114,16 +116,17 @@ var Modal = createReactClass({
ReactDOM.unmountComponentAtNode(this.node);
var parent = getParentElement(this.props.parentSelector);
parent.removeChild(this.node);

if (refCount.count() === 0) {
elementClass(document.body).remove('ReactModal__Body--open');
elementClass(document.body).remove(this.props.bodyOpenClassName);
}
},

renderPortal: function(props) {
if (props.isOpen || refCount.count() > 0) {
elementClass(document.body).add('ReactModal__Body--open');
elementClass(document.body).add(this.props.bodyOpenClassName);
} else {
elementClass(document.body).remove('ReactModal__Body--open');
elementClass(document.body).remove(this.props.bodyOpenClassName);
}

if (props.ariaHideApp) {
Expand Down
5 changes: 5 additions & 0 deletions specs/Modal.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,11 @@ describe('State', () => {
).toBeTruthy();
});

it('supports overriding react modal open class in document.body.', () => {
const modal = renderModal({ isOpen: true, bodyOpenClassName: 'custom-modal-open' });
expect(document.body.className.indexOf('custom-modal-open') !== -1).toBeTruthy();
});

it('don\'t append class to document.body if modal is not open', () => {
renderModal({ isOpen: false });
expect(!isBodyWithReactModalOpenClass()).toBeTruthy();
Expand Down

0 comments on commit e5bb415

Please sign in to comment.