Skip to content

Commit a12246e

Browse files
committed
[changed] use object className and overlayClassName prop to override...
This is a backport of @cassln's feature to version 1.8.x. Original PR is #330. #330
1 parent bf5e288 commit a12246e

File tree

3 files changed

+57
-16
lines changed

3 files changed

+57
-16
lines changed

Diff for: README.md

+20
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,26 @@ you can pass `className` and `overlayClassName` props to the Modal. If you do
7979
this then none of the default styles will apply and you will have full control
8080
over styling via CSS.
8181

82+
If you want to override default content and overlay classes you can pass object
83+
with three required properties: `base`, `afterOpen`, `beforeClose`.
84+
85+
```jsx
86+
<Modal
87+
...
88+
className={{
89+
base: 'myClass',
90+
afterOpen: 'myClass_after-open',
91+
beforeClose: 'myClass_before-close'
92+
}}
93+
overlayClassName={{
94+
base: 'myOverlayClass',
95+
afterOpen: 'myOverlayClass_after-open',
96+
beforeClose: 'myOverlayClass_before-close'
97+
}}
98+
...
99+
>
100+
```
101+
82102
You can also pass a `portalClassName` to change the wrapper's class (*ReactModalPortal*).
83103
This doesn't affect styling as no styles are applied to this element by default.
84104

Diff for: lib/components/ModalPortal.js

+11-16
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,8 @@ var createReactClass = require('create-react-class');
77

88
// so that our CSS is statically analyzable
99
var CLASS_NAMES = {
10-
overlay: {
11-
base: 'ReactModal__Overlay',
12-
afterOpen: 'ReactModal__Overlay--after-open',
13-
beforeClose: 'ReactModal__Overlay--before-close'
14-
},
15-
content: {
16-
base: 'ReactModal__Content',
17-
afterOpen: 'ReactModal__Content--after-open',
18-
beforeClose: 'ReactModal__Content--before-close'
19-
}
10+
overlay: 'ReactModal__Overlay',
11+
content: 'ReactModal__Content'
2012
};
2113

2214
var ModalPortal = module.exports = createReactClass({
@@ -169,12 +161,15 @@ var ModalPortal = module.exports = createReactClass({
169161
},
170162

171163
buildClassName: function(which, additional) {
172-
var className = CLASS_NAMES[which].base;
173-
if (this.state.afterOpen)
174-
className += ' '+CLASS_NAMES[which].afterOpen;
175-
if (this.state.beforeClose)
176-
className += ' '+CLASS_NAMES[which].beforeClose;
177-
return additional ? className + ' ' + additional : className;
164+
var classNames = (typeof additional === 'object') ? additional : {
165+
base: CLASS_NAMES[which],
166+
afterOpen: CLASS_NAMES[which] + "--after-open",
167+
beforeClose: CLASS_NAMES[which] + "--before-close"
168+
};
169+
var className = classNames.base;
170+
if (this.state.afterOpen) { className += " " + classNames.afterOpen; }
171+
if (this.state.beforeClose) { className += " " + classNames.beforeClose; }
172+
return (typeof additional === 'string' && additional) ? [className, additional].join(" ") : className;
178173
},
179174

180175
render: function() {

Diff for: specs/Modal.spec.js

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

174+
it('overrides the default content classes when a custom object className is used', () => {
175+
const modal = renderModal({
176+
isOpen: true,
177+
className: {
178+
base: 'myClass',
179+
afterOpen: 'myClass_after-open',
180+
beforeClose: 'myClass_before-close'
181+
}
182+
});
183+
expect(mcontent(modal).className).toEqual('myClass myClass_after-open');
184+
unmountModal();
185+
});
186+
187+
it('overrides the default overlay classes when a custom object overlayClassName is used', () => {
188+
const modal = renderModal({
189+
isOpen: true,
190+
overlayClassName: {
191+
base: 'myOverlayClass',
192+
afterOpen: 'myOverlayClass_after-open',
193+
beforeClose: 'myOverlayClass_before-close'
194+
}
195+
});
196+
expect(moverlay(modal).className).toEqual('myOverlayClass myOverlayClass_after-open');
197+
unmountModal();
198+
});
199+
174200
it('supports overriding react modal open class in document.body.', () => {
175201
const modal = renderModal({ isOpen: true, bodyOpenClassName: 'custom-modal-open' });
176202
expect(document.body.className.indexOf('custom-modal-open') !== -1).toBeTruthy();

0 commit comments

Comments
 (0)