Skip to content

Commit

Permalink
[fix] keep references of modals.
Browse files Browse the repository at this point in the history
keeping a reference of all modals in order to manage
when to add/remove classes and aria from elements
correctly.
  • Loading branch information
diasbruno authored and claydiffrient committed Apr 10, 2017
1 parent 02a16ce commit e579a0d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 6 deletions.
13 changes: 11 additions & 2 deletions lib/components/Modal.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var ReactDOM = require('react-dom');
var ExecutionEnvironment = require('exenv');
var ModalPortal = React.createFactory(require('./ModalPortal'));
var ariaAppHider = require('../helpers/ariaAppHider');
var refCount = require('../helpers/refCount');
var elementClass = require('element-class');
var renderSubtreeIntoContainer = require("react-dom").unstable_renderSubtreeIntoContainer;
var Assign = require('lodash.assign');
Expand Down Expand Up @@ -61,12 +62,16 @@ var Modal = React.createClass({
this.node = document.createElement('div');
this.node.className = this.props.portalClassName;

if (this.props.isOpen) refCount.add(this);

var parent = getParentElement(this.props.parentSelector);
parent.appendChild(this.node);
this.renderPortal(this.props);
},

componentWillReceiveProps: function(newProps) {
if (newProps.isOpen) refCount.add(this);
if (!newProps.isOpen) refCount.remove(this);
var currentParent = getParentElement(this.props.parentSelector);
var newParent = getParentElement(newProps.parentSelector);

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

componentWillUnmount: function() {
refCount.remove(this);

if (this.props.ariaHideApp) {
ariaAppHider.show(this.props.appElement);
}
Expand All @@ -105,11 +112,13 @@ var Modal = React.createClass({
ReactDOM.unmountComponentAtNode(this.node);
var parent = getParentElement(this.props.parentSelector);
parent.removeChild(this.node);
elementClass(document.body).remove('ReactModal__Body--open');
if (refCount.count() === 0) {
elementClass(document.body).remove('ReactModal__Body--open');
}
},

renderPortal: function(props) {
if (props.isOpen) {
if (props.isOpen || refCount.count() > 0) {
elementClass(document.body).add('ReactModal__Body--open');
} else {
elementClass(document.body).remove('ReactModal__Body--open');
Expand Down
19 changes: 19 additions & 0 deletions lib/helpers/refCount.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const modals = [];

export function add (element) {
if (modals.indexOf(element) === -1) {
modals.push(element);
}
}

export function remove (element) {
const index = modals.indexOf(element);
if (index === -1) {
return;
}
modals.splice(index, 1);
}

export function count () {
return modals.length;
}
23 changes: 19 additions & 4 deletions specs/Modal.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,15 @@ describe('Modal', () => {
});
});

it('give back focus to previous element or modal.', function (done) {
var modal = renderModal({
it('give back focus to previous element or modal.', (done) => {
function cleanup () {
unmountModal();
done();
}
const modal = renderModal({
isOpen: true,
onRequestClose: function () {
done();
onRequestClose () {
cleanup();
}
}, null);

Expand Down Expand Up @@ -215,6 +219,17 @@ describe('Modal', () => {
Modal.defaultStyles.content.position = previousStyle;
});

it('should remove class from body when no modals opened', () => {
const findReactModalOpenClass = () => document.body.className.indexOf('ReactModal__Body--open');
renderModal({ isOpen: true });
renderModal({ isOpen: true });
expect(findReactModalOpenClass() > -1).toBe(true);
unmountModal();
expect(findReactModalOpenClass() > -1).toBe(true);
unmountModal();
expect(findReactModalOpenClass() === -1).toBe(true);
});

it('adds class to body when open', function() {
renderModal({ isOpen: false });
expect(document.body.className.indexOf('ReactModal__Body--open') !== -1).toEqual(false);
Expand Down

0 comments on commit e579a0d

Please sign in to comment.