Skip to content

Commit e579a0d

Browse files
diasbrunoclaydiffrient
authored andcommittedApr 10, 2017
[fix] keep references of modals.
keeping a reference of all modals in order to manage when to add/remove classes and aria from elements correctly.
1 parent 02a16ce commit e579a0d

File tree

3 files changed

+49
-6
lines changed

3 files changed

+49
-6
lines changed
 

Diff for: ‎lib/components/Modal.js

+11-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ var ReactDOM = require('react-dom');
33
var ExecutionEnvironment = require('exenv');
44
var ModalPortal = React.createFactory(require('./ModalPortal'));
55
var ariaAppHider = require('../helpers/ariaAppHider');
6+
var refCount = require('../helpers/refCount');
67
var elementClass = require('element-class');
78
var renderSubtreeIntoContainer = require("react-dom").unstable_renderSubtreeIntoContainer;
89
var Assign = require('lodash.assign');
@@ -61,12 +62,16 @@ var Modal = React.createClass({
6162
this.node = document.createElement('div');
6263
this.node.className = this.props.portalClassName;
6364

65+
if (this.props.isOpen) refCount.add(this);
66+
6467
var parent = getParentElement(this.props.parentSelector);
6568
parent.appendChild(this.node);
6669
this.renderPortal(this.props);
6770
},
6871

6972
componentWillReceiveProps: function(newProps) {
73+
if (newProps.isOpen) refCount.add(this);
74+
if (!newProps.isOpen) refCount.remove(this);
7075
var currentParent = getParentElement(this.props.parentSelector);
7176
var newParent = getParentElement(newProps.parentSelector);
7277

@@ -79,6 +84,8 @@ var Modal = React.createClass({
7984
},
8085

8186
componentWillUnmount: function() {
87+
refCount.remove(this);
88+
8289
if (this.props.ariaHideApp) {
8390
ariaAppHider.show(this.props.appElement);
8491
}
@@ -105,11 +112,13 @@ var Modal = React.createClass({
105112
ReactDOM.unmountComponentAtNode(this.node);
106113
var parent = getParentElement(this.props.parentSelector);
107114
parent.removeChild(this.node);
108-
elementClass(document.body).remove('ReactModal__Body--open');
115+
if (refCount.count() === 0) {
116+
elementClass(document.body).remove('ReactModal__Body--open');
117+
}
109118
},
110119

111120
renderPortal: function(props) {
112-
if (props.isOpen) {
121+
if (props.isOpen || refCount.count() > 0) {
113122
elementClass(document.body).add('ReactModal__Body--open');
114123
} else {
115124
elementClass(document.body).remove('ReactModal__Body--open');

Diff for: ‎lib/helpers/refCount.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const modals = [];
2+
3+
export function add (element) {
4+
if (modals.indexOf(element) === -1) {
5+
modals.push(element);
6+
}
7+
}
8+
9+
export function remove (element) {
10+
const index = modals.indexOf(element);
11+
if (index === -1) {
12+
return;
13+
}
14+
modals.splice(index, 1);
15+
}
16+
17+
export function count () {
18+
return modals.length;
19+
}

Diff for: ‎specs/Modal.spec.js

+19-4
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,15 @@ describe('Modal', () => {
9898
});
9999
});
100100

101-
it('give back focus to previous element or modal.', function (done) {
102-
var modal = renderModal({
101+
it('give back focus to previous element or modal.', (done) => {
102+
function cleanup () {
103+
unmountModal();
104+
done();
105+
}
106+
const modal = renderModal({
103107
isOpen: true,
104-
onRequestClose: function () {
105-
done();
108+
onRequestClose () {
109+
cleanup();
106110
}
107111
}, null);
108112

@@ -215,6 +219,17 @@ describe('Modal', () => {
215219
Modal.defaultStyles.content.position = previousStyle;
216220
});
217221

222+
it('should remove class from body when no modals opened', () => {
223+
const findReactModalOpenClass = () => document.body.className.indexOf('ReactModal__Body--open');
224+
renderModal({ isOpen: true });
225+
renderModal({ isOpen: true });
226+
expect(findReactModalOpenClass() > -1).toBe(true);
227+
unmountModal();
228+
expect(findReactModalOpenClass() > -1).toBe(true);
229+
unmountModal();
230+
expect(findReactModalOpenClass() === -1).toBe(true);
231+
});
232+
218233
it('adds class to body when open', function() {
219234
renderModal({ isOpen: false });
220235
expect(document.body.className.indexOf('ReactModal__Body--open') !== -1).toEqual(false);

0 commit comments

Comments
 (0)
Please sign in to comment.