Skip to content

Commit e5bb415

Browse files
committed
[change] allow to customize the react-modal document.body open class.
This is a backport of @cassln's feature to version 1.8.x.
1 parent 5cf1867 commit e5bb415

File tree

4 files changed

+17
-3
lines changed

4 files changed

+17
-3
lines changed

Diff for: docs/README.md

+5
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ import ReactModal from 'react-modal';
5252
See the `Styles` section for more details.
5353
*/
5454
className="ReactModal__Content"
55+
/*
56+
String className to be applied to the document.body.
57+
See the `Styles` section for more details.
58+
*/
59+
bodyOpenClassName="ReactModal__Body--open"
5560
/*
5661
Boolean indicating if the appElement should be hidden
5762
*/

Diff for: docs/styles/classes.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
### CSS Classes
22

33
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.
4+
You can override the default class that is added to `document.body` when the modal is open by defining a property `bodyOpenClassName`.
45
Note: If you provide those props all default styles will not be applied, leaving all styles under control of the CSS class.
56
The `portalClassName` can also be used however there are no styles by default applied

Diff for: lib/components/Modal.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ var Modal = createReactClass({
3838
overlay: PropTypes.object
3939
}),
4040
portalClassName: PropTypes.string,
41+
bodyOpenClassName: React.PropTypes.string,
4142
appElement: PropTypes.instanceOf(SafeHTMLElement),
4243
onAfterOpen: PropTypes.func,
4344
onRequestClose: PropTypes.func,
@@ -53,6 +54,7 @@ var Modal = createReactClass({
5354
return {
5455
isOpen: false,
5556
portalClassName: 'ReactModalPortal',
57+
bodyOpenClassName: 'ReactModal__Body--open',
5658
ariaHideApp: true,
5759
closeTimeoutMS: 0,
5860
shouldCloseOnOverlayClick: true,
@@ -114,16 +116,17 @@ var Modal = createReactClass({
114116
ReactDOM.unmountComponentAtNode(this.node);
115117
var parent = getParentElement(this.props.parentSelector);
116118
parent.removeChild(this.node);
119+
117120
if (refCount.count() === 0) {
118-
elementClass(document.body).remove('ReactModal__Body--open');
121+
elementClass(document.body).remove(this.props.bodyOpenClassName);
119122
}
120123
},
121124

122125
renderPortal: function(props) {
123126
if (props.isOpen || refCount.count() > 0) {
124-
elementClass(document.body).add('ReactModal__Body--open');
127+
elementClass(document.body).add(this.props.bodyOpenClassName);
125128
} else {
126-
elementClass(document.body).remove('ReactModal__Body--open');
129+
elementClass(document.body).remove(this.props.bodyOpenClassName);
127130
}
128131

129132
if (props.ariaHideApp) {

Diff for: specs/Modal.spec.js

+5
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,11 @@ describe('State', () => {
171171
).toBeTruthy();
172172
});
173173

174+
it('supports overriding react modal open class in document.body.', () => {
175+
const modal = renderModal({ isOpen: true, bodyOpenClassName: 'custom-modal-open' });
176+
expect(document.body.className.indexOf('custom-modal-open') !== -1).toBeTruthy();
177+
});
178+
174179
it('don\'t append class to document.body if modal is not open', () => {
175180
renderModal({ isOpen: false });
176181
expect(!isBodyWithReactModalOpenClass()).toBeTruthy();

0 commit comments

Comments
 (0)